< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.SctClaimsHandler
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SctClaimsHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 48
Coverable lines: 48
Total lines: 163
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%440%
SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(...)0%12120%
ContainsEndpointAuthPolicy(...)0%660%
GetPrimaryIdentityClaim(...)0%660%
OnTokenIssued(...)100%110%
OnTokenRenewed(...)100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SctClaimsHandler.cs

#LineLine coverage
 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
 4using System.Collections.Generic;
 5using System.Collections.ObjectModel;
 6using CoreWCF.IdentityModel.Policy;
 7using CoreWCF.IdentityModel.Tokens;
 8using CoreWCF.Security.Tokens;
 9using SysClaim = CoreWCF.IdentityModel.Claims.Claim;
 10using SystemAuthorizationContext = CoreWCF.IdentityModel.Policy.AuthorizationContext;
 11
 12namespace CoreWCF.Security
 13{
 14    internal class SctClaimsHandler
 15    {
 16        /// <summary>
 17        /// Creates an instance of <see cref="SctClaimsHandler"/>
 18        /// </summary>
 019        public SctClaimsHandler(
 020            SecurityTokenHandlerCollection securityTokenHandlerCollection,
 021            string endpointId)
 22        {
 023            if (securityTokenHandlerCollection == null)
 24            {
 025                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandlerCollection))
 26            }
 27
 028            if (endpointId == null)
 29            {
 030                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointId));
 31            }
 32
 033            SecurityTokenHandlerCollection = securityTokenHandlerCollection;
 034            EndpointId = endpointId;
 035        }
 36
 37        /// <summary>
 38        /// Gets the Endpoint Id to which all <see cref="SecurityContextSecurityToken"/> should be scoped.
 39        /// </summary>
 040        public string EndpointId { get; }
 41
 42        /// <summary>
 43        /// Gets the <see cref="SecurityTokenHandlerCollection" /> used to validate the SCT.
 44        /// </summary>
 045        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        {
 060            if (sct == null)
 61            {
 062                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(sct));
 63            }
 64
 065            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            //
 072            if ((sct.AuthorizationPolicies != null) &&
 073                (sct.AuthorizationPolicies.Count > 0) &&
 074                (ContainsEndpointAuthPolicy(sct.AuthorizationPolicies)))
 75            {
 76                // We have already seen this sct and have fixed up the AuthorizationPolicy
 77                // collection. Just return.
 078                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            //
 085            if ((sct.AuthorizationPolicies != null) &&
 086                (sct.AuthorizationPolicies.Count > 0))
 87            {
 88                //
 89                // Create a principal with known policies.
 90                //
 091                AuthorizationPolicy sctAp = IdentityModelServiceAuthorizationManager.TransformAuthorizationPolicies(sct.
 092                                                                                                                     Sec
 093                                                                                                                     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.
 096                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                //
 0104                SysClaim claim = GetPrimaryIdentityClaim(SystemAuthorizationContext.CreateDefaultAuthorizationContext(sc
 105
 0106                SctAuthorizationPolicy sctAuthPolicy = new SctAuthorizationPolicy(claim);
 0107                iaps.Add(sctAuthPolicy);
 108            }
 109
 0110            iaps.Add(new EndpointAuthorizationPolicy(EndpointId));
 0111            sct.AuthorizationPolicies = iaps.AsReadOnly();
 0112        }
 113
 114        private bool ContainsEndpointAuthPolicy(ReadOnlyCollection<IAuthorizationPolicy> policies)
 115        {
 0116            if (policies == null)
 117            {
 0118                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(policies));
 119            }
 120
 0121            for (int i = 0; i < policies.Count; ++i)
 122            {
 0123                if (policies[i] is EndpointAuthorizationPolicy)
 124                {
 0125                    return true;
 126                }
 127            }
 128
 0129            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        {
 0139            if (authContext != null)
 140            {
 0141                for (int i = 0; i < authContext.ClaimSets.Count; ++i)
 142                {
 0143                    CoreWCF.IdentityModel.Claims.ClaimSet claimSet = authContext.ClaimSets[i];
 0144                    foreach (CoreWCF.IdentityModel.Claims.Claim claim in claimSet.FindClaims(null, CoreWCF.IdentityModel
 145                    {
 0146                        return claim;
 147                    }
 148                }
 149            }
 0150            return null;
 0151        }
 152
 153        public void OnTokenIssued(SecurityToken issuedToken, EndpointAddress tokenRequestor)
 154        {
 0155            SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(issuedToken as SecurityContextSecurityToken);
 0156        }
 157
 158        public void OnTokenRenewed(SecurityToken issuedToken, SecurityToken oldToken)
 159        {
 0160            SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(issuedToken as SecurityContextSecurityToken);
 0161        }
 162    }
 163}