SecurityContextSecurityTokenResolver.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // SecurityContextSecurityTokenResolver.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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.Collections.ObjectModel;
  31. using System.IdentityModel.Selectors;
  32. using System.IdentityModel.Tokens;
  33. using System.Xml;
  34. using Table = System.Collections.Generic.Dictionary<System.Xml.UniqueId,System.ServiceModel.Security.Tokens.SecurityContextSecurityToken>;
  35. namespace System.ServiceModel.Security.Tokens
  36. {
  37. public class SecurityContextSecurityTokenResolver : SecurityTokenResolver, ISecurityContextSecurityTokenCache
  38. {
  39. int capacity;
  40. bool allow_removal;
  41. Dictionary<UniqueId,Table> cache =
  42. new Dictionary<UniqueId,Table> ();
  43. public SecurityContextSecurityTokenResolver (
  44. int securityContextCacheCapacity,
  45. bool removeOldestTokensOnCacheFull)
  46. {
  47. capacity = securityContextCacheCapacity;
  48. allow_removal = removeOldestTokensOnCacheFull;
  49. }
  50. public bool RemoveOldestTokensOnCacheFull {
  51. get { return allow_removal; }
  52. }
  53. public int SecurityContextTokenCacheCapacity {
  54. get { return capacity; }
  55. }
  56. public void AddContext (SecurityContextSecurityToken token)
  57. {
  58. if (!TryAddContext (token))
  59. throw new InvalidOperationException ("Argument token is already in the cache.");
  60. }
  61. public void ClearContexts ()
  62. {
  63. cache.Clear ();
  64. }
  65. public Collection<SecurityContextSecurityToken> GetAllContexts (UniqueId contextId)
  66. {
  67. Table table;
  68. if (!cache.TryGetValue (contextId, out table))
  69. return new Collection<SecurityContextSecurityToken> ();
  70. SecurityContextSecurityToken [] arr =
  71. new SecurityContextSecurityToken [table.Count];
  72. table.Values.CopyTo (arr, 0);
  73. return new Collection<SecurityContextSecurityToken> (arr);
  74. }
  75. public SecurityContextSecurityToken GetContext (UniqueId contextId, UniqueId generation)
  76. {
  77. Table table;
  78. if (!cache.TryGetValue (contextId, out table))
  79. return null;
  80. SecurityContextSecurityToken ret;
  81. return table.TryGetValue (generation, out ret) ? ret : null;
  82. }
  83. public void RemoveAllContexts (UniqueId contextId)
  84. {
  85. cache.Remove (contextId);
  86. }
  87. public void RemoveContext (UniqueId contextId, UniqueId generation)
  88. {
  89. Table table;
  90. if (!cache.TryGetValue (contextId, out table))
  91. return;
  92. table.Remove (generation);
  93. }
  94. public bool TryAddContext (SecurityContextSecurityToken token)
  95. {
  96. Table table;
  97. if (!cache.TryGetValue (token.ContextId, out table)) {
  98. table = new Table ();
  99. table [token.KeyGeneration] = token;
  100. } else {
  101. if (table.ContainsKey (token.KeyGeneration))
  102. return false;
  103. table [token.KeyGeneration] = token;
  104. }
  105. return true;
  106. }
  107. [MonoTODO]
  108. public void UpdateContextCachingTime (SecurityContextSecurityToken context, DateTime expirationTime)
  109. {
  110. throw new NotImplementedException ();
  111. }
  112. // SecurityTokenResolver
  113. [MonoTODO]
  114. protected override bool TryResolveSecurityKeyCore (
  115. SecurityKeyIdentifierClause keyIdentifierClause,
  116. out SecurityKey key)
  117. {
  118. throw new NotImplementedException ();
  119. }
  120. [MonoTODO]
  121. protected override bool TryResolveTokenCore (
  122. SecurityKeyIdentifier keyIdentifier,
  123. out SecurityToken token)
  124. {
  125. throw new NotImplementedException ();
  126. }
  127. [MonoTODO]
  128. protected override bool TryResolveTokenCore (
  129. SecurityKeyIdentifierClause keyIdentifierClause,
  130. out SecurityToken token)
  131. {
  132. throw new NotImplementedException ();
  133. }
  134. }
  135. }