| | | 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 UserNameSecurityTokenHandler. Delegates the token authentication call to |
| | | 17 | | /// this wrapped tokenAuthenticator. Wraps the returned ClaimsIdentities into |
| | | 18 | | /// an IAuthorizationPolicy. |
| | | 19 | | /// </summary> |
| | | 20 | | internal class WrappedUserNameSecurityTokenAuthenticator : UserNameSecurityTokenAuthenticator |
| | | 21 | | { |
| | | 22 | | private readonly UserNameSecurityTokenHandler _wrappedUserNameSecurityTokenHandler; |
| | | 23 | | private readonly ExceptionMapper _exceptionMapper; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Initializes an instance of <see cref="WrappedUserNameSecurityTokenAuthenticator"/> |
| | | 27 | | /// </summary> |
| | | 28 | | /// <param name="wrappedUserNameSecurityTokenHandler">The UserNameSecurityTokenHandler to wrap.</param> |
| | | 29 | | /// <param name="exceptionMapper">Converts token validation exceptions to SOAP faults.</param> |
| | | 30 | | public WrappedUserNameSecurityTokenAuthenticator( |
| | | 31 | | UserNameSecurityTokenHandler wrappedUserNameSecurityTokenHandler, |
| | | 32 | | ExceptionMapper exceptionMapper) |
| | 0 | 33 | | : base() |
| | | 34 | | { |
| | 0 | 35 | | _wrappedUserNameSecurityTokenHandler = wrappedUserNameSecurityTokenHandler ?? throw DiagnosticUtility.Except |
| | 0 | 36 | | _exceptionMapper = exceptionMapper ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameo |
| | 0 | 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 | | { |
| | 0 | 47 | | ReadOnlyCollection<ClaimsIdentity> identities = null; |
| | | 48 | | try |
| | | 49 | | { |
| | 0 | 50 | | identities = _wrappedUserNameSecurityTokenHandler.ValidateToken(token); |
| | 0 | 51 | | } |
| | 0 | 52 | | catch (Exception ex) |
| | | 53 | | { |
| | 0 | 54 | | if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex)) |
| | | 55 | | { |
| | 0 | 56 | | throw; |
| | | 57 | | } |
| | 0 | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1); |
| | 0 | 61 | | policies.Add(new AuthorizationPolicy(identities)); |
| | 0 | 62 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly()); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateUserNamePasswordCoreAsync(string |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.Format(SR.ID4008, " |
| | | 68 | | } |
| | | 69 | | } |
| | | 70 | | } |