WindowsUserNameCachingSecurityTokenAuthenticator.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Security.Tokens
  5. {
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.IdentityModel.Policy;
  10. using System.IdentityModel.Selectors;
  11. using System.IdentityModel.Tokens;
  12. using System.Security.Cryptography;
  13. using System.Text;
  14. public interface ILogonTokenCacheManager
  15. {
  16. bool RemoveCachedLogonToken(string username);
  17. void FlushLogonTokenCache();
  18. }
  19. class LogonTokenCache : TimeBoundedCache
  20. {
  21. const int lowWaterMarkFactor = 75;
  22. const int saltSize = 4;
  23. TimeSpan cachedLogonTokenLifetime;
  24. RNGCryptoServiceProvider random;
  25. public LogonTokenCache(int maxCachedLogonTokens, TimeSpan cachedLogonTokenLifetime)
  26. : base((maxCachedLogonTokens * lowWaterMarkFactor) / 100, maxCachedLogonTokens, StringComparer.OrdinalIgnoreCase, PurgingMode.TimerBasedPurge, TimeSpan.FromTicks(cachedLogonTokenLifetime.Ticks >> 2), true)
  27. {
  28. this.cachedLogonTokenLifetime = cachedLogonTokenLifetime;
  29. this.random = new RNGCryptoServiceProvider();
  30. }
  31. public bool TryGetTokenCache(string userName, out LogonToken token)
  32. {
  33. token = (LogonToken)GetItem(userName);
  34. return token != null;
  35. }
  36. public bool TryAddTokenCache(string userName, string password, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies)
  37. {
  38. byte[] salt = new byte[saltSize];
  39. this.random.GetBytes(salt);
  40. LogonToken token = new LogonToken(userName, password, salt, authorizationPolicies);
  41. DateTime expirationTime = DateTime.UtcNow.Add(this.cachedLogonTokenLifetime);
  42. return TryAddItem(userName, token, expirationTime, true);
  43. }
  44. // Remove those about to expire
  45. protected override ArrayList OnQuotaReached(Hashtable cacheTable)
  46. {
  47. List<IExpirableItem> items = new List<IExpirableItem>(cacheTable.Count);
  48. foreach (IExpirableItem value in cacheTable.Values)
  49. {
  50. items.Add(value);
  51. }
  52. // Those expired soon in front
  53. items.Sort(ExpirableItemComparer.Default);
  54. int pruningAmount = (items.Count * (100 - lowWaterMarkFactor)) / 100;
  55. // edge case
  56. pruningAmount = pruningAmount <= 0 ? items.Count : pruningAmount;
  57. ArrayList keys = new ArrayList(pruningAmount);
  58. for (int i = 0; i < pruningAmount; ++i)
  59. {
  60. LogonToken token = (LogonToken)ExtractItem(items[i]);
  61. keys.Add(token.UserName);
  62. OnRemove(token);
  63. }
  64. return keys;
  65. }
  66. public bool TryRemoveTokenCache(string userName)
  67. {
  68. return this.TryRemoveItem(userName);
  69. }
  70. public void Flush()
  71. {
  72. this.ClearItems();
  73. }
  74. protected override void OnRemove(object item)
  75. {
  76. ((LogonToken)item).Dispose();
  77. base.OnRemove(item);
  78. }
  79. }
  80. class LogonToken : IDisposable
  81. {
  82. string userName;
  83. byte[] passwordHash;
  84. byte[] salt;
  85. ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies;
  86. public LogonToken(string userName, string password, byte[] salt, ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies)
  87. {
  88. this.userName = userName;
  89. this.passwordHash = ComputeHash(password, salt);
  90. this.salt = salt;
  91. this.authorizationPolicies = System.IdentityModel.SecurityUtils.CloneAuthorizationPoliciesIfNecessary(authorizationPolicies);
  92. }
  93. public bool PasswordEquals(string password)
  94. {
  95. byte[] passwordHash = ComputeHash(password, this.salt);
  96. return CryptoHelper.IsEqual(this.passwordHash, passwordHash);
  97. }
  98. public string UserName
  99. {
  100. get { return this.userName; }
  101. }
  102. public ReadOnlyCollection<IAuthorizationPolicy> GetAuthorizationPolicies()
  103. {
  104. return System.IdentityModel.SecurityUtils.CloneAuthorizationPoliciesIfNecessary(this.authorizationPolicies);
  105. }
  106. public void Dispose()
  107. {
  108. System.IdentityModel.SecurityUtils.DisposeAuthorizationPoliciesIfNecessary(this.authorizationPolicies);
  109. }
  110. static byte[] ComputeHash(string password, byte[] salt)
  111. {
  112. if (String.IsNullOrEmpty(password))
  113. {
  114. return salt;
  115. }
  116. byte[] bytes = Encoding.Unicode.GetBytes(password);
  117. int saltSize = salt.Length;
  118. for (int i = 0; i < bytes.Length; ++i)
  119. {
  120. bytes[i] ^= salt[i % saltSize];
  121. }
  122. using (HashAlgorithm hashAlgorithm = CryptoHelper.NewSha1HashAlgorithm())
  123. {
  124. return hashAlgorithm.ComputeHash(bytes);
  125. }
  126. }
  127. }
  128. class WindowsUserNameCachingSecurityTokenAuthenticator : WindowsUserNameSecurityTokenAuthenticator, ILogonTokenCacheManager, IDisposable
  129. {
  130. LogonTokenCache logonTokenCache;
  131. public WindowsUserNameCachingSecurityTokenAuthenticator(bool includeWindowsGroups, int maxCachedLogonTokens, TimeSpan cachedLogonTokenLifetime)
  132. : base(includeWindowsGroups)
  133. {
  134. this.logonTokenCache = new LogonTokenCache(maxCachedLogonTokens, cachedLogonTokenLifetime);
  135. }
  136. public void Dispose()
  137. {
  138. FlushLogonTokenCache();
  139. }
  140. protected override ReadOnlyCollection<IAuthorizationPolicy> ValidateUserNamePasswordCore(string userName, string password)
  141. {
  142. LogonToken token;
  143. if (this.logonTokenCache.TryGetTokenCache(userName, out token))
  144. {
  145. if (token.PasswordEquals(password))
  146. {
  147. return token.GetAuthorizationPolicies();
  148. }
  149. else
  150. {
  151. // this prevents logon with old password.
  152. this.logonTokenCache.TryRemoveTokenCache(userName);
  153. }
  154. }
  155. ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies = base.ValidateUserNamePasswordCore(userName, password);
  156. this.logonTokenCache.TryAddTokenCache(userName, password, authorizationPolicies);
  157. return authorizationPolicies;
  158. }
  159. public bool RemoveCachedLogonToken(string username)
  160. {
  161. if (this.logonTokenCache == null)
  162. return false;
  163. return this.logonTokenCache.TryRemoveTokenCache(username);
  164. }
  165. public void FlushLogonTokenCache()
  166. {
  167. if (this.logonTokenCache != null)
  168. this.logonTokenCache.Flush();
  169. }
  170. }
  171. }