private async Task <AuthenticationTicket> DeserializeAccessTokenAsync(string token, OpenIdConnectRequest request) { var notification = new DeserializeAccessTokenContext(Context, Scheme, Options, request, token) { DataFormat = Options.AccessTokenFormat, SecurityTokenHandler = Options.AccessTokenHandler }; // Note: ValidateAudience and ValidateLifetime are always set to false: // if necessary, the audience and the expiration can be validated // in InvokeIntrospectionEndpointAsync or InvokeTokenEndpointAsync. notification.TokenValidationParameters = new TokenValidationParameters { IssuerSigningKeys = Options.SigningCredentials.Select(credentials => credentials.Key), NameClaimType = OpenIdConnectConstants.Claims.Name, RoleClaimType = OpenIdConnectConstants.Claims.Role, TokenDecryptionKeys = Options.EncryptingCredentials.Select(credentials => credentials.Key) .Where(key => key is SymmetricSecurityKey), ValidIssuer = Context.GetIssuer(Options), ValidateAudience = false, ValidateLifetime = false }; await Provider.DeserializeAccessToken(notification); if (notification.IsHandled || notification.Ticket != null) { notification.Ticket?.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken); return(notification.Ticket); } var handler = notification.SecurityTokenHandler as ISecurityTokenValidator; if (handler == null) { if (notification.DataFormat == null) { throw new InvalidOperationException("A security token handler or data formatter must be provided."); } var value = notification.DataFormat.Unprotect(token); if (value == null) { Logger.LogTrace("The received token was invalid or malformed: {Token}.", token); return(null); } // Note: since the data formatter relies on a data protector using different "purposes" strings // per token type, the ticket returned by Unprotect() is guaranteed to be an access token. value.SetTokenUsage(OpenIdConnectConstants.TokenUsages.AccessToken); Logger.LogTrace("The access token '{Token}' was successfully validated using " + "the specified token data format: {Claims} ; {Properties}.", token, value.Principal.Claims, value.Properties.Items); return(value); } SecurityToken securityToken; ClaimsPrincipal principal; try { if (!handler.CanReadToken(token)) { Logger.LogTrace("The access token '{Token}' was rejected by the security token handler.", token); return(null); } principal = handler.ValidateToken(token, notification.TokenValidationParameters, out securityToken); } catch (Exception exception) { Logger.LogDebug("An exception occured while deserializing an identity token: {Exception}.", exception); return(null); } // Parameters stored in AuthenticationProperties are lost // when the identity token is serialized using a security token handler. // To mitigate that, they are inferred from the claims or the security token. var properties = new AuthenticationProperties { ExpiresUtc = securityToken.ValidTo, IssuedUtc = securityToken.ValidFrom }; var ticket = new AuthenticationTicket(principal, properties, Scheme.Name) .SetAudiences(principal.FindAll(OpenIdConnectConstants.Claims.Audience).Select(claim => claim.Value)) .SetConfidentialityLevel(principal.GetClaim(OpenIdConnectConstants.Claims.ConfidentialityLevel)) .SetPresenters(principal.FindAll(OpenIdConnectConstants.Claims.AuthorizedParty).Select(claim => claim.Value)) .SetScopes(principal.FindAll(OpenIdConnectConstants.Claims.Scope).Select(claim => claim.Value)) .SetTokenId(principal.GetClaim(OpenIdConnectConstants.Claims.JwtId)) .SetTokenUsage(principal.GetClaim(OpenIdConnectConstants.Claims.TokenUsage)); // Ensure that the received ticket is an access token. if (!ticket.IsAccessToken()) { Logger.LogTrace("The received token was not an access token: {Token}.", token); return(null); } Logger.LogTrace("The access token '{Token}' was successfully validated using " + "the specified security token handler: {Claims} ; {Properties}.", token, ticket.Principal.Claims, ticket.Properties.Items); return(ticket); }
/// <summary> /// Represents an event called when deserializing an access token. /// </summary> /// <param name="context">The context instance associated with this event.</param> /// <returns>A <see cref="Task"/> that can be used to monitor the asynchronous operation.</returns> public virtual Task DeserializeAccessToken(DeserializeAccessTokenContext context) => OnDeserializeAccessToken(context);