1     //-----------------------------------------------------------------------
2 // <copyright file="SecurityTokenHandlerCollectionManager.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6 
7 namespace System.IdentityModel.Tokens
8 {
9     using System;
10     using System.Collections.Generic;
11     using System.IdentityModel.Configuration;
12     using System.IdentityModel.Selectors;
13     using System.Linq;
14     using System.Text;
15 
16     /// <summary>
17     /// A class which manages multiple named <see cref="SecurityTokenHandlerCollection"/>.
18     /// </summary>
19     public class SecurityTokenHandlerCollectionManager
20     {
21         private Dictionary<string, SecurityTokenHandlerCollection> collections = new Dictionary<string, SecurityTokenHandlerCollection>();
22         private string serviceName = ConfigurationStrings.DefaultServiceName;
23 
24         /// <summary>
25         /// Initialize an instance of <see cref="SecurityTokenHandlerCollectionManager"/> for a given named service.
26         /// </summary>
27         /// <param name="serviceName">A <see cref="String"/> indicating the name of the associated service.</param>
SecurityTokenHandlerCollectionManager(string serviceName)28         public SecurityTokenHandlerCollectionManager(string serviceName)
29         {
30             if (serviceName == null)
31             {
32                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serviceName");
33             }
34 
35             this.serviceName = serviceName;
36         }
37 
38         /// <summary>
39         /// Initialized with default service configuration.
40         /// </summary>
SecurityTokenHandlerCollectionManager()41         private SecurityTokenHandlerCollectionManager()
42             : this(ConfigurationStrings.DefaultServiceName)
43         {
44         }
45 
46         /// <summary>
47         /// Gets a count of the number of SecurityTokenHandlerCollections in this
48         /// SecurityTokenHandlerCollectionManager.
49         /// </summary>
50         public int Count
51         {
52             get { return this.collections.Count; }
53         }
54 
55         /// <summary>
56         ///  Gets the service name.
57         /// </summary>
58         public string ServiceName
59         {
60             get { return this.serviceName; }
61         }
62 
63         /// <summary>
64         /// Gets an enumeration over the SecurityTokenHandlerCollection list.
65         /// </summary>
66         public IEnumerable<SecurityTokenHandlerCollection> SecurityTokenHandlerCollections
67         {
68             get
69             {
70                 return this.collections.Values;
71             }
72         }
73 
74         /// <summary>
75         /// The SecurityTokenHandlerCollection for the specified usage.
76         /// </summary>
77         /// <param name="usage">The usage name for the SecurityTokenHandlerCollection.</param>
78         /// <returns>A SecurityTokenHandlerCollection</returns>
79         /// <remarks>
80         /// Behaves like a dictionary in that it will throw an exception if there is no
81         /// value for the specified key.
82         /// </remarks>
83         public SecurityTokenHandlerCollection this[string usage]
84         {
85             get
86             {
87                 // Empty String is valid (Usage.Default)
88                 if (null == usage)
89                 {
90                     throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("usage");
91                 }
92 
93                 return this.collections[usage];
94             }
95 
96             set
97             {
98                 // Empty String is valid (Usage.Default)
99                 if (null == usage)
100                 {
101                     throw DiagnosticUtility.ThrowHelperArgumentNullOrEmptyString("usage");
102                 }
103 
104                 this.collections[usage] = value;
105             }
106         }
107 
108         /// <summary>
109         /// No token handlers are created.
110         /// </summary>
111         /// <returns>An empty token handler collection manager.</returns>
CreateEmptySecurityTokenHandlerCollectionManager()112         public static SecurityTokenHandlerCollectionManager CreateEmptySecurityTokenHandlerCollectionManager()
113         {
114             return new SecurityTokenHandlerCollectionManager(ConfigurationStrings.DefaultConfigurationElementName);
115         }
116 
117         /// <summary>
118         /// Creates the default set of SecurityTokenHandlers.
119         /// </summary>
120         /// <returns>A SecurityTokenHandlerCollectionManager with a default collection of token handlers.</returns>
CreateDefaultSecurityTokenHandlerCollectionManager()121         public static SecurityTokenHandlerCollectionManager CreateDefaultSecurityTokenHandlerCollectionManager()
122         {
123             SecurityTokenHandlerCollection defaultHandlers = SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection();
124             SecurityTokenHandlerCollectionManager defaultManager = new SecurityTokenHandlerCollectionManager(ConfigurationStrings.DefaultServiceName);
125 
126             defaultManager.collections.Clear();
127             defaultManager.collections.Add(SecurityTokenHandlerCollectionManager.Usage.Default, defaultHandlers);
128 
129             return defaultManager;
130         }
131 
132         /// <summary>
133         /// Checks if a SecurityTokenHandlerCollection exists for the given usage.
134         /// </summary>
135         /// <param name="usage">A string that represents the usage of the SecurityTokenHandlerCollection.</param>
136         /// <returns>Whether or not a token handler collection exists for the given usage.</returns>
ContainsKey(string usage)137         public bool ContainsKey(string usage)
138         {
139             return this.collections.ContainsKey(usage);
140         }
141 
142         /// <summary>
143         /// Defines standard collection names used by the framework.
144         /// </summary>
145         public static class Usage
146         {
147             /// <summary>
148             /// Used to reference the default collection of handlers.
149             /// </summary>
150             public const string Default = "";
151 
152             /// <summary>
153             /// Used to reference a collection of handlers for ActAs element processing.
154             /// </summary>
155             public const string ActAs = "ActAs";
156 
157             /// <summary>
158             /// Used to reference a collection of handlers for OnBehalfOf element processing.
159             /// </summary>
160             public const string OnBehalfOf = "OnBehalfOf";
161         }
162     }
163 }
164