| | | 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.Selectors; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using MSIdentitySAML = Microsoft.IdentityModel.Tokens.Saml; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 16 | | { |
| | | 17 | | internal class InternalSamlSubjectStatement |
| | | 18 | | { |
| | | 19 | | private readonly MSIdentitySAML.SamlSubjectStatement _samlSubjectStatement; |
| | | 20 | | private readonly MSIdentitySAML.SamlSubject _samlSubject; |
| | | 21 | | readonly SecurityKeyIdentifier _securityKeyIdentifier; |
| | | 22 | | private readonly SecurityToken _subjectToken; |
| | | 23 | | private IIdentity _identity; |
| | | 24 | | private ClaimSet _subjectKeyClaimset; |
| | | 25 | | private IAuthorizationPolicy _policy; |
| | | 26 | | |
| | 4 | 27 | | public InternalSamlSubjectStatement(MSIdentitySAML.SamlSubjectStatement samlSubjectStatement, |
| | 4 | 28 | | SecurityKeyIdentifier securityKeyIdentifier, SecurityToken securityToken) |
| | | 29 | | { |
| | 4 | 30 | | _samlSubjectStatement = samlSubjectStatement; |
| | 4 | 31 | | _samlSubject = _samlSubjectStatement.Subject; |
| | 4 | 32 | | _securityKeyIdentifier = securityKeyIdentifier; |
| | 4 | 33 | | _subjectToken = securityToken; |
| | 4 | 34 | | } |
| | | 35 | | |
| | | 36 | | //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode |
| | | 37 | | internal async ValueTask<IAuthorizationPolicy> CreatePolicyAsync(ClaimSet issuer, SamlSecurityTokenAuthenticator |
| | | 38 | | { |
| | 0 | 39 | | if (issuer == null) |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuer)); |
| | | 41 | | |
| | 0 | 42 | | if (_policy == null) |
| | | 43 | | { |
| | 0 | 44 | | List<ClaimSet> claimSets = new List<ClaimSet>(); |
| | 0 | 45 | | ClaimSet subjectKeyClaimset = await ExtractSubjectKeyClaimSetAsync(samlAuthenticator); |
| | 0 | 46 | | if (subjectKeyClaimset != null) |
| | 0 | 47 | | claimSets.Add(subjectKeyClaimset); |
| | | 48 | | |
| | 0 | 49 | | List<Claim> claims = new List<Claim>(); |
| | 0 | 50 | | ReadOnlyCollection<Claim> subjectClaims = ExtractClaims(); |
| | 0 | 51 | | for (int i = 0; i < subjectClaims.Count; ++i) |
| | | 52 | | { |
| | 0 | 53 | | claims.Add(subjectClaims[i]); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | AddClaimsToList(claims); |
| | 0 | 57 | | claimSets.Add(new DefaultClaimSet(issuer, claims)); |
| | 0 | 58 | | _policy = new UnconditionalPolicy(_identity, claimSets.AsReadOnly(), SecurityUtils.MaxUtcDateTime); |
| | 0 | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | return _policy; |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | private void AddClaimsToList(List<Claim> claims) |
| | | 65 | | { |
| | 0 | 66 | | if (_samlSubjectStatement is MSIdentitySAML.SamlAuthorizationDecisionStatement samlAuthorizationStatement) |
| | | 67 | | { |
| | | 68 | | //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.Iden |
| | 0 | 69 | | foreach (MSIdentitySAML.SamlAction action in samlAuthorizationStatement.Actions) |
| | | 70 | | { |
| | 0 | 71 | | Enum.TryParse(samlAuthorizationStatement.Decision, out SamlAccessDecision decision); |
| | 0 | 72 | | claims.Add(new Claim(ClaimTypes.AuthorizationDecision, new SamlAuthorizationDecisionClaimResource(sa |
| | | 73 | | } |
| | | 74 | | } |
| | 0 | 75 | | else if (_samlSubjectStatement is MSIdentitySAML.SamlAuthenticationStatement) |
| | | 76 | | { |
| | | 77 | | //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.Iden |
| | 0 | 78 | | MSIdentitySAML.SamlAuthenticationStatement samlAuthStatement = _samlSubjectStatement as MSIdentitySAML.S |
| | 0 | 79 | | claims.Add(new Claim(ClaimTypes.Authentication, new SamlAuthenticationClaimResource(samlAuthStatement.Au |
| | | 80 | | } |
| | 0 | 81 | | else if (_samlSubjectStatement is MSIdentitySAML.SamlAttributeStatement attributeStatement) |
| | | 82 | | { |
| | 0 | 83 | | foreach (MSIdentitySAML.SamlAttribute attribute in attributeStatement.Attributes) |
| | | 84 | | { |
| | 0 | 85 | | foreach (string attrVal in attribute.Values) |
| | | 86 | | { |
| | 0 | 87 | | claims.Add(new Claim(attribute.ClaimType, attrVal, Rights.PossessProperty)); |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode |
| | | 94 | | private async ValueTask<ClaimSet> ExtractSubjectKeyClaimSetAsync(SamlSecurityTokenAuthenticator samlAuthenticato |
| | | 95 | | { |
| | 0 | 96 | | if (_samlSubject.KeyInfo != null) |
| | | 97 | | { |
| | 0 | 98 | | if (samlAuthenticator == null) |
| | 0 | 99 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(samlAuthenticator)); |
| | | 100 | | |
| | 0 | 101 | | if (_subjectToken != null) |
| | | 102 | | { |
| | 0 | 103 | | _subjectKeyClaimset = samlAuthenticator.ResolveClaimSet(_securityKeyIdentifier); |
| | 0 | 104 | | _identity = await samlAuthenticator.ResolveIdentityAsync(_subjectToken); |
| | 0 | 105 | | if ((_identity == null) && (_subjectKeyClaimset != null)) |
| | | 106 | | { |
| | 0 | 107 | | Claim identityClaim = null; |
| | 0 | 108 | | foreach (Claim claim in _subjectKeyClaimset.FindClaims(null, Rights.Identity)) |
| | | 109 | | { |
| | 0 | 110 | | identityClaim = claim; |
| | 0 | 111 | | break; |
| | | 112 | | } |
| | | 113 | | |
| | 0 | 114 | | if (identityClaim != null) |
| | | 115 | | { |
| | 0 | 116 | | _identity = SecurityUtils.CreateIdentity(identityClaim.Resource.ToString(), GetType().Name); |
| | | 117 | | } |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (_subjectKeyClaimset == null) |
| | | 122 | | { |
| | | 123 | | // Add the type of the primary claim as the Identity claim. |
| | 0 | 124 | | _subjectKeyClaimset = samlAuthenticator.ResolveClaimSet(_securityKeyIdentifier); |
| | 0 | 125 | | _identity = samlAuthenticator.ResolveIdentity(_securityKeyIdentifier); |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return _subjectKeyClaimset; |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode |
| | | 133 | | private ReadOnlyCollection<Claim> ExtractClaims() |
| | | 134 | | { |
| | 0 | 135 | | var claims = new List<Claim>(); |
| | 0 | 136 | | if (!string.IsNullOrEmpty(_samlSubject.Name)) |
| | | 137 | | { |
| | 0 | 138 | | claims.Add(new Claim(ClaimTypes.NameIdentifier, new SamlNameIdentifierClaimResource(_samlSubject.Name, _ |
| | 0 | 139 | | claims.Add(new Claim(ClaimTypes.NameIdentifier, new SamlNameIdentifierClaimResource(_samlSubject.Name, _ |
| | | 140 | | } |
| | | 141 | | |
| | 0 | 142 | | return claims.AsReadOnly(); |
| | | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |