WrappedRsaSecurityTokenAuthenticator.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 RsaSecurityTokenHandler. Delegates the token authentication call to
  14. /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into
  15. /// an IAuthorizationPolicy.
  16. /// </summary>
  17. internal class WrappedRsaSecurityTokenAuthenticator : RsaSecurityTokenAuthenticator
  18. {
  19. RsaSecurityTokenHandler _wrappedRsaSecurityTokenHandler;
  20. ExceptionMapper _exceptionMapper;
  21. /// <summary>
  22. /// Initializes an instance of <see cref="WrappedRsaSecurityTokenAuthenticator"/>
  23. /// </summary>
  24. /// <param name="wrappedRsaSecurityTokenHandler">The RsaSecurityTokenHandler to wrap.</param>
  25. /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param>
  26. public WrappedRsaSecurityTokenAuthenticator(
  27. RsaSecurityTokenHandler wrappedRsaSecurityTokenHandler,
  28. ExceptionMapper exceptionMapper )
  29. : base()
  30. {
  31. if ( wrappedRsaSecurityTokenHandler == null )
  32. {
  33. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "wrappedRsaSecurityTokenHandler" );
  34. }
  35. if ( exceptionMapper == null )
  36. {
  37. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "exceptionMapper" );
  38. }
  39. _wrappedRsaSecurityTokenHandler = wrappedRsaSecurityTokenHandler;
  40. _exceptionMapper = exceptionMapper;
  41. }
  42. /// <summary>
  43. /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy
  44. /// wrapping the returned ClaimsIdentities.
  45. /// </summary>
  46. /// <param name="token">Token to be validated.</param>
  47. /// <returns>Read-only collection of IAuthorizationPolicy</returns>
  48. protected override ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore( SecurityToken token )
  49. {
  50. IEnumerable<ClaimsIdentity> identities = null;
  51. try
  52. {
  53. identities = _wrappedRsaSecurityTokenHandler.ValidateToken( token );
  54. }
  55. catch ( Exception ex )
  56. {
  57. if ( !_exceptionMapper.HandleSecurityTokenProcessingException( ex ) )
  58. {
  59. throw;
  60. }
  61. }
  62. List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1);
  63. policies.Add(new AuthorizationPolicy(identities));
  64. return policies.AsReadOnly();
  65. }
  66. }
  67. }