| | | 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 | | public class X509SecurityTokenAuthenticator : SecurityTokenAuthenticator |
| | | 16 | | { |
| | | 17 | | private readonly X509CertificateValidator _validator; |
| | | 18 | | private readonly bool _includeWindowsGroups; |
| | | 19 | | private readonly bool _cloneHandle; |
| | | 20 | | |
| | | 21 | | public X509SecurityTokenAuthenticator() |
| | 0 | 22 | | : this(X509CertificateValidator.ChainTrust) |
| | | 23 | | { |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public X509SecurityTokenAuthenticator(X509CertificateValidator validator) |
| | 0 | 27 | | : this(validator, false) |
| | | 28 | | { |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public X509SecurityTokenAuthenticator(X509CertificateValidator validator, bool mapToWindows) |
| | 0 | 32 | | : this(validator, mapToWindows, WindowsClaimSet.DefaultIncludeWindowsGroups) |
| | | 33 | | { |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | public X509SecurityTokenAuthenticator(X509CertificateValidator validator, bool mapToWindows, bool includeWindows |
| | 16 | 37 | | : this(validator, mapToWindows, includeWindowsGroups, true) |
| | | 38 | | { |
| | 16 | 39 | | } |
| | | 40 | | |
| | 16 | 41 | | internal X509SecurityTokenAuthenticator(X509CertificateValidator validator, bool mapToWindows, bool includeWindo |
| | | 42 | | { |
| | 16 | 43 | | _validator = validator ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(validator) |
| | 16 | 44 | | MapCertificateToWindowsAccount = mapToWindows; |
| | 16 | 45 | | _includeWindowsGroups = includeWindowsGroups; |
| | 16 | 46 | | _cloneHandle = cloneHandle; |
| | 16 | 47 | | } |
| | | 48 | | |
| | 5 | 49 | | public bool MapCertificateToWindowsAccount { get; } |
| | | 50 | | |
| | | 51 | | protected override bool CanValidateTokenCore(SecurityToken token) |
| | | 52 | | { |
| | 8 | 53 | | return token is X509SecurityToken; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke |
| | | 57 | | { |
| | 5 | 58 | | X509SecurityToken x509Token = (X509SecurityToken)token; |
| | 5 | 59 | | _validator.Validate(x509Token.Certificate); |
| | | 60 | | |
| | 5 | 61 | | X509CertificateClaimSet x509ClaimSet = new X509CertificateClaimSet(x509Token.Certificate, _cloneHandle); |
| | 5 | 62 | | if (!MapCertificateToWindowsAccount) |
| | | 63 | | { |
| | 5 | 64 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(SecurityUtils.CreateAuthorizationPolicies |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | WindowsClaimSet windowsClaimSet; |
| | 0 | 68 | | if (token is X509WindowsSecurityToken x509token) |
| | | 69 | | { |
| | 0 | 70 | | windowsClaimSet = new WindowsClaimSet(x509token.WindowsIdentity, SecurityUtils.AuthTypeCertMap, _include |
| | | 71 | | } |
| | | 72 | | else |
| | | 73 | | { |
| | 0 | 74 | | throw new PlatformNotSupportedException(); |
| | | 75 | | // Ensure NT_AUTH chain policy for certificate account mapping |
| | | 76 | | //X509CertificateValidator.NTAuthChainTrust.Validate(x509Token.Certificate); |
| | | 77 | | |
| | | 78 | | //WindowsIdentity windowsIdentity = null; |
| | | 79 | | //windowsIdentity = KerberosCertificateLogon(x509Token.Certificate); |
| | | 80 | | |
| | | 81 | | |
| | | 82 | | //windowsClaimSet = new WindowsClaimSet(windowsIdentity, SecurityUtils.AuthTypeCertMap, this.includeWind |
| | | 83 | | } |
| | 0 | 84 | | List<ClaimSet> claimSets = new List<ClaimSet>(2) |
| | 0 | 85 | | { |
| | 0 | 86 | | windowsClaimSet, |
| | 0 | 87 | | x509ClaimSet |
| | 0 | 88 | | }; |
| | | 89 | | |
| | 0 | 90 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1) |
| | 0 | 91 | | { |
| | 0 | 92 | | new UnconditionalPolicy(claimSets.AsReadOnly(), x509Token.ValidTo) |
| | 0 | 93 | | }; |
| | 0 | 94 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly()); |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |