| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.IdentityModel; |
| | | 9 | | using CoreWCF.IdentityModel.Selectors; |
| | | 10 | | using CoreWCF.IdentityModel.Tokens; |
| | | 11 | | using CoreWCF.Runtime; |
| | | 12 | | using CoreWCF.Security.Tokens; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.Security |
| | | 15 | | { |
| | | 16 | | public class WSSecurityTokenSerializer : SecurityTokenSerializer |
| | | 17 | | { |
| | | 18 | | private const int DefaultMaximumKeyDerivationOffset = 64; // bytes |
| | | 19 | | private const int DefaultMaximumKeyDerivationLabelLength = 128; // bytes |
| | | 20 | | private const int DefaultMaximumKeyDerivationNonceLength = 128; // bytes |
| | | 21 | | |
| | | 22 | | private static WSSecurityTokenSerializer s_instance; |
| | | 23 | | private readonly List<SerializerEntries> _serializerEntries; |
| | | 24 | | private readonly WSSecureConversation _secureConversation; |
| | 132 | 25 | | private readonly List<TokenEntry> _tokenEntries = new List<TokenEntry>(); |
| | | 26 | | private readonly KeyInfoSerializer _keyInfoSerializer; |
| | | 27 | | |
| | | 28 | | public WSSecurityTokenSerializer() |
| | 4 | 29 | | : this(SecurityVersion.WSSecurity11) |
| | | 30 | | { |
| | 4 | 31 | | } |
| | | 32 | | |
| | | 33 | | public WSSecurityTokenSerializer(bool emitBspRequiredAttributes) |
| | 0 | 34 | | : this(SecurityVersion.WSSecurity11, emitBspRequiredAttributes) |
| | | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion) |
| | 4 | 39 | | : this(securityVersion, false) |
| | | 40 | | { |
| | 4 | 41 | | } |
| | | 42 | | |
| | | 43 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes) |
| | 4 | 44 | | : this(securityVersion, emitBspRequiredAttributes, null) |
| | | 45 | | { |
| | 4 | 46 | | } |
| | | 47 | | |
| | | 48 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer |
| | 4 | 49 | | : this(securityVersion, emitBspRequiredAttributes, samlSerializer, null, null) |
| | | 50 | | { |
| | 4 | 51 | | } |
| | | 52 | | |
| | | 53 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer |
| | 4 | 54 | | : this(securityVersion, emitBspRequiredAttributes, samlSerializer, securityStateEncoder, knownTypes, Default |
| | | 55 | | { |
| | 4 | 56 | | } |
| | | 57 | | |
| | | 58 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, TrustVersion trustVersion, SecureConversationV |
| | 128 | 59 | | : this(securityVersion, trustVersion, secureConversationVersion, emitBspRequiredAttributes, samlSerializer, |
| | | 60 | | { |
| | 128 | 61 | | } |
| | | 62 | | |
| | | 63 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, bool emitBspRequiredAttributes, SamlSerializer |
| | | 64 | | int maximumKeyDerivationOffset, int maximumKeyDerivationLabelLength, int maximumKeyDerivationNonceLength) |
| | 4 | 65 | | : this(securityVersion, TrustVersion.Default, SecureConversationVersion.Default, emitBspRequiredAttributes, |
| | | 66 | | { |
| | 4 | 67 | | } |
| | | 68 | | |
| | 132 | 69 | | public WSSecurityTokenSerializer(SecurityVersion securityVersion, TrustVersion trustVersion, SecureConversationV |
| | 132 | 70 | | int maximumKeyDerivationOffset, int maximumKeyDerivationLabelLength, int maximumKeyDerivationNonceLength) |
| | | 71 | | { |
| | 132 | 72 | | if (maximumKeyDerivationOffset < 0) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maximum |
| | | 75 | | } |
| | 132 | 76 | | if (maximumKeyDerivationLabelLength < 0) |
| | | 77 | | { |
| | 0 | 78 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maximum |
| | | 79 | | } |
| | 132 | 80 | | if (maximumKeyDerivationNonceLength <= 0) |
| | | 81 | | { |
| | 0 | 82 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maximum |
| | | 83 | | } |
| | | 84 | | |
| | 132 | 85 | | SecurityVersion = securityVersion ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentN |
| | 132 | 86 | | EmitBspRequiredAttributes = emitBspRequiredAttributes; |
| | 132 | 87 | | MaximumKeyDerivationOffset = maximumKeyDerivationOffset; |
| | 132 | 88 | | MaximumKeyDerivationNonceLength = maximumKeyDerivationNonceLength; |
| | 132 | 89 | | MaximumKeyDerivationLabelLength = maximumKeyDerivationLabelLength; |
| | | 90 | | |
| | 132 | 91 | | _serializerEntries = new List<SerializerEntries>(); |
| | | 92 | | |
| | 132 | 93 | | if (secureConversationVersion == SecureConversationVersion.WSSecureConversationFeb2005) |
| | | 94 | | { |
| | 65 | 95 | | _secureConversation = new WSSecureConversationFeb2005(this, securityStateEncoder, knownTypes, maximumKey |
| | | 96 | | } |
| | 67 | 97 | | else if (secureConversationVersion == SecureConversationVersion.WSSecureConversation13) |
| | | 98 | | { |
| | 67 | 99 | | _secureConversation = new WSSecureConversationDec2005(this, securityStateEncoder, knownTypes, maximumKey |
| | | 100 | | } |
| | | 101 | | else |
| | | 102 | | { |
| | 0 | 103 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 104 | | } |
| | | 105 | | |
| | 132 | 106 | | if (securityVersion == SecurityVersion.WSSecurity10) |
| | | 107 | | { |
| | 6 | 108 | | _serializerEntries.Add(new WSSecurityJan2004(this, samlSerializer)); |
| | | 109 | | } |
| | 126 | 110 | | else if (securityVersion == SecurityVersion.WSSecurity11) |
| | | 111 | | { |
| | 126 | 112 | | _serializerEntries.Add(new WSSecurityXXX2005(this, samlSerializer)); |
| | | 113 | | } |
| | | 114 | | else |
| | | 115 | | { |
| | 0 | 116 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(securit |
| | | 117 | | } |
| | 132 | 118 | | _serializerEntries.Add(_secureConversation); |
| | | 119 | | //TODO later |
| | | 120 | | TrustDictionary trustDictionary; |
| | 132 | 121 | | if (trustVersion == TrustVersion.WSTrustFeb2005) |
| | | 122 | | { |
| | 65 | 123 | | _serializerEntries.Add(new WSTrustFeb2005(this)); |
| | 65 | 124 | | trustDictionary = DXD.TrustDec2005Dictionary; |
| | | 125 | | } |
| | 67 | 126 | | else if (trustVersion == TrustVersion.WSTrust13) |
| | | 127 | | { |
| | 67 | 128 | | _serializerEntries.Add(new WSTrustDec2005(this)); |
| | 67 | 129 | | trustDictionary = DXD.TrustDec2005Dictionary; |
| | | 130 | | } |
| | | 131 | | else |
| | | 132 | | { |
| | 0 | 133 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 134 | | } |
| | | 135 | | |
| | 132 | 136 | | _tokenEntries = new List<TokenEntry>(); |
| | | 137 | | |
| | 1056 | 138 | | for (int i = 0; i < _serializerEntries.Count; ++i) |
| | | 139 | | { |
| | 396 | 140 | | SerializerEntries serializerEntry = _serializerEntries[i]; |
| | 396 | 141 | | serializerEntry.PopulateTokenEntries(_tokenEntries); |
| | | 142 | | } |
| | | 143 | | |
| | 132 | 144 | | DictionaryManager dictionaryManager = new DictionaryManager(ServiceModelDictionary.CurrentVersion) |
| | 132 | 145 | | { |
| | 132 | 146 | | SecureConversationDec2005Dictionary = DXD.SecureConversationDec2005Dictionary, |
| | 132 | 147 | | SecurityAlgorithmDec2005Dictionary = DXD.SecurityAlgorithmDec2005Dictionary |
| | 132 | 148 | | }; |
| | | 149 | | |
| | 132 | 150 | | _keyInfoSerializer = new WSKeyInfoSerializer(EmitBspRequiredAttributes, dictionaryManager, trustDictionary, |
| | 132 | 151 | | } |
| | | 152 | | |
| | | 153 | | public static WSSecurityTokenSerializer DefaultInstance |
| | | 154 | | { |
| | | 155 | | get |
| | | 156 | | { |
| | 6 | 157 | | if (s_instance == null) |
| | | 158 | | { |
| | 4 | 159 | | s_instance = new WSSecurityTokenSerializer(); |
| | | 160 | | } |
| | | 161 | | |
| | 6 | 162 | | return s_instance; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | 132 | 166 | | public bool EmitBspRequiredAttributes { get; } |
| | | 167 | | |
| | 0 | 168 | | public SecurityVersion SecurityVersion { get; } |
| | | 169 | | |
| | 0 | 170 | | public int MaximumKeyDerivationOffset { get; } |
| | | 171 | | |
| | 0 | 172 | | public int MaximumKeyDerivationLabelLength { get; } |
| | | 173 | | |
| | 0 | 174 | | public int MaximumKeyDerivationNonceLength { get; } |
| | | 175 | | |
| | | 176 | | private bool ShouldWrapException(Exception e) |
| | | 177 | | { |
| | 0 | 178 | | if (Fx.IsFatal(e)) |
| | | 179 | | { |
| | 0 | 180 | | return false; |
| | | 181 | | } |
| | 0 | 182 | | return ((e is ArgumentException) || (e is FormatException) || (e is InvalidOperationException)); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | protected override bool CanReadTokenCore(XmlReader reader) |
| | | 186 | | { |
| | 0 | 187 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 0 | 188 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 189 | | { |
| | 0 | 190 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | 0 | 191 | | if (tokenEntry.CanReadTokenCore(localReader)) |
| | | 192 | | { |
| | 0 | 193 | | return true; |
| | | 194 | | } |
| | | 195 | | } |
| | 0 | 196 | | return false; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | protected override SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver) |
| | | 200 | | { |
| | 51 | 201 | | XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader); |
| | 530 | 202 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 203 | | { |
| | 265 | 204 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | 265 | 205 | | if (tokenEntry.CanReadTokenCore(localReader)) |
| | | 206 | | { |
| | | 207 | | try |
| | | 208 | | { |
| | 51 | 209 | | return tokenEntry.ReadTokenCore(localReader, tokenResolver); |
| | | 210 | | } |
| | 0 | 211 | | catch (Exception e) |
| | | 212 | | { |
| | 0 | 213 | | if (!ShouldWrapException(e)) |
| | | 214 | | { |
| | 0 | 215 | | throw; |
| | | 216 | | } |
| | 0 | 217 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.ErrorDeserializing |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | } |
| | 0 | 221 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CannotReadToken, rea |
| | 51 | 222 | | } |
| | | 223 | | |
| | | 224 | | protected override bool CanWriteTokenCore(SecurityToken token) |
| | | 225 | | { |
| | 0 | 226 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 227 | | { |
| | 0 | 228 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | 0 | 229 | | if (tokenEntry.SupportsCore(token.GetType())) |
| | | 230 | | { |
| | 0 | 231 | | return true; |
| | | 232 | | } |
| | | 233 | | } |
| | 0 | 234 | | return false; |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | protected override void WriteTokenCore(XmlWriter writer, SecurityToken token) |
| | | 238 | | { |
| | 20 | 239 | | bool wroteToken = false; |
| | 20 | 240 | | XmlDictionaryWriter localWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); |
| | 300 | 241 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 242 | | { |
| | 150 | 243 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | 150 | 244 | | if (tokenEntry.SupportsCore(token.GetType())) |
| | | 245 | | { |
| | | 246 | | try |
| | | 247 | | { |
| | 20 | 248 | | tokenEntry.WriteTokenCore(localWriter, token); |
| | 20 | 249 | | } |
| | 0 | 250 | | catch (Exception e) |
| | | 251 | | { |
| | 0 | 252 | | if (!ShouldWrapException(e)) |
| | | 253 | | { |
| | 0 | 254 | | throw; |
| | | 255 | | } |
| | 0 | 256 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ErrorSer |
| | | 257 | | } |
| | 20 | 258 | | wroteToken = true; |
| | 20 | 259 | | break; |
| | | 260 | | } |
| | | 261 | | } |
| | | 262 | | |
| | 20 | 263 | | if (!wroteToken) |
| | | 264 | | { |
| | 0 | 265 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Sta |
| | | 266 | | } |
| | | 267 | | |
| | 20 | 268 | | localWriter.Flush(); |
| | 20 | 269 | | } |
| | | 270 | | |
| | | 271 | | protected override bool CanReadKeyIdentifierCore(XmlReader reader) |
| | | 272 | | { |
| | 0 | 273 | | throw new PlatformNotSupportedException(); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | protected override SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader) |
| | | 277 | | { |
| | | 278 | | try |
| | | 279 | | { |
| | 23 | 280 | | return _keyInfoSerializer.ReadKeyIdentifier(reader); |
| | | 281 | | } |
| | 0 | 282 | | catch (Exception ex) |
| | | 283 | | { |
| | 0 | 284 | | throw Fx.Exception.AsError(new MessageSecurityException(ex.Message)); |
| | | 285 | | } |
| | 23 | 286 | | } |
| | | 287 | | |
| | | 288 | | protected override bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier) |
| | | 289 | | { |
| | 0 | 290 | | throw new PlatformNotSupportedException(); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | protected override void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier) |
| | | 294 | | { |
| | | 295 | | try |
| | | 296 | | { |
| | 0 | 297 | | _keyInfoSerializer.WriteKeyIdentifier(writer, keyIdentifier); |
| | 0 | 298 | | } |
| | 0 | 299 | | catch (Exception ex) |
| | | 300 | | { |
| | 0 | 301 | | throw Fx.Exception.AsError(new MessageSecurityException(ex.Message)); |
| | | 302 | | } |
| | 0 | 303 | | } |
| | | 304 | | |
| | | 305 | | protected override bool CanReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 306 | | { |
| | | 307 | | try |
| | | 308 | | { |
| | 6 | 309 | | return _keyInfoSerializer.CanReadKeyIdentifierClause(reader); |
| | | 310 | | } |
| | 0 | 311 | | catch (Exception ex) |
| | | 312 | | { |
| | 0 | 313 | | throw Fx.Exception.AsError(new MessageSecurityException(ex.Message)); |
| | | 314 | | } |
| | 6 | 315 | | } |
| | | 316 | | |
| | | 317 | | protected override SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 318 | | { |
| | | 319 | | try |
| | | 320 | | { |
| | 39 | 321 | | return _keyInfoSerializer.ReadKeyIdentifierClause(reader); |
| | | 322 | | } |
| | 0 | 323 | | catch (Exception ex) |
| | | 324 | | { |
| | 0 | 325 | | throw Fx.Exception.AsError(new MessageSecurityException(ex.Message)); |
| | | 326 | | } |
| | 39 | 327 | | } |
| | | 328 | | |
| | | 329 | | protected override bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 330 | | { |
| | 0 | 331 | | throw new PlatformNotSupportedException(); |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | protected override void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier |
| | | 335 | | { |
| | | 336 | | try |
| | | 337 | | { |
| | 20 | 338 | | _keyInfoSerializer.WriteKeyIdentifierClause(writer, keyIdentifierClause); |
| | 20 | 339 | | } |
| | 0 | 340 | | catch (Exception ex) |
| | | 341 | | { |
| | 0 | 342 | | throw Fx.Exception.AsError(new MessageSecurityException(ex.Message)); |
| | | 343 | | } |
| | 20 | 344 | | } |
| | | 345 | | |
| | | 346 | | internal Type[] GetTokenTypes(string tokenTypeUri) |
| | | 347 | | { |
| | 0 | 348 | | if (tokenTypeUri != null) |
| | | 349 | | { |
| | 0 | 350 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 351 | | { |
| | 0 | 352 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | | 353 | | |
| | 0 | 354 | | if (tokenEntry.SupportsTokenTypeUri(tokenTypeUri)) |
| | | 355 | | { |
| | 0 | 356 | | return tokenEntry.GetTokenTypes(); |
| | | 357 | | } |
| | | 358 | | } |
| | | 359 | | } |
| | 0 | 360 | | return null; |
| | | 361 | | } |
| | | 362 | | |
| | | 363 | | protected internal virtual string GetTokenTypeUri(Type tokenType) |
| | | 364 | | { |
| | 0 | 365 | | if (tokenType != null) |
| | | 366 | | { |
| | 0 | 367 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 368 | | { |
| | 0 | 369 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | | 370 | | |
| | 0 | 371 | | if (tokenEntry.SupportsCore(tokenType)) |
| | | 372 | | { |
| | 0 | 373 | | return tokenEntry.TokenTypeUri; |
| | | 374 | | } |
| | | 375 | | } |
| | | 376 | | } |
| | 0 | 377 | | return null; |
| | | 378 | | } |
| | | 379 | | |
| | | 380 | | public virtual bool TryCreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTokenReferenceStyle tok |
| | | 381 | | { |
| | 0 | 382 | | if (element == null) |
| | | 383 | | { |
| | 0 | 384 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 385 | | } |
| | | 386 | | |
| | 0 | 387 | | securityKeyIdentifierClause = null; |
| | | 388 | | |
| | | 389 | | try |
| | | 390 | | { |
| | 0 | 391 | | securityKeyIdentifierClause = CreateKeyIdentifierClauseFromTokenXml(element, tokenReferenceStyle); |
| | 0 | 392 | | } |
| | 0 | 393 | | catch (XmlException) |
| | | 394 | | { |
| | 0 | 395 | | return false; |
| | | 396 | | } |
| | | 397 | | |
| | 0 | 398 | | return true; |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | public virtual SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXml(XmlElement element, SecurityTok |
| | | 402 | | { |
| | 0 | 403 | | if (element == null) |
| | | 404 | | { |
| | 0 | 405 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 406 | | } |
| | | 407 | | |
| | 0 | 408 | | for (int i = 0; i < _tokenEntries.Count; i++) |
| | | 409 | | { |
| | 0 | 410 | | TokenEntry tokenEntry = _tokenEntries[i]; |
| | 0 | 411 | | if (tokenEntry.CanReadTokenCore(element)) |
| | | 412 | | { |
| | | 413 | | try |
| | | 414 | | { |
| | 0 | 415 | | return tokenEntry.CreateKeyIdentifierClauseFromTokenXmlCore(element, tokenReferenceStyle); |
| | | 416 | | } |
| | 0 | 417 | | catch (Exception e) |
| | | 418 | | { |
| | 0 | 419 | | if (!ShouldWrapException(e)) |
| | | 420 | | { |
| | 0 | 421 | | throw; |
| | | 422 | | } |
| | 0 | 423 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.ErrorDeserializing |
| | | 424 | | } |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | 0 | 428 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.CannotReadToken, ele |
| | 0 | 429 | | } |
| | | 430 | | |
| | | 431 | | internal new abstract class TokenEntry |
| | | 432 | | { |
| | | 433 | | private Type[] _tokenTypes = null; |
| | | 434 | | protected abstract XmlDictionaryString LocalName { get; } |
| | | 435 | | protected abstract XmlDictionaryString NamespaceUri { get; } |
| | 0 | 436 | | public Type TokenType => GetTokenTypes()[0]; |
| | | 437 | | public abstract string TokenTypeUri { get; } |
| | | 438 | | protected abstract string ValueTypeUri { get; } |
| | | 439 | | |
| | | 440 | | protected abstract Type[] GetTokenTypesCore(); |
| | | 441 | | |
| | | 442 | | public Type[] GetTokenTypes() |
| | | 443 | | { |
| | 150 | 444 | | if (_tokenTypes == null) |
| | | 445 | | { |
| | 80 | 446 | | _tokenTypes = GetTokenTypesCore(); |
| | | 447 | | } |
| | | 448 | | |
| | 150 | 449 | | return _tokenTypes; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | public bool SupportsCore(Type tokenType) |
| | | 453 | | { |
| | 150 | 454 | | Type[] tokenTypes = GetTokenTypes(); |
| | 560 | 455 | | for (int i = 0; i < tokenTypes.Length; ++i) |
| | | 456 | | { |
| | 150 | 457 | | if (tokenTypes[i].IsAssignableFrom(tokenType)) |
| | | 458 | | { |
| | 20 | 459 | | return true; |
| | | 460 | | } |
| | | 461 | | } |
| | 130 | 462 | | return false; |
| | | 463 | | } |
| | | 464 | | |
| | | 465 | | public virtual bool SupportsTokenTypeUri(string tokenTypeUri) |
| | | 466 | | { |
| | 0 | 467 | | return (TokenTypeUri == tokenTypeUri); |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | protected static SecurityKeyIdentifierClause CreateDirectReference(XmlElement issuedTokenXml, string idAttri |
| | | 471 | | { |
| | 0 | 472 | | string id = issuedTokenXml.GetAttribute(idAttributeLocalName, idAttributeNamespace); |
| | 0 | 473 | | if (id == null) |
| | | 474 | | { |
| | 0 | 475 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.RequiredAttr |
| | | 476 | | } |
| | 0 | 477 | | return new LocalIdKeyIdentifierClause(id, tokenType); |
| | | 478 | | } |
| | | 479 | | |
| | | 480 | | public virtual bool CanReadTokenCore(XmlElement element) |
| | | 481 | | { |
| | 0 | 482 | | string valueTypeUri = null; |
| | | 483 | | |
| | 0 | 484 | | if (element.HasAttribute(SecurityJan2004Strings.ValueType, null)) |
| | | 485 | | { |
| | 0 | 486 | | valueTypeUri = element.GetAttribute(SecurityJan2004Strings.ValueType, null); |
| | | 487 | | } |
| | | 488 | | |
| | 0 | 489 | | return element.LocalName == LocalName.Value && element.NamespaceURI == NamespaceUri.Value && valueTypeUr |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | public virtual bool CanReadTokenCore(XmlDictionaryReader reader) |
| | | 493 | | { |
| | 204 | 494 | | return reader.IsStartElement(LocalName, NamespaceUri) && |
| | 204 | 495 | | reader.GetAttribute(XD.SecurityJan2004Dictionary.ValueType, null) == ValueTypeUri; |
| | | 496 | | } |
| | | 497 | | |
| | | 498 | | public virtual Task<SecurityToken> ReadTokenCoreAsync(XmlDictionaryReader reader, SecurityTokenResolver toke |
| | | 499 | | { |
| | 0 | 500 | | return Task.FromResult(ReadTokenCore(reader, tokenResolver)); |
| | | 501 | | } |
| | | 502 | | |
| | | 503 | | public abstract SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedToken |
| | | 504 | | |
| | | 505 | | public abstract SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver) |
| | | 506 | | |
| | | 507 | | public abstract void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token); |
| | | 508 | | } |
| | | 509 | | |
| | | 510 | | internal new abstract class SerializerEntries |
| | | 511 | | { |
| | 0 | 512 | | public virtual void PopulateTokenEntries(IList<TokenEntry> tokenEntries) { } |
| | | 513 | | } |
| | | 514 | | |
| | | 515 | | internal class CollectionDictionary : IXmlDictionary |
| | | 516 | | { |
| | | 517 | | private readonly List<XmlDictionaryString> _dictionaryStrings; |
| | | 518 | | |
| | 0 | 519 | | public CollectionDictionary(List<XmlDictionaryString> dictionaryStrings) |
| | | 520 | | { |
| | 0 | 521 | | _dictionaryStrings = dictionaryStrings ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new |
| | 0 | 522 | | } |
| | | 523 | | |
| | | 524 | | public bool TryLookup(string value, out XmlDictionaryString result) |
| | | 525 | | { |
| | 0 | 526 | | if (value == null) |
| | | 527 | | { |
| | 0 | 528 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(value))); |
| | | 529 | | } |
| | | 530 | | |
| | 0 | 531 | | for (int i = 0; i < _dictionaryStrings.Count; ++i) |
| | | 532 | | { |
| | 0 | 533 | | if (_dictionaryStrings[i].Value.Equals(value)) |
| | | 534 | | { |
| | 0 | 535 | | result = _dictionaryStrings[i]; |
| | 0 | 536 | | return true; |
| | | 537 | | } |
| | | 538 | | } |
| | 0 | 539 | | result = null; |
| | 0 | 540 | | return false; |
| | | 541 | | } |
| | | 542 | | |
| | | 543 | | public bool TryLookup(int key, out XmlDictionaryString result) |
| | | 544 | | { |
| | 0 | 545 | | for (int i = 0; i < _dictionaryStrings.Count; ++i) |
| | | 546 | | { |
| | 0 | 547 | | if (_dictionaryStrings[i].Key == key) |
| | | 548 | | { |
| | 0 | 549 | | result = _dictionaryStrings[i]; |
| | 0 | 550 | | return true; |
| | | 551 | | } |
| | | 552 | | } |
| | 0 | 553 | | result = null; |
| | 0 | 554 | | return false; |
| | | 555 | | } |
| | | 556 | | |
| | | 557 | | public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result) |
| | | 558 | | { |
| | 0 | 559 | | if (value == null) |
| | | 560 | | { |
| | 0 | 561 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(value))); |
| | | 562 | | } |
| | | 563 | | |
| | 0 | 564 | | for (int i = 0; i < _dictionaryStrings.Count; ++i) |
| | | 565 | | { |
| | 0 | 566 | | if ((_dictionaryStrings[i].Key == value.Key) && |
| | 0 | 567 | | (_dictionaryStrings[i].Value.Equals(value.Value))) |
| | | 568 | | { |
| | 0 | 569 | | result = _dictionaryStrings[i]; |
| | 0 | 570 | | return true; |
| | | 571 | | } |
| | | 572 | | } |
| | 0 | 573 | | result = null; |
| | 0 | 574 | | return false; |
| | | 575 | | } |
| | | 576 | | } |
| | | 577 | | } |
| | | 578 | | } |