< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.IdentityModel.Tokens.SamlSecurityTokenRequirement
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SamlSecurityTokenRequirement.cs
Line coverage
50%
Covered lines: 24
Uncovered lines: 24
Coverable lines: 48
Total lines: 180
Line coverage: 50%
Branch coverage
47%
Covered branches: 18
Total branches: 38
Branch coverage: 47.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%11100%
ShouldEnforceAudienceRestriction(...)37.5%8850%
ValidateAudienceRestriction(...)53.57%282848.57%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/IdentityModel/Tokens/SamlSecurityTokenRequirement.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.Security.Claims;
 7using System.Text;
 8using CoreWCF.IdentityModel.Selectors;
 9
 10namespace CoreWCF.IdentityModel.Tokens
 11{
 12    public class SamlSecurityTokenRequirement
 13    {
 14        private X509CertificateValidator _certificateValidator;
 15
 16        /// <summary>
 17        /// Creates an instance of <see cref="SamlSecurityTokenRequirement"/>
 18        /// </summary>
 1819        public SamlSecurityTokenRequirement()
 20        {
 1821        }
 22
 23        /// <summary>
 24        /// Gets/sets the X509CertificateValidator associated with this token requirement
 25        /// </summary>
 26        public X509CertificateValidator CertificateValidator
 27        {
 28            get
 29            {
 030                return _certificateValidator;
 31            }
 32            set
 33            {
 034                _certificateValidator = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof
 035            }
 36        }
 37
 38        /// <summary>
 39        /// Gets or sets the Claim Type that will be used to generate the
 40        /// FederatedIdentity.Name property.
 41        /// </summary>
 1842        public string NameClaimType { get; set; } = ClaimsIdentity.DefaultNameClaimType;
 43
 44        /// <summary>
 45        /// Gets the Claim Types that are used to generate the
 46        /// FederatedIdentity.Roles property.
 47        /// </summary>
 1848        public string RoleClaimType { get; set; } = ClaimTypes.Role;
 49
 50        /// <summary>
 51        /// Checks if Audience Enforcement checks are required for the given token
 52        /// based on this SamlSecurityTokenRequirement settings.
 53        /// </summary>
 54        /// <param name="audienceUriMode">
 55        /// The <see cref="AudienceUriMode"/> defining the audience requirement.
 56        /// </param>
 57        /// <param name="token">The Security token to be tested for Audience
 58        /// Enforcement.</param>
 59        /// <returns>True if Audience Enforcement should be applied.</returns>
 60        /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception>
 61        public virtual bool ShouldEnforceAudienceRestriction(AudienceUriMode audienceUriMode, SecurityToken token)
 62        {
 4863            if (null == token)
 64            {
 065                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token));
 66            }
 67
 68            switch (audienceUriMode)
 69            {
 70                case AudienceUriMode.Always:
 4771                    return true;
 72
 73                case AudienceUriMode.Never:
 174                    return false;
 75
 76                case AudienceUriMode.BearerKeyOnly:
 077                    return (null == token.SecurityKeys || 0 == token.SecurityKeys.Count);
 78
 79                default:
 080                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 81            }
 82        }
 83
 84        /// <summary>
 85        /// Checks the given list of Audience URIs with the AllowedAudienceUri list.
 86        /// </summary>
 87        /// <param name="allowedAudienceUris">Collection of AudienceUris.</param>
 88        /// <param name="tokenAudiences">Collection of audience URIs the token applies to.</param>
 89        /// <exception cref="ArgumentNullException">The input argument 'allowedAudienceUris' is null.</exception>
 90        /// <exception cref="ArgumentNullException">The input argument 'tokenAudiences' is null.</exception>
 91        /// <exception cref="AudienceUriValidationFailedException">Either the input argument 'tokenAudiences' or the con
 92        /// 'AudienceUris' collection is empty.</exception>
 93        public virtual void ValidateAudienceRestriction(IList<Uri> allowedAudienceUris, IList<Uri> tokenAudiences)
 94        {
 3995            if (null == allowedAudienceUris)
 96            {
 097                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(allowedAudienceUris));
 98            }
 99
 39100            if (null == tokenAudiences)
 101            {
 0102                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenAudiences));
 103            }
 104
 39105            if (0 == tokenAudiences.Count)
 106            {
 0107                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AudienceUriValidationFailedException(
 0108                    SR.Format(SR.ID1036)));
 109            }
 110
 39111            if (0 == allowedAudienceUris.Count)
 112            {
 0113                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AudienceUriValidationFailedException(
 0114                    SR.Format(SR.ID1043)));
 115            }
 116
 39117            bool found = false;
 123118            foreach (Uri audience in tokenAudiences)
 119            {
 39120                if (audience != null)
 121                {
 122                    // Strip off any query string or fragment. This is necessary because the
 123                    // CardSpace uses the raw Request-URI to form the audience when issuing
 124                    // tokens for personal cards, but we clearly don't want things like the
 125                    // ReturnUrl parameter affecting the audience matching.
 126                    Uri audienceLeftPart;
 39127                    if (audience.IsAbsoluteUri)
 128                    {
 39129                        audienceLeftPart = new Uri(audience.GetLeftPart(UriPartial.Path));
 130                    }
 131                    else
 132                    {
 0133                        Uri baseUri = new Uri("http://www.example.com");
 0134                        Uri resolved = new Uri(baseUri, audience);
 0135                        audienceLeftPart = baseUri.MakeRelativeUri(new Uri(resolved.GetLeftPart(UriPartial.Path)));
 136                    }
 137
 39138                    if (allowedAudienceUris.Contains(audienceLeftPart))
 139                    {
 33140                        found = true;
 33141                        break;
 142                    }
 143                }
 144            }
 145
 39146            if (!found)
 147            {
 6148                if (1 == tokenAudiences.Count || null != tokenAudiences[0])
 149                {
 6150                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AudienceUriValidationFailedException(
 6151                        SR.Format(SR.ID1038, tokenAudiences[0].OriginalString)));
 152                }
 153                else
 154                {
 0155                    StringBuilder sb = new StringBuilder(SR.Format(SR.ID8007));
 0156                    bool first = true;
 157
 0158                    foreach (Uri a in tokenAudiences)
 159                    {
 0160                        if (a != null)
 161                        {
 0162                            if (first)
 163                            {
 0164                                first = false;
 165                            }
 166                            else
 167                            {
 0168                                sb.Append(", ");
 169                            }
 170
 0171                            sb.Append(a.OriginalString);
 172                        }
 173                    }
 0174                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new AudienceUriValidationFailedException(S
 175                }
 176            }
 33177        }
 178    }
 179}
 180