| | | 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 CoreWCF.IdentityModel.Policy; |
| | | 7 | | using CoreWCF.IdentityModel.Tokens; |
| | | 8 | | using CoreWCF.Security.Tokens; |
| | | 9 | | using SysClaim = CoreWCF.IdentityModel.Claims.Claim; |
| | | 10 | | using SystemAuthorizationContext = CoreWCF.IdentityModel.Policy.AuthorizationContext; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Security |
| | | 13 | | { |
| | | 14 | | internal class SctClaimsHandler |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates an instance of <see cref="SctClaimsHandler"/> |
| | | 18 | | /// </summary> |
| | 0 | 19 | | public SctClaimsHandler( |
| | 0 | 20 | | SecurityTokenHandlerCollection securityTokenHandlerCollection, |
| | 0 | 21 | | string endpointId) |
| | | 22 | | { |
| | 0 | 23 | | if (securityTokenHandlerCollection == null) |
| | | 24 | | { |
| | 0 | 25 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandlerCollection)) |
| | | 26 | | } |
| | | 27 | | |
| | 0 | 28 | | if (endpointId == null) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointId)); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | SecurityTokenHandlerCollection = securityTokenHandlerCollection; |
| | 0 | 34 | | EndpointId = endpointId; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the Endpoint Id to which all <see cref="SecurityContextSecurityToken"/> should be scoped. |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public string EndpointId { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the <see cref="SecurityTokenHandlerCollection" /> used to validate the SCT. |
| | | 44 | | /// </summary> |
| | 0 | 45 | | public SecurityTokenHandlerCollection SecurityTokenHandlerCollection { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// The the purposes of this method are: |
| | | 49 | | /// 1. To enable layers above to get to the bootstrap tokens |
| | | 50 | | /// 2. To ensure an ClaimsPrincipal is inside the SCT authorization policies. This is needed so that |
| | | 51 | | /// a CustomPrincipal will be created and can be set. This is required as we set the principal permission mo |
| | | 52 | | /// 3. To set the IAuthorizationPolicy collection on the SCT to be one of IDFx's Authpolicy. |
| | | 53 | | /// This allows SCT cookie and SCT cached to be treated the same, futher up the stack. |
| | | 54 | | /// |
| | | 55 | | /// This method is call AFTER the final SCT has been created and the bootstrap tokens are around. Itis not call |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="sct"></param> |
| | | 58 | | internal void SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(SecurityContextSecurityToken sct) |
| | | 59 | | { |
| | 0 | 60 | | if (sct == null) |
| | | 61 | | { |
| | 0 | 62 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sct)); |
| | | 63 | | } |
| | | 64 | | |
| | 0 | 65 | | List<IAuthorizationPolicy> iaps = new List<IAuthorizationPolicy>(); |
| | | 66 | | |
| | | 67 | | // |
| | | 68 | | // The SecurityContextToken is cached first before the OnTokenIssued is called. So in the Session SCT |
| | | 69 | | // case the AuthorizationPolicies will have already been updated. So check the sct.AuthorizationPolicies |
| | | 70 | | // policy to see if the first is a AuthorizationPolicy. |
| | | 71 | | // |
| | 0 | 72 | | if ((sct.AuthorizationPolicies != null) && |
| | 0 | 73 | | (sct.AuthorizationPolicies.Count > 0) && |
| | 0 | 74 | | (ContainsEndpointAuthPolicy(sct.AuthorizationPolicies))) |
| | | 75 | | { |
| | | 76 | | // We have already seen this sct and have fixed up the AuthorizationPolicy |
| | | 77 | | // collection. Just return. |
| | 0 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | // |
| | | 82 | | // Nego SCT just has a cookie, there are no IAuthorizationPolicy. In this case, |
| | | 83 | | // we want to add the EndpointAuthorizationPolicy alone to the SCT. |
| | | 84 | | // |
| | 0 | 85 | | if ((sct.AuthorizationPolicies != null) && |
| | 0 | 86 | | (sct.AuthorizationPolicies.Count > 0)) |
| | | 87 | | { |
| | | 88 | | // |
| | | 89 | | // Create a principal with known policies. |
| | | 90 | | // |
| | 0 | 91 | | AuthorizationPolicy sctAp = IdentityModelServiceAuthorizationManager.TransformAuthorizationPolicies(sct. |
| | 0 | 92 | | Sec |
| | 0 | 93 | | fal |
| | | 94 | | // Replace the WCF authorization policies with our IDFx policies. |
| | | 95 | | // The principal is needed later on to set the custom principal by WCF runtime. |
| | 0 | 96 | | iaps.Add(sctAp); |
| | | 97 | | |
| | | 98 | | // |
| | | 99 | | // Convert the claim from WCF unconditional policy to an SctAuthorizationPolicy. The SctAuthorizationPol |
| | | 100 | | // captures the primary identity claim from the WCF unconditional policy which IdFX will eventually thro |
| | | 101 | | // If we don't capture that claim, then in a token renewal scenario WCF will fail due to identities bein |
| | | 102 | | // for the issuedToken and the renewedToken. |
| | | 103 | | // |
| | 0 | 104 | | SysClaim claim = GetPrimaryIdentityClaim(SystemAuthorizationContext.CreateDefaultAuthorizationContext(sc |
| | | 105 | | |
| | 0 | 106 | | SctAuthorizationPolicy sctAuthPolicy = new SctAuthorizationPolicy(claim); |
| | 0 | 107 | | iaps.Add(sctAuthPolicy); |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | iaps.Add(new EndpointAuthorizationPolicy(EndpointId)); |
| | 0 | 111 | | sct.AuthorizationPolicies = iaps.AsReadOnly(); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | private bool ContainsEndpointAuthPolicy(ReadOnlyCollection<IAuthorizationPolicy> policies) |
| | | 115 | | { |
| | 0 | 116 | | if (policies == null) |
| | | 117 | | { |
| | 0 | 118 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policies)); |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | for (int i = 0; i < policies.Count; ++i) |
| | | 122 | | { |
| | 0 | 123 | | if (policies[i] is EndpointAuthorizationPolicy) |
| | | 124 | | { |
| | 0 | 125 | | return true; |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | return false; |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Gets the primary identity claim to create the SCTAuthorizationPolicy |
| | | 134 | | /// </summary> |
| | | 135 | | /// <param name="authContext">The authorization context</param> |
| | | 136 | | /// <returns>The primary identity claim from the authorization context.</returns> |
| | | 137 | | private SysClaim GetPrimaryIdentityClaim(SystemAuthorizationContext authContext) |
| | | 138 | | { |
| | 0 | 139 | | if (authContext != null) |
| | | 140 | | { |
| | 0 | 141 | | for (int i = 0; i < authContext.ClaimSets.Count; ++i) |
| | | 142 | | { |
| | 0 | 143 | | CoreWCF.IdentityModel.Claims.ClaimSet claimSet = authContext.ClaimSets[i]; |
| | 0 | 144 | | foreach (CoreWCF.IdentityModel.Claims.Claim claim in claimSet.FindClaims(null, CoreWCF.IdentityModel |
| | | 145 | | { |
| | 0 | 146 | | return claim; |
| | | 147 | | } |
| | | 148 | | } |
| | | 149 | | } |
| | 0 | 150 | | return null; |
| | 0 | 151 | | } |
| | | 152 | | |
| | | 153 | | public void OnTokenIssued(SecurityToken issuedToken, EndpointAddress tokenRequestor) |
| | | 154 | | { |
| | 0 | 155 | | SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(issuedToken as SecurityContextSecurityToken); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | public void OnTokenRenewed(SecurityToken issuedToken, SecurityToken oldToken) |
| | | 159 | | { |
| | 0 | 160 | | SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(issuedToken as SecurityContextSecurityToken); |
| | 0 | 161 | | } |
| | | 162 | | } |
| | | 163 | | } |