public override async Task SerializeAuthorizationCode([NotNull] SerializeAuthorizationCodeContext context)
        {
            var options = (OpenIddictServerOptions)context.Options;

            if (options.DisableTokenStorage)
            {
                return;
            }

            Debug.Assert(context.Request.IsAuthorizationRequest(), "The request should be an authorization request.");

            var token = await CreateTokenAsync(
                OpenIdConnectConstants.TokenUsages.AuthorizationCode,
                context.Ticket, options, context.Request, context.DataFormat);

            // If a reference token was returned by CreateTokenAsync(),
            // force the OpenID Connect server middleware to use it.
            if (!string.IsNullOrEmpty(token))
            {
                context.AuthorizationCode = token;
                context.HandleSerialization();
            }

            // Otherwise, let the OpenID Connect server middleware
            // serialize the token using its default internal logic.

            await _eventService.PublishAsync(new OpenIddictServerEvents.SerializeAuthorizationCode(context));
        }
        public override async Task SerializeAuthorizationCode([NotNull] SerializeAuthorizationCodeContext context)
        {
            var applications = context.HttpContext.RequestServices.GetRequiredService <OpenIddictApplicationManager <TApplication> >();
            var options      = context.HttpContext.RequestServices.GetRequiredService <IOptions <OpenIddictOptions> >();
            var tokens       = context.HttpContext.RequestServices.GetRequiredService <OpenIddictTokenManager <TToken> >();

            Debug.Assert(!string.IsNullOrEmpty(context.Request.ClientId), "The client identifier shouldn't be null or empty.");

            if (!options.Value.DisableTokenRevocation)
            {
                // Resolve the subject from the authentication ticket. If it cannot be found, throw an exception.
                var subject = context.Ticket.Principal.GetClaim(OpenIdConnectConstants.Claims.Subject) ??
                              context.Ticket.Principal.GetClaim(ClaimTypes.NameIdentifier) ??
                              context.Ticket.Principal.GetClaim(ClaimTypes.Upn);

                if (string.IsNullOrEmpty(subject))
                {
                    throw new InvalidOperationException("The subject associated with the authentication ticket cannot be retrieved.");
                }

                // If a null value was returned by CreateAsync, return immediately.
                var token = await tokens.CreateAsync(OpenIdConnectConstants.TokenTypeHints.AuthorizationCode, subject, context.HttpContext.RequestAborted);

                if (token == null)
                {
                    return;
                }

                // Throw an exception if the token identifier can't be resolved.
                var identifier = await tokens.GetIdAsync(token, context.HttpContext.RequestAborted);

                if (string.IsNullOrEmpty(identifier))
                {
                    throw new InvalidOperationException("The unique key associated with an authorization code cannot be null or empty.");
                }

                // Attach the key returned by the underlying store
                // to the authorization code to override the default GUID
                // generated by the OpenID Connect server middleware.
                context.Ticket.SetTicketId(identifier);

                var application = await applications.FindByClientIdAsync(context.Request.ClientId, context.HttpContext.RequestAborted);

                if (application == null)
                {
                    throw new InvalidOperationException("The client application cannot be retrieved from the database.");
                }

                await tokens.SetClientAsync(token, await applications.GetIdAsync(application, context.HttpContext.RequestAborted), context.HttpContext.RequestAborted);

                // If an authorization identifier was specified, bind it to the token.
                var authorization = context.Ticket.GetProperty(OpenIddictConstants.Properties.AuthorizationId);
                if (!string.IsNullOrEmpty(authorization))
                {
                    await tokens.SetAuthorizationAsync(token, authorization, context.HttpContext.RequestAborted);
                }
            }
        }
        public override async Task SerializeAuthorizationCode([NotNull] SerializeAuthorizationCodeContext context)
        {
            var tokens = context.HttpContext.RequestServices.GetRequiredService <OpenIddictTokenManager <TToken> >();

            var identifier = await tokens.CreateAsync(OpenIdConnectConstants.TokenTypeHints.AuthorizationCode, context.HttpContext.RequestAborted);

            if (string.IsNullOrEmpty(identifier))
            {
                throw new InvalidOperationException("The unique key associated with an authorization code cannot be null or empty.");
            }

            // Attach the key returned by the underlying store
            // to the authorization code to override the default GUID
            // generated by the OpenID Connect server middleware.
            context.Ticket.SetTicketId(identifier);
        }
Example #4
0
        public override async Task SerializeAuthorizationCode([NotNull] SerializeAuthorizationCodeContext context)
        {
            var services = context.HttpContext.RequestServices.GetRequiredService <OpenIddictServices <TUser, TApplication, TAuthorization, TScope, TToken> >();

            Debug.Assert(context.Request.IsAuthorizationRequest(), "The request should be an authorization request.");
            Debug.Assert(!string.IsNullOrEmpty(context.Request.ClientId), "The client_id parameter shouldn't be null or empty.");

            // Note: a null value could be returned by FindByIdAsync. In this case, throw an exception to abort the token request.
            var user = await services.Users.FindByIdAsync(context.Ticket.Principal.GetClaim(ClaimTypes.NameIdentifier));

            if (user == null)
            {
                throw new InvalidOperationException("The token request was aborted because the user associated " +
                                                    "with the authorization code was not found in the database.");
            }

            var application = await services.Applications.FindByClientIdAsync(context.Request.ClientId);

            if (application == null)
            {
                throw new InvalidOperationException("The application cannot be retrieved from the database.");
            }

            // Persist a new token entry in the database and attach it
            // to the user and the client application it is issued to.
            var identifier = await services.Users.CreateTokenAsync(user, context.Request.ClientId,
                                                                   OpenIdConnectConstants.TokenTypeHints.AuthorizationCode);

            if (string.IsNullOrEmpty(identifier))
            {
                throw new InvalidOperationException("The unique key associated with an authorization code cannot be null or empty.");
            }

            // Attach the key returned by the underlying store
            // to the authorization code to override the default GUID
            // generated by the OpenID Connect server middleware.
            context.Ticket.SetTicketId(identifier);
        }
Example #5
0
            /// <summary>
            /// Processes the event.
            /// </summary>
            /// <param name="context">The context associated with the event to process.</param>
            /// <returns>
            /// A <see cref="Task"/> that can be used to monitor the asynchronous operation.
            /// </returns>
            public async Task HandleAsync([NotNull] ProcessSigninResponseContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                var principal = context.Principal.Clone(_ => true)
                                .SetTokenId(Guid.NewGuid().ToString())
                                .SetCreationDate(DateTimeOffset.UtcNow);

                var lifetime = context.Principal.GetAuthorizationCodeLifetime() ?? context.Options.AuthorizationCodeLifetime;

                if (lifetime.HasValue)
                {
                    principal.SetExpirationDate(principal.GetCreationDate() + lifetime.Value);
                }

                // Attach the redirect_uri to allow for later comparison when
                // receiving a grant_type=authorization_code token request.
                if (!string.IsNullOrEmpty(context.Request.RedirectUri))
                {
                    principal.SetClaim(Claims.Private.OriginalRedirectUri, context.Request.RedirectUri);
                }

                // Attach the code challenge and the code challenge methods to allow the ValidateCodeVerifier
                // handler to validate the code verifier sent by the client as part of the token request.
                if (!string.IsNullOrEmpty(context.Request.CodeChallenge))
                {
                    principal.SetClaim(Claims.Private.CodeChallenge, context.Request.CodeChallenge);

                    // Default to S256 if no explicit code challenge method was specified.
                    principal.SetClaim(Claims.Private.CodeChallengeMethod,
                                       !string.IsNullOrEmpty(context.Request.CodeChallengeMethod) ?
                                       context.Request.CodeChallengeMethod : CodeChallengeMethods.Sha256);
                }

                // Attach the nonce so that it can be later returned by
                // the token endpoint as part of the JWT identity token.
                if (!string.IsNullOrEmpty(context.Request.Nonce))
                {
                    principal.SetClaim(Claims.Nonce, context.Request.Nonce);
                }

                var notification = new SerializeAuthorizationCodeContext(context.Transaction)
                {
                    Principal = principal
                };

                await _provider.DispatchAsync(notification);

                if (!notification.IsHandled)
                {
                    throw new InvalidOperationException(new StringBuilder()
                                                        .Append("The authorization code was not correctly processed. This may indicate ")
                                                        .Append("that the event handler responsible of generating authorization codes ")
                                                        .Append("was not registered or was explicitly removed from the handlers list.")
                                                        .ToString());
                }

                context.Response.Code = notification.Token;
            }
 public Task SerializeAuthorizationCode(SerializeAuthorizationCodeContext context)
 {
     throw new NotImplementedException();
 }