| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.IdentityModel.Claims; |
| | | 9 | | using CoreWCF.IdentityModel.Policy; |
| | | 10 | | using CoreWCF.IdentityModel.Tokens; |
| | | 11 | | using CoreWCF.Security; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 14 | | { |
| | | 15 | | internal class GenericSecurityTokenAuthenticator : SecurityTokenAuthenticator |
| | | 16 | | { |
| | | 17 | | private readonly LdapSettings _ldapSettings; |
| | 0 | 18 | | public GenericSecurityTokenAuthenticator() |
| | | 19 | | { |
| | 0 | 20 | | } |
| | | 21 | | |
| | 0 | 22 | | public GenericSecurityTokenAuthenticator(LdapSettings ldapSettings) |
| | | 23 | | { |
| | 0 | 24 | | _ldapSettings = ldapSettings; |
| | 0 | 25 | | } |
| | | 26 | | |
| | | 27 | | protected override bool CanValidateTokenCore(SecurityToken token) |
| | | 28 | | { |
| | 0 | 29 | | return token is GenericIdentitySecurityToken; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | protected override async ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToke |
| | | 33 | | { |
| | 0 | 34 | | var genericToken = (GenericIdentitySecurityToken)token; |
| | 0 | 35 | | string principalName = genericToken.Name; |
| | 0 | 36 | | if (principalName == null) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(principalName)); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | Claim identityClaim; |
| | | 42 | | Claim primaryPrincipal; |
| | 0 | 43 | | if (principalName.Contains("@") || principalName.Contains(@"\")) |
| | | 44 | | { |
| | 0 | 45 | | identityClaim = new Claim(ClaimTypes.Upn, principalName, Rights.Identity); |
| | 0 | 46 | | primaryPrincipal = Claim.CreateUpnClaim(principalName); |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 0 | 50 | | identityClaim = new Claim(ClaimTypes.Spn, principalName, Rights.Identity); |
| | 0 | 51 | | primaryPrincipal = Claim.CreateSpnClaim(principalName); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | List<Claim> claims = new List<Claim>(2) |
| | 0 | 55 | | { |
| | 0 | 56 | | identityClaim, |
| | 0 | 57 | | primaryPrincipal |
| | 0 | 58 | | }; |
| | | 59 | | |
| | | 60 | | |
| | 0 | 61 | | if (_ldapSettings != null) |
| | | 62 | | { |
| | 0 | 63 | | List<Claim> allCaims = await LdapAdapter.RetrieveClaimsAsync(_ldapSettings, genericToken.GenericIdentity |
| | | 64 | | // if this is made async, many other API changes has to happen. COnsidering this is one of the scenario, |
| | 0 | 65 | | foreach (Claim claim in allCaims) |
| | | 66 | | { |
| | 0 | 67 | | claims.Add(claim); |
| | | 68 | | } |
| | | 69 | | } |
| | 0 | 70 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1) |
| | 0 | 71 | | { |
| | 0 | 72 | | new UnconditionalPolicy(genericToken.GenericIdentity, new DefaultClaimSet(ClaimSet.Anonymous, claims)) |
| | 0 | 73 | | }; |
| | 0 | 74 | | return policies.AsReadOnly(); |
| | 0 | 75 | | } |
| | | 76 | | } |
| | | 77 | | } |