< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.InternalSamlSubjectStatement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/InternalSamlSubjectStatement.cs
Line coverage
11%
Covered lines: 7
Uncovered lines: 52
Coverable lines: 59
Total lines: 145
Line coverage: 11.8%
Branch coverage
0%
Covered branches: 0
Total branches: 38
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(...)100%11100%
CreatePolicyAsync()0%880%
AddClaimsToList(...)0%12120%
ExtractSubjectKeyClaimSetAsync()0%16160%
ExtractClaims()0%220%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/InternalSamlSubjectStatement.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;
 5using System.Collections.Generic;
 6using System.Collections.ObjectModel;
 7using System.Security.Principal;
 8using System.Threading.Tasks;
 9using CoreWCF.IdentityModel.Claims;
 10using CoreWCF.IdentityModel.Policy;
 11using CoreWCF.IdentityModel.Selectors;
 12using CoreWCF.Security;
 13using MSIdentitySAML = Microsoft.IdentityModel.Tokens.Saml;
 14
 15namespace 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
 427        public InternalSamlSubjectStatement(MSIdentitySAML.SamlSubjectStatement samlSubjectStatement,
 428            SecurityKeyIdentifier securityKeyIdentifier, SecurityToken securityToken)
 29        {
 430            _samlSubjectStatement = samlSubjectStatement;
 431            _samlSubject = _samlSubjectStatement.Subject;
 432            _securityKeyIdentifier = securityKeyIdentifier;
 433            _subjectToken = securityToken;
 434        }
 35
 36        //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode
 37        internal async ValueTask<IAuthorizationPolicy> CreatePolicyAsync(ClaimSet issuer, SamlSecurityTokenAuthenticator
 38        {
 039            if (issuer == null)
 040                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuer));
 41
 042            if (_policy == null)
 43            {
 044                List<ClaimSet> claimSets = new List<ClaimSet>();
 045                ClaimSet subjectKeyClaimset = await ExtractSubjectKeyClaimSetAsync(samlAuthenticator);
 046                if (subjectKeyClaimset != null)
 047                    claimSets.Add(subjectKeyClaimset);
 48
 049                List<Claim> claims = new List<Claim>();
 050                ReadOnlyCollection<Claim> subjectClaims = ExtractClaims();
 051                for (int i = 0; i < subjectClaims.Count; ++i)
 52                {
 053                    claims.Add(subjectClaims[i]);
 54                }
 55
 056                AddClaimsToList(claims);
 057                claimSets.Add(new DefaultClaimSet(issuer, claims));
 058                _policy = new UnconditionalPolicy(_identity, claimSets.AsReadOnly(), SecurityUtils.MaxUtcDateTime);
 059            }
 60
 061            return _policy;
 062        }
 63
 64        private void AddClaimsToList(List<Claim> claims)
 65        {
 066            if (_samlSubjectStatement is MSIdentitySAML.SamlAuthorizationDecisionStatement samlAuthorizationStatement)
 67            {
 68                //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.Iden
 069                foreach (MSIdentitySAML.SamlAction action in samlAuthorizationStatement.Actions)
 70                {
 071                    Enum.TryParse(samlAuthorizationStatement.Decision, out SamlAccessDecision decision);
 072                    claims.Add(new Claim(ClaimTypes.AuthorizationDecision, new SamlAuthorizationDecisionClaimResource(sa
 73                }
 74            }
 075            else if (_samlSubjectStatement is MSIdentitySAML.SamlAuthenticationStatement)
 76            {
 77                //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.Iden
 078                MSIdentitySAML.SamlAuthenticationStatement samlAuthStatement = _samlSubjectStatement as MSIdentitySAML.S
 079                claims.Add(new Claim(ClaimTypes.Authentication, new SamlAuthenticationClaimResource(samlAuthStatement.Au
 80            }
 081            else if (_samlSubjectStatement is MSIdentitySAML.SamlAttributeStatement attributeStatement)
 82            {
 083                foreach (MSIdentitySAML.SamlAttribute attribute in attributeStatement.Attributes)
 84                {
 085                    foreach (string attrVal in attribute.Values)
 86                    {
 087                        claims.Add(new Claim(attribute.ClaimType, attrVal, Rights.PossessProperty));
 88                    }
 89                }
 90            }
 091        }
 92
 93        //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode
 94        private async ValueTask<ClaimSet> ExtractSubjectKeyClaimSetAsync(SamlSecurityTokenAuthenticator samlAuthenticato
 95        {
 096            if (_samlSubject.KeyInfo != null)
 97            {
 098                if (samlAuthenticator == null)
 099                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(samlAuthenticator));
 100
 0101                if (_subjectToken != null)
 102                {
 0103                    _subjectKeyClaimset = samlAuthenticator.ResolveClaimSet(_securityKeyIdentifier);
 0104                    _identity = await samlAuthenticator.ResolveIdentityAsync(_subjectToken);
 0105                    if ((_identity == null) && (_subjectKeyClaimset != null))
 106                    {
 0107                        Claim identityClaim = null;
 0108                        foreach (Claim claim in _subjectKeyClaimset.FindClaims(null, Rights.Identity))
 109                        {
 0110                            identityClaim = claim;
 0111                            break;
 112                        }
 113
 0114                        if (identityClaim != null)
 115                        {
 0116                            _identity = SecurityUtils.CreateIdentity(identityClaim.Resource.ToString(), GetType().Name);
 117                        }
 118                    }
 119                }
 120
 0121                if (_subjectKeyClaimset == null)
 122                {
 123                    // Add the type of the primary claim as the Identity claim.
 0124                    _subjectKeyClaimset = samlAuthenticator.ResolveClaimSet(_securityKeyIdentifier);
 0125                    _identity = samlAuthenticator.ResolveIdentity(_securityKeyIdentifier);
 126                }
 127            }
 128
 0129            return _subjectKeyClaimset;
 0130        }
 131
 132        //https://github.com/microsoft/referencesource/blob/4e6dea7a9c7cbb4e6b000b05a099e7168d1b6960/System.IdentityMode
 133        private ReadOnlyCollection<Claim> ExtractClaims()
 134        {
 0135            var claims = new List<Claim>();
 0136            if (!string.IsNullOrEmpty(_samlSubject.Name))
 137            {
 0138                claims.Add(new Claim(ClaimTypes.NameIdentifier, new SamlNameIdentifierClaimResource(_samlSubject.Name, _
 0139                claims.Add(new Claim(ClaimTypes.NameIdentifier, new SamlNameIdentifierClaimResource(_samlSubject.Name, _
 140            }
 141
 0142            return claims.AsReadOnly();
 143        }
 144    }
 145}