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