// 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.Net.Http; using Microsoft.AspNetCore.Authentication.Internal; using Microsoft.AspNetCore.DataProtection; using Microsoft.AspNetCore.Http; namespace Microsoft.AspNetCore.Authentication { /// /// Contains the options used by the . /// public class RemoteAuthenticationOptions : AuthenticationSchemeOptions { private const string CorrelationPrefix = ".AspNetCore.Correlation."; private CookieBuilder _correlationCookieBuilder; /// /// Initializes a new . /// public RemoteAuthenticationOptions() { _correlationCookieBuilder = new CorrelationCookieBuilder(this) { Name = CorrelationPrefix, HttpOnly = true, SameSite = SameSiteMode.None, SecurePolicy = CookieSecurePolicy.SameAsRequest, }; } /// /// Checks that the options are valid for a specific scheme /// /// The scheme being validated. public override void Validate(string scheme) { base.Validate(scheme); if (string.Equals(scheme, SignInScheme, StringComparison.Ordinal)) { throw new InvalidOperationException(Resources.Exception_RemoteSignInSchemeCannotBeSelf); } } /// /// Check that the options are valid. Should throw an exception if things are not ok. /// public override void Validate() { base.Validate(); if (CallbackPath == null || !CallbackPath.HasValue) { throw new ArgumentException(Resources.FormatException_OptionMustBeProvided(nameof(CallbackPath)), nameof(CallbackPath)); } } /// /// Gets or sets timeout value in milliseconds for back channel communications with the remote identity provider. /// /// /// The back channel timeout. /// public TimeSpan BackchannelTimeout { get; set; } = TimeSpan.FromSeconds(60); /// /// The HttpMessageHandler used to communicate with remote identity provider. /// This cannot be set at the same time as BackchannelCertificateValidator unless the value /// can be downcast to a WebRequestHandler. /// public HttpMessageHandler BackchannelHttpHandler { get; set; } /// /// Used to communicate with the remote identity provider. /// public HttpClient Backchannel { get; set; } /// /// Gets or sets the type used to secure data. /// public IDataProtectionProvider DataProtectionProvider { get; set; } /// /// The request path within the application's base path where the user-agent will be returned. /// The middleware will process this request when it arrives. /// public PathString CallbackPath { get; set; } /// /// Gets or sets the authentication scheme corresponding to the middleware /// responsible of persisting user's identity after a successful authentication. /// This value typically corresponds to a cookie middleware registered in the Startup class. /// When omitted, is used as a fallback value. /// public string SignInScheme { get; set; } /// /// Gets or sets the time limit for completing the authentication flow (15 minutes by default). /// public TimeSpan RemoteAuthenticationTimeout { get; set; } = TimeSpan.FromMinutes(15); public new RemoteAuthenticationEvents Events { get => (RemoteAuthenticationEvents)base.Events; set => base.Events = value; } /// /// Defines whether access and refresh tokens should be stored in the /// after a successful authorization. /// This property is set to false by default to reduce /// the size of the final authentication cookie. /// public bool SaveTokens { get; set; } /// /// Determines the settings used to create the correlation cookie before the /// cookie gets added to the response. /// public CookieBuilder CorrelationCookie { get => _correlationCookieBuilder; set => _correlationCookieBuilder = value ?? throw new ArgumentNullException(nameof(value)); } private class CorrelationCookieBuilder : RequestPathBaseCookieBuilder { private readonly RemoteAuthenticationOptions _options; public CorrelationCookieBuilder(RemoteAuthenticationOptions remoteAuthenticationOptions) { _options = remoteAuthenticationOptions; } protected override string AdditionalPath => _options.CallbackPath; public override CookieOptions Build(HttpContext context, DateTimeOffset expiresFrom) { var cookieOptions = base.Build(context, expiresFrom); if (!Expiration.HasValue || !cookieOptions.Expires.HasValue) { cookieOptions.Expires = expiresFrom.Add(_options.RemoteAuthenticationTimeout); } return cookieOptions; } } } }