Changes BackOfficeExternalLoginProviderOptions to use the IOptions pattern with named options so they can be set after DI.

This commit is contained in:
Shannon
2021-07-21 14:53:25 -06:00
parent 1155210209
commit 7280c22e0a
4 changed files with 38 additions and 26 deletions

View File

@@ -12,18 +12,16 @@ namespace Umbraco.Cms.Web.BackOffice.Security
/// </summary>
public class BackOfficeAuthenticationBuilder : AuthenticationBuilder
{
private readonly BackOfficeExternalLoginProviderOptions _loginProviderOptions;
private readonly Action<BackOfficeExternalLoginProviderOptions> _loginProviderOptions;
public BackOfficeAuthenticationBuilder(IServiceCollection services, BackOfficeExternalLoginProviderOptions loginProviderOptions)
public BackOfficeAuthenticationBuilder(
IServiceCollection services,
Action<BackOfficeExternalLoginProviderOptions> loginProviderOptions = null)
: base(services)
{
_loginProviderOptions = loginProviderOptions;
}
=> _loginProviderOptions = loginProviderOptions ?? (x => { });
public string SchemeForBackOffice(string scheme)
{
return Constants.Security.BackOfficeExternalAuthenticationTypePrefix + scheme;
}
=> Constants.Security.BackOfficeExternalAuthenticationTypePrefix + scheme;
/// <summary>
/// Overridden to track the final authenticationScheme being registered for the external login
@@ -43,7 +41,11 @@ namespace Umbraco.Cms.Web.BackOffice.Security
}
// add our login provider to the container along with a custom options configuration
Services.AddSingleton(x => new BackOfficeExternalLoginProvider(displayName, authenticationScheme, _loginProviderOptions));
Services.Configure(authenticationScheme, _loginProviderOptions);
Services.AddSingleton(x => new BackOfficeExternalLoginProvider(
displayName,
authenticationScheme,
x.GetRequiredService<IOptions<BackOfficeExternalLoginProviderOptions>>()));
Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<TOptions>, EnsureBackOfficeScheme<TOptions>>());
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);

View File

@@ -1,4 +1,5 @@
using System;
using System;
using Microsoft.Extensions.Options;
namespace Umbraco.Cms.Web.BackOffice.Security
{
@@ -7,11 +8,16 @@ namespace Umbraco.Cms.Web.BackOffice.Security
/// </summary>
public class BackOfficeExternalLoginProvider : IEquatable<BackOfficeExternalLoginProvider>
{
public BackOfficeExternalLoginProvider(string name, string authenticationType, BackOfficeExternalLoginProviderOptions properties)
public BackOfficeExternalLoginProvider(string name, string authenticationType, IOptions<BackOfficeExternalLoginProviderOptions> properties)
{
if (properties is null)
{
throw new ArgumentNullException(nameof(properties));
}
Name = name ?? throw new ArgumentNullException(nameof(name));
AuthenticationType = authenticationType ?? throw new ArgumentNullException(nameof(authenticationType));
Options = properties ?? throw new ArgumentNullException(nameof(properties));
Options = properties.Value;
}
public string Name { get; }

View File

@@ -1,14 +1,13 @@
namespace Umbraco.Cms.Web.BackOffice.Security
namespace Umbraco.Cms.Web.BackOffice.Security
{
/// <summary>
/// Options used to configure back office external login providers
/// </summary>
public class BackOfficeExternalLoginProviderOptions
{
public BackOfficeExternalLoginProviderOptions(
string buttonStyle, string icon,
string buttonStyle,
string icon,
ExternalSignInAutoLinkOptions autoLinkOptions = null,
bool denyLocalLogin = false,
bool autoRedirectLoginToExternalProvider = false,
@@ -22,18 +21,23 @@
CustomBackOfficeView = customBackOfficeView;
}
public string ButtonStyle { get; }
public string Icon { get; }
public BackOfficeExternalLoginProviderOptions()
{
}
public string ButtonStyle { get; set; } = "btn-openid";
public string Icon { get; set; } = "fa-user";
/// <summary>
/// Options used to control how users can be auto-linked/created/updated based on the external login provider
/// </summary>
public ExternalSignInAutoLinkOptions AutoLinkOptions { get; }
public ExternalSignInAutoLinkOptions AutoLinkOptions { get; set; }
/// <summary>
/// When set to true will disable all local user login functionality
/// </summary>
public bool DenyLocalLogin { get; }
public bool DenyLocalLogin { get; set; }
/// <summary>
/// When specified this will automatically redirect to the OAuth login provider instead of prompting the user to click on the OAuth button first.
@@ -42,7 +46,7 @@
/// This is generally used in conjunction with <see cref="DenyLocalLogin"/>. If more than one OAuth provider specifies this, the last registered
/// provider's redirect settings will win.
/// </remarks>
public bool AutoRedirectLoginToExternalProvider { get; }
public bool AutoRedirectLoginToExternalProvider { get; set; }
/// <summary>
/// A virtual path to a custom angular view that is used to replace the entire UI that renders the external login button that the user interacts with
@@ -51,6 +55,6 @@
/// If this view is specified it is 100% up to the user to render the html responsible for rendering the link/un-link buttons along with showing any errors
/// that occur. This overrides what Umbraco normally does by default.
/// </remarks>
public string CustomBackOfficeView { get; }
public string CustomBackOfficeView { get; set; }
}
}

View File

@@ -1,4 +1,4 @@
using System;
using System;
using Microsoft.Extensions.DependencyInjection;
namespace Umbraco.Cms.Web.BackOffice.Security
@@ -21,9 +21,9 @@ namespace Umbraco.Cms.Web.BackOffice.Security
/// <param name="loginProviderOptions"></param>
/// <param name="build"></param>
/// <returns></returns>
public BackOfficeExternalLoginsBuilder AddBackOfficeLogin(
BackOfficeExternalLoginProviderOptions loginProviderOptions,
Action<BackOfficeAuthenticationBuilder> build)
public BackOfficeExternalLoginsBuilder AddBackOfficeLogin(
Action<BackOfficeAuthenticationBuilder> build,
Action<BackOfficeExternalLoginProviderOptions> loginProviderOptions = null)
{
build(new BackOfficeAuthenticationBuilder(_services, loginProviderOptions));
return this;