// 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 Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Microsoft.AspNetCore.Authentication
{
///
/// Used to configure authentication
///
public class AuthenticationBuilder
{
///
/// Constructor.
///
/// The services being configured.
public AuthenticationBuilder(IServiceCollection services)
=> Services = services;
///
/// The services being configured.
///
public virtual IServiceCollection Services { get; }
private AuthenticationBuilder AddSchemeHelper(string authenticationScheme, string displayName, Action configureOptions)
where TOptions : class, new()
where THandler : class, IAuthenticationHandler
{
Services.Configure(o =>
{
o.AddScheme(authenticationScheme, scheme => {
scheme.HandlerType = typeof(THandler);
scheme.DisplayName = displayName;
});
});
if (configureOptions != null)
{
Services.Configure(authenticationScheme, configureOptions);
}
Services.AddTransient();
return this;
}
///
/// Adds a which can be used by .
///
/// The type to configure the handler."/>.
/// The used to handle this scheme.
/// The name of this scheme.
/// The display name of this scheme.
/// Used to configure the scheme options.
/// The builder.
public virtual AuthenticationBuilder AddScheme(string authenticationScheme, string displayName, Action configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler
=> AddSchemeHelper(authenticationScheme, displayName, configureOptions);
///
/// Adds a which can be used by .
///
/// The type to configure the handler."/>.
/// The used to handle this scheme.
/// The name of this scheme.
/// Used to configure the scheme options.
/// The builder.
public virtual AuthenticationBuilder AddScheme(string authenticationScheme, Action configureOptions)
where TOptions : AuthenticationSchemeOptions, new()
where THandler : AuthenticationHandler
=> AddScheme(authenticationScheme, displayName: null, configureOptions: configureOptions);
///
/// Adds a based that supports remote authentication
/// which can be used by .
///
/// The type to configure the handler."/>.
/// The used to handle this scheme.
/// The name of this scheme.
/// The display name of this scheme.
/// Used to configure the scheme options.
/// The builder.
public virtual AuthenticationBuilder AddRemoteScheme(string authenticationScheme, string displayName, Action configureOptions)
where TOptions : RemoteAuthenticationOptions, new()
where THandler : RemoteAuthenticationHandler
{
Services.TryAddEnumerable(ServiceDescriptor.Singleton, EnsureSignInScheme>());
return AddScheme(authenticationScheme, displayName, configureOptions: configureOptions);
}
///
/// Adds a based authentication handler which can be used to
/// redirect to other authentication schemes.
///
/// The name of this scheme.
/// The display name of this scheme.
/// Used to configure the scheme options.
/// The builder.
public virtual AuthenticationBuilder AddPolicyScheme(string authenticationScheme, string displayName, Action configureOptions)
=> AddSchemeHelper(authenticationScheme, displayName, configureOptions);
// Used to ensure that there's always a default sign in scheme that's not itself
private class EnsureSignInScheme : IPostConfigureOptions where TOptions : RemoteAuthenticationOptions
{
private readonly AuthenticationOptions _authOptions;
public EnsureSignInScheme(IOptions authOptions)
{
_authOptions = authOptions.Value;
}
public void PostConfigure(string name, TOptions options)
{
options.SignInScheme = options.SignInScheme ?? _authOptions.DefaultSignInScheme ?? _authOptions.DefaultScheme;
}
}
}
}