WrappedX509SecurityTokenAuthenticator.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IdentityModel.Policy;
  7. using System.IdentityModel.Selectors;
  8. using System.IdentityModel.Tokens;
  9. using System.Security.Claims;
  10. namespace System.ServiceModel.Security
  11. {
  12. /// <summary>
  13. /// Wraps a X509SecurityTokenHandler. Delegates the token authentication call the inner tokenAuthenticator.
  14. /// Wraps the returned ClaimsIdentities into an AuthorizationPolicy that supports IAuthorizationPolicy
  15. /// </summary>
  16. internal class WrappedX509SecurityTokenAuthenticator : X509SecurityTokenAuthenticator
  17. {
  18. X509SecurityTokenHandler _wrappedX509SecurityTokenHandler;
  19. ExceptionMapper _exceptionMapper;
  20. /// <summary>
  21. /// Initializes an instance of <see cref="WrappedX509SecurityTokenAuthenticator"/>
  22. /// </summary>
  23. /// <param name="wrappedX509SecurityTokenHandler">X509SecurityTokenHandler to wrap.</param>
  24. /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param>
  25. public WrappedX509SecurityTokenAuthenticator(
  26. X509SecurityTokenHandler wrappedX509SecurityTokenHandler,
  27. ExceptionMapper exceptionMapper )
  28. : base( X509CertificateValidator.None, GetMapToWindowsSetting( wrappedX509SecurityTokenHandler ), true )
  29. {
  30. if ( wrappedX509SecurityTokenHandler == null )
  31. {
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "wrappedX509SecurityTokenHandler" );
  33. }
  34. if ( exceptionMapper == null )
  35. {
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "exceptionMapper" );
  37. }
  38. _wrappedX509SecurityTokenHandler = wrappedX509SecurityTokenHandler;
  39. _exceptionMapper = exceptionMapper;
  40. }
  41. /// <summary>
  42. /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
  43. /// wrapping the returned ClaimsIdentities.
  44. /// </summary>
  45. /// <param name="token">Token to be validated.</param>
  46. /// <returns>Read-only collection of IAuthorizationPolicy</returns>
  47. protected override ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore( SecurityToken token )
  48. {
  49. ReadOnlyCollection<ClaimsIdentity> identities = null;
  50. try
  51. {
  52. identities = _wrappedX509SecurityTokenHandler.ValidateToken(token);
  53. }
  54. catch ( Exception ex )
  55. {
  56. if ( !_exceptionMapper.HandleSecurityTokenProcessingException( ex ) )
  57. {
  58. throw;
  59. }
  60. }
  61. // tlsnego will dispose of the x509, when we write out the bootstrap we will get a dispose error.
  62. bool shouldSaveBootstrapContext = SecurityTokenHandlerConfiguration.DefaultSaveBootstrapContext;
  63. if ( _wrappedX509SecurityTokenHandler.Configuration != null )
  64. {
  65. shouldSaveBootstrapContext = _wrappedX509SecurityTokenHandler.Configuration.SaveBootstrapContext;
  66. }
  67. if ( shouldSaveBootstrapContext )
  68. {
  69. X509SecurityToken x509Token = token as X509SecurityToken;
  70. SecurityToken tokenToCache;
  71. if ( x509Token != null )
  72. {
  73. tokenToCache = new X509SecurityToken( x509Token.Certificate );
  74. }
  75. else
  76. {
  77. tokenToCache = token;
  78. }
  79. BootstrapContext bootstrapContext = new BootstrapContext(tokenToCache, _wrappedX509SecurityTokenHandler);
  80. foreach (ClaimsIdentity identity in identities)
  81. {
  82. identity.BootstrapContext = bootstrapContext;
  83. }
  84. }
  85. List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
  86. policies.Add(new AuthorizationPolicy(identities));
  87. return policies.AsReadOnly();
  88. }
  89. static bool GetMapToWindowsSetting( X509SecurityTokenHandler securityTokenHandler )
  90. {
  91. if ( securityTokenHandler == null )
  92. {
  93. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "securityTokenHandler" );
  94. }
  95. return securityTokenHandler.MapToWindows;
  96. }
  97. }
  98. }