2 // SecurityTokenRequirement.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Collections.Generic;
30 using System.IdentityModel.Tokens;
32 namespace System.IdentityModel.Selectors
34 public class SecurityTokenRequirement
36 // huh, why not const?
38 public static string KeySizeProperty {
39 get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeySize"; }
42 public static string KeyTypeProperty {
43 get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyType"; }
46 public static string KeyUsageProperty {
47 get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyUsage"; }
50 public static string RequireCryptographicTokenProperty {
51 get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/RequireCryptographicToken"; }
54 public static string TokenTypeProperty {
55 get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/TokenType"; }
60 public SecurityTokenRequirement ()
64 Dictionary<string,object> properties;
69 if (TryGetProperty<int> (KeySizeProperty, out ret))
73 set { Properties [KeySizeProperty] = value; }
76 public SecurityKeyType KeyType {
79 if (TryGetProperty<SecurityKeyType> (KeyTypeProperty, out ret))
81 return default (SecurityKeyType);
83 set { Properties [KeyTypeProperty] = value; }
86 public string TokenType {
89 if (TryGetProperty<string> (TokenTypeProperty, out ret))
91 return default (string);
93 set { Properties [TokenTypeProperty] = value; }
96 public SecurityKeyUsage KeyUsage {
99 if (TryGetProperty<SecurityKeyUsage> (KeyUsageProperty, out ret))
101 return SecurityKeyUsage.Signature;// not default!!
103 set { Properties [KeyUsageProperty] = value; }
106 public bool RequireCryptographicToken {
109 if (TryGetProperty<bool> (RequireCryptographicTokenProperty, out ret))
111 return default (bool);
113 set { Properties [RequireCryptographicTokenProperty] = value; }
116 public IDictionary<string,object> Properties {
118 if (properties == null) {
119 properties = new Dictionary<string,object> ();
120 properties [KeyTypeProperty] = SecurityKeyType.SymmetricKey;
121 properties [KeySizeProperty] = 0;
122 properties [RequireCryptographicTokenProperty] = false;
128 public TValue GetProperty<TValue> (string property)
131 if (TryGetProperty<TValue> (property, out ret))
133 throw new ArgumentException (String.Format ("Property '{0}' was not found.", property));
136 public bool TryGetProperty<TValue> (string property, out TValue value)
139 value = default (TValue);
141 if (!Properties.TryGetValue (property, out tmp))
143 if (tmp == null && !typeof (TValue).IsValueType)
144 value = default (TValue);
145 else if (tmp is TValue)
146 value = (TValue) tmp;
148 throw new ArgumentException (String.Format ("The value of property '{0}' is of type '{1}', while '{2}' is expected.", property, tmp.GetType (), typeof (TValue)));