| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Security.Claims; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.IdentityModel.Selectors; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Defines a collection of SecurityTokenHandlers. |
| | | 15 | | /// </summary> |
| | | 16 | | public class SecurityTokenHandlerCollection : Collection<SecurityTokenHandler> |
| | | 17 | | { |
| | 1 | 18 | | internal static int s_defaultHandlerCollectionCount = 8; |
| | 1 | 19 | | internal static SecurityTokenHandlerCollection s_defaultSecurityTokenHandlerCollection = null; //TODO if better |
| | 2 | 20 | | private readonly Dictionary<string, SecurityTokenHandler> _handlersByIdentifier = new Dictionary<string, Securit |
| | 2 | 21 | | private readonly Dictionary<Type, SecurityTokenHandler> _handlersByType = new Dictionary<Type, SecurityTokenHand |
| | | 22 | | private readonly KeyInfoSerializer _keyInfoSerializer; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>. |
| | | 26 | | /// Creates an empty set. |
| | | 27 | | /// </summary> |
| | | 28 | | public SecurityTokenHandlerCollection() |
| | 0 | 29 | | : this(new SecurityTokenHandlerConfiguration()) |
| | | 30 | | { |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/>. |
| | | 35 | | /// Creates an empty set. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="configuration">The configuration to associate with the collection.</param> |
| | 2 | 38 | | public SecurityTokenHandlerCollection(SecurityTokenHandlerConfiguration configuration) |
| | | 39 | | { |
| | 2 | 40 | | Configuration = configuration ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(con |
| | 2 | 41 | | _keyInfoSerializer = new KeyInfoSerializer(true); |
| | 2 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/> |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="handlers">List of SecurityTokenHandlers to initialize from.</param> |
| | | 48 | | /// <remarks> |
| | | 49 | | /// Do not use this constructor to attempt to clone an instance of a SecurityTokenHandlerCollection, |
| | | 50 | | /// use the Clone method instead. |
| | | 51 | | /// </remarks> |
| | | 52 | | public SecurityTokenHandlerCollection(IEnumerable<SecurityTokenHandler> handlers) |
| | 0 | 53 | | : this(handlers, new SecurityTokenHandlerConfiguration()) |
| | | 54 | | { |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates an instance of <see cref="SecurityTokenHandlerCollection"/> |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="handlers">List of SecurityTokenHandlers to initialize from.</param> |
| | | 61 | | /// <param name="configuration">The <see cref="SecurityTokenHandlerConfiguration"/> in effect.</param> |
| | | 62 | | /// <remarks> |
| | | 63 | | /// Do not use this constructor to attempt to clone an instance of a SecurityTokenHandlerCollection, |
| | | 64 | | /// use the Clone method instead. |
| | | 65 | | /// </remarks> |
| | | 66 | | public SecurityTokenHandlerCollection(IEnumerable<SecurityTokenHandler> handlers, SecurityTokenHandlerConfigurat |
| | 0 | 67 | | : this(configuration) |
| | | 68 | | { |
| | 0 | 69 | | if (handlers == null) |
| | | 70 | | { |
| | 0 | 71 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(handlers)); |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | foreach (SecurityTokenHandler handler in handlers) |
| | | 75 | | { |
| | 0 | 76 | | Add(handler); |
| | | 77 | | } |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets an instance of <see cref="SecurityTokenHandlerConfiguration"/> |
| | | 82 | | /// </summary> |
| | 22 | 83 | | public SecurityTokenHandlerConfiguration Configuration { get; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Gets the List of System.Type of the Token Handlers in this collection. |
| | | 87 | | /// </summary> |
| | | 88 | | public IEnumerable<Type> TokenTypes |
| | | 89 | | { |
| | 0 | 90 | | get { return _handlersByType.Keys; } |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Gets the list of Token type Identifier of the Token Handlers. |
| | | 95 | | /// </summary> |
| | | 96 | | public IEnumerable<string> TokenTypeIdentifiers |
| | | 97 | | { |
| | | 98 | | get |
| | | 99 | | { |
| | 0 | 100 | | return _handlersByIdentifier.Keys; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Gets a Token Handler by its Token Type Identifier. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="tokenTypeIdentifier">The Token Type Identfier string to search for.</param> |
| | | 108 | | /// <returns>Instance of a SecurityTokenHandler.</returns> |
| | | 109 | | public SecurityTokenHandler this[string tokenTypeIdentifier] |
| | | 110 | | { |
| | | 111 | | get |
| | | 112 | | { |
| | 4 | 113 | | if (string.IsNullOrEmpty(tokenTypeIdentifier)) |
| | | 114 | | { |
| | 0 | 115 | | return null; |
| | | 116 | | } |
| | | 117 | | |
| | 4 | 118 | | _handlersByIdentifier.TryGetValue(tokenTypeIdentifier, out SecurityTokenHandler handler); |
| | 4 | 119 | | return handler; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary> |
| | | 124 | | /// Gets a Token Handler that can handle a given SecurityToken. |
| | | 125 | | /// </summary> |
| | | 126 | | /// <param name=nameof(token)>SecurityToken for which a Token Handler is requested.</param> |
| | | 127 | | /// <returns>Instance of SecurityTokenHandler.</returns> |
| | | 128 | | public SecurityTokenHandler this[SecurityToken token] |
| | | 129 | | { |
| | | 130 | | get |
| | | 131 | | { |
| | 0 | 132 | | if (null == token) |
| | | 133 | | { |
| | 0 | 134 | | return null; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return this[token.GetType()]; |
| | | 138 | | } |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Gets a Token Handler based on the System.Type of the token. |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="tokenType">System.Type of the Token that needs to be handled.</param> |
| | | 145 | | /// <returns>Instance of SecurityTokenHandler.</returns> |
| | | 146 | | public SecurityTokenHandler this[Type tokenType] |
| | | 147 | | { |
| | | 148 | | get |
| | | 149 | | { |
| | 10 | 150 | | SecurityTokenHandler handler = null; |
| | 10 | 151 | | if (tokenType != null) |
| | | 152 | | { |
| | 10 | 153 | | _handlersByType.TryGetValue(tokenType, out handler); |
| | | 154 | | } |
| | | 155 | | |
| | 10 | 156 | | return handler; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | |
| | | 161 | | //TODO - Check which one is important apart from Saml and port accordingly.. |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Creates a system default collection of basic SecurityTokenHandlers, each of which has the system default con |
| | | 165 | | /// The SecurityTokenHandlers in this collection must be configured with service specific data before they can b |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="configuration">The configuration to associate with the collection.</param> |
| | | 168 | | /// <returns>A SecurityTokenHandlerCollection with default basic SecurityTokenHandlers.</returns> |
| | | 169 | | internal static SecurityTokenHandlerCollection CreateDefaultSecurityTokenHandlerCollection(SecurityTokenHandlerC |
| | | 170 | | IEnumerable<SecurityTokenHandler> securityTokenHandlers) |
| | | 171 | | { |
| | 2 | 172 | | SecurityTokenHandlerCollection collection = new SecurityTokenHandlerCollection(configuration); |
| | 44 | 173 | | foreach(SecurityTokenHandler handler in securityTokenHandlers) |
| | | 174 | | { |
| | 20 | 175 | | collection.AddOrReplace(handler); |
| | | 176 | | } |
| | 2 | 177 | | s_defaultHandlerCollectionCount = collection.Count; |
| | 2 | 178 | | s_defaultSecurityTokenHandlerCollection = collection; |
| | 2 | 179 | | return collection; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | internal SecurityTokenSerializer KeyInfoSerializer |
| | | 183 | | { |
| | 0 | 184 | | get { return _keyInfoSerializer; } |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Adds a new handler or replace the existing handler with the same token type identifier |
| | | 189 | | /// with with the new handler. |
| | | 190 | | /// </summary> |
| | | 191 | | /// <param name="handler">The SecurityTokenHandler to add or replace</param> |
| | | 192 | | /// <exception cref="ArgumentNullException">When the input parameter is null.</exception> |
| | | 193 | | public void AddOrReplace(SecurityTokenHandler handler) |
| | | 194 | | { |
| | 20 | 195 | | if (handler == null) |
| | | 196 | | { |
| | 0 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(handler)); |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | // Remove the old one if it exists |
| | 20 | 201 | | Type tokenType = handler.TokenType; |
| | 20 | 202 | | if (tokenType != null && _handlersByType.ContainsKey(tokenType)) |
| | | 203 | | { |
| | 10 | 204 | | Remove(this[tokenType]); |
| | | 205 | | } |
| | | 206 | | else |
| | | 207 | | { |
| | 10 | 208 | | string[] identifiers = handler.GetTokenTypeIdentifiers(); |
| | 10 | 209 | | if (identifiers != null) |
| | | 210 | | { |
| | 56 | 211 | | foreach (string tokenTypeIdentifier in identifiers) |
| | | 212 | | { |
| | 18 | 213 | | if (tokenTypeIdentifier != null && _handlersByIdentifier.ContainsKey(tokenTypeIdentifier)) |
| | | 214 | | { |
| | 0 | 215 | | Remove(this[tokenTypeIdentifier]); |
| | 0 | 216 | | break; |
| | | 217 | | } |
| | | 218 | | } |
| | | 219 | | } |
| | | 220 | | } |
| | | 221 | | |
| | | 222 | | // Add the new handler in the collection |
| | 20 | 223 | | Add(handler); |
| | 20 | 224 | | } |
| | | 225 | | |
| | | 226 | | /// <summary> |
| | | 227 | | /// Checks if a token can be read using the SecurityTokenHandlers. |
| | | 228 | | /// </summary> |
| | | 229 | | /// <param name=nameof(reader)>XmlReader pointing at token.</param> |
| | | 230 | | /// <returns>True if the token can be read, false otherwise</returns> |
| | | 231 | | /// <exception cref="ArgumentNullException">The input argument 'reader' is null.</exception> |
| | | 232 | | public bool CanReadToken(XmlReader reader) |
| | | 233 | | { |
| | 0 | 234 | | if (reader == null) |
| | | 235 | | { |
| | 0 | 236 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 237 | | } |
| | | 238 | | |
| | 0 | 239 | | foreach (SecurityTokenHandler handler in this) |
| | | 240 | | { |
| | 0 | 241 | | if (null != handler && handler.CanReadToken(reader)) |
| | | 242 | | { |
| | 0 | 243 | | return true; |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | return false; |
| | 0 | 248 | | } |
| | | 249 | | |
| | | 250 | | /// <summary> |
| | | 251 | | /// Checks if a token can be read using the SecurityTokenHandlers. |
| | | 252 | | /// </summary> |
| | | 253 | | /// <param name="tokenString">The token string thats needs to be read.</param> |
| | | 254 | | /// <returns>True if the token can be read, false otherwise</returns> |
| | | 255 | | /// <exception cref="ArgumentException">The input argument 'tokenString' is null or empty.</exception> |
| | | 256 | | public bool CanReadToken(string tokenString) |
| | | 257 | | { |
| | 0 | 258 | | if (string.IsNullOrEmpty(tokenString)) |
| | | 259 | | { |
| | 0 | 260 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenString)); |
| | | 261 | | } |
| | | 262 | | |
| | 0 | 263 | | foreach (SecurityTokenHandler handler in this) |
| | | 264 | | { |
| | 0 | 265 | | if (null != handler && handler.CanReadToken(tokenString)) |
| | | 266 | | { |
| | 0 | 267 | | return true; |
| | | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | return false; |
| | 0 | 272 | | } |
| | | 273 | | |
| | | 274 | | /// <summary> |
| | | 275 | | /// Checks if a token can be written using the SecurityTokenHandlers. |
| | | 276 | | /// </summary> |
| | | 277 | | /// <param name=token>SecurityToken to be written out.</param> |
| | | 278 | | /// <returns>True if the token can be written, false otherwise</returns> |
| | | 279 | | /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception> |
| | | 280 | | public bool CanWriteToken(SecurityToken token) |
| | | 281 | | { |
| | 0 | 282 | | if (token == null) |
| | | 283 | | { |
| | 0 | 284 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | SecurityTokenHandler handler = this[token]; |
| | 0 | 288 | | if (null != handler && handler.CanWriteToken) |
| | | 289 | | { |
| | 0 | 290 | | return true; |
| | | 291 | | } |
| | | 292 | | |
| | 0 | 293 | | return false; |
| | | 294 | | } |
| | | 295 | | |
| | | 296 | | /// <summary> |
| | | 297 | | /// Validates a given token using the SecurityTokenHandlers. |
| | | 298 | | /// </summary> |
| | | 299 | | /// <param name=nameof(token)>The SecurityToken to be validated.</param> |
| | | 300 | | /// <returns>A <see cref="ReadOnlyCollection{T}"/> of <see cref="ClaimsIdentity"/> representing the identities c |
| | | 301 | | /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception> |
| | | 302 | | /// <exception cref="InvalidOperationException">A <see cref="SecurityTokenHandler"/> cannot be found that can va |
| | | 303 | | public ReadOnlyCollection<ClaimsIdentity> ValidateToken(SecurityToken token) |
| | | 304 | | { |
| | 0 | 305 | | if (token == null) |
| | | 306 | | { |
| | 0 | 307 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 308 | | } |
| | | 309 | | |
| | 0 | 310 | | SecurityTokenHandler handler = this[token]; |
| | 0 | 311 | | if (null == handler || !handler.CanValidateToken) |
| | | 312 | | { |
| | 0 | 313 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 314 | | } |
| | | 315 | | |
| | 0 | 316 | | return handler.ValidateToken(token); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | /// <summary> |
| | | 320 | | /// Reads a token using the TokenHandlers. |
| | | 321 | | /// </summary> |
| | | 322 | | /// <param name=nameof(reader)>XmlReader pointing at token.</param> |
| | | 323 | | /// <returns>Instance of <see cref="SecurityToken"/></returns> |
| | | 324 | | /// <exception cref="ArgumentNullException">The input argument 'reader' is null.</exception> |
| | | 325 | | public SecurityToken ReadToken(XmlReader reader) |
| | | 326 | | { |
| | 0 | 327 | | if (reader == null) |
| | | 328 | | { |
| | 0 | 329 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 330 | | } |
| | | 331 | | |
| | 0 | 332 | | foreach (SecurityTokenHandler handler in this) |
| | | 333 | | { |
| | 0 | 334 | | if (null != handler && handler.CanReadToken(reader)) |
| | | 335 | | { |
| | 0 | 336 | | return handler.ReadToken(reader); |
| | | 337 | | } |
| | | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | return null; |
| | 0 | 341 | | } |
| | | 342 | | |
| | | 343 | | /// <summary> |
| | | 344 | | /// Reads a token using the TokenHandlers. |
| | | 345 | | /// </summary> |
| | | 346 | | /// <param name="tokenString">The token string to be deserialized.</param> |
| | | 347 | | /// <returns>Instance of <see cref="SecurityToken"/></returns> |
| | | 348 | | /// <exception cref="ArgumentException">The input argument 'tokenString' is null or empty.</exception> |
| | | 349 | | public SecurityToken ReadToken(string tokenString) |
| | | 350 | | { |
| | 0 | 351 | | if (string.IsNullOrEmpty(tokenString)) |
| | | 352 | | { |
| | 0 | 353 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenString)); |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | foreach (SecurityTokenHandler handler in this) |
| | | 357 | | { |
| | 0 | 358 | | if (null != handler && handler.CanReadToken(tokenString)) |
| | | 359 | | { |
| | 0 | 360 | | return handler.ReadToken(tokenString); |
| | | 361 | | } |
| | | 362 | | } |
| | | 363 | | |
| | 0 | 364 | | return null; |
| | 0 | 365 | | } |
| | | 366 | | |
| | | 367 | | /// <summary> |
| | | 368 | | /// Writes a given SecurityToken to the XmlWriter. |
| | | 369 | | /// </summary> |
| | | 370 | | /// <param name="writer">XmlWriter to write the token into.</param> |
| | | 371 | | /// <param name=nameof(token)>SecurityToken to be written out.</param> |
| | | 372 | | /// <exception cref="ArgumentNullException">The input argument 'writer' or 'token' is null.</exception> |
| | | 373 | | public void WriteToken(XmlWriter writer, SecurityToken token) |
| | | 374 | | { |
| | 0 | 375 | | if (writer == null) |
| | | 376 | | { |
| | 0 | 377 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 378 | | } |
| | | 379 | | |
| | 0 | 380 | | if (token == null) |
| | | 381 | | { |
| | 0 | 382 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 383 | | } |
| | | 384 | | |
| | 0 | 385 | | SecurityTokenHandler handler = this[token]; |
| | 0 | 386 | | if (null == handler || !handler.CanWriteToken) |
| | | 387 | | { |
| | 0 | 388 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 389 | | } |
| | | 390 | | |
| | 0 | 391 | | handler.WriteToken(writer, token); |
| | 0 | 392 | | } |
| | | 393 | | |
| | | 394 | | /// <summary> |
| | | 395 | | /// Writes a given SecurityToken to a string. |
| | | 396 | | /// </summary> |
| | | 397 | | /// <param name=nameof(token)>SecurityToken to be written out.</param> |
| | | 398 | | /// <returns>The serialized token.</returns> |
| | | 399 | | /// <exception cref="ArgumentNullException">The input argument 'token' is null.</exception> |
| | | 400 | | public string WriteToken(SecurityToken token) |
| | | 401 | | { |
| | 0 | 402 | | if (token == null) |
| | | 403 | | { |
| | 0 | 404 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 405 | | } |
| | | 406 | | |
| | 0 | 407 | | SecurityTokenHandler handler = this[token]; |
| | 0 | 408 | | if (null == handler || !handler.CanWriteToken) |
| | | 409 | | { |
| | 0 | 410 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 411 | | } |
| | | 412 | | |
| | 0 | 413 | | return handler.WriteToken(token); |
| | | 414 | | } |
| | | 415 | | |
| | | 416 | | /// <summary> |
| | | 417 | | /// Override. (Inherited from Collection<T>"/> |
| | | 418 | | /// </summary> |
| | | 419 | | protected override void ClearItems() |
| | | 420 | | { |
| | 0 | 421 | | base.ClearItems(); |
| | 0 | 422 | | _handlersByIdentifier.Clear(); |
| | 0 | 423 | | _handlersByType.Clear(); |
| | 0 | 424 | | } |
| | | 425 | | |
| | | 426 | | /// <summary> |
| | | 427 | | /// Override. (Inherited from Collection<T>"/> |
| | | 428 | | /// </summary> |
| | | 429 | | /// <param name="index">The zero-based index at which item should be inserted.</param> |
| | | 430 | | /// <param name="item">The object to insert. The value can be null for reference types.</param> |
| | | 431 | | protected override void InsertItem(int index, SecurityTokenHandler item) |
| | | 432 | | { |
| | 20 | 433 | | base.InsertItem(index, item); |
| | | 434 | | |
| | | 435 | | try |
| | | 436 | | { |
| | 20 | 437 | | AddToDictionaries(item); |
| | 20 | 438 | | } |
| | 0 | 439 | | catch |
| | | 440 | | { |
| | 0 | 441 | | base.RemoveItem(index); |
| | 0 | 442 | | throw; |
| | | 443 | | } |
| | 20 | 444 | | } |
| | | 445 | | |
| | | 446 | | /// <summary> |
| | | 447 | | /// Override. (Inherited from Collection<T>"/> |
| | | 448 | | /// </summary> |
| | | 449 | | /// <param name="index">The zero-based index of the element to remove.</param> |
| | | 450 | | protected override void RemoveItem(int index) |
| | | 451 | | { |
| | 10 | 452 | | SecurityTokenHandler removedItem = Items[index]; |
| | 10 | 453 | | base.RemoveItem(index); |
| | 10 | 454 | | RemoveFromDictionaries(removedItem); |
| | 10 | 455 | | } |
| | | 456 | | |
| | | 457 | | /// <summary> |
| | | 458 | | /// Override. (Inherited from Collection<T>"/> |
| | | 459 | | /// </summary> |
| | | 460 | | /// <param name="index">The zero-based index of the element to replace.</param> |
| | | 461 | | /// <param name="item">The new value for the element at the specified index. The value can be null for reference |
| | | 462 | | protected override void SetItem(int index, SecurityTokenHandler item) |
| | | 463 | | { |
| | 0 | 464 | | SecurityTokenHandler replaced = Items[index]; |
| | 0 | 465 | | base.SetItem(index, item); |
| | | 466 | | |
| | 0 | 467 | | RemoveFromDictionaries(replaced); |
| | | 468 | | |
| | | 469 | | try |
| | | 470 | | { |
| | 0 | 471 | | AddToDictionaries(item); |
| | 0 | 472 | | } |
| | 0 | 473 | | catch |
| | | 474 | | { |
| | 0 | 475 | | base.SetItem(index, replaced); |
| | 0 | 476 | | AddToDictionaries(replaced); |
| | 0 | 477 | | throw; |
| | | 478 | | } |
| | 0 | 479 | | } |
| | | 480 | | |
| | | 481 | | public bool CanReadKeyIdentifierClause(XmlReader reader) |
| | | 482 | | { |
| | 0 | 483 | | if (reader == null) |
| | | 484 | | { |
| | 0 | 485 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 486 | | } |
| | | 487 | | |
| | 0 | 488 | | return CanReadKeyIdentifierClauseCore(reader); |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | /// <summary> |
| | | 492 | | /// Checks if the wrapped SecurityTokenHandler or the base WSSecurityTokenSerializer can read the |
| | | 493 | | /// SecurityKeyIdentifierClause. |
| | | 494 | | /// </summary> |
| | | 495 | | /// <param name=nameof(reader)>Reader to a SecurityKeyIdentifierClause.</param> |
| | | 496 | | /// <returns>'True' if the SecurityKeyIdentifierCause can be read.</returns> |
| | | 497 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception> |
| | | 498 | | protected virtual bool CanReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 499 | | { |
| | 0 | 500 | | if (reader == null) |
| | | 501 | | { |
| | 0 | 502 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 503 | | } |
| | | 504 | | |
| | 0 | 505 | | foreach (SecurityTokenHandler securityTokenHandler in this) |
| | | 506 | | { |
| | 0 | 507 | | if (securityTokenHandler.CanReadKeyIdentifierClause(reader)) |
| | | 508 | | { |
| | 0 | 509 | | return true; |
| | | 510 | | } |
| | | 511 | | } |
| | | 512 | | |
| | 0 | 513 | | return false; |
| | 0 | 514 | | } |
| | | 515 | | |
| | | 516 | | public SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader) |
| | | 517 | | { |
| | 0 | 518 | | if (reader == null) |
| | | 519 | | { |
| | 0 | 520 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 521 | | } |
| | | 522 | | |
| | 0 | 523 | | return ReadKeyIdentifierClauseCore(reader); |
| | | 524 | | } |
| | | 525 | | |
| | | 526 | | /// <summary> |
| | | 527 | | /// Deserializes a SecurityKeyIdentifierClause from the given reader. |
| | | 528 | | /// </summary> |
| | | 529 | | /// <param name=nameof(reader)>XmlReader to a SecurityKeyIdentifierClause.</param> |
| | | 530 | | /// <returns>The deserialized SecurityKeyIdentifierClause.</returns> |
| | | 531 | | /// <exception cref="ArgumentNullException">The input parameter 'reader' is null.</exception> |
| | | 532 | | protected virtual SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader) |
| | | 533 | | { |
| | 0 | 534 | | if (reader == null) |
| | | 535 | | { |
| | 0 | 536 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 537 | | } |
| | | 538 | | |
| | 0 | 539 | | foreach (SecurityTokenHandler securityTokenHandler in this) |
| | | 540 | | { |
| | 0 | 541 | | if (securityTokenHandler.CanReadKeyIdentifierClause(reader)) |
| | | 542 | | { |
| | 0 | 543 | | return securityTokenHandler.ReadKeyIdentifierClause(reader); |
| | | 544 | | } |
| | | 545 | | } |
| | | 546 | | |
| | 0 | 547 | | return _keyInfoSerializer.ReadKeyIdentifierClause(reader); |
| | 0 | 548 | | } |
| | | 549 | | |
| | | 550 | | public void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 551 | | { |
| | 0 | 552 | | if (writer == null) |
| | | 553 | | { |
| | 0 | 554 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 555 | | } |
| | | 556 | | |
| | 0 | 557 | | if (keyIdentifierClause == null) |
| | | 558 | | { |
| | 0 | 559 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 560 | | } |
| | | 561 | | |
| | 0 | 562 | | WriteKeyIdentifierClauseCore(writer, keyIdentifierClause); |
| | 0 | 563 | | } |
| | | 564 | | |
| | | 565 | | /// <summary> |
| | | 566 | | /// Serializes the given SecurityKeyIdentifierClause in a XmlWriter. |
| | | 567 | | /// </summary> |
| | | 568 | | /// <param name="writer">XmlWriter to write into.</param> |
| | | 569 | | /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to be written.</param> |
| | | 570 | | /// <exception cref="ArgumentNullException">The input parameter 'writer' or 'keyIdentifierClause' is null.</exce |
| | | 571 | | protected virtual void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierC |
| | | 572 | | { |
| | 0 | 573 | | if (writer == null) |
| | | 574 | | { |
| | 0 | 575 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 576 | | } |
| | | 577 | | |
| | 0 | 578 | | if (keyIdentifierClause == null) |
| | | 579 | | { |
| | 0 | 580 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 581 | | } |
| | | 582 | | |
| | 0 | 583 | | foreach (SecurityTokenHandler securityTokenHandler in this) |
| | | 584 | | { |
| | 0 | 585 | | if (securityTokenHandler.CanWriteKeyIdentifierClause(keyIdentifierClause)) |
| | | 586 | | { |
| | 0 | 587 | | securityTokenHandler.WriteKeyIdentifierClause(writer, keyIdentifierClause); |
| | 0 | 588 | | return; |
| | | 589 | | } |
| | | 590 | | } |
| | | 591 | | |
| | 0 | 592 | | _keyInfoSerializer.WriteKeyIdentifierClause(writer, keyIdentifierClause); |
| | 0 | 593 | | } |
| | | 594 | | |
| | | 595 | | private void AddToDictionaries(SecurityTokenHandler handler) |
| | | 596 | | { |
| | 20 | 597 | | if (handler == null) |
| | | 598 | | { |
| | 0 | 599 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("handler"); |
| | | 600 | | } |
| | | 601 | | |
| | 20 | 602 | | bool firstSucceeded = false; |
| | | 603 | | |
| | 20 | 604 | | string[] identifiers = handler.GetTokenTypeIdentifiers(); |
| | 20 | 605 | | if (identifiers != null) |
| | | 606 | | { |
| | 112 | 607 | | foreach (string typeId in identifiers) |
| | | 608 | | { |
| | 36 | 609 | | if (typeId != null) |
| | | 610 | | { |
| | 32 | 611 | | _handlersByIdentifier.Add(typeId, handler); |
| | 32 | 612 | | firstSucceeded = true; |
| | | 613 | | } |
| | | 614 | | } |
| | | 615 | | } |
| | | 616 | | |
| | 20 | 617 | | Type type = handler.TokenType; |
| | 20 | 618 | | if (handler.TokenType != null) |
| | | 619 | | { |
| | | 620 | | try |
| | | 621 | | { |
| | 20 | 622 | | _handlersByType.Add(type, handler); |
| | 20 | 623 | | } |
| | 0 | 624 | | catch |
| | | 625 | | { |
| | 0 | 626 | | if (firstSucceeded) |
| | | 627 | | { |
| | 0 | 628 | | RemoveFromDictionaries(handler); |
| | | 629 | | } |
| | | 630 | | |
| | 0 | 631 | | throw; |
| | | 632 | | } |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | // Ensure that the handler knows which collection it is in. |
| | 20 | 636 | | handler.ContainingCollection = this; |
| | | 637 | | |
| | | 638 | | // Propagate this collection's STH configuration to the handler |
| | | 639 | | // if the handler's configuration is unset. |
| | 20 | 640 | | if (handler.Configuration == null) |
| | | 641 | | { |
| | 20 | 642 | | handler.Configuration = Configuration; |
| | | 643 | | } |
| | 20 | 644 | | } |
| | | 645 | | |
| | | 646 | | private void RemoveFromDictionaries(SecurityTokenHandler handler) |
| | | 647 | | { |
| | 10 | 648 | | string[] identifiers = handler.GetTokenTypeIdentifiers(); |
| | 10 | 649 | | if (identifiers != null) |
| | | 650 | | { |
| | 56 | 651 | | foreach (string typeId in identifiers) |
| | | 652 | | { |
| | 18 | 653 | | if (typeId != null) |
| | | 654 | | { |
| | 16 | 655 | | _handlersByIdentifier.Remove(typeId); |
| | | 656 | | } |
| | | 657 | | } |
| | | 658 | | } |
| | | 659 | | |
| | 10 | 660 | | Type type = handler.TokenType; |
| | 10 | 661 | | if (type != null && _handlersByType.ContainsKey(type)) |
| | | 662 | | { |
| | 10 | 663 | | _handlersByType.Remove(type); |
| | | 664 | | } |
| | | 665 | | |
| | | 666 | | // Ensure that the handler knows that it is no longer |
| | | 667 | | // in a collection. |
| | 10 | 668 | | handler.ContainingCollection = null; |
| | 10 | 669 | | handler.Configuration = null; |
| | 10 | 670 | | } |
| | | 671 | | } |
| | | 672 | | } |