Coder Social home page Coder Social logo

aklaus / transparent-auth-gateway Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 3.0 77 KB

Implementations of a "transparent" Auth Gateway that links a third-party Identity Provider with own authorisation rules (scopes, roles, etc.)

Home Page: https://alex-klaus.com/transparent-auth-gateway-1

License: MIT License

C# 98.84% JavaScript 1.16%
identity oauth2 oidc azure-ad

transparent-auth-gateway's Introduction

Build

Code samples for a series of articles about implementing Transparent Auth Gateway:

  1. Needs and means. Requirements for enterprise apps and available off-the-shelf solutions.
  2. Auth Flows. Relevant authentication/authorisation flows (OAuth2, OIDC) with sequence diagrams.
  3. Writing the code in C#. Comments to the code in this repo – a Transparent Auth Gateway in .NET.
  4. Deploying to Azure. App Registrations and Firewall settings (Azure WAF / Front Door).

Transparent Auth Gateway for Enterprise apps

A trusted authority for our enterprise application(s) that

  • transparently (without additional user interaction) confirms the identity with the linked Identity Provider (an Azure AD tenant in this case), supporting SSO;
  • conducts extra authentication checks (with a potential for own user management);
  • issues an access token with app-specific attributes (user’s roles/groups/etc.);
  • is self-hosted without reliance on third-party services.

The code uses Azure AD as the linked Identity Provider (for the identity checks) and its own bespoke authorisation server.

Transparent Auth Gateway

The implemented protocols:

Code structure

There are 3 projects:

  • AzureADAuthClient – a quick way to ensure that Azure AD authentication is configured. Uses Swagger UI to acquire a token and the standard Microsoft.Identity way to validate the token on WebAPI.
  • OpenIdDict.Server – a bespoke Transparent Auth Gateway to confirm the user's identity from the linked provider and authorise the user (issue own access token) according to the bespoke rules:
    • implements OAuth 2 Authorization Code Flow with PKCE to serve other client apps as the trusted authority;
    • perform authentication from the linked Identity Provider (a specified tenant of Azure AD).
  • OpenIdDict.Client.Api – A Web API app that validates the access token issued by the Auth Gateway (OpenIdDict.Server). Contains:
    • Swagger front-end to request the token and run HTTP requests;
    • test API end-points.

How's it different?

The key differences:

  • Issues its own access token based on internal rules and confirmed user's identity from an Azure AD tenant.
  • Requires no database.
  • Has minimum code and "magical" behaviour from the packages.

transparent-auth-gateway's People

Contributors

aklaus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

transparent-auth-gateway's Issues

Shorting the returned code in the Authorisation Code with PKCE implementation

CONTEXT: the Authorisation Code with PKCE (RFC 7636) implementation with no authorization storage (e.g. for the Degraded mode) and hence no reference tokens in use.

PROBLEM
The server returns the Code (encrypted by default) wrapped in a signed JWT by calling link to GenerateIdentityModelToken.ValueTask() method:
context.Token = context.SecurityTokenHandler.CreateToken(descriptor).
This (especially the signature) blows out the size of the generated code string (becomes > 2,048 symbols) that gets returned in the Query String.

TL;DR: Due to the heavy size of the token, in its turn, the client's request may hit limitations on the Query String and/or URL length.

GOAL:
Keep the generated code string well under 2,048 symbols.
e.g. code=lBm8NqlNa0qKalXTRc7cYA&state=V2VkIEp1bCAyNiAyMDIzIDE4OjE3OjMzIEdNVCsxMDAwIChBdXN0cmFsaWFuIEVhc3Rlcm4gU3RhbmRhcmQgVGltZSk%3D&iss=https%3A%2F%2Flocalhost%3A5003%2F

Fake POST requests to /signin-oidc cause exception

Sending empty/fake POST requests to /signin-oidc end-point causes this exception:

Microsoft.AspNetCore.Authentication.AuthenticationFailureException:
   at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1+<HandleRequestAsync>d__12.MoveNext (Microsoft.AspNetCore.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware+<Invoke>d__6.MoveNext (Microsoft.AspNetCore.Authentication, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol+<ProcessRequests>d__238`1.MoveNext (Microsoft.AspNetCore.Server.Kestrel.Core, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
Inner exception Microsoft.AspNetCore.Authentication.AuthenticationFailureException handled at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1+<HandleRequestAsync>d__12.MoveNext:

Here's a cURL command to reproduce:
curl --location --request POST 'https://localhost:5003/signin-oidc'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.