| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using CoreWCF.IdentityModel.Policy; |
| | | 7 | | using CoreWCF.IdentityModel.Selectors; |
| | | 8 | | using CoreWCF.IdentityModel.Tokens; |
| | | 9 | | using System.Security.Claims; |
| | | 10 | | using System; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF.Security |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Wraps a Samll1SecurityTokenHandler. Delegates the token authentication call to |
| | | 17 | | /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into |
| | | 18 | | /// an IAuthorizationPolicy. |
| | | 19 | | /// </summary> |
| | | 20 | | internal class WrappedSaml11SecurityTokenAuthenticator : SamlSecurityTokenAuthenticator |
| | | 21 | | { |
| | | 22 | | private readonly SamlSecurityTokenHandler _wrappedSaml11SecurityTokenHandler; |
| | | 23 | | private readonly ExceptionMapper _exceptionMapper; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes an instance of <see cref="WrappedSaml11SecurityTokenAuthenticator"/> |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="saml11SecurityTokenHandler">The Saml11SecurityTokenHandler to wrap.</param> |
| | | 29 | | /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param> |
| | | 30 | | public WrappedSaml11SecurityTokenAuthenticator( |
| | | 31 | | SamlSecurityTokenHandler saml11SecurityTokenHandler, |
| | | 32 | | ExceptionMapper exceptionMapper) |
| | 2 | 33 | | : base(new List<SecurityTokenAuthenticator>()) |
| | | 34 | | { |
| | 2 | 35 | | _wrappedSaml11SecurityTokenHandler = saml11SecurityTokenHandler ?? throw DiagnosticUtility.ExceptionUtility. |
| | 2 | 36 | | _exceptionMapper = exceptionMapper ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Validates the token using the wrapped token handler and generates IAuthorizationPolicy |
| | | 41 | | /// wrapping the returned ClaimsIdentities. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="token">Token to be validated.</param> |
| | | 44 | | /// <returns>Read-only collection of IAuthorizationPolicy</returns> |
| | | 45 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke |
| | | 46 | | { |
| | 47 | 47 | | IEnumerable<ClaimsIdentity> identities = null; |
| | | 48 | | try |
| | | 49 | | { |
| | 47 | 50 | | identities = _wrappedSaml11SecurityTokenHandler.ValidateToken(token); |
| | 28 | 51 | | } |
| | 19 | 52 | | catch (Exception ex) |
| | | 53 | | { |
| | 19 | 54 | | if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex)) |
| | | 55 | | { |
| | 19 | 56 | | throw; |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | 28 | 60 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1); |
| | 28 | 61 | | policies.Add(new AuthorizationPolicy(identities)); |
| | 28 | 62 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly()); |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |