| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using System.Security.Principal; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.IdentityModel.Claims; |
| | | 9 | | using CoreWCF.IdentityModel.Policy; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 13 | | { |
| | | 14 | | public class CustomUserNameSecurityTokenAuthenticator : UserNameSecurityTokenAuthenticator |
| | | 15 | | { |
| | | 16 | | private readonly UserNamePasswordValidator _validator; |
| | | 17 | | |
| | 17 | 18 | | public CustomUserNameSecurityTokenAuthenticator(UserNamePasswordValidator validator) |
| | | 19 | | { |
| | 17 | 20 | | _validator = validator ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(validator) |
| | 17 | 21 | | } |
| | | 22 | | |
| | | 23 | | protected override async ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateUserNamePasswordCoreAsync(s |
| | | 24 | | { |
| | 16 | 25 | | await _validator.ValidateAsync(userName, password); |
| | 12 | 26 | | return SecurityUtils.CreateAuthorizationPolicies(new UserNameClaimSet(userName, _validator.GetType().Name)); |
| | 12 | 27 | | } |
| | | 28 | | |
| | | 29 | | private class UserNameClaimSet : DefaultClaimSet, IIdentityInfo |
| | | 30 | | { |
| | 12 | 31 | | public UserNameClaimSet(string userName, string authType) |
| | | 32 | | { |
| | 12 | 33 | | if (userName == null) |
| | | 34 | | { |
| | 0 | 35 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(userName)); |
| | | 36 | | } |
| | | 37 | | |
| | 12 | 38 | | Identity = SecurityUtils.CreateIdentity(userName, authType); |
| | | 39 | | |
| | 12 | 40 | | List<Claim> claims = new List<Claim>(2) |
| | 12 | 41 | | { |
| | 12 | 42 | | new Claim(ClaimTypes.Name, userName, Rights.Identity), |
| | 12 | 43 | | Claim.CreateNameClaim(userName) |
| | 12 | 44 | | }; |
| | 12 | 45 | | Initialize(System, claims); |
| | 12 | 46 | | } |
| | | 47 | | |
| | 12 | 48 | | public IIdentity Identity { get; } |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | } |