| | | 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 | | internal class SecurityTokenAuthenticatorAdapter : SecurityTokenAuthenticator |
| | | 16 | | { |
| | | 17 | | private SecurityTokenHandler _securityTokenHandler; |
| | | 18 | | private ExceptionMapper _exceptionMapper; |
| | | 19 | | |
| | 0 | 20 | | public SecurityTokenAuthenticatorAdapter(SecurityTokenHandler securityTokenHandler, ExceptionMapper exceptionMap |
| | | 21 | | { |
| | 0 | 22 | | if (securityTokenHandler == null) |
| | | 23 | | { |
| | 0 | 24 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(securityTokenHandler)); |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | if (exceptionMapper == null) |
| | | 28 | | { |
| | 0 | 29 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(exceptionMapper)); |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | _securityTokenHandler = securityTokenHandler; |
| | 0 | 33 | | _exceptionMapper = exceptionMapper; |
| | 0 | 34 | | } |
| | | 35 | | |
| | | 36 | | protected override bool CanValidateTokenCore(SecurityToken token) |
| | | 37 | | { |
| | 0 | 38 | | if (token == null) |
| | | 39 | | { |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | return ((token.GetType() == _securityTokenHandler.TokenType) && (_securityTokenHandler.CanValidateToken)); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | protected override ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken toke |
| | | 47 | | { |
| | 0 | 48 | | IEnumerable<ClaimsIdentity> subjectCollection = null; |
| | | 49 | | |
| | | 50 | | try |
| | | 51 | | { |
| | 0 | 52 | | subjectCollection = _securityTokenHandler.ValidateToken(token); |
| | 0 | 53 | | } |
| | 0 | 54 | | catch (Exception ex) |
| | | 55 | | { |
| | 0 | 56 | | if (!_exceptionMapper.HandleSecurityTokenProcessingException(ex)) |
| | | 57 | | { |
| | 0 | 58 | | throw; |
| | | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | List<IAuthorizationPolicy> policies = new List<IAuthorizationPolicy>(1); |
| | 0 | 63 | | policies.Add(new AuthorizationPolicy(subjectCollection)); |
| | 0 | 64 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(policies.AsReadOnly()); |
| | | 65 | | } |
| | | 66 | | } |
| | | 67 | | } |