[msvc] Update csproj files
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Selectors / SecurityTokenResolver.cs
1 //
2 // SecurityTokenResolver.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-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.ObjectModel;
30 using System.IdentityModel.Tokens;
31
32 namespace System.IdentityModel.Selectors
33 {
34         public abstract class SecurityTokenResolver
35         {
36                 protected SecurityTokenResolver ()
37                 {
38                 }
39
40                 public static SecurityTokenResolver  CreateDefaultSecurityTokenResolver (
41                         ReadOnlyCollection<SecurityToken> tokens,
42                         bool canMatchLocalId)
43                 {
44                         return new DefaultSecurityTokenResolver (tokens, canMatchLocalId);
45                 }
46
47                 public SecurityKey ResolveSecurityKey (
48                         SecurityKeyIdentifierClause keyIdentifierClause)
49                 {
50                         if (keyIdentifierClause == null)
51                                 throw new ArgumentNullException ("keyIdentifierClause");
52                         SecurityKey ret;
53                         if (!TryResolveSecurityKey (keyIdentifierClause, out ret))
54                                 throw new InvalidOperationException (String.Format ("Could not resolve security key with the key identifier clause '{0}'", keyIdentifierClause));
55                         return ret;
56                 }
57
58                 public SecurityToken ResolveToken (
59                         SecurityKeyIdentifier keyIdentifier)
60                 {
61                         if (keyIdentifier == null)
62                                 throw new ArgumentNullException ("keyIdentifierClause");
63                         SecurityToken ret;
64                         if (!TryResolveToken (keyIdentifier, out ret))
65                                 throw new InvalidOperationException (String.Format ("Could not resolve security token from the key identifier '{0}'", keyIdentifier));
66                         return ret;
67                 }
68
69                 public SecurityToken ResolveToken (
70                         SecurityKeyIdentifierClause keyIdentifierClause)
71                 {
72                         if (keyIdentifierClause == null)
73                                 throw new ArgumentNullException ("keyIdentifierClause");
74                         SecurityToken ret;
75                         if (!TryResolveToken (keyIdentifierClause, out ret))
76                                 throw new InvalidOperationException (String.Format ("Could not resolve security token from the key identifier clause '{0}'", keyIdentifierClause));
77                         return ret;
78                 }
79
80                 public bool TryResolveSecurityKey (
81                         SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
82                 {
83                         return TryResolveSecurityKeyCore (keyIdentifierClause, out key);
84                 }
85
86                 public bool TryResolveToken (
87                         SecurityKeyIdentifier keyIdentifier,
88                         out SecurityToken token)
89                 {
90                         return TryResolveTokenCore (keyIdentifier, out token);
91                 }
92
93                 public bool TryResolveToken (
94                         SecurityKeyIdentifierClause keyIdentifierClause,
95                         out SecurityToken token)
96                 {
97                         return TryResolveTokenCore (keyIdentifierClause, out token);
98                 }
99
100                 protected abstract bool TryResolveSecurityKeyCore (
101                         SecurityKeyIdentifierClause keyIdentifierClause,
102                         out SecurityKey key);
103
104                 protected abstract bool TryResolveTokenCore (
105                         SecurityKeyIdentifier keyIdentifier,
106                         out SecurityToken token);
107
108                 protected abstract bool TryResolveTokenCore (
109                         SecurityKeyIdentifierClause keyIdentifierClause,
110                         out SecurityToken token);
111
112
113                 class DefaultSecurityTokenResolver : SecurityTokenResolver
114                 {
115                         ReadOnlyCollection<SecurityToken> tokens;
116                         bool match_local;
117
118                         public DefaultSecurityTokenResolver (
119                                 ReadOnlyCollection<SecurityToken> tokens,
120                                 bool canMatchLocalId)
121                         {
122                                 this.tokens = tokens;
123                                 this.match_local = canMatchLocalId;
124                         }
125
126                         protected override bool TryResolveSecurityKeyCore (
127                                 SecurityKeyIdentifierClause clause,
128                                 out SecurityKey key)
129                         {
130                                 if (clause == null)
131                                         throw new ArgumentNullException ("clause");
132                                 foreach (SecurityToken token in tokens)
133                                         if (TokenMatchesClause (token, clause)) {
134                                                 key = token.ResolveKeyIdentifierClause (clause);
135                                                 if (key != null)
136                                                         return true;
137                                         }
138                                 key = null;
139                                 return false;
140                         }
141
142                         protected override bool TryResolveTokenCore (
143                                 SecurityKeyIdentifier keyIdentifier,
144                                 out SecurityToken token)
145                         {
146                                 if (keyIdentifier == null)
147                                         throw new ArgumentNullException ("keyIdentifier");
148                                 foreach (SecurityKeyIdentifierClause kic in keyIdentifier)
149                                         if (TryResolveTokenCore (kic, out token))
150                                                 return true;
151                                 token = null;
152                                 return false;
153                         }
154
155                         protected override bool TryResolveTokenCore (
156                                 SecurityKeyIdentifierClause clause,
157                                 out SecurityToken token)
158                         {
159                                 if (clause == null)
160                                         throw new ArgumentNullException ("clause");
161                                 foreach (SecurityToken t in tokens)
162                                         if (TokenMatchesClause (t, clause)) {
163                                                 token = t;
164                                                 return true;
165                                         }
166                                 token = null;
167                                 return false;
168                         }
169
170                         bool TokenMatchesClause (SecurityToken token, SecurityKeyIdentifierClause clause)
171                         {
172                                 if (token.MatchesKeyIdentifierClause (clause))
173                                         return true;
174                                 if (!match_local)
175                                         return false;
176                                 LocalIdKeyIdentifierClause l =
177                                         clause as LocalIdKeyIdentifierClause;
178                                 return l != null && l.Matches (token.Id, token.GetType ());
179                         }
180                 }
181         }
182 }