| | | 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 CoreWCF.Channels; |
| | | 8 | | using CoreWCF.Dispatcher; |
| | | 9 | | using CoreWCF.IdentityModel; |
| | | 10 | | using CoreWCF.IdentityModel.Configuration; |
| | | 11 | | using CoreWCF.IdentityModel.Selectors; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using Microsoft.Extensions.DependencyInjection; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Description |
| | | 16 | | { |
| | | 17 | | public class ServiceCredentials : SecurityCredentialsManager, IServiceBehavior |
| | | 18 | | { |
| | | 19 | | private bool _isReadOnly = false; |
| | 118 | 20 | | private readonly bool _saveBootstrapTokenInSession = true; |
| | | 21 | | private ExceptionMapper _exceptionMapper; |
| | | 22 | | private IServiceProvider _provider; |
| | | 23 | | private IdentityConfiguration _identityConfiguration; |
| | | 24 | | |
| | 52 | 25 | | public ServiceCredentials() |
| | | 26 | | { |
| | 52 | 27 | | UserNameAuthentication = new UserNamePasswordServiceCredential(); |
| | 52 | 28 | | ClientCertificate = new X509CertificateInitiatorServiceCredential(); |
| | 52 | 29 | | ServiceCertificate = new X509CertificateRecipientServiceCredential(); |
| | 52 | 30 | | WindowsAuthentication = new WindowsServiceCredential(); |
| | 52 | 31 | | IssuedTokenAuthentication = new IssuedTokenServiceCredential(); |
| | 52 | 32 | | SecureConversationAuthentication = new SecureConversationServiceCredential(); |
| | 52 | 33 | | _exceptionMapper = new ExceptionMapper(); |
| | 52 | 34 | | UseIdentityConfiguration = false; |
| | 52 | 35 | | } |
| | | 36 | | |
| | 66 | 37 | | protected ServiceCredentials(ServiceCredentials other) |
| | | 38 | | { |
| | 66 | 39 | | if (other == null) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(other)); |
| | | 42 | | } |
| | 66 | 43 | | _provider = other._provider; |
| | 66 | 44 | | UserNameAuthentication = new UserNamePasswordServiceCredential(other.UserNameAuthentication); |
| | 66 | 45 | | ClientCertificate = new X509CertificateInitiatorServiceCredential(other.ClientCertificate); |
| | 66 | 46 | | ServiceCertificate = new X509CertificateRecipientServiceCredential(other.ServiceCertificate); |
| | 66 | 47 | | WindowsAuthentication = new WindowsServiceCredential(other.WindowsAuthentication); |
| | 66 | 48 | | IssuedTokenAuthentication = new IssuedTokenServiceCredential(other.IssuedTokenAuthentication); |
| | 66 | 49 | | SecureConversationAuthentication = new SecureConversationServiceCredential(other.SecureConversationAuthentic |
| | 66 | 50 | | _saveBootstrapTokenInSession = other._saveBootstrapTokenInSession; |
| | 66 | 51 | | _exceptionMapper = other._exceptionMapper; |
| | 66 | 52 | | _identityConfiguration = other._identityConfiguration; |
| | 66 | 53 | | UseIdentityConfiguration = other.UseIdentityConfiguration; |
| | 66 | 54 | | } |
| | | 55 | | |
| | 7 | 56 | | internal ServiceCredentials(IServiceProvider provider):this() |
| | | 57 | | { |
| | 7 | 58 | | _provider = provider; |
| | 7 | 59 | | } |
| | | 60 | | |
| | 138 | 61 | | public UserNamePasswordServiceCredential UserNameAuthentication { get; } |
| | | 62 | | |
| | 93 | 63 | | public X509CertificateInitiatorServiceCredential ClientCertificate { get; } |
| | | 64 | | |
| | 89 | 65 | | public X509CertificateRecipientServiceCredential ServiceCertificate { get; } |
| | | 66 | | |
| | 114 | 67 | | public WindowsServiceCredential WindowsAuthentication { get; } |
| | | 68 | | |
| | 72 | 69 | | public IssuedTokenServiceCredential IssuedTokenAuthentication { get; } |
| | | 70 | | |
| | 224 | 71 | | public SecureConversationServiceCredential SecureConversationAuthentication { get; } |
| | | 72 | | |
| | | 73 | | public IdentityConfiguration IdentityConfiguration |
| | | 74 | | { |
| | | 75 | | get |
| | | 76 | | { |
| | 6 | 77 | | if(_identityConfiguration == null) |
| | | 78 | | { |
| | 2 | 79 | | _identityConfiguration = _provider.GetRequiredService<IdentityConfiguration>(); |
| | | 80 | | } |
| | 6 | 81 | | return _identityConfiguration; |
| | | 82 | | } |
| | | 83 | | set |
| | | 84 | | { |
| | 0 | 85 | | _identityConfiguration = value; |
| | 0 | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | 252 | 89 | | public bool UseIdentityConfiguration { get; set; } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Gets or sets the ExceptionMapper to be used when throwing exceptions. |
| | | 93 | | /// </summary> |
| | | 94 | | public ExceptionMapper ExceptionMapper |
| | | 95 | | { |
| | | 96 | | get |
| | | 97 | | { |
| | 2 | 98 | | return _exceptionMapper; |
| | | 99 | | } |
| | | 100 | | set |
| | | 101 | | { |
| | 0 | 102 | | ThrowIfImmutable(); |
| | 0 | 103 | | _exceptionMapper = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(valu |
| | 0 | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | internal static ServiceCredentials CreateDefaultCredentials() |
| | | 108 | | { |
| | 1 | 109 | | return new ServiceCredentials(); |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public override SecurityTokenManager CreateSecurityTokenManager() |
| | | 113 | | { |
| | 66 | 114 | | if (UseIdentityConfiguration) |
| | | 115 | | { |
| | 2 | 116 | | var list = new List<CookieTransform> |
| | 2 | 117 | | { |
| | 2 | 118 | | new DeflateCookieTransform(), |
| | 2 | 119 | | _provider.GetRequiredService<ProtectedDataCookieTransform>() |
| | 2 | 120 | | }; |
| | | 121 | | // |
| | | 122 | | // Note: the token manager we create here is always a wrapper over the default collection of token handl |
| | | 123 | | // |
| | 2 | 124 | | return new FederatedSecurityTokenManager(Clone(), list.AsReadOnly()); |
| | | 125 | | } |
| | | 126 | | else |
| | | 127 | | { |
| | 64 | 128 | | return new ServiceCredentialsSecurityTokenManager(Clone()); |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | protected virtual ServiceCredentials CloneCore() |
| | | 133 | | { |
| | 66 | 134 | | return new ServiceCredentials(this); |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | public ServiceCredentials Clone() |
| | | 138 | | { |
| | 66 | 139 | | ServiceCredentials result = CloneCore(); |
| | 66 | 140 | | if (result == null || result.GetType() != GetType()) |
| | | 141 | | { |
| | 0 | 142 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotImplementedException(SR.Format(SR.Clone |
| | | 143 | | } |
| | 66 | 144 | | return result; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase) |
| | | 148 | | { |
| | 37 | 149 | | } |
| | | 150 | | |
| | | 151 | | void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Coll |
| | | 152 | | { |
| | 57 | 153 | | if (parameters == null) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(parameters)); |
| | | 156 | | } |
| | | 157 | | // throw if bindingParameters already has a SecurityCredentialsManager |
| | 57 | 158 | | SecurityCredentialsManager otherCredentialsManager = parameters.Find<SecurityCredentialsManager>(); |
| | 57 | 159 | | if (otherCredentialsManager != null) |
| | | 160 | | { |
| | 0 | 161 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Mul |
| | | 162 | | } |
| | 57 | 163 | | parameters.Add(this); |
| | 57 | 164 | | } |
| | | 165 | | |
| | | 166 | | void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) |
| | | 167 | | { |
| | 182 | 168 | | for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++) |
| | | 169 | | { |
| | | 170 | | // TODO: ServiceMetadataBehavior |
| | 54 | 171 | | if (serviceHostBase.ChannelDispatchers[i] is ChannelDispatcher channelDispatcher /*&& !ServiceMetadataBe |
| | | 172 | | { |
| | 216 | 173 | | foreach (EndpointDispatcher endpointDispatcher in channelDispatcher.Endpoints) |
| | | 174 | | { |
| | 54 | 175 | | DispatchRuntime behavior = endpointDispatcher.DispatchRuntime; |
| | 54 | 176 | | behavior.RequireClaimsPrincipalOnOperationContext = false; // _useIdentityConfiguration; |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |
| | 37 | 180 | | } |
| | | 181 | | |
| | | 182 | | internal void MakeReadOnly() |
| | | 183 | | { |
| | 0 | 184 | | _isReadOnly = true; |
| | 0 | 185 | | ClientCertificate.MakeReadOnly(); |
| | 0 | 186 | | IssuedTokenAuthentication.MakeReadOnly(); |
| | 0 | 187 | | SecureConversationAuthentication.MakeReadOnly(); |
| | 0 | 188 | | ServiceCertificate.MakeReadOnly(); |
| | 0 | 189 | | UserNameAuthentication.MakeReadOnly(); |
| | 0 | 190 | | WindowsAuthentication.MakeReadOnly(); |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | private void ThrowIfImmutable() |
| | | 194 | | { |
| | 0 | 195 | | if (_isReadOnly) |
| | | 196 | | { |
| | 0 | 197 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 198 | | } |
| | 0 | 199 | | } |
| | | 200 | | } |
| | | 201 | | } |