| | | 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.Security.Principal; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using CoreWCF.IdentityModel.Claims; |
| | | 10 | | using CoreWCF.IdentityModel.Policy; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using MSSaml = Microsoft.IdentityModel.Tokens.Saml; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 16 | | { |
| | | 17 | | public class SamlSecurityTokenAuthenticator : SecurityTokenAuthenticator |
| | | 18 | | { |
| | | 19 | | private readonly List<SecurityTokenAuthenticator> _supportingAuthenticators; |
| | | 20 | | private AudienceUriMode _audienceUriMode; |
| | | 21 | | private TimeSpan _maxClockSkew; |
| | | 22 | | |
| | | 23 | | public SamlSecurityTokenAuthenticator(IList<SecurityTokenAuthenticator> supportingAuthenticators) |
| | 2 | 24 | | : this(supportingAuthenticators, TimeSpan.Zero) |
| | 2 | 25 | | { } |
| | | 26 | | |
| | 2 | 27 | | public SamlSecurityTokenAuthenticator(IList<SecurityTokenAuthenticator> supportingAuthenticators, TimeSpan maxCl |
| | | 28 | | { |
| | 2 | 29 | | _supportingAuthenticators = new List<SecurityTokenAuthenticator>(supportingAuthenticators ?? |
| | 2 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(supportingAuthenticators))); |
| | 2 | 31 | | _maxClockSkew = maxClockSkew; |
| | 2 | 32 | | _audienceUriMode = AudienceUriMode.Always; |
| | 2 | 33 | | } |
| | | 34 | | |
| | | 35 | | public AudienceUriMode AudienceUriMode |
| | | 36 | | { |
| | 0 | 37 | | get { return _audienceUriMode; } |
| | | 38 | | set |
| | | 39 | | { |
| | 0 | 40 | | AudienceUriModeValidationHelper.Validate(_audienceUriMode); |
| | 0 | 41 | | _audienceUriMode = value; |
| | 0 | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | public IList<string> AllowedAudienceUris { get; } = new Collection<string>(); |
| | | 46 | | |
| | | 47 | | protected override bool CanValidateTokenCore(SecurityToken token) |
| | | 48 | | { |
| | 188 | 49 | | return token is SamlSecurityToken; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | protected override async ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToke |
| | | 53 | | { |
| | 0 | 54 | | if (token == null) |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 56 | | |
| | | 57 | | |
| | 0 | 58 | | if (!(token is SamlSecurityToken samlToken)) |
| | 0 | 59 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SamlTo |
| | | 60 | | |
| | 0 | 61 | | if (samlToken.Assertion.Signature == null) |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SamlTo |
| | | 63 | | |
| | 0 | 64 | | if (!IsCurrentlyTimeEffective(samlToken)) |
| | 0 | 65 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SAMLTo |
| | | 66 | | |
| | 0 | 67 | | if (samlToken.SigningToken == null) |
| | 0 | 68 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SamlSi |
| | | 69 | | |
| | | 70 | | // Build the Issuer ClaimSet for this Saml token. |
| | 0 | 71 | | ClaimSet issuer = null; |
| | 0 | 72 | | bool canBeValidated = false; |
| | 0 | 73 | | for (int i = 0; i < _supportingAuthenticators.Count; ++i) |
| | | 74 | | { |
| | 0 | 75 | | canBeValidated = _supportingAuthenticators[i].CanValidateToken(samlToken.SigningToken); |
| | 0 | 76 | | if (canBeValidated) |
| | | 77 | | break; |
| | | 78 | | } |
| | 0 | 79 | | if (!canBeValidated) |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SamlIn |
| | | 81 | | |
| | 0 | 82 | | issuer = (await ResolveClaimSetAsyc(samlToken.SigningToken)) ?? ClaimSet.Anonymous; |
| | | 83 | | |
| | 0 | 84 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(); |
| | 0 | 85 | | foreach (InternalSamlSubjectStatement subject in samlToken.SamlStatements) |
| | | 86 | | { |
| | 0 | 87 | | policies.Add(await subject.CreatePolicyAsync(issuer, this)); |
| | | 88 | | } |
| | | 89 | | |
| | 0 | 90 | | if ((_audienceUriMode == AudienceUriMode.Always) |
| | 0 | 91 | | || (_audienceUriMode == AudienceUriMode.BearerKeyOnly) && (samlToken.SecurityKeys.Count < 1)) |
| | | 92 | | { |
| | | 93 | | // throws if not found. |
| | 0 | 94 | | bool foundAudienceCondition = false; |
| | 0 | 95 | | if (AllowedAudienceUris == null) |
| | | 96 | | { |
| | 0 | 97 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SA |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | foreach (MSSaml.SamlCondition samlCondition in samlToken.Assertion.Conditions.Conditions) |
| | | 101 | | { |
| | 0 | 102 | | if (samlCondition is MSSaml.SamlAudienceRestrictionCondition condition) |
| | | 103 | | { |
| | 0 | 104 | | foundAudienceCondition = true; |
| | 0 | 105 | | if (!ValidateAudienceRestriction(condition)) |
| | | 106 | | { |
| | 0 | 107 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Form |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | if (!foundAudienceCondition) |
| | 0 | 113 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SA |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return policies.AsReadOnly(); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | protected virtual bool ValidateAudienceRestriction(MSSaml.SamlAudienceRestrictionCondition audienceRestrictionCo |
| | | 120 | | { |
| | 0 | 121 | | foreach (Uri audienceUri in audienceRestrictionCondition.Audiences) |
| | | 122 | | { |
| | 0 | 123 | | if (audienceUri == null) |
| | | 124 | | continue; |
| | | 125 | | |
| | 0 | 126 | | for (int j = 0; j < AllowedAudienceUris.Count; j++) |
| | | 127 | | { |
| | 0 | 128 | | if (StringComparer.Ordinal.Compare(audienceUri.AbsoluteUri, AllowedAudienceUris[j]) == 0) |
| | 0 | 129 | | return true; |
| | 0 | 130 | | else if (Uri.IsWellFormedUriString(AllowedAudienceUris[j], UriKind.Absolute)) |
| | | 131 | | { |
| | 0 | 132 | | Uri uri = new Uri(AllowedAudienceUris[j]); |
| | 0 | 133 | | if (audienceUri.Equals(uri)) |
| | 0 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | return false; |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | public virtual async ValueTask<ClaimSet> ResolveClaimSetAsyc(SecurityToken token) |
| | | 143 | | { |
| | 0 | 144 | | if (token == null) |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 146 | | |
| | 0 | 147 | | foreach (var authenticator in _supportingAuthenticators) |
| | | 148 | | { |
| | 0 | 149 | | if (authenticator.CanValidateToken(token)) |
| | | 150 | | { |
| | 0 | 151 | | var authorizationPolicies = await authenticator.ValidateTokenAsync(token); ; |
| | 0 | 152 | | AuthorizationContext authContext = AuthorizationContext.CreateDefaultAuthorizationContext(authorizat |
| | 0 | 153 | | if (authContext.ClaimSets.Count > 0) |
| | | 154 | | { |
| | 0 | 155 | | return authContext.ClaimSets[0]; |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | 0 | 160 | | return null; |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | public virtual ClaimSet ResolveClaimSet(SecurityKeyIdentifier keyIdentifier) |
| | | 164 | | { |
| | 0 | 165 | | if (keyIdentifier == null) |
| | 0 | 166 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 167 | | |
| | 0 | 168 | | if (keyIdentifier.TryFind(out RsaKeyIdentifierClause rsaKeyIdentifierClause)) |
| | | 169 | | { |
| | 0 | 170 | | return new DefaultClaimSet(new Claim(ClaimTypes.Rsa, rsaKeyIdentifierClause.Rsa, Rights.PossessProperty) |
| | | 171 | | } |
| | 0 | 172 | | else if (keyIdentifier.TryFind(out EncryptedKeyIdentifierClause encryptedKeyIdentifierClause)) |
| | | 173 | | { |
| | 0 | 174 | | return new DefaultClaimSet(Claim.CreateHashClaim(encryptedKeyIdentifierClause.GetBuffer())); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | return null; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | public virtual async ValueTask<IIdentity> ResolveIdentityAsync(SecurityToken token) |
| | | 181 | | { |
| | 0 | 182 | | if (token == null) |
| | 0 | 183 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 184 | | |
| | 0 | 185 | | for (int i = 0; i < _supportingAuthenticators.Count; ++i) |
| | | 186 | | { |
| | 0 | 187 | | if (_supportingAuthenticators[i].CanValidateToken(token)) |
| | | 188 | | { |
| | 0 | 189 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies = await _supportingAuthenticators[i]. |
| | 0 | 190 | | if (authorizationPolicies != null && authorizationPolicies.Count != 0) |
| | | 191 | | { |
| | 0 | 192 | | for (int j = 0; j < authorizationPolicies.Count; ++j) |
| | | 193 | | { |
| | 0 | 194 | | IAuthorizationPolicy policy = authorizationPolicies[j]; |
| | 0 | 195 | | if (policy is UnconditionalPolicy policy1) |
| | | 196 | | { |
| | 0 | 197 | | return policy1.PrimaryIdentity; |
| | | 198 | | } |
| | | 199 | | } |
| | | 200 | | } |
| | | 201 | | } |
| | | 202 | | } |
| | | 203 | | |
| | 0 | 204 | | return null; |
| | 0 | 205 | | } |
| | | 206 | | |
| | | 207 | | public virtual IIdentity ResolveIdentity(SecurityKeyIdentifier keyIdentifier) |
| | | 208 | | { |
| | 0 | 209 | | if (keyIdentifier == null) |
| | 0 | 210 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 211 | | |
| | 0 | 212 | | if (keyIdentifier.TryFind(out RsaKeyIdentifierClause rsaKeyIdentifierClause)) |
| | | 213 | | { |
| | 0 | 214 | | return SecurityUtils.CreateIdentity(rsaKeyIdentifierClause.Rsa.ToXmlString(false), GetType().Name); |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | return null; |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private bool IsCurrentlyTimeEffective(SamlSecurityToken token) |
| | | 221 | | { |
| | 0 | 222 | | if (token.Assertion.Conditions != null) |
| | | 223 | | { |
| | 0 | 224 | | return SecurityUtils.IsCurrentlyTimeEffective(token.Assertion.Conditions.NotBefore, token.Assertion.Cond |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | // If SAML Condition is not present then the assertion is valid at any given time. |
| | 0 | 228 | | return true; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | } |
| | | 232 | | |
| | | 233 | | } |