2 // SecurityTokenResolver.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-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.ObjectModel;
30 using System.IdentityModel.Tokens;
32 namespace System.IdentityModel.Selectors
34 public abstract class SecurityTokenResolver
36 protected SecurityTokenResolver ()
40 public static SecurityTokenResolver CreateDefaultSecurityTokenResolver (
41 ReadOnlyCollection<SecurityToken> tokens,
44 return new DefaultSecurityTokenResolver (tokens, canMatchLocalId);
47 public SecurityKey ResolveSecurityKey (
48 SecurityKeyIdentifierClause keyIdentifierClause)
50 if (keyIdentifierClause == null)
51 throw new ArgumentNullException ("keyIdentifierClause");
53 if (!TryResolveSecurityKey (keyIdentifierClause, out ret))
54 throw new InvalidOperationException (String.Format ("Could not resolve security key with the key identifier clause '{0}'", keyIdentifierClause));
58 public SecurityToken ResolveToken (
59 SecurityKeyIdentifier keyIdentifier)
61 if (keyIdentifier == null)
62 throw new ArgumentNullException ("keyIdentifierClause");
64 if (!TryResolveToken (keyIdentifier, out ret))
65 throw new InvalidOperationException (String.Format ("Could not resolve security token from the key identifier '{0}'", keyIdentifier));
69 public SecurityToken ResolveToken (
70 SecurityKeyIdentifierClause keyIdentifierClause)
72 if (keyIdentifierClause == null)
73 throw new ArgumentNullException ("keyIdentifierClause");
75 if (!TryResolveToken (keyIdentifierClause, out ret))
76 throw new InvalidOperationException (String.Format ("Could not resolve security token from the key identifier clause '{0}'", keyIdentifierClause));
80 public bool TryResolveSecurityKey (
81 SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
83 return TryResolveSecurityKeyCore (keyIdentifierClause, out key);
86 public bool TryResolveToken (
87 SecurityKeyIdentifier keyIdentifier,
88 out SecurityToken token)
90 return TryResolveTokenCore (keyIdentifier, out token);
93 public bool TryResolveToken (
94 SecurityKeyIdentifierClause keyIdentifierClause,
95 out SecurityToken token)
97 return TryResolveTokenCore (keyIdentifierClause, out token);
100 protected abstract bool TryResolveSecurityKeyCore (
101 SecurityKeyIdentifierClause keyIdentifierClause,
102 out SecurityKey key);
104 protected abstract bool TryResolveTokenCore (
105 SecurityKeyIdentifier keyIdentifier,
106 out SecurityToken token);
108 protected abstract bool TryResolveTokenCore (
109 SecurityKeyIdentifierClause keyIdentifierClause,
110 out SecurityToken token);
113 class DefaultSecurityTokenResolver : SecurityTokenResolver
115 ReadOnlyCollection<SecurityToken> tokens;
118 public DefaultSecurityTokenResolver (
119 ReadOnlyCollection<SecurityToken> tokens,
120 bool canMatchLocalId)
122 this.tokens = tokens;
123 this.match_local = canMatchLocalId;
126 protected override bool TryResolveSecurityKeyCore (
127 SecurityKeyIdentifierClause clause,
131 throw new ArgumentNullException ("clause");
132 foreach (SecurityToken token in tokens)
133 if (TokenMatchesClause (token, clause)) {
134 key = token.ResolveKeyIdentifierClause (clause);
142 protected override bool TryResolveTokenCore (
143 SecurityKeyIdentifier keyIdentifier,
144 out SecurityToken token)
146 if (keyIdentifier == null)
147 throw new ArgumentNullException ("keyIdentifier");
148 foreach (SecurityKeyIdentifierClause kic in keyIdentifier)
149 if (TryResolveTokenCore (kic, out token))
155 protected override bool TryResolveTokenCore (
156 SecurityKeyIdentifierClause clause,
157 out SecurityToken token)
160 throw new ArgumentNullException ("clause");
161 foreach (SecurityToken t in tokens)
162 if (TokenMatchesClause (t, clause)) {
170 bool TokenMatchesClause (SecurityToken token, SecurityKeyIdentifierClause clause)
172 if (token.MatchesKeyIdentifierClause (clause))
176 LocalIdKeyIdentifierClause l =
177 clause as LocalIdKeyIdentifierClause;
178 return l != null && l.Matches (token.Id, token.GetType ());