V10: Build warnings in Web.Website (#12332)
* add new rule to globalconfig * Fix warnings in Web.Website * Fix more warnings in Web.Website * Fix more build warnings in Web.Website * Fix more warnings in Web.Website * Fix tests * Fix proper constructor call * Fix not being able to run project * Fix Obsolete method Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
This commit is contained in:
@@ -1,75 +1,75 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Web.Common.Security;
|
||||
using Umbraco.Extensions;
|
||||
using Constants = Umbraco.Cms.Core.Constants;
|
||||
|
||||
namespace Umbraco.Cms.Web.Website.Security
|
||||
namespace Umbraco.Cms.Web.Website.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Custom <see cref="AuthenticationBuilder" /> used to associate external logins with umbraco external login options
|
||||
/// </summary>
|
||||
public class MemberAuthenticationBuilder : AuthenticationBuilder
|
||||
{
|
||||
private readonly Action<MemberExternalLoginProviderOptions> _loginProviderOptions;
|
||||
|
||||
public MemberAuthenticationBuilder(
|
||||
IServiceCollection services,
|
||||
Action<MemberExternalLoginProviderOptions>? loginProviderOptions = null)
|
||||
: base(services)
|
||||
=> _loginProviderOptions = loginProviderOptions ?? (x => { });
|
||||
|
||||
public string SchemeForMembers(string scheme)
|
||||
=> scheme.EnsureStartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix);
|
||||
|
||||
/// <summary>
|
||||
/// Custom <see cref="AuthenticationBuilder"/> used to associate external logins with umbraco external login options
|
||||
/// Overridden to track the final authenticationScheme being registered for the external login
|
||||
/// </summary>
|
||||
public class MemberAuthenticationBuilder : AuthenticationBuilder
|
||||
/// <typeparam name="TOptions"></typeparam>
|
||||
/// <typeparam name="THandler"></typeparam>
|
||||
/// <param name="authenticationScheme"></param>
|
||||
/// <param name="displayName"></param>
|
||||
/// <param name="configureOptions"></param>
|
||||
/// <returns></returns>
|
||||
public override AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(
|
||||
string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
|
||||
{
|
||||
private readonly Action<MemberExternalLoginProviderOptions> _loginProviderOptions;
|
||||
|
||||
public MemberAuthenticationBuilder(
|
||||
IServiceCollection services,
|
||||
Action<MemberExternalLoginProviderOptions>? loginProviderOptions = null)
|
||||
: base(services)
|
||||
=> _loginProviderOptions = loginProviderOptions ?? (x => { });
|
||||
|
||||
public string? SchemeForMembers(string scheme)
|
||||
=> scheme?.EnsureStartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix);
|
||||
|
||||
/// <summary>
|
||||
/// Overridden to track the final authenticationScheme being registered for the external login
|
||||
/// </summary>
|
||||
/// <typeparam name="TOptions"></typeparam>
|
||||
/// <typeparam name="THandler"></typeparam>
|
||||
/// <param name="authenticationScheme"></param>
|
||||
/// <param name="displayName"></param>
|
||||
/// <param name="configureOptions"></param>
|
||||
/// <returns></returns>
|
||||
public override AuthenticationBuilder AddRemoteScheme<TOptions, THandler>(string authenticationScheme, string? displayName, Action<TOptions>? configureOptions)
|
||||
// Validate that the prefix is set
|
||||
if (!authenticationScheme.StartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix))
|
||||
{
|
||||
// Validate that the prefix is set
|
||||
if (!authenticationScheme.StartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix))
|
||||
{
|
||||
throw new InvalidOperationException($"The {nameof(authenticationScheme)} is not prefixed with {Constants.Security.MemberExternalAuthenticationTypePrefix}. The scheme must be created with a call to the method {nameof(SchemeForMembers)}");
|
||||
}
|
||||
|
||||
// add our login provider to the container along with a custom options configuration
|
||||
Services.Configure(authenticationScheme, _loginProviderOptions);
|
||||
base.Services.AddSingleton(services =>
|
||||
{
|
||||
return new MemberExternalLoginProvider(
|
||||
authenticationScheme,
|
||||
services.GetRequiredService<IOptionsMonitor<MemberExternalLoginProviderOptions>>());
|
||||
});
|
||||
Services.TryAddEnumerable(ServiceDescriptor.Singleton<IPostConfigureOptions<TOptions>, EnsureMemberScheme<TOptions>>());
|
||||
|
||||
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
|
||||
throw new InvalidOperationException(
|
||||
$"The {nameof(authenticationScheme)} is not prefixed with {Constants.Security.MemberExternalAuthenticationTypePrefix}. The scheme must be created with a call to the method {nameof(SchemeForMembers)}");
|
||||
}
|
||||
|
||||
// Ensures that the sign in scheme is always the Umbraco member external type
|
||||
private class EnsureMemberScheme<TOptions> : IPostConfigureOptions<TOptions> where TOptions : RemoteAuthenticationOptions
|
||||
// add our login provider to the container along with a custom options configuration
|
||||
Services.Configure(authenticationScheme, _loginProviderOptions);
|
||||
Services.AddSingleton(services =>
|
||||
{
|
||||
public void PostConfigure(string name, TOptions options)
|
||||
{
|
||||
if (!name.StartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix))
|
||||
{
|
||||
return;
|
||||
}
|
||||
return new MemberExternalLoginProvider(
|
||||
authenticationScheme,
|
||||
services.GetRequiredService<IOptionsMonitor<MemberExternalLoginProviderOptions>>());
|
||||
});
|
||||
Services.TryAddEnumerable(ServiceDescriptor
|
||||
.Singleton<IPostConfigureOptions<TOptions>, EnsureMemberScheme<TOptions>>());
|
||||
|
||||
options.SignInScheme = IdentityConstants.ExternalScheme;
|
||||
}
|
||||
}
|
||||
return base.AddRemoteScheme<TOptions, THandler>(authenticationScheme, displayName, configureOptions);
|
||||
}
|
||||
|
||||
// Ensures that the sign in scheme is always the Umbraco member external type
|
||||
private class EnsureMemberScheme<TOptions> : IPostConfigureOptions<TOptions>
|
||||
where TOptions : RemoteAuthenticationOptions
|
||||
{
|
||||
public void PostConfigure(string name, TOptions options)
|
||||
{
|
||||
if (!name.StartsWith(Constants.Security.MemberExternalAuthenticationTypePrefix))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
options.SignInScheme = IdentityConstants.ExternalScheme;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,28 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Umbraco.Cms.Web.Common.Security;
|
||||
|
||||
namespace Umbraco.Cms.Web.Website.Security
|
||||
namespace Umbraco.Cms.Web.Website.Security;
|
||||
|
||||
/// <summary>
|
||||
/// Used to add back office login providers
|
||||
/// </summary>
|
||||
public class MemberExternalLoginsBuilder
|
||||
{
|
||||
private readonly IServiceCollection _services;
|
||||
|
||||
public MemberExternalLoginsBuilder(IServiceCollection services) => _services = services;
|
||||
|
||||
/// <summary>
|
||||
/// Used to add back office login providers
|
||||
/// Add a back office login provider with options
|
||||
/// </summary>
|
||||
public class MemberExternalLoginsBuilder
|
||||
/// <param name="loginProviderOptions"></param>
|
||||
/// <param name="build"></param>
|
||||
/// <returns></returns>
|
||||
public MemberExternalLoginsBuilder AddMemberLogin(
|
||||
Action<MemberAuthenticationBuilder> build,
|
||||
Action<MemberExternalLoginProviderOptions>? loginProviderOptions = null)
|
||||
{
|
||||
public MemberExternalLoginsBuilder(IServiceCollection services)
|
||||
{
|
||||
_services = services;
|
||||
}
|
||||
|
||||
private readonly IServiceCollection _services;
|
||||
|
||||
/// <summary>
|
||||
/// Add a back office login provider with options
|
||||
/// </summary>
|
||||
/// <param name="loginProviderOptions"></param>
|
||||
/// <param name="build"></param>
|
||||
/// <returns></returns>
|
||||
public MemberExternalLoginsBuilder AddMemberLogin(
|
||||
Action<MemberAuthenticationBuilder> build,
|
||||
Action<MemberExternalLoginProviderOptions>? loginProviderOptions = null)
|
||||
{
|
||||
build(new MemberAuthenticationBuilder(_services, loginProviderOptions));
|
||||
return this;
|
||||
}
|
||||
build(new MemberAuthenticationBuilder(_services, loginProviderOptions));
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user