public async Task<Dictionary<string, object>> ProcessAsync(string subject, IEnumerable<string> scopes, Client client) { _logger.LogVerbose("Creating userinfo response"); var profileData = new Dictionary<string, object>(); var requestedClaimTypes = await GetRequestedClaimTypesAsync(scopes); var principal = Principal.Create("UserInfo", new Claim("sub", subject)); IEnumerable<Claim> profileClaims; if (requestedClaimTypes.IncludeAllClaims) { _logger.LogInformation("Requested claim types: all"); var context = new ProfileDataRequestContext( principal, client, Constants.ProfileDataCallers.UserInfoEndpoint); await _profile.GetProfileDataAsync(context); profileClaims = context.IssuedClaims; } else { _logger.LogInformation("Requested claim types: {types}", requestedClaimTypes.ClaimTypes.ToSpaceSeparatedString()); var context = new ProfileDataRequestContext( principal, client, Constants.ProfileDataCallers.UserInfoEndpoint, requestedClaimTypes.ClaimTypes); await _profile.GetProfileDataAsync(context); profileClaims = context.IssuedClaims; } if (profileClaims != null) { profileData = profileClaims.ToClaimsDictionary(); _logger.LogInformation("Profile service returned to the following claim types: {types}", profileClaims.Select(c => c.Type).ToSpaceSeparatedString()); } else { _logger.LogInformation("Profile service returned no claims (null)"); } return profileData; }
/// <summary> /// This method is called whenever claims about the user are requested (e.g. during token creation or via the userinfo endpoint) /// </summary> /// <param name="context">The context.</param> /// <returns></returns> public Task GetProfileDataAsync(ProfileDataRequestContext context) { var query = from u in _users where u.Subject == context.Subject.GetSubjectId() select u; var user = query.Single(); var claims = new List<Claim>{ new Claim(JwtClaimTypes.Subject, user.Subject), }; claims.AddRange(user.Claims); if (!context.AllClaimsRequested) { claims = claims.Where(x => context.RequestedClaimTypes.Contains(x.Type)).ToList(); } context.IssuedClaims = claims; return Task.FromResult(0); }
public Task GetProfileDataAsync(ProfileDataRequestContext context) { return Task.FromResult(0); }
/// <summary> /// Returns claims for an identity token /// </summary> /// <param name="subject">The subject</param> /// <param name="client">The client</param> /// <param name="scopes">The requested scopes</param> /// <param name="includeAllIdentityClaims">Specifies if all claims should be included in the token, or if the userinfo endpoint can be used to retrieve them</param> /// <param name="request">The raw request</param> /// <returns> /// Claims for the identity token /// </returns> public virtual async Task<IEnumerable<Claim>> GetIdentityTokenClaimsAsync(ClaimsPrincipal subject, Client client, IEnumerable<Scope> scopes, bool includeAllIdentityClaims, ValidatedRequest request) { _logger.LogInformation("Getting claims for identity token for subject: " + subject.GetSubjectId()); var outputClaims = new List<Claim>(GetStandardSubjectClaims(subject)); outputClaims.AddRange(GetOptionalClaims(subject)); var additionalClaims = new List<string>(); // if a include all claims rule exists, call the user service without a claims filter if (scopes.IncludesAllClaimsForUserRule(ScopeType.Identity)) { _logger.LogInformation("All claims rule found - emitting all claims for user."); var context = new ProfileDataRequestContext( subject, client, Constants.ProfileDataCallers.ClaimsProviderIdentityToken); await _profile.GetProfileDataAsync(context); var claims = FilterProtocolClaims(context.IssuedClaims); if (claims != null) { outputClaims.AddRange(claims); } return outputClaims; } // fetch all identity claims that need to go into the id token foreach (var scope in scopes) { if (scope.Type == ScopeType.Identity) { foreach (var scopeClaim in scope.Claims) { if (includeAllIdentityClaims || scopeClaim.AlwaysIncludeInIdToken) { additionalClaims.Add(scopeClaim.Name); } } } } if (additionalClaims.Count > 0) { var context = new ProfileDataRequestContext( subject, client, Constants.ProfileDataCallers.ClaimsProviderIdentityToken, additionalClaims); await _profile.GetProfileDataAsync(context); var claims = FilterProtocolClaims(context.IssuedClaims); if (claims != null) { outputClaims.AddRange(claims); } } return outputClaims; }
/// <summary> /// Returns claims for an identity token. /// </summary> /// <param name="subject">The subject.</param> /// <param name="client">The client.</param> /// <param name="scopes">The requested scopes.</param> /// <param name="request">The raw request.</param> /// <returns> /// Claims for the access token /// </returns> public virtual async Task<IEnumerable<Claim>> GetAccessTokenClaimsAsync(ClaimsPrincipal subject, Client client, IEnumerable<Scope> scopes, ValidatedRequest request) { // add client_id var outputClaims = new List<Claim> { new Claim(JwtClaimTypes.ClientId, client.ClientId), }; // check for client claims if (client.Claims != null && client.Claims.Any()) { if (subject == null || client.AlwaysSendClientClaims) { foreach (var claim in client.Claims) { var claimType = claim.Type; if (client.PrefixClientClaims) { claimType = "client_" + claimType; } outputClaims.Add(new Claim(claimType, claim.Value, claim.ValueType)); } } } // add scopes foreach (var scope in scopes) { outputClaims.Add(new Claim(JwtClaimTypes.Scope, scope.Name)); } // a user is involved if (subject != null) { outputClaims.AddRange(GetStandardSubjectClaims(subject)); outputClaims.AddRange(GetOptionalClaims(subject)); // if a include all claims rule exists, call the user service without a claims filter if (scopes.IncludesAllClaimsForUserRule(ScopeType.Resource)) { var context = new ProfileDataRequestContext( subject, client, Constants.ProfileDataCallers.ClaimsProviderAccessToken); await _profile.GetProfileDataAsync(context); var claims = FilterProtocolClaims(context.IssuedClaims); if (claims != null) { outputClaims.AddRange(claims); } return outputClaims; } // fetch all resource claims that need to go into the access token var additionalClaims = new List<string>(); foreach (var scope in scopes) { if (scope.Type == ScopeType.Resource) { if (scope.Claims != null) { foreach (var scopeClaim in scope.Claims) { additionalClaims.Add(scopeClaim.Name); } } } } if (additionalClaims.Count > 0) { var context = new ProfileDataRequestContext( subject, client, Constants.ProfileDataCallers.ClaimsProviderAccessToken, additionalClaims.Distinct()); await _profile.GetProfileDataAsync(context); var claims = FilterProtocolClaims(context.IssuedClaims); if (claims != null) { outputClaims.AddRange(claims); } } } return outputClaims; }