Giter Club home page Giter Club logo

jaylabs.owin.oauthauthorization's Introduction

#Jaylib.Owin.OAuthAuthorization

Provides a Custom OAuth Provider for Implicit Grant. Allowing usage of the included ClaimAuthorize attribute.

Authentication is made by other middleware like OpenID Connect.

##Usage

[ClaimAuthorize(CustomClaims.CanChangeAddress)]

Setup

The Custom provider is used with the OAuthAutorizationServer.

app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
    AccessTokenFormat =
        new JwtFormat(jwtOptions.Audience,
        symmetricKeyIssuerSecurityTokenProvider),
    ApplicationCanDisplayErrors = true,
    Provider = new CustomOAuthProvider(providerOptions), 
    AuthorizeEndpointPath = new PathString("/authorize"),
    AllowInsecureHttp = _appConfiguration.AllowInsecureHttp
});

The provider options allow you issue custom claims and set scope.

var handleConsentOptions = new HandleConsentOptions(consentParameterName:"consentAnswer");

var jwtOptions = new JwtOptions {
    JwtSigningKeyAsUtf8 = "your key",
    Issuer = "your issuer name",
    Audience, "your oauth audience (uri)",
    JwtTokenParameterName = "jwt_token",
    SupportedScope = "Your scope"
}

new CustomProviderOptions(jwtOptions, handleConsentOptions)
        {
            TransformPrincipal =
                principal =>
                {
                    var claims = new List<Claim>();

                    List<Claim> userIdentityTokens =
                        principal.Claims
                            .Where(claim =>
                                claim.Type == ClaimTypes.Name || claim.Type == ClaimTypes.NameIdentifier ||
                                claim.Type == JwtRegisteredClaimNames.UniqueName ||
                                claim.Type == JwtRegisteredClaimNames.Email)
                            .ToList();

                    claims.AddRange(userIdentityTokens);
                    claims.Add(new Claim(CustomClaims.IsCustom, "true"));                  

                    return Task.FromResult(new ClaimsIdentity(claims, "YourAuthType"));
                }
        };

There is also utlilities to ease OpenID Connect configuration, with consent page support.

By default, there is an implicit consent if no implementation is provided by setting CreateConsentAsync. In this case we redirect to a consent view that will POST the consent result back to the authorization URI.

var createConsentOptions = new CreateConsentOptions
        {
            CreateConsentAsync = (response, redirectUri) =>
            {
                var consentUrl = new Uri(string.Format("/consent?redirectUri={0}&consentParamName={1}",
                    Uri.EscapeDataString(redirectUri.ToString()), 
                    Uri.EscapeDataString(customProviderOptions.HandleConsentOptions.ConsentParameterName)), UriKind.Relative);

                response.Redirect(consentUrl.ToString());

                return Task.FromResult(0);
            }
        };

var notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthorizationCodeReceived = consentBuilder.HandleOpenIdAuthorizationCodeAsync
        };

var openIdConnectOptions = new OpenIdConnectAuthenticationOptions
        {
            ClientId = _appConfiguration.OpenIdClientId,
            Authority = _appConfiguration.OpenIdAuthority,
            CallbackPath = new PathString("/openid"),
            Notifications = notifications,
            AuthenticationMode = AuthenticationMode.Active
        };

app.UseOpenIdConnectAuthentication(openIdConnectOptions);

jaylabs.owin.oauthauthorization's People

Watchers

 avatar

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.