/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ using System.ComponentModel; using System.Security.Claims; using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; namespace OpenIddict.Client; /// /// Provides various settings needed to configure the OpenIddict client handler. /// public sealed class OpenIddictClientOptions { /// /// Gets or sets the optional URI used to uniquely identify the client/relying party. /// The URI must be absolute and may contain a path, but no query string or fragment part. /// public Uri? ClientUri { get; set; } /// /// Gets the list of the handlers responsible for processing the OpenIddict client operations. /// Note: the list is automatically sorted based on the order assigned to each handler descriptor. /// As such, it MUST NOT be mutated after options initialization to preserve the exact order. /// public List Handlers { get; } = [.. DefaultHandlers]; /// /// Gets the list of encryption credentials used by the OpenIddict client services. /// Multiple credentials can be added to support key rollover, but if X.509 keys /// are used, at least one of them must have a valid creation/expiration date. /// Note: the encryption credentials are not used to protect/unprotect tokens issued /// by ASP.NET Core Data Protection, that uses its own key ring, configured separately. /// /// /// Note: OpenIddict automatically sorts the credentials based on the following algorithm: /// /// Symmetric keys are always preferred when they can be used for the operation (e.g token encryption). /// X.509 keys are always preferred to non-X.509 asymmetric keys. /// X.509 keys with the furthest expiration date are preferred. /// X.509 keys whose backing certificate is not yet valid are never preferred. /// /// public List EncryptionCredentials { get; } = []; /// /// Gets the list of signing credentials used by the OpenIddict client services. /// Multiple credentials can be added to support key rollover, but if X.509 keys /// are used, at least one of them must have a valid creation/expiration date. /// Note: the signing credentials are not used to protect/unprotect tokens issued /// by ASP.NET Core Data Protection, that uses its own key ring, configured separately. /// /// /// Note: OpenIddict automatically sorts the credentials based on the following algorithm: /// /// Symmetric keys are always preferred when they can be used for the operation (e.g token signing). /// X.509 keys are always preferred to non-X.509 asymmetric keys. /// X.509 keys with the furthest expiration date are preferred. /// X.509 keys whose backing certificate is not yet valid are never preferred. /// /// public List SigningCredentials { get; } = []; /// /// Gets or sets the period of time client assertions remain valid after being issued. The default value is 5 minutes. /// While not recommended, this property can be set to to issue client assertions that never expire. /// public TimeSpan? ClientAssertionLifetime { get; set; } = TimeSpan.FromMinutes(5); /// /// Gets or sets the period of time state tokens remain valid after being issued. The default value is 15 minutes. /// While not recommended, this property can be set to to issue state tokens that never expire. /// public TimeSpan? StateTokenLifetime { get; set; } = TimeSpan.FromMinutes(15); /// /// Gets or sets the security token handler used to protect and unprotect tokens. /// public JsonWebTokenHandler JsonWebTokenHandler { get; set; } = new JsonWebTokenHandler { SetDefaultTimesOnTokenCreation = false }; /// /// Gets the absolute and relative URIs associated to the post-logout redirection endpoint. /// public List PostLogoutRedirectionEndpointUris { get; } = []; /// /// Gets the absolute and relative URIs associated to the redirection endpoint. /// public List RedirectionEndpointUris { get; } = []; /// /// Gets the static client registrations used by the OpenIddict client services. /// public List Registrations { get; } = []; /// /// Gets the token validation parameters used by the OpenIddict client services. /// /// /// This instance is not used to validate tokens issued by remote authorization servers /// and is only used with tokens produced and validated by the client itself (e.g state tokens). /// public TokenValidationParameters TokenValidationParameters { get; } = new() { AuthenticationType = TokenValidationParameters.DefaultAuthenticationType, ClockSkew = TimeSpan.Zero, NameClaimType = Claims.Name, RoleClaimType = Claims.Role, TypeValidator = static (type, token, parameters) => { // Assume that tokens that don't have an explicit "typ" header attached are generic JSON Web Tokens. if (string.IsNullOrEmpty(type)) { type = JsonWebTokenTypes.GenericJsonWebToken; } // Note: unlike IdentityModel, this custom validator deliberately uses case-insensitive comparisons. if (parameters.ValidTypes is not null && parameters.ValidTypes.Any() && !parameters.ValidTypes.Contains(type, StringComparer.OrdinalIgnoreCase)) { throw new SecurityTokenInvalidTypeException(SR.GetResourceString(SR.ID0271)) { InvalidType = type }; } return type; }, // Note: audience and lifetime are manually validated by OpenIddict itself. ValidateAudience = false, ValidateLifetime = false }; /// /// Gets or sets a boolean indicating whether token storage should be disabled. /// When disabled, no database entry is created for the tokens created by the /// OpenIddict client services. Using this option is generally NOT recommended. /// public bool DisableTokenStorage { get; set; } /// /// Gets or sets a boolean indicating whether the claim mapping feature inferring /// WS-Federation claims (exposed by the class) from their /// OpenID Connect/JSON Web Token or provider-specific equivalent should be disabled. /// /// /// Note: if automatic claim mapping is disabled, no WS-Federation claim will /// be added to . /// public bool DisableWebServicesFederationClaimMapping { get; set; } /// /// Gets the OAuth 2.0 client authentication methods enabled for this application. /// public HashSet ClientAuthenticationMethods { get; } = new(StringComparer.Ordinal) { // Note: client_secret_basic is deliberately not added here as it requires // a dedicated event handler (typically provided by the HTTP integration) // to attach the client credentials to the standard Authorization header. // // The System.Net.Http integration supports the client_secret_basic, // self_signed_tls_client_auth and tls_client_auth authentication // methods and automatically add them to this list at runtime. OpenIddictConstants.ClientAuthenticationMethods.ClientSecretPost, OpenIddictConstants.ClientAuthenticationMethods.PrivateKeyJwt }; /// /// Gets the OAuth 2.0 code challenge methods enabled for this application. /// public HashSet CodeChallengeMethods { get; } = new(StringComparer.Ordinal) { OpenIddictConstants.CodeChallengeMethods.Plain, OpenIddictConstants.CodeChallengeMethods.Sha256 }; /// /// Gets the OAuth 2.0/OpenID Connect flows enabled for this application. /// public HashSet GrantTypes { get; } = new(StringComparer.Ordinal); /// /// Gets the OAuth 2.0/OpenID Connect response modes enabled for this application. /// [EditorBrowsable(EditorBrowsableState.Advanced)] public HashSet ResponseModes { get; } = new(StringComparer.Ordinal) { // Note: fragment is deliberately not added here as it typically doesn't work // with server-based applications without offering an HTML/JS page extracting // the parameters from the URI fragment and flowing them differently. // // The system integration package supports the fragment response mode in // specific cases (e.g when using the UWP Web Authentication Broker API) // and automatically adds fragment to this list when it is enabled. OpenIddictConstants.ResponseModes.FormPost, OpenIddictConstants.ResponseModes.Query }; /// /// Gets the OAuth 2.0/OpenID Connect response types enabled for this application. /// [EditorBrowsable(EditorBrowsableState.Advanced)] public HashSet ResponseTypes { get; } = new(StringComparer.Ordinal); /// /// Gets or sets the time provider. /// /// /// Note: if this property is not explicitly set, the time provider is /// automatically resolved from the dependency injection container. /// If no service can be found, is used. /// public TimeProvider TimeProvider { get; set; } = default!; /// /// Gets the OAuth 2.0 token binding methods enabled for this application. /// public HashSet TokenBindingMethods { get; } = new(StringComparer.Ordinal); }