WrappedTokenCache.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //------------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.IdentityModel;
  8. using System.IdentityModel.Selectors;
  9. using System.IdentityModel.Tokens;
  10. using System.ServiceModel.Security.Tokens;
  11. using SystemUniqueId = System.Xml.UniqueId;
  12. using SR = System.ServiceModel.SR;
  13. namespace System.ServiceModel.Security
  14. {
  15. /// <summary>
  16. /// The purpose of this class is to provide an ISecurityContextSecurityTokenCache contract over a SecurityTokenCache.
  17. /// This allows for a consistent interface for the SecurityContextSecurityTokenHandler and a SessionSecurityTokenHandler.
  18. /// The SecurityTokenCache can be passed to the SecurityContextSecurityTokenHandler and wrapped to expose an ISecurityContextSecurityTokenCache
  19. /// that can be set to the be the token cache for WCF context tokens
  20. /// </summary>
  21. class WrappedTokenCache : SecurityTokenResolver, ISecurityContextSecurityTokenCache
  22. {
  23. SessionSecurityTokenCache _tokenCache;
  24. SctClaimsHandler _claimsHandler;
  25. public WrappedTokenCache(SessionSecurityTokenCache tokenCache, SctClaimsHandler sctClaimsHandler)
  26. {
  27. if (tokenCache == null)
  28. {
  29. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenCache");
  30. }
  31. if (sctClaimsHandler == null)
  32. {
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("sctClaimsHandler");
  34. }
  35. _tokenCache = tokenCache;
  36. _claimsHandler = sctClaimsHandler;
  37. }
  38. #region ISecurityContextSecurityTokenCache Members
  39. public void AddContext(SecurityContextSecurityToken token)
  40. {
  41. //
  42. // WCF will cache the token first before calling the WrappedSessionSecurityTokenHandler.OnTokenIssued.
  43. // We need to map the claims here so we will be caching the correct token with Geneva Claims substitued
  44. // in place of the WCF claims.
  45. //
  46. _claimsHandler.SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(token);
  47. SessionSecurityTokenCacheKey key = new SessionSecurityTokenCacheKey(_claimsHandler.EndpointId, token.ContextId, token.KeyGeneration);
  48. SessionSecurityToken sessionToken = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(token, SecureConversationVersion.Default);
  49. DateTime expiryTime = DateTimeUtil.Add(sessionToken.ValidTo, _claimsHandler.SecurityTokenHandlerCollection.Configuration.MaxClockSkew);
  50. _tokenCache.AddOrUpdate(key, sessionToken, expiryTime);
  51. }
  52. public void ClearContexts()
  53. {
  54. _tokenCache.RemoveAll(_claimsHandler.EndpointId);
  55. }
  56. /// <summary>
  57. /// Called to retrieve all tokens that match a particular contextId. WCF will call this
  58. /// </summary>
  59. /// <param name="contextId"></param>
  60. /// <returns></returns>
  61. public Collection<SecurityContextSecurityToken> GetAllContexts(System.Xml.UniqueId contextId)
  62. {
  63. Collection<SecurityContextSecurityToken> tokens = new Collection<SecurityContextSecurityToken>();
  64. IEnumerable<SessionSecurityToken> cachedTokens = _tokenCache.GetAll(_claimsHandler.EndpointId, contextId);
  65. if (cachedTokens != null)
  66. {
  67. foreach (SessionSecurityToken sessionSct in cachedTokens)
  68. {
  69. if (sessionSct != null && sessionSct.IsSecurityContextSecurityTokenWrapper)
  70. {
  71. SecurityContextSecurityToken sctToken = SecurityContextSecurityTokenHelper.ConvertSessionTokenToSecurityContextSecurityToken(sessionSct);
  72. tokens.Add(sctToken);
  73. }
  74. }
  75. }
  76. return tokens;
  77. }
  78. public SecurityContextSecurityToken GetContext(System.Xml.UniqueId contextId, System.Xml.UniqueId generation)
  79. {
  80. SessionSecurityToken token = null;
  81. SessionSecurityTokenCacheKey key = new SessionSecurityTokenCacheKey(_claimsHandler.EndpointId, contextId, generation);
  82. token = _tokenCache.Get(key);
  83. SecurityContextSecurityToken sctToken = null;
  84. if (token != null && token.IsSecurityContextSecurityTokenWrapper)
  85. {
  86. sctToken = SecurityContextSecurityTokenHelper.ConvertSessionTokenToSecurityContextSecurityToken(token);
  87. }
  88. return sctToken;
  89. }
  90. /// <summary>
  91. /// Removes all the tokens that match the contextId.
  92. /// </summary>
  93. /// <param name="contextId">The context id.</param>
  94. /// <remarks>
  95. /// When WCF renews a token, its context id is the same as the issuedToken. The only
  96. /// difference is in the generationId. When WCF closes the session channel, all the tokens that
  97. /// were issued need to be removed that match the contextId.
  98. /// </remarks>
  99. public void RemoveAllContexts(System.Xml.UniqueId contextId)
  100. {
  101. _tokenCache.RemoveAll(_claimsHandler.EndpointId, contextId);
  102. }
  103. public void RemoveContext(System.Xml.UniqueId contextId, System.Xml.UniqueId generation)
  104. {
  105. SessionSecurityTokenCacheKey key = new SessionSecurityTokenCacheKey(_claimsHandler.EndpointId, contextId, generation);
  106. _tokenCache.Remove(key);
  107. }
  108. public bool TryAddContext(SecurityContextSecurityToken token)
  109. {
  110. //
  111. // WCF will cache the token first before calling the WrappedSessionSecurityTokenHandler.OnTokenIssued.
  112. // We need to map the claims here so we will be caching the correct token with Geneva Claims substitued
  113. // in place of the WCF claims.
  114. //
  115. _claimsHandler.SetPrincipalBootstrapTokensAndBindIdfxAuthPolicy(token);
  116. SessionSecurityTokenCacheKey key = new SessionSecurityTokenCacheKey(_claimsHandler.EndpointId, token.ContextId, token.KeyGeneration);
  117. SessionSecurityToken sessionToken = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(token, SecureConversationVersion.Default);
  118. DateTime expiryTime = DateTimeUtil.Add(token.ValidTo, _claimsHandler.SecurityTokenHandlerCollection.Configuration.MaxClockSkew);
  119. _tokenCache.AddOrUpdate(key, sessionToken, expiryTime);
  120. return true;
  121. }
  122. public void UpdateContextCachingTime(SecurityContextSecurityToken token, DateTime expirationTime)
  123. {
  124. if (token.ValidTo <= expirationTime.ToUniversalTime())
  125. {
  126. return;
  127. }
  128. SessionSecurityTokenCacheKey key = new SessionSecurityTokenCacheKey(_claimsHandler.EndpointId, token.ContextId, token.KeyGeneration);
  129. SessionSecurityToken sessionToken = SecurityContextSecurityTokenHelper.ConvertSctToSessionToken(token, SecureConversationVersion.Default);
  130. DateTime expiryTime = DateTimeUtil.Add(sessionToken.ValidTo, _claimsHandler.SecurityTokenHandlerCollection.Configuration.MaxClockSkew);
  131. if (_tokenCache.Get(key) == null)
  132. {
  133. throw DiagnosticUtility.ExceptionUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4285, sessionToken.ContextId.ToString()));
  134. }
  135. _tokenCache.AddOrUpdate(key, sessionToken, expiryTime);
  136. }
  137. #endregion
  138. // these are not needed as this will never be used as an SecurityTokenResolver.
  139. protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityKey key)
  140. {
  141. SecurityToken sct;
  142. if (TryResolveTokenCore(keyIdentifierClause, out sct))
  143. {
  144. key = ((SecurityContextSecurityToken)sct).SecurityKeys[0];
  145. return true;
  146. }
  147. else
  148. {
  149. key = null;
  150. return false;
  151. }
  152. }
  153. protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken token)
  154. {
  155. SecurityContextKeyIdentifierClause sctSkiClause = keyIdentifierClause as SecurityContextKeyIdentifierClause;
  156. if (sctSkiClause != null)
  157. {
  158. token = GetContext(sctSkiClause.ContextId, sctSkiClause.Generation) as SecurityToken;
  159. }
  160. else
  161. {
  162. token = null;
  163. }
  164. return (token != null);
  165. }
  166. protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token)
  167. {
  168. SecurityContextKeyIdentifierClause sctSkiClause;
  169. if (keyIdentifier.TryFind<SecurityContextKeyIdentifierClause>(out sctSkiClause))
  170. {
  171. return TryResolveTokenCore(sctSkiClause, out token);
  172. }
  173. else
  174. {
  175. token = null;
  176. return false;
  177. }
  178. }
  179. }
  180. }