/* * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ using Amazon.Extensions.CognitoAuthentication; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace Amazon.AspNetCore.Identity.Cognito { public class CognitoUserClaimsPrincipalFactory : IUserClaimsPrincipalFactory where TUser : CognitoUser { private readonly CognitoUserManager _userManager; private readonly IdentityOptions _identityOptions; private readonly Dictionary claimToAttributesMapping = new Dictionary() { { ClaimTypes.Email, CognitoAttribute.Email }, { ClaimTypes.DateOfBirth, CognitoAttribute.BirthDate }, { ClaimTypes.Surname, CognitoAttribute.FamilyName }, { ClaimTypes.Gender, CognitoAttribute.Gender }, { ClaimTypes.GivenName, CognitoAttribute.GivenName }, { ClaimTypes.Name, CognitoAttribute.Name }, { ClaimTypes.MobilePhone, CognitoAttribute.PhoneNumber }, { ClaimTypes.Webpage, CognitoAttribute.Website } }; public CognitoUserClaimsPrincipalFactory(UserManager userManager, IOptions optionsAccessor) { _userManager = userManager as CognitoUserManager; if (_userManager == null) throw new ArgumentNullException("The userManager must be of type CognitoUserManager", nameof(userManager)); if (optionsAccessor?.Value == null) { throw new ArgumentNullException(nameof(optionsAccessor)); } _identityOptions = optionsAccessor.Value; } public async Task CreateAsync(TUser user) { var claims = await _userManager.GetClaimsAsync(user).ConfigureAwait(false) as List; claimToAttributesMapping.ToList().ForEach(claim => MapClaimTypesToCognito(claims, claim.Key, claim.Value.AttributeName)); claims.Add(new Claim(_identityOptions.ClaimsIdentity.UserNameClaimType, user.Username)); claims.Add(new Claim(_identityOptions.ClaimsIdentity.UserIdClaimType, user.Username)); var roles = await _userManager.GetRolesAsync(user).ConfigureAwait(false); var roleClaimType = _identityOptions.ClaimsIdentity.RoleClaimType; // Roles are claims with a specific schema uri roles.ToList().ForEach(role => claims.Add(new Claim(roleClaimType, role))); var claimsIdentity = new ClaimsIdentity(claims, IdentityConstants.ApplicationScheme, _identityOptions.ClaimsIdentity.UserNameClaimType, roleClaimType); return new ClaimsPrincipal(claimsIdentity); } /// /// Internal method to map System.Security.Claims.ClaimTypes to Cognito Standard Attributes /// /// private void MapClaimTypesToCognito(List claims, string claimType, string cognitoAttribute) { var claim = claims.FirstOrDefault(c => c.Type == cognitoAttribute); if (claim != null) claims.Add(new Claim(claimType, claim.Value)); } } }