| | | 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 | | namespace CoreWCF.Security.Tokens |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Text; |
| | | 9 | | using CoreWCF; |
| | | 10 | | using CoreWCF.IdentityModel; |
| | | 11 | | using CoreWCF.IdentityModel.Selectors; |
| | | 12 | | using CoreWCF.IdentityModel.Tokens; |
| | | 13 | | using CoreWCF.Security; |
| | | 14 | | |
| | | 15 | | public abstract class SecurityTokenParameters |
| | | 16 | | { |
| | | 17 | | internal const SecurityTokenInclusionMode defaultInclusionMode = SecurityTokenInclusionMode.AlwaysToRecipient; |
| | | 18 | | internal const SecurityTokenReferenceStyle defaultReferenceStyle = SecurityTokenReferenceStyle.Internal; |
| | | 19 | | internal const bool defaultRequireDerivedKeys = true; |
| | | 20 | | private SecurityTokenInclusionMode _inclusionMode = defaultInclusionMode; |
| | | 21 | | private SecurityTokenReferenceStyle _referenceStyle = defaultReferenceStyle; |
| | | 22 | | |
| | 1417 | 23 | | protected SecurityTokenParameters(SecurityTokenParameters other) |
| | | 24 | | { |
| | 1417 | 25 | | if (other == null) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other)); |
| | | 28 | | } |
| | | 29 | | |
| | 1417 | 30 | | RequireDerivedKeys = other.RequireDerivedKeys; |
| | 1417 | 31 | | _inclusionMode = other._inclusionMode; |
| | 1417 | 32 | | _referenceStyle = other._referenceStyle; |
| | 1417 | 33 | | } |
| | | 34 | | |
| | 393 | 35 | | protected SecurityTokenParameters() |
| | | 36 | | { |
| | | 37 | | // empty |
| | 393 | 38 | | } |
| | | 39 | | |
| | | 40 | | protected internal abstract bool HasAsymmetricKey { get; } |
| | | 41 | | |
| | | 42 | | public SecurityTokenInclusionMode InclusionMode |
| | | 43 | | { |
| | | 44 | | get |
| | | 45 | | { |
| | 19 | 46 | | return _inclusionMode; |
| | | 47 | | } |
| | | 48 | | set |
| | | 49 | | { |
| | 55 | 50 | | SecurityTokenInclusionModeHelper.Validate(value); |
| | 55 | 51 | | _inclusionMode = value; |
| | 55 | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public SecurityTokenReferenceStyle ReferenceStyle |
| | | 56 | | { |
| | | 57 | | get |
| | | 58 | | { |
| | 0 | 59 | | return _referenceStyle; |
| | | 60 | | } |
| | | 61 | | set |
| | | 62 | | { |
| | 0 | 63 | | TokenReferenceStyleHelper.Validate(value); |
| | 0 | 64 | | _referenceStyle = value; |
| | 0 | 65 | | } |
| | | 66 | | } |
| | | 67 | | |
| | 5749 | 68 | | public bool RequireDerivedKeys { get; set; } = defaultRequireDerivedKeys; |
| | | 69 | | |
| | | 70 | | protected internal abstract bool SupportsClientAuthentication { get; } |
| | | 71 | | protected internal abstract bool SupportsServerAuthentication { get; } |
| | | 72 | | protected internal abstract bool SupportsClientWindowsIdentity { get; } |
| | | 73 | | |
| | | 74 | | public SecurityTokenParameters Clone() |
| | | 75 | | { |
| | 1417 | 76 | | SecurityTokenParameters result = CloneCore(); |
| | | 77 | | |
| | 1417 | 78 | | if (result == null) |
| | | 79 | | { |
| | 0 | 80 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sec |
| | | 81 | | } |
| | | 82 | | |
| | 1417 | 83 | | return result; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | protected abstract SecurityTokenParameters CloneCore(); |
| | | 87 | | |
| | | 88 | | protected internal abstract SecurityKeyIdentifierClause CreateKeyIdentifierClause(SecurityToken token, SecurityT |
| | | 89 | | |
| | | 90 | | protected internal abstract void InitializeSecurityTokenRequirement(SecurityTokenRequirement requirement); |
| | | 91 | | |
| | | 92 | | internal SecurityKeyIdentifierClause CreateKeyIdentifierClause<TExternalClause, TInternalClause>(SecurityToken t |
| | | 93 | | where TExternalClause : SecurityKeyIdentifierClause |
| | | 94 | | where TInternalClause : SecurityKeyIdentifierClause |
| | | 95 | | { |
| | 30 | 96 | | if (token == null) |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | SecurityKeyIdentifierClause result; |
| | | 102 | | |
| | | 103 | | switch (referenceStyle) |
| | | 104 | | { |
| | | 105 | | default: |
| | 0 | 106 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 107 | | SR.Format(SR.TokenDoesNotSupportKeyIdentifierClauseCreation, token.GetType().Name, referenceStyl |
| | | 108 | | case SecurityTokenReferenceStyle.External: |
| | 20 | 109 | | result = token.CreateKeyIdentifierClause<TExternalClause>(); |
| | 20 | 110 | | break; |
| | | 111 | | case SecurityTokenReferenceStyle.Internal: |
| | 10 | 112 | | result = token.CreateKeyIdentifierClause<TInternalClause>(); |
| | | 113 | | break; |
| | | 114 | | } |
| | | 115 | | |
| | 30 | 116 | | return result; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal SecurityKeyIdentifierClause CreateGenericXmlTokenKeyIdentifierClause(SecurityToken token, SecurityToken |
| | | 120 | | { |
| | 0 | 121 | | if (token is GenericXmlSecurityToken xmlToken) |
| | | 122 | | { |
| | 0 | 123 | | if (referenceStyle == SecurityTokenReferenceStyle.Internal && xmlToken.InternalTokenReference != null) |
| | | 124 | | { |
| | 0 | 125 | | return xmlToken.InternalTokenReference; |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | if (referenceStyle == SecurityTokenReferenceStyle.External && xmlToken.ExternalTokenReference != null) |
| | | 129 | | { |
| | 0 | 130 | | return xmlToken.ExternalTokenReference; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.UnableTo |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | protected internal virtual bool MatchesKeyIdentifierClause(SecurityToken token, SecurityKeyIdentifierClause keyI |
| | | 138 | | { |
| | 23 | 139 | | if (token == null) |
| | | 140 | | { |
| | 0 | 141 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 142 | | } |
| | | 143 | | |
| | 23 | 144 | | if (token is GenericXmlSecurityToken) |
| | | 145 | | { |
| | 0 | 146 | | return MatchesGenericXmlTokenKeyIdentifierClause(token, keyIdentifierClause, referenceStyle); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | bool result; |
| | | 150 | | |
| | | 151 | | switch (referenceStyle) |
| | | 152 | | { |
| | | 153 | | default: |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException( |
| | 0 | 155 | | SR.Format(SR.TokenDoesNotSupportKeyIdentifierClauseCreation, token.GetType().Name, referenceStyl |
| | | 156 | | case SecurityTokenReferenceStyle.External: |
| | 0 | 157 | | if (keyIdentifierClause is LocalIdKeyIdentifierClause) |
| | | 158 | | { |
| | 0 | 159 | | result = false; |
| | | 160 | | } |
| | | 161 | | else |
| | | 162 | | { |
| | 0 | 163 | | result = token.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | break; |
| | | 167 | | case SecurityTokenReferenceStyle.Internal: |
| | 23 | 168 | | result = token.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 169 | | break; |
| | | 170 | | } |
| | | 171 | | |
| | 23 | 172 | | return result; |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | internal bool MatchesGenericXmlTokenKeyIdentifierClause(SecurityToken token, SecurityKeyIdentifierClause keyIden |
| | | 176 | | { |
| | 0 | 177 | | if (token == null) |
| | | 178 | | { |
| | 0 | 179 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | bool result; |
| | | 183 | | |
| | | 184 | | |
| | 0 | 185 | | if (!(token is GenericXmlSecurityToken xmlToken)) |
| | | 186 | | { |
| | 0 | 187 | | result = false; |
| | | 188 | | } |
| | 0 | 189 | | else if (referenceStyle == SecurityTokenReferenceStyle.External && xmlToken.ExternalTokenReference != null) |
| | | 190 | | { |
| | 0 | 191 | | result = xmlToken.ExternalTokenReference.Matches(keyIdentifierClause); |
| | | 192 | | } |
| | 0 | 193 | | else if (referenceStyle == SecurityTokenReferenceStyle.Internal) |
| | | 194 | | { |
| | 0 | 195 | | result = xmlToken.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 196 | | } |
| | | 197 | | else |
| | | 198 | | { |
| | 0 | 199 | | result = false; |
| | | 200 | | } |
| | | 201 | | |
| | 0 | 202 | | return result; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | public override string ToString() |
| | | 206 | | { |
| | 0 | 207 | | StringBuilder sb = new StringBuilder(); |
| | | 208 | | |
| | 0 | 209 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "{0}:", GetType().ToString())); |
| | 0 | 210 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "InclusionMode: {0}", _inclusionMode.ToString())); |
| | 0 | 211 | | sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "ReferenceStyle: {0}", _referenceStyle.ToString()) |
| | 0 | 212 | | sb.Append(string.Format(CultureInfo.InvariantCulture, "RequireDerivedKeys: {0}", RequireDerivedKeys.ToString |
| | | 213 | | |
| | 0 | 214 | | return sb.ToString(); |
| | | 215 | | } |
| | | 216 | | } |
| | | 217 | | } |