WrappedSaml2SecurityTokenAuthenticator.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 Saml2SecurityTokenHandler. Delegates the token authentication call to
  14. /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into
  15. /// an IAuthorizationPolicy.
  16. /// </summary>
  17. internal class WrappedSaml2SecurityTokenAuthenticator : SecurityTokenAuthenticator
  18. {
  19. Saml2SecurityTokenHandler _wrappedSaml2SecurityTokenHandler;
  20. ExceptionMapper _exceptionMapper;
  21. /// <summary>
  22. /// Initializes an instance of <see cref="WrappedSaml2SecurityTokenAuthenticator"/>
  23. /// </summary>
  24. /// <param name="saml2SecurityTokenHandler">The Saml2SecurityTokenHandler to wrap.</param>
  25. /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param>
  26. public WrappedSaml2SecurityTokenAuthenticator(
  27. Saml2SecurityTokenHandler saml2SecurityTokenHandler,
  28. ExceptionMapper exceptionMapper )
  29. {
  30. if ( saml2SecurityTokenHandler == null )
  31. {
  32. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "wrappedSaml2SecurityTokenHandler" );
  33. }
  34. if ( exceptionMapper == null )
  35. {
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "exceptionMapper" );
  37. }
  38. _wrappedSaml2SecurityTokenHandler = saml2SecurityTokenHandler;
  39. _exceptionMapper = exceptionMapper;
  40. }
  41. /// <summary>
  42. /// Checks if the given token can be validated. Returns true if the token is of type
  43. /// Saml2SecurityToken and if the wrapped SecurityTokenHandler can validate tokens.
  44. /// </summary>
  45. /// <param name="token">The token to be checked.</param>
  46. /// <returns>True if the token is of type Saml2SecurityToken and if the wrapped
  47. /// SecurityTokenHandler can validate tokens.</returns>
  48. protected override bool CanValidateTokenCore( SecurityToken token )
  49. {
  50. return (token is Saml2SecurityToken) && _wrappedSaml2SecurityTokenHandler.CanValidateToken;
  51. }
  52. /// <summary>
  53. /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
  54. /// wrapping the returned ClaimsIdentities.
  55. /// </summary>
  56. /// <param name="token">Token to be validated.</param>
  57. /// <returns>Read-only collection of IAuthorizationPolicy</returns>
  58. protected override ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore( SecurityToken token )
  59. {
  60. IEnumerable<ClaimsIdentity> identities = null;
  61. try
  62. {
  63. identities = _wrappedSaml2SecurityTokenHandler.ValidateToken( token );
  64. }
  65. catch ( Exception ex )
  66. {
  67. if ( !_exceptionMapper.HandleSecurityTokenProcessingException( ex ) )
  68. {
  69. throw;
  70. }
  71. }
  72. List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
  73. policies.Add(new AuthorizationPolicy(identities));
  74. return policies.AsReadOnly();
  75. }
  76. }
  77. }