| | | 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 CoreWCF.IdentityModel.Configuration; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// A class which manages multiple named <see cref="SecurityTokenHandlerCollection"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public class SecurityTokenHandlerCollectionManager |
| | | 14 | | { |
| | 2 | 15 | | private readonly Dictionary<string, SecurityTokenHandlerCollection> _collections = new Dictionary<string, Securi |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initialize an instance of <see cref="SecurityTokenHandlerCollectionManager"/> for a given named service. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="serviceName">A <see cref="String"/> indicating the name of the associated service.</param> |
| | 2 | 21 | | public SecurityTokenHandlerCollectionManager(string serviceName) |
| | | 22 | | { |
| | 2 | 23 | | ServiceName = serviceName ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(service |
| | 2 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initialized with default service configuration. |
| | | 28 | | /// </summary> |
| | | 29 | | private SecurityTokenHandlerCollectionManager() |
| | 0 | 30 | | : this(ConfigurationStrings.DefaultServiceName) |
| | | 31 | | { |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets a count of the number of SecurityTokenHandlerCollections in this |
| | | 36 | | /// SecurityTokenHandlerCollectionManager. |
| | | 37 | | /// </summary> |
| | 0 | 38 | | public int Count => _collections.Count; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets the service name. |
| | | 42 | | /// </summary> |
| | 2 | 43 | | public string ServiceName { get; } = ConfigurationStrings.DefaultServiceName; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets an enumeration over the SecurityTokenHandlerCollection list. |
| | | 47 | | /// </summary> |
| | 0 | 48 | | public IEnumerable<SecurityTokenHandlerCollection> SecurityTokenHandlerCollections => _collections.Values; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// The SecurityTokenHandlerCollection for the specified usage. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name=nameof(usage)>The usage name for the SecurityTokenHandlerCollection.</param> |
| | | 54 | | /// <returns>A SecurityTokenHandlerCollection</returns> |
| | | 55 | | /// <remarks> |
| | | 56 | | /// Behaves like a dictionary in that it will throw an exception if there is no |
| | | 57 | | /// value for the specified key. |
| | | 58 | | /// </remarks> |
| | | 59 | | public SecurityTokenHandlerCollection this[string usage] |
| | | 60 | | { |
| | | 61 | | get |
| | | 62 | | { |
| | | 63 | | // Empty String is valid (Usage.Default) |
| | 2 | 64 | | if (null == usage) |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(usage)); |
| | | 67 | | } |
| | | 68 | | |
| | 2 | 69 | | return _collections[usage]; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | set |
| | | 73 | | { |
| | | 74 | | // Empty String is valid (Usage.Default) |
| | 2 | 75 | | if (null == usage) |
| | | 76 | | { |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(usage)); |
| | | 78 | | } |
| | | 79 | | |
| | 2 | 80 | | _collections[usage] = value; |
| | 2 | 81 | | } |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// No token handlers are created. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <returns>An empty token handler collection manager.</returns> |
| | | 88 | | public static SecurityTokenHandlerCollectionManager CreateEmptySecurityTokenHandlerCollectionManager() |
| | | 89 | | { |
| | 2 | 90 | | return new SecurityTokenHandlerCollectionManager(ConfigurationStrings.DefaultConfigurationElementName); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Checks if a SecurityTokenHandlerCollection exists for the given usage. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name=nameof(usage)>A string that represents the usage of the SecurityTokenHandlerCollection.</param> |
| | | 97 | | /// <returns>Whether or not a token handler collection exists for the given usage.</returns> |
| | | 98 | | public bool ContainsKey(string usage) |
| | | 99 | | { |
| | 2 | 100 | | return _collections.ContainsKey(usage); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Defines standard collection names used by the framework. |
| | | 105 | | /// </summary> |
| | | 106 | | public static class Usage |
| | | 107 | | { |
| | | 108 | | /// <summary> |
| | | 109 | | /// Used to reference the default collection of handlers. |
| | | 110 | | /// </summary> |
| | | 111 | | public const string Default = ""; |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Used to reference a collection of handlers for ActAs element processing. |
| | | 115 | | /// </summary> |
| | | 116 | | public const string ActAs = "ActAs"; |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Used to reference a collection of handlers for OnBehalfOf element processing. |
| | | 120 | | /// </summary> |
| | | 121 | | public const string OnBehalfOf = "OnBehalfOf"; |
| | | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |