| | | 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.Runtime.CompilerServices; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.IdentityModel.Policy; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Dispatcher |
| | | 13 | | { |
| | | 14 | | internal sealed class AuthenticationBehavior |
| | | 15 | | { |
| | | 16 | | private readonly ServiceAuthenticationManager _serviceAuthenticationManager; |
| | | 17 | | |
| | 0 | 18 | | private AuthenticationBehavior(ServiceAuthenticationManager authenticationManager) |
| | | 19 | | { |
| | 0 | 20 | | _serviceAuthenticationManager = authenticationManager; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async ValueTask<MessageRpc> AuthenticateAsync(MessageRpc rpc) |
| | | 24 | | { |
| | 0 | 25 | | SecurityMessageProperty security = SecurityMessageProperty.GetOrCreate(rpc.Request); |
| | 0 | 26 | | ReadOnlyCollection<IAuthorizationPolicy> authPolicy = security.ServiceSecurityContext.AuthorizationPolicies; |
| | | 27 | | try |
| | | 28 | | { |
| | 0 | 29 | | (authPolicy, rpc.Request) = await _serviceAuthenticationManager.AuthenticateAsync(security.ServiceSecuri |
| | 0 | 30 | | if (authPolicy == null) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Authentic |
| | | 33 | | } |
| | 0 | 34 | | } |
| | | 35 | | catch (Exception ex) |
| | | 36 | | { |
| | 0 | 37 | | if (Fx.IsFatal(ex)) |
| | | 38 | | { |
| | 0 | 39 | | throw; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | // TODO: PerformanceCounters |
| | | 43 | | // TODO: Decide if we want Auditing and add back |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateFailedAuthenticationFaultException()); |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | rpc.Request.Properties.Security.ServiceSecurityContext.AuthorizationPolicies = authPolicy; |
| | | 48 | | |
| | 0 | 49 | | return rpc; |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 53 | | private static AuthenticationBehavior CreateAuthenticationBehavior(DispatchRuntime dispatch) |
| | | 54 | | { |
| | 0 | 55 | | AuthenticationBehavior authenticationBehavior = new AuthenticationBehavior(dispatch.ServiceAuthenticationMan |
| | 0 | 56 | | return authenticationBehavior; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | public static AuthenticationBehavior TryCreate(DispatchRuntime dispatch) |
| | | 60 | | { |
| | 664 | 61 | | if (dispatch == null) |
| | | 62 | | { |
| | 0 | 63 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(dispatch)); |
| | | 64 | | } |
| | | 65 | | |
| | 664 | 66 | | if (!dispatch.RequiresAuthentication) |
| | | 67 | | { |
| | 664 | 68 | | return null; |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | return CreateAuthenticationBehavior(dispatch); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | internal static Exception CreateFailedAuthenticationFaultException() |
| | | 75 | | { |
| | 0 | 76 | | SecurityVersion wss = SecurityVersion.Default; |
| | 0 | 77 | | FaultCode faultCode = FaultCode.CreateSenderFaultCode(wss.InvalidSecurityFaultCode.Value, wss.HeaderNamespac |
| | 0 | 78 | | FaultReason faultReason = new FaultReason(SR.AuthenticationOfClientFailed); |
| | 0 | 79 | | return new FaultException(faultReason, faultCode); |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | } |