Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Selectors / SecurityTokenRequirement.cs
1 //
2 // SecurityTokenRequirement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
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:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
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.
27 //
28 using System;
29 using System.Collections.Generic;
30 using System.IdentityModel.Tokens;
31
32 namespace System.IdentityModel.Selectors
33 {
34         public class SecurityTokenRequirement
35         {
36                 // huh, why not const?
37
38                 public static string KeySizeProperty {
39                         get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeySize"; }
40                 }
41
42                 public static string KeyTypeProperty {
43                         get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyType"; }
44                 }
45
46                 public static string KeyUsageProperty {
47                         get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/KeyUsage"; }
48                 }
49
50                 public static string RequireCryptographicTokenProperty {
51                         get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/RequireCryptographicToken"; }
52                 }
53
54                 public static string TokenTypeProperty {
55                         get { return "http://schemas.microsoft.com/ws/2006/05/identitymodel/securitytokenrequirement/TokenType"; }
56                 }
57
58                 // Instance members
59
60                 public SecurityTokenRequirement ()
61                 {
62                 }
63
64                 Dictionary<string,object> properties;
65
66                 public int KeySize {
67                         get {
68                                 int ret;
69                                 if (TryGetProperty<int> (KeySizeProperty, out ret))
70                                         return ret;
71                                 return default (int);
72                         }
73                         set { Properties [KeySizeProperty] = value; }
74                 }
75
76                 public SecurityKeyType KeyType {
77                         get {
78                                 SecurityKeyType ret;
79                                 if (TryGetProperty<SecurityKeyType> (KeyTypeProperty, out ret))
80                                         return ret;
81                                 return default (SecurityKeyType);
82                         }
83                         set { Properties [KeyTypeProperty] = value; }
84                 }
85
86                 public string TokenType {
87                         get {
88                                 string ret;
89                                 if (TryGetProperty<string> (TokenTypeProperty, out ret))
90                                         return ret;
91                                 return default (string);
92                         }
93                         set { Properties [TokenTypeProperty] = value; }
94                 }
95
96                 public SecurityKeyUsage KeyUsage {
97                         get {
98                                 SecurityKeyUsage ret;
99                                 if (TryGetProperty<SecurityKeyUsage> (KeyUsageProperty, out ret))
100                                         return ret;
101                                 return SecurityKeyUsage.Signature;// not default!!
102                         }
103                         set { Properties [KeyUsageProperty] = value; }
104                 }
105
106                 public bool RequireCryptographicToken {
107                         get {
108                                 bool ret;
109                                 if (TryGetProperty<bool> (RequireCryptographicTokenProperty, out ret))
110                                         return ret;
111                                 return default (bool);
112                         }
113                         set { Properties [RequireCryptographicTokenProperty] = value; }
114                 }
115
116                 public IDictionary<string,object> Properties {
117                         get {
118                                 if (properties == null) {
119                                         properties = new Dictionary<string,object> ();
120                                         properties [KeyTypeProperty] = SecurityKeyType.SymmetricKey;
121                                         properties [KeySizeProperty] = 0;
122                                         properties [RequireCryptographicTokenProperty] = false;
123                                 }
124                                 return properties;
125                         }
126                 }
127
128                 public TValue GetProperty<TValue> (string property)
129                 {
130                         TValue ret;
131                         if (TryGetProperty<TValue> (property, out ret))
132                                 return ret;
133                         throw new ArgumentException (String.Format ("Property '{0}' was not found.", property));
134                 }
135
136                 public bool TryGetProperty<TValue> (string property, out TValue value)
137                 {
138                         object tmp;
139                         value = default (TValue);
140
141                         if (!Properties.TryGetValue (property, out tmp))
142                                 return false;
143                         if (tmp == null && !typeof (TValue).IsValueType)
144                                 value = default (TValue);
145                         else if (tmp is TValue)
146                                 value = (TValue) tmp;
147                         else
148                                 throw new ArgumentException (String.Format ("The value of property '{0}' is of type '{1}', while '{2}' is expected.", property, tmp.GetType (), typeof (TValue)));
149                         return true;
150                 }
151         }
152 }