ProviderBackedSecurityToken.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IdentityModel.Selectors;
  7. using System.IdentityModel.Tokens;
  8. using System.Runtime;
  9. using System.Security.Cryptography;
  10. using System.Security.Authentication.ExtendedProtection;
  11. using System.ServiceModel.Diagnostics;
  12. namespace System.ServiceModel.Security.Tokens
  13. {
  14. /// <summary>
  15. /// The ProviderBackedSecurityToken was added for the ChannelBindingToken work for Win7.
  16. /// It is used to delay the resolution of a token until it is needed.
  17. /// For the CBT, this delay is necessary as the CBT is not available until SecurityAppliedMessage.OnWriteMessage is called.
  18. /// The CBT binds a token to the
  19. /// </summary>
  20. internal class ProviderBackedSecurityToken : SecurityToken
  21. {
  22. SecurityTokenProvider _tokenProvider;
  23. // Double-checked locking pattern requires volatile for read/write synchronization
  24. volatile SecurityToken _securityToken;
  25. TimeSpan _timeout;
  26. ChannelBinding _channelBinding;
  27. object _lock;
  28. /// <summary>
  29. /// Constructor to create an instance of this class.
  30. /// </summary>
  31. /// <param name="securityToken">SecurityToken that represents the SecurityTokenElement element.</param>
  32. public ProviderBackedSecurityToken( SecurityTokenProvider tokenProvider, TimeSpan timeout )
  33. {
  34. _lock = new object();
  35. if ( tokenProvider == null )
  36. {
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("tokenProvider"));
  38. }
  39. _tokenProvider = tokenProvider;
  40. _timeout = timeout;
  41. }
  42. public SecurityTokenProvider TokenProvider
  43. {
  44. get { return _tokenProvider; }
  45. }
  46. public ChannelBinding ChannelBinding
  47. {
  48. set { _channelBinding = value; }
  49. }
  50. void ResolveSecurityToken()
  51. {
  52. if ( _securityToken == null )
  53. {
  54. lock ( _lock )
  55. {
  56. if ( _securityToken == null )
  57. {
  58. ClientCredentialsSecurityTokenManager.KerberosSecurityTokenProviderWrapper kerbTokenProvider = _tokenProvider
  59. as ClientCredentialsSecurityTokenManager.KerberosSecurityTokenProviderWrapper;
  60. if (kerbTokenProvider != null)
  61. {
  62. _securityToken = kerbTokenProvider.GetToken((new TimeoutHelper(_timeout)).RemainingTime(), _channelBinding);
  63. }
  64. else
  65. {
  66. _securityToken = _tokenProvider.GetToken((new TimeoutHelper(_timeout)).RemainingTime());
  67. }
  68. }
  69. }
  70. }
  71. if ( _securityToken == null )
  72. {
  73. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityTokenException( SR.GetString( SR.SecurityTokenNotResolved, _tokenProvider.GetType().ToString() ) ) );
  74. }
  75. return;
  76. }
  77. public SecurityToken Token
  78. {
  79. get
  80. {
  81. if ( _securityToken == null )
  82. {
  83. ResolveSecurityToken();
  84. }
  85. return _securityToken;
  86. }
  87. }
  88. public override string Id
  89. {
  90. get
  91. {
  92. if ( _securityToken == null )
  93. {
  94. ResolveSecurityToken();
  95. }
  96. return _securityToken.Id;
  97. }
  98. }
  99. public override System.Collections.ObjectModel.ReadOnlyCollection<SecurityKey> SecurityKeys
  100. {
  101. get
  102. {
  103. if ( _securityToken == null )
  104. {
  105. ResolveSecurityToken();
  106. }
  107. return _securityToken.SecurityKeys;
  108. }
  109. }
  110. public override DateTime ValidFrom
  111. {
  112. get
  113. {
  114. if ( _securityToken == null )
  115. {
  116. ResolveSecurityToken();
  117. }
  118. return _securityToken.ValidFrom;
  119. }
  120. }
  121. public override DateTime ValidTo
  122. {
  123. get
  124. {
  125. if ( _securityToken == null )
  126. {
  127. ResolveSecurityToken();
  128. }
  129. return _securityToken.ValidTo;
  130. }
  131. }
  132. }
  133. }