| | | 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.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.IdentityModel.Policy; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | using CoreWCF.Security.Tokens; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF |
| | | 14 | | { |
| | | 15 | | public class ServiceAuthenticationManager |
| | | 16 | | { |
| | | 17 | | [Obsolete("Implementers should override AuthenticateAsync.")] |
| | | 18 | | public virtual ReadOnlyCollection<IAuthorizationPolicy> Authenticate(ReadOnlyCollection<IAuthorizationPolicy> au |
| | | 19 | | { |
| | 0 | 20 | | return authPolicy; |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public virtual ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)> AuthenticateAsync |
| | | 24 | | { |
| | 0 | 25 | | var policies = Authenticate(authPolicy, listenUri, ref message); |
| | 0 | 26 | | return new ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)>((policies, messag |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | internal class SCTServiceAuthenticationManagerWrapper : ServiceAuthenticationManager |
| | | 31 | | { |
| | | 32 | | private readonly ServiceAuthenticationManager _wrappedAuthenticationManager; |
| | | 33 | | |
| | | 34 | | internal SCTServiceAuthenticationManagerWrapper(ServiceAuthenticationManager wrappedServiceAuthManager) |
| | | 35 | | { |
| | | 36 | | _wrappedAuthenticationManager = wrappedServiceAuthManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowH |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public override ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)> AuthenticateAsyn |
| | | 40 | | { |
| | | 41 | | if ((message != null) && |
| | | 42 | | (message.Properties != null) && |
| | | 43 | | (message.Properties.Security != null) && |
| | | 44 | | (message.Properties.Security.TransportToken != null) && |
| | | 45 | | (message.Properties.Security.ServiceSecurityContext != null) && |
| | | 46 | | (message.Properties.Security.ServiceSecurityContext.AuthorizationPolicies != null)) |
| | | 47 | | { |
| | | 48 | | List<IAuthorizationPolicy> authPolicies = new List<IAuthorizationPolicy>(message.Properties.Security.Ser |
| | | 49 | | foreach (IAuthorizationPolicy policy in message.Properties.Security.TransportToken.SecurityTokenPolicies |
| | | 50 | | { |
| | | 51 | | authPolicies.Remove(policy); |
| | | 52 | | } |
| | | 53 | | authPolicy = authPolicies.AsReadOnly(); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | return _wrappedAuthenticationManager.AuthenticateAsync(authPolicy, listenUri, message); |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | internal class ServiceAuthenticationManagerWrapper : ServiceAuthenticationManager |
| | | 61 | | { |
| | | 62 | | private readonly ServiceAuthenticationManager _wrappedAuthenticationManager; |
| | | 63 | | private readonly string[] _filteredActionUriCollection; |
| | | 64 | | |
| | | 65 | | internal ServiceAuthenticationManagerWrapper(ServiceAuthenticationManager wrappedServiceAuthManager, string[] ac |
| | | 66 | | { |
| | | 67 | | if ((actionUriFilter != null) && (actionUriFilter.Length > 0)) |
| | | 68 | | { |
| | | 69 | | _filteredActionUriCollection = new string[actionUriFilter.Length]; |
| | | 70 | | for (int i = 0; i < actionUriFilter.Length; ++i) |
| | | 71 | | { |
| | | 72 | | _filteredActionUriCollection[i] = actionUriFilter[i]; |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | _wrappedAuthenticationManager = wrappedServiceAuthManager ?? throw DiagnosticUtility.ExceptionUtility.ThrowH |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public override ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)> AuthenticateAsyn |
| | | 80 | | { |
| | | 81 | | if (CanSkipAuthentication(message)) |
| | | 82 | | { |
| | | 83 | | return new ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)>((authPolicy, |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | if (_filteredActionUriCollection != null) |
| | | 87 | | { |
| | | 88 | | for (int i = 0; i < _filteredActionUriCollection.Length; ++i) |
| | | 89 | | { |
| | | 90 | | if ((message != null) && |
| | | 91 | | (message.Headers != null) && |
| | | 92 | | !string.IsNullOrEmpty(message.Headers.Action) && |
| | | 93 | | (message.Headers.Action == _filteredActionUriCollection[i])) |
| | | 94 | | { |
| | | 95 | | return new ValueTask<(ReadOnlyCollection<IAuthorizationPolicy> policies, Message message)>((auth |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | return _wrappedAuthenticationManager.AuthenticateAsync(authPolicy, listenUri, message); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | // |
| | | 104 | | // We skip the authentication step if the client already has an SCT and there are no Transport level tokens. |
| | | 105 | | // ServiceAuthenticationManager would have been called when the SCT was issued and there is no need to do |
| | | 106 | | // Authentication again. If TransportToken was present then we would call ServiceAutenticationManager as |
| | | 107 | | // TransportTokens are not authenticated during SCT issuance. |
| | | 108 | | // |
| | | 109 | | private bool CanSkipAuthentication(Message message) |
| | | 110 | | { |
| | | 111 | | if ((message != null) && (message.Properties != null) && (message.Properties.Security != null) && (message.P |
| | | 112 | | { |
| | | 113 | | if ((message.Properties.Security.ProtectionToken != null) && |
| | | 114 | | (message.Properties.Security.ProtectionToken.SecurityToken != null) && |
| | | 115 | | (message.Properties.Security.ProtectionToken.SecurityToken.GetType() == typeof(SecurityContextSecuri |
| | | 116 | | { |
| | | 117 | | return true; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | if (message.Properties.Security.HasIncomingSupportingTokens) |
| | | 121 | | { |
| | | 122 | | foreach (SupportingTokenSpecification tokenSpecification in message.Properties.Security.IncomingSupp |
| | | 123 | | { |
| | | 124 | | if ((tokenSpecification.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endorsing) && |
| | | 125 | | (tokenSpecification.SecurityToken.GetType() == typeof(SecurityContextSecurityToken))) |
| | | 126 | | { |
| | | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | return false; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |