| | | 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; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using CoreWCF.IdentityModel.Policy; |
| | | 8 | | using CoreWCF.IdentityModel.Tokens; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 11 | | { |
| | | 12 | | public abstract class SecurityTokenAuthenticator |
| | | 13 | | { |
| | 174 | 14 | | protected SecurityTokenAuthenticator() { } |
| | | 15 | | |
| | | 16 | | public bool CanValidateToken(SecurityToken token) |
| | | 17 | | { |
| | 367 | 18 | | if (token == null) |
| | | 19 | | { |
| | 0 | 20 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 21 | | } |
| | 367 | 22 | | return CanValidateTokenCore(token); |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | public ReadOnlyCollection<IAuthorizationPolicy> ValidateToken(SecurityToken token) |
| | | 26 | | { |
| | | 27 | | // We don't call this internally, so we won't have problems with sync over async causing performance issues |
| | 0 | 28 | | var validationResult = ValidateTokenAsync(token); |
| | 0 | 29 | | return validationResult.IsCompleted |
| | 0 | 30 | | ? validationResult.Result |
| | 0 | 31 | | : validationResult.AsTask().GetAwaiter().GetResult(); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public async ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenAsync(SecurityToken token) |
| | | 35 | | { |
| | 138 | 36 | | if (token == null) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 39 | | } |
| | 138 | 40 | | if (!CanValidateToken(token)) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenValidationException(SR.Format |
| | | 43 | | } |
| | | 44 | | |
| | 138 | 45 | | ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies = await ValidateTokenCoreAsync(token); |
| | 95 | 46 | | if (authorizationPolicies == null) |
| | | 47 | | { |
| | 0 | 48 | | string errorMsg = SR.Format(SR.CannotValidateSecurityTokenType, this, token.GetType()); |
| | 0 | 49 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenValidationException(errorMsg) |
| | | 50 | | } |
| | | 51 | | |
| | 95 | 52 | | return authorizationPolicies; |
| | 95 | 53 | | } |
| | | 54 | | |
| | | 55 | | protected abstract bool CanValidateTokenCore(SecurityToken token); |
| | | 56 | | |
| | | 57 | | [Obsolete("Implementers should override ValidateTokenCoreAsync.")] |
| | 0 | 58 | | protected virtual ReadOnlyCollection<IAuthorizationPolicy> ValidateTokenCore(SecurityToken token) => throw new N |
| | | 59 | | |
| | | 60 | | protected virtual ValueTask<ReadOnlyCollection<IAuthorizationPolicy>> ValidateTokenCoreAsync(SecurityToken token |
| | | 61 | | { |
| | | 62 | | // Default to calling sync implementation to support existing derived types which haven't overridden this me |
| | | 63 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 0 | 64 | | return new ValueTask<ReadOnlyCollection<IAuthorizationPolicy>>(ValidateTokenCore(token)); |
| | | 65 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |