| | | 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.Security.Cryptography.X509Certificates; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using CoreWCF.IdentityModel.Tokens; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.IdentityModel.Configuration |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Defines the collection of configurable properties controlling the behavior of the Windows Identity Foundation. |
| | | 16 | | /// </summary> |
| | | 17 | | public class IdentityConfiguration |
| | | 18 | | { |
| | | 19 | | public const string DefaultServiceName = ConfigurationStrings.DefaultServiceName; |
| | 2 | 20 | | public static readonly TimeSpan DefaultMaxClockSkew = new TimeSpan(0, 5, 0); |
| | | 21 | | internal const string DefaultMaxClockSkewString = "00:05:00"; |
| | 2 | 22 | | public static readonly X509CertificateValidationMode DefaultCertificateValidationMode = X509CertificateValidatio |
| | 2 | 23 | | public static readonly Type DefaultIssuerNameRegistryType = typeof(ConfigurationBasedIssuerNameRegistry); |
| | 2 | 24 | | public static readonly X509RevocationMode DefaultRevocationMode = X509RevocationMode.Online; |
| | 2 | 25 | | public static readonly StoreLocation DefaultTrustedStoreLocation = StoreLocation.LocalMachine; |
| | 2 | 26 | | private TimeSpan _serviceMaxClockSkew = DefaultMaxClockSkew; |
| | | 27 | | private SecurityTokenHandlerConfiguration _serviceHandlerConfiguration; |
| | | 28 | | |
| | 2 | 29 | | public IdentityConfiguration(IEnumerable<SecurityTokenHandler> securityTokenHandlers) |
| | | 30 | | { |
| | 2 | 31 | | LoadHandlersNoConfig(securityTokenHandlers); |
| | 2 | 32 | | } |
| | | 33 | | |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the AudienceRestriction. |
| | | 37 | | /// </summary> |
| | | 38 | | public AudienceRestriction AudienceRestriction |
| | | 39 | | { |
| | 2 | 40 | | get { return _serviceHandlerConfiguration.AudienceRestriction; } |
| | 0 | 41 | | set { _serviceHandlerConfiguration.AudienceRestriction = value; } |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the Caches configured. |
| | | 46 | | /// </summary> |
| | | 47 | | public IdentityModelCaches Caches |
| | | 48 | | { |
| | 0 | 49 | | get { return _serviceHandlerConfiguration.Caches; } |
| | 0 | 50 | | set { _serviceHandlerConfiguration.Caches = value; } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Gets or sets the certificate validation mode used by handlers to validate issuer certificates |
| | | 55 | | /// </summary> |
| | | 56 | | public X509CertificateValidationMode CertificateValidationMode |
| | | 57 | | { |
| | 0 | 58 | | get { return _serviceHandlerConfiguration.CertificateValidationMode; } |
| | 4 | 59 | | set { _serviceHandlerConfiguration.CertificateValidationMode = value; } |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets or sets the certificate validator used by handlers to validate issuer certificates |
| | | 64 | | /// </summary> |
| | | 65 | | public X509CertificateValidator CertificateValidator |
| | | 66 | | { |
| | 0 | 67 | | get { return _serviceHandlerConfiguration.CertificateValidator; } |
| | 4 | 68 | | set { _serviceHandlerConfiguration.CertificateValidator = value; } |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets or Sets detection of replaying of tokens by handlers in the default handler configuration. |
| | | 73 | | /// </summary> |
| | | 74 | | public bool DetectReplayedTokens |
| | | 75 | | { |
| | 0 | 76 | | get { return _serviceHandlerConfiguration.DetectReplayedTokens; } |
| | 0 | 77 | | set { _serviceHandlerConfiguration.DetectReplayedTokens = value; } |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Determines if <see cref="IdentityConfiguration.Initialize"/> has been called. |
| | | 82 | | /// </summary> |
| | 0 | 83 | | public virtual bool IsInitialized { get; set; } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Updates properties in the <see cref="SecurityTokenHandlerConfiguration"/> objects for the |
| | | 87 | | /// <see cref="SecurityTokenHandlerCollection"/> objects contained in |
| | | 88 | | /// <see cref="IdentityConfiguration.SecurityTokenHandlerCollectionManager"/> to be consistent with the property |
| | | 89 | | /// values on this <see cref="IdentityConfiguration"/> instance. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <remarks> |
| | | 92 | | /// This method should be invoked prior to using these token handlers |
| | | 93 | | /// for token processing. |
| | | 94 | | /// </remarks> |
| | | 95 | | /// <exception cref="InvalidOperationException">If this method is invoked more than once.</exception> |
| | | 96 | | public virtual void Initialize() |
| | | 97 | | { |
| | 0 | 98 | | if (IsInitialized) |
| | | 99 | | { |
| | 0 | 100 | | throw new InvalidOperationException(SR.Format(SR.ID7009)); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | SecurityTokenHandlerCollection defaultCollection = SecurityTokenHandlers; |
| | | 104 | | |
| | 0 | 105 | | if (!object.ReferenceEquals(_serviceHandlerConfiguration, defaultCollection.Configuration)) |
| | | 106 | | { |
| | | 107 | | // |
| | | 108 | | // If someone has created their own new STHConfig and set it as default, leave that config alone. |
| | | 109 | | // |
| | | 110 | | // TraceUtility.TraceString(TraceEventType.Information, SR.Format(SR.ID4283)); |
| | 0 | 111 | | IsInitialized = true; |
| | 0 | 112 | | return; |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Update the ServiceTokenResolver of the default TokenHandlerCollection's configuration, if serviceCertific |
| | 0 | 116 | | if (ServiceCertificate != null) |
| | | 117 | | { |
| | 0 | 118 | | SecurityTokenResolver serviceCertificateResolver = SecurityTokenResolver.CreateDefaultSecurityTokenResol |
| | 0 | 119 | | new SecurityToken[] { new X509SecurityToken(ServiceCertificate) }) |
| | | 120 | | |
| | 0 | 121 | | SecurityTokenResolver tokenResolver = SecurityTokenHandlers.Configuration.ServiceTokenResolver; |
| | | 122 | | |
| | 0 | 123 | | if ((tokenResolver != null) && (tokenResolver != EmptySecurityTokenResolver.Instance)) |
| | | 124 | | { |
| | 0 | 125 | | SecurityTokenHandlers.Configuration.ServiceTokenResolver = new AggregateTokenResolver(new SecurityTo |
| | | 126 | | } |
| | | 127 | | else |
| | | 128 | | { |
| | 0 | 129 | | SecurityTokenHandlers.Configuration.ServiceTokenResolver = serviceCertificateResolver; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | SecurityTokenResolver configuredIssuerTokenResolver = IssuerTokenResolver; |
| | | 134 | | |
| | 0 | 135 | | if (IssuerTokenResolver == SecurityTokenHandlerConfiguration.DefaultIssuerTokenResolver) |
| | | 136 | | { |
| | | 137 | | // |
| | | 138 | | // Add the known certificates from WCF's ServiceCredentials in front of |
| | | 139 | | // the default issuer token resolver. |
| | | 140 | | // |
| | 0 | 141 | | if (KnownIssuerCertificates != null) |
| | | 142 | | { |
| | 0 | 143 | | int count = KnownIssuerCertificates.Count; |
| | 0 | 144 | | if (count > 0) |
| | | 145 | | { |
| | 0 | 146 | | SecurityToken[] tokens = new SecurityToken[count]; |
| | 0 | 147 | | for (int i = 0; i < count; i++) |
| | | 148 | | { |
| | 0 | 149 | | tokens[i] = new X509SecurityToken(KnownIssuerCertificates[i]); |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | SecurityTokenResolver knownCertificateTokenResolver = SecurityTokenResolver.CreateDefaultSecurit |
| | | 153 | | |
| | 0 | 154 | | IssuerTokenResolver = new AggregateTokenResolver(new SecurityTokenResolver[] { knownCertificateT |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | if (CertificateValidationMode != X509CertificateValidationMode.Custom) |
| | | 160 | | { |
| | 0 | 161 | | defaultCollection.Configuration.CertificateValidator = X509Util.CreateCertificateValidator(defaultCollec |
| | 0 | 162 | | defaultColle |
| | 0 | 163 | | defaultColle |
| | | 164 | | } |
| | 0 | 165 | | else if (object.ReferenceEquals(defaultCollection.Configuration.CertificateValidator, SecurityTokenHandlerCo |
| | | 166 | | { |
| | | 167 | | // |
| | | 168 | | // If the mode is custom but the validator or still default, something has gone wrong. |
| | | 169 | | // |
| | 0 | 170 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID4 |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | IsInitialized = true; |
| | 0 | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// TODO : Discuss better way to separate the configurations from Primitives |
| | | 178 | | /// </summary> |
| | | 179 | | private void LoadHandlersNoConfig(IEnumerable<SecurityTokenHandler> serviceTokenHandlers) |
| | | 180 | | { |
| | 2 | 181 | | SecurityTokenHandlerCollectionManager manager = SecurityTokenHandlerCollectionManager.CreateEmptySecurityTok |
| | 2 | 182 | | _serviceHandlerConfiguration = new SecurityTokenHandlerConfiguration |
| | 2 | 183 | | { |
| | 2 | 184 | | MaxClockSkew = _serviceMaxClockSkew |
| | 2 | 185 | | }; |
| | | 186 | | |
| | 2 | 187 | | if (!manager.ContainsKey(SecurityTokenHandlerCollectionManager.Usage.Default)) |
| | | 188 | | { |
| | 2 | 189 | | manager[SecurityTokenHandlerCollectionManager.Usage.Default] = SecurityTokenHandlerCollection.CreateDefa |
| | | 190 | | } |
| | 2 | 191 | | SecurityTokenHandlerCollectionManager = manager; |
| | 2 | 192 | | } |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Gets or sets the maximum allowable time difference between the |
| | | 196 | | /// system clocks of the two parties that are communicating. |
| | | 197 | | /// </summary> |
| | | 198 | | public TimeSpan MaxClockSkew |
| | | 199 | | { |
| | 0 | 200 | | get { return _serviceHandlerConfiguration.MaxClockSkew; } |
| | 0 | 201 | | set { _serviceHandlerConfiguration.MaxClockSkew = value; } |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Gets or sets the service name of this configuration. |
| | | 206 | | /// </summary> |
| | 2 | 207 | | public string Name { get; } = DefaultServiceName; |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Gets or sets the IssuerNameRegistry used to resolve issuer names. |
| | | 211 | | /// </summary> |
| | | 212 | | public IssuerNameRegistry IssuerNameRegistry |
| | | 213 | | { |
| | | 214 | | get |
| | | 215 | | { |
| | 0 | 216 | | return _serviceHandlerConfiguration.IssuerNameRegistry; |
| | | 217 | | } |
| | | 218 | | set |
| | | 219 | | { |
| | 2 | 220 | | _serviceHandlerConfiguration.IssuerNameRegistry = value ?? throw DiagnosticUtility.ExceptionUtility.Thro |
| | 2 | 221 | | } |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// The service certificate to initialize the ServiceTokenResolver and the SessionSecurityTokenHandler. |
| | | 226 | | /// </summary> |
| | 0 | 227 | | public X509Certificate2 ServiceCertificate { get; set; } |
| | | 228 | | |
| | 0 | 229 | | internal List<X509Certificate2> KnownIssuerCertificates { get; set; } |
| | | 230 | | |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Gets or Sets the Issuer token resolver. |
| | | 234 | | /// </summary> |
| | | 235 | | public SecurityTokenResolver IssuerTokenResolver |
| | | 236 | | { |
| | | 237 | | get |
| | | 238 | | { |
| | 0 | 239 | | return _serviceHandlerConfiguration.IssuerTokenResolver; |
| | | 240 | | } |
| | | 241 | | set |
| | | 242 | | { |
| | 2 | 243 | | _serviceHandlerConfiguration.IssuerTokenResolver = value ?? throw DiagnosticUtility.ExceptionUtility.Thr |
| | 2 | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | | 247 | | /// <summary> |
| | | 248 | | /// Gets or sets the revocation mode used by handlers to validate issuer certificates |
| | | 249 | | /// </summary> |
| | | 250 | | public X509RevocationMode RevocationMode |
| | | 251 | | { |
| | 0 | 252 | | get { return _serviceHandlerConfiguration.RevocationMode; } |
| | 4 | 253 | | set { _serviceHandlerConfiguration.RevocationMode = value; } |
| | | 254 | | } |
| | | 255 | | |
| | | 256 | | /// <summary> |
| | | 257 | | /// Gets or Sets the Service token resolver. |
| | | 258 | | /// </summary> |
| | | 259 | | public SecurityTokenResolver ServiceTokenResolver |
| | | 260 | | { |
| | | 261 | | get |
| | | 262 | | { |
| | 0 | 263 | | return _serviceHandlerConfiguration.ServiceTokenResolver; |
| | | 264 | | } |
| | | 265 | | set |
| | | 266 | | { |
| | 0 | 267 | | _serviceHandlerConfiguration.ServiceTokenResolver = value ?? throw DiagnosticUtility.ExceptionUtility.Th |
| | 0 | 268 | | } |
| | | 269 | | } |
| | | 270 | | |
| | | 271 | | /// <summary> |
| | | 272 | | /// Gets or sets if BootstrapContext is saved in the ClaimsIdentity and Sessions after token validation. |
| | | 273 | | /// </summary> |
| | | 274 | | public bool SaveBootstrapContext |
| | | 275 | | { |
| | 0 | 276 | | get { return _serviceHandlerConfiguration.SaveBootstrapContext; } |
| | 4 | 277 | | set { _serviceHandlerConfiguration.SaveBootstrapContext = value; } |
| | | 278 | | } |
| | | 279 | | |
| | | 280 | | /// <summary> |
| | | 281 | | /// The <see cref="SecurityTokenHandlerCollectionManager" /> containing the set of <see cref="SecurityTokenHandl |
| | | 282 | | /// objects used for serializing and validating tokens found in WS-Trust messages. |
| | | 283 | | /// </summary> |
| | 4 | 284 | | public SecurityTokenHandlerCollectionManager SecurityTokenHandlerCollectionManager { get; private set; } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// The <see cref="SecurityTokenHandlerCollection" /> collection of <see cref="SecurityTokenHandler" /> |
| | | 288 | | /// objects used for serializing and validating tokens found in WS-Trust messages. |
| | | 289 | | /// If user wants to register their own token handler, they |
| | | 290 | | /// can simply add their own handler to this collection. |
| | | 291 | | /// </summary> |
| | | 292 | | public SecurityTokenHandlerCollection SecurityTokenHandlers |
| | | 293 | | { |
| | | 294 | | get |
| | | 295 | | { |
| | 2 | 296 | | return SecurityTokenHandlerCollectionManager[SecurityTokenHandlerCollectionManager.Usage.Default]; |
| | | 297 | | } |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Gets or Sets the expiration period for items placed in the TokenReplayCache. |
| | | 302 | | /// </summary> |
| | | 303 | | public TimeSpan TokenReplayCacheExpirationPeriod |
| | | 304 | | { |
| | 0 | 305 | | get { return _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod; } |
| | 0 | 306 | | set { _serviceHandlerConfiguration.TokenReplayCacheExpirationPeriod = value; } |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Gets or sets the trusted store location used by handlers to validate issuer certificates |
| | | 311 | | /// </summary> |
| | | 312 | | public StoreLocation TrustedStoreLocation |
| | | 313 | | { |
| | 0 | 314 | | get { return _serviceHandlerConfiguration.TrustedStoreLocation; } |
| | 0 | 315 | | set { _serviceHandlerConfiguration.TrustedStoreLocation = value; } |
| | | 316 | | } |
| | | 317 | | } |
| | | 318 | | } |