SecurityTokenResolver.cs 5.6 KB

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