// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IdentityModel.Tokens; using System.IdentityModel.Tokens.Jwt; using System.Net.Http; using System.Security.Claims; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Authentication; using Microsoft.Framework.Caching.Distributed; using Microsoft.Framework.WebEncoders; using Microsoft.IdentityModel.Protocols; using Microsoft.IdentityModel.Protocols.OpenIdConnect; namespace Microsoft.AspNet.Authentication.OpenIdConnect { /// /// Configuration options for /// public class OpenIdConnectAuthenticationOptions : AuthenticationOptions { /// /// Initializes a new /// public OpenIdConnectAuthenticationOptions() : this(OpenIdConnectAuthenticationDefaults.AuthenticationScheme) { } /// /// Initializes a new /// /// /// Defaults: /// AddNonceToRequest: true. /// BackchannelTimeout: 1 minute. /// Caption: . /// ProtocolValidator: new . /// RefreshOnIssuerKeyNotFound: true /// ResponseType: /// Scope: . /// TokenValidationParameters: new with AuthenticationScheme = authenticationScheme. /// UseTokenLifetime: true. /// /// will be used to when creating the for the AuthenticationScheme property. [SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions.set_Caption(System.String)", Justification = "Not a LOC field")] public OpenIdConnectAuthenticationOptions(string authenticationScheme) { AuthenticationScheme = authenticationScheme; Caption = OpenIdConnectAuthenticationDefaults.Caption; } /// /// Gets or sets the expected audience for any received JWT token. /// /// /// The expected audience for any received JWT token. /// public string Audience { get; set; } /// /// Gets or sets the Authority to use when making OpenIdConnect calls. /// public string Authority { get; set; } #if DNX451 /// /// Gets or sets the a pinned certificate validator to use to validate the endpoints used /// when retrieving metadata. /// /// /// The pinned certificate validator. /// /// If this property is null then the default certificate checks are performed, /// validating the subject name and if the signing chain is a trusted party. public ICertificateValidator BackchannelCertificateValidator { get; set; } #endif /// /// The HttpMessageHandler used to retrieve metadata. /// This cannot be set at the same time as BackchannelCertificateValidator unless the value /// is a WebRequestHandler. /// public HttpMessageHandler BackchannelHttpHandler { get; set; } /// /// Gets or sets the timeout when using the backchannel to make an http call. /// [SuppressMessage("Microsoft.Usage", "CA2208:InstantiateArgumentExceptionsCorrectly", Justification = "By design we use the property name in the exception")] public TimeSpan BackchannelTimeout { get; set; } = TimeSpan.FromSeconds(60); /// /// Get or sets the text that the user can display on a sign in user interface. /// public string Caption { get { return Description.Caption; } set { Description.Caption = value; } } /// /// An optional constrained path on which to process the authentication callback. /// If not provided and RedirectUri is available, this value will be generated from RedirectUri. /// /// If you set this value, then the will only listen for posts at this address. /// If the IdentityProvider does not post to this address, you may end up in a 401 -> IdentityProvider -> Client -> 401 -> ... public PathString CallbackPath { get; set; } /// /// Gets or sets the 'client_id'. /// public string ClientId { get; set; } /// /// Gets or sets the 'client_secret'. /// public string ClientSecret { get; set; } /// /// Configuration provided directly by the developer. If provided, then MetadataAddress and the Backchannel properties /// will not be used. This information should not be updated during request processing. /// public OpenIdConnectConfiguration Configuration { get; set; } /// /// Responsible for retrieving, caching, and refreshing the configuration from metadata. /// If not provided, then one will be created using the MetadataAddress and Backchannel properties. /// public IConfigurationManager ConfigurationManager { get; set; } /// /// Gets or sets a value controlling if the 'CurrentUri' should be used as the 'local redirect' post authentication /// if AuthenticationProperties.RedirectUri is null or empty. /// public bool DefaultToCurrentUriOnRedirect { get; set; } /// /// Boolean to set whether the middleware should go to user info endpoint to retrieve additional claims or not after creating an identity from id_token received from token endpoint. /// public bool GetClaimsFromUserInfoEndpoint { get; set; } /// /// Gets or sets the discovery endpoint for obtaining metadata /// public string MetadataAddress { get; set; } /// /// The OpenIdConnect protocol http://openid.net/specs/openid-connect-core-1_0.html /// recommends adding a nonce to a request as a mitigation against replay attacks when requesting id_tokens. /// By default the runtime uses cookies with unique names generated from a hash of the nonce. /// public IDistributedCache NonceCache { get; set; } /// /// Gets or sets the value indicating whether nonces should be stored in the distributed cache or not. /// The default value, false, is used to store nonces in client cookies. /// public bool CacheNonces { get; set; } /// /// Gets or sets the to notify when processing OpenIdConnect messages. /// public IOpenIdConnectAuthenticationEvents Events { get; set; } = new OpenIdConnectAuthenticationEvents(); /// /// Gets or sets the that is used to ensure that the 'id_token' received /// is valid per: http://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation /// /// if 'value' is null. public OpenIdConnectProtocolValidator ProtocolValidator { get; set; } = new OpenIdConnectProtocolValidator() { RequireState = false, NonceLifetime = TimeSpan.FromMinutes(15) }; /// /// Gets or sets the 'post_logout_redirect_uri' /// /// This is sent to the OP as the redirect for the user-agent. [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "By design")] [SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Logout", Justification = "This is the term used in the spec.")] public string PostLogoutRedirectUri { get; set; } /// /// Gets or sets the 'redirect_uri'. /// [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "By Design")] public string RedirectUri { get; set; } /// /// Gets or sets if a metadata refresh should be attempted after a SecurityTokenSignatureKeyNotFoundException. This allows for automatic /// recovery in the event of a signature key rollover. This is enabled by default. /// public bool RefreshOnIssuerKeyNotFound { get; set; } = true; /// /// Gets or sets the method used to redirect the user agent to the identity provider. /// public OpenIdConnectAuthenticationMethod AuthenticationMethod { get; set; } /// /// Gets or sets the 'resource'. /// public string Resource { get; set; } /// /// Gets or sets the 'response_mode'. /// public string ResponseMode { get; set; } = OpenIdConnectResponseModes.FormPost; /// /// Gets or sets the 'response_type'. /// public string ResponseType { get; set; } = OpenIdConnectResponseTypes.CodeIdToken; /// /// Gets the list of permissions to request. /// public IList Scope { get; } = new List { "openid", "profile" }; /// /// Gets or sets the SignInScheme which will be used to set the . /// public string SignInScheme { get; set; } /// /// Gets or sets the type used to secure data handled by the middleware. /// public ISecureDataFormat StateDataFormat { get; set; } /// /// Gets or sets the type used to secure strings used by the middleware. /// public ISecureDataFormat StringDataFormat { get; set; } /// /// Gets or sets the used to validate identity tokens. /// public ISecurityTokenValidator SecurityTokenValidator { get; set; } = new JwtSecurityTokenHandler(); /// /// Gets or sets the parameters used to validate identity tokens. /// /// Contains the types and definitions required for validating a token. public TokenValidationParameters TokenValidationParameters { get; set; } = new TokenValidationParameters(); /// /// Indicates that the authentication session lifetime (e.g. cookies) should match that of the authentication token. /// If the token does not provide lifetime information then normal session lifetimes will be used. /// This is enabled by default. /// public bool UseTokenLifetime { get; set; } = true; /// /// Gets or sets the used to sanitize HTML outputs. /// public IHtmlEncoder HtmlEncoder { get; set; } } }