Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure Token provider from the corresponding provider #15627

Merged
merged 33 commits into from
Apr 12, 2024
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
20a097a
Configure Token provider from the corresponding provider
MikeAlhayek Mar 29, 2024
a0e10f8
rename
MikeAlhayek Mar 29, 2024
574acbd
Update Startup.cs
MikeAlhayek Apr 7, 2024
1876438
Update Startup.cs
MikeAlhayek Apr 7, 2024
067d7fa
Merge branch 'main' into ma/configure-token-providers
hishamco Apr 7, 2024
d04b1a6
Add a way to configure the email-confirmation, password-reset, change…
MikeAlhayek Apr 7, 2024
b44ec21
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 7, 2024
73a9631
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 8, 2024
462def1
Use 15 mins for email change
MikeAlhayek Apr 8, 2024
70b53cf
Fix the TwoFactor provider
MikeAlhayek Apr 8, 2024
d1b3242
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 8, 2024
aecad0f
update comments
MikeAlhayek Apr 8, 2024
2b09441
Rename
MikeAlhayek Apr 8, 2024
fbcb3e8
Update 1.9.0.md
MikeAlhayek Apr 9, 2024
8c7b73c
Update 1.9.0.md
MikeAlhayek Apr 9, 2024
16a4cae
Update TwoFactorEmailTokenProvider.cs
MikeAlhayek Apr 9, 2024
eadcc90
Update TwoFactorEmailTokenProvider.cs
MikeAlhayek Apr 9, 2024
a2782e1
Update TwoFactorEmailTokenProvider.cs
MikeAlhayek Apr 9, 2024
119befd
Fix build
MikeAlhayek Apr 9, 2024
9941023
Use 3 mins by default for TOTP
MikeAlhayek Apr 9, 2024
9f66cda
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 9, 2024
0c76454
Fix build
MikeAlhayek Apr 9, 2024
6ffca4a
Use IClock
MikeAlhayek Apr 9, 2024
812d56d
cleanup
MikeAlhayek Apr 9, 2024
e284ca0
Merge branch 'main' into ma/configure-token-providers
hishamco Apr 9, 2024
f5f20f5
Add a test
MikeAlhayek Apr 9, 2024
cfd9c53
cleanup
MikeAlhayek Apr 9, 2024
77a7b0b
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 9, 2024
45b8c30
Use the default RFC6238 implementation
MikeAlhayek Apr 10, 2024
ffcda7e
Fix services. The issue is generating numeric tokens
MikeAlhayek Apr 10, 2024
1a8a0b9
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 11, 2024
eebfa80
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 12, 2024
5e51c0e
Merge branch 'main' into ma/configure-token-providers
MikeAlhayek Apr 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix services. The issue is generating numeric tokens
MikeAlhayek committed Apr 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ffcda7e8c806f0e5d48cb7829c057af737c51eaa
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Users/AdminMenu.cs
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ public Task BuildNavigationAsync(string name, NavigationBuilder builder)
}
}

[Feature("OrchardCore.Users.Registration")]
[Feature(UserConstants.Features.UserRegistration)]
public class RegistrationAdminMenu : INavigationProvider
{
private static readonly RouteValueDictionary _routeValues = new()
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@

namespace OrchardCore.Users.AuditTrail.Registration
{
[RequireFeatures("OrchardCore.Users.AuditTrail", "OrchardCore.Users.Registration")]
[RequireFeatures("OrchardCore.Users.AuditTrail", UserConstants.Features.UserRegistration)]
public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Environment.Shell;
using OrchardCore.Modules;
using OrchardCore.Mvc.Core.Utilities;
using OrchardCore.Settings;
@@ -37,10 +38,12 @@ public class AccountController : AccountBaseController
private readonly ISiteService _siteService;
private readonly IEnumerable<ILoginFormEvent> _accountEvents;
private readonly IDataProtectionProvider _dataProtectionProvider;
private readonly IShellFeaturesManager _shellFeaturesManager;
private readonly INotifier _notifier;
private readonly IClock _clock;
private readonly IDistributedCache _distributedCache;
private readonly IEnumerable<IExternalLoginEventHandler> _externalLoginHandlers;

protected readonly IHtmlLocalizer H;
protected readonly IStringLocalizer S;

@@ -57,6 +60,7 @@ public AccountController(
IClock clock,
IDistributedCache distributedCache,
IDataProtectionProvider dataProtectionProvider,
IShellFeaturesManager shellFeaturesManager,
IEnumerable<IExternalLoginEventHandler> externalLoginHandlers)
{
_signInManager = signInManager;
@@ -69,6 +73,7 @@ public AccountController(
_clock = clock;
_distributedCache = distributedCache;
_dataProtectionProvider = dataProtectionProvider;
_shellFeaturesManager = shellFeaturesManager;
_externalLoginHandlers = externalLoginHandlers;

H = htmlLocalizer;
@@ -845,6 +850,14 @@ private bool AddUserEnabledError(IUser user)

private async Task<bool> AddConfirmEmailErrorAsync(IUser user)
{
var registrationFeatureIsAvailable = (await _shellFeaturesManager.GetAvailableFeaturesAsync())
.Any(feature => feature.Id == UserConstants.Features.UserRegistration);

if (!registrationFeatureIsAvailable)
{
return false;
}

var registrationSettings = (await _siteService.GetSiteSettingsAsync()).As<RegistrationSettings>();
if (registrationSettings.UsersMustValidateEmail)
{
Original file line number Diff line number Diff line change
@@ -25,7 +25,8 @@ namespace OrchardCore.Users.Controllers;
public class AuthenticatorAppController : TwoFactorAuthenticationBaseController
{
private const string AuthenticatorUriFormat = "otpauth://totp/{0}:{1}?secret={2}&digits={3}&issuer={0}";
private readonly TokenOptions _tokenOptions;

private readonly IdentityOptions _identityOptions;
private readonly UrlEncoder _urlEncoder;
private readonly ShellSettings _shellSettings;

@@ -36,7 +37,7 @@ public AuthenticatorAppController(
IHtmlLocalizer<AccountController> htmlLocalizer,
IStringLocalizer<AccountController> stringLocalizer,
IOptions<TwoFactorOptions> twoFactorOptions,
IOptions<TokenOptions> tokenOptions,
IOptions<IdentityOptions> identityOptions,
INotifier notifier,
IDistributedCache distributedCache,
UrlEncoder urlEncoder,
@@ -53,7 +54,7 @@ public AuthenticatorAppController(
stringLocalizer,
twoFactorOptions)
{
_tokenOptions = tokenOptions.Value;
_identityOptions = identityOptions.Value;
_urlEncoder = urlEncoder;
_shellSettings = shellSettings;
}
@@ -90,7 +91,7 @@ public async Task<IActionResult> Index(EnableAuthenticatorViewModel model)
return View(model);
}

var isValid = await UserManager.VerifyTwoFactorTokenAsync(user, _tokenOptions.AuthenticatorTokenProvider, StripToken(model.Code));
var isValid = await UserManager.VerifyTwoFactorTokenAsync(user, _identityOptions.Tokens.AuthenticatorTokenProvider, StripToken(model.Code));

if (!isValid)
{
Original file line number Diff line number Diff line change
@@ -26,7 +26,6 @@ namespace OrchardCore.Users.Controllers;
[Authorize, Feature(UserConstants.Features.EmailAuthenticator)]
public class EmailAuthenticatorController : TwoFactorAuthenticationBaseController
{
private readonly TokenOptions _tokenOptions;
private readonly IUserService _userService;
private readonly IEmailService _emailService;
private readonly ILiquidTemplateManager _liquidTemplateManager;
@@ -41,7 +40,6 @@ public EmailAuthenticatorController(
IOptions<TwoFactorOptions> twoFactorOptions,
INotifier notifier,
IDistributedCache distributedCache,
IOptions<TokenOptions> tokenOptions,
IUserService userService,
IEmailService emailService,
ILiquidTemplateManager liquidTemplateManager,
@@ -58,7 +56,6 @@ public EmailAuthenticatorController(
stringLocalizer,
twoFactorOptions)
{
_tokenOptions = tokenOptions.Value;
_userService = userService;
_emailService = emailService;
_liquidTemplateManager = liquidTemplateManager;
@@ -97,7 +94,6 @@ public async Task<IActionResult> RequestCode()
return RedirectToTwoFactorIndex();
}

var t = _tokenOptions;
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

var settings = (await SiteService.GetSiteSettingsAsync()).As<EmailAuthenticatorLoginSettings>();
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@

namespace OrchardCore.Users.Controllers
{
[Feature("OrchardCore.Users.Registration")]
[Feature(UserConstants.Features.UserRegistration)]
public class RegistrationController : Controller
{
private readonly UserManager<IUser> _userManager;
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ namespace OrchardCore.Users.Controllers;
[Authorize, Feature(UserConstants.Features.SmsAuthenticator)]
public class SmsAuthenticatorController : TwoFactorAuthenticationBaseController
{
private readonly TokenOptions _tokenOptions;
private readonly IdentityOptions _identityOptions;
private readonly IUserService _userService;
private readonly ISmsService _smsService;
private readonly ILiquidTemplateManager _liquidTemplateManager;
@@ -42,7 +42,7 @@ public SmsAuthenticatorController(
IHtmlLocalizer<AccountController> htmlLocalizer,
IStringLocalizer<AccountController> stringLocalizer,
IOptions<TwoFactorOptions> twoFactorOptions,
IOptions<TokenOptions> tokenOptions,
IOptions<IdentityOptions> identityOptions,
INotifier notifier,
IDistributedCache distributedCache,
IUserService userService,
@@ -62,7 +62,7 @@ public SmsAuthenticatorController(
stringLocalizer,
twoFactorOptions)
{
_tokenOptions = tokenOptions.Value;
_identityOptions = identityOptions.Value;
_userService = userService;
_smsService = smsService;
_liquidTemplateManager = liquidTemplateManager;
@@ -223,7 +223,7 @@ public async Task<IActionResult> SendCode()
}

var settings = (await SiteService.GetSiteSettingsAsync()).As<SmsAuthenticatorLoginSettings>();
var code = await UserManager.GenerateTwoFactorTokenAsync(user, _tokenOptions.ChangePhoneNumberTokenProvider);
var code = await UserManager.GenerateTwoFactorTokenAsync(user, _identityOptions.Tokens.ChangePhoneNumberTokenProvider);

var message = new SmsMessage()
{
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@

namespace OrchardCore.Users.Drivers
{
[Feature("OrchardCore.Users.Registration")]
[Feature(UserConstants.Features.UserRegistration)]
public class RegistrationSettingsDisplayDriver : SectionDisplayDriver<ISite, RegistrationSettings>
{
public const string GroupId = "userRegistration";
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Users/Manifest.cs
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@
)]

[assembly: Feature(
Id = "OrchardCore.Users.Registration",
Id = UserConstants.Features.UserRegistration,
Name = "Users Registration",
Description = "The registration feature allows external users to sign up to the site and ask to confirm their email.",
Dependencies =
26 changes: 10 additions & 16 deletions src/OrchardCore.Modules/OrchardCore.Users/Startup.cs
Original file line number Diff line number Diff line change
@@ -286,11 +286,9 @@ public class EmailConfirmationStartup : StartupBase
{
public override void ConfigureServices(IServiceCollection services)
{
services.AddOptions<EmailConfirmationTokenProviderOptions>();

services.AddTransient<IConfigureOptions<TokenOptions>, EmailConfirmationTokenOptionsConfigurations>()
.AddTransient<IConfigureOptions<IdentityOptions>, EmailConfirmationIdentityOptionsConfigurations>()
.AddTransient<EmailConfirmationTokenProvider>();
services.AddTransient<IConfigureOptions<IdentityOptions>, EmailConfirmationIdentityOptionsConfigurations>()
.AddTransient<EmailConfirmationTokenProvider>()
.AddOptions<EmailConfirmationTokenProviderOptions>();
}
}

@@ -320,11 +318,9 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro

public override void ConfigureServices(IServiceCollection services)
{
services.AddOptions<ChangeEmailTokenProviderOptions>();

services.AddTransient<IConfigureOptions<TokenOptions>, ChangeEmailTokenOptionsConfigurations>()
.AddTransient<IConfigureOptions<IdentityOptions>, ChangeEmailIdentityOptionsConfigurations>()
.TryAddTransient<ChangeEmailTokenProvider>();
services.AddTransient<IConfigureOptions<IdentityOptions>, ChangeEmailIdentityOptionsConfigurations>()
.AddTransient<ChangeEmailTokenProvider>()
.AddOptions<ChangeEmailTokenProviderOptions>();

services.Configure<TemplateOptions>(o =>
{
@@ -347,7 +343,7 @@ public override void ConfigureServices(IServiceCollection services)
}
}

[Feature("OrchardCore.Users.Registration")]
[Feature(UserConstants.Features.UserRegistration)]
public class RegistrationStartup : StartupBase
{
private const string RegisterPath = nameof(RegistrationController.Register);
@@ -391,7 +387,7 @@ public override void ConfigureServices(IServiceCollection services)
}
}

[Feature("OrchardCore.Users.Registration")]
[Feature(UserConstants.Features.UserRegistration)]
[RequireFeatures("OrchardCore.Deployment")]
public class RegistrationDeploymentStartup : StartupBase
{
@@ -440,11 +436,9 @@ public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder ro

public override void ConfigureServices(IServiceCollection services)
{
services.AddOptions<PasswordResetTokenProviderOptions>();

services.AddTransient<IConfigureOptions<IdentityOptions>, PasswordResetIdentityOptionsConfigurations>()
.AddTransient<IConfigureOptions<TokenOptions>, PasswordResetTokenOptionsConfigurations>()
.TryAddTransient<PasswordResetTokenProvider>();
.AddTransient<PasswordResetTokenProvider>()
.AddOptions<PasswordResetTokenProviderOptions>();

services.Configure<TemplateOptions>(o =>
{
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using OrchardCore.Users.Models;

namespace OrchardCore.Users.Services;

public sealed class ChangeEmailIdentityOptionsConfigurations : IConfigureOptions<IdentityOptions>
{
private readonly TokenOptions _tokenOptions;
private readonly ChangeEmailTokenProviderOptions _tokenOptions;

public ChangeEmailIdentityOptionsConfigurations(IOptions<TokenOptions> tokenOptions)
public ChangeEmailIdentityOptionsConfigurations(IOptions<ChangeEmailTokenProviderOptions> tokenOptions)
{
_tokenOptions = tokenOptions.Value;
}

public void Configure(IdentityOptions options)
{
options.Tokens.ProviderMap.TryAdd(_tokenOptions.ChangeEmailTokenProvider, new TokenProviderDescriptor(typeof(ChangeEmailTokenProvider)));
options.Tokens.ChangeEmailTokenProvider = _tokenOptions.Name;
options.Tokens.ProviderMap[_tokenOptions.Name] = new TokenProviderDescriptor(typeof(ChangeEmailTokenProvider));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using OrchardCore.Users.Models;

namespace OrchardCore.Users.Services;

public sealed class EmailConfirmationIdentityOptionsConfigurations : IConfigureOptions<IdentityOptions>
{
private readonly TokenOptions _tokenOptions;
private readonly EmailConfirmationTokenProviderOptions _tokenOptions;

public EmailConfirmationIdentityOptionsConfigurations(IOptions<TokenOptions> tokenOptions)
public EmailConfirmationIdentityOptionsConfigurations(IOptions<EmailConfirmationTokenProviderOptions> tokenOptions)
{
_tokenOptions = tokenOptions.Value;
}

public void Configure(IdentityOptions options)
{
options.Tokens.ProviderMap[_tokenOptions.EmailConfirmationTokenProvider] = new TokenProviderDescriptor(typeof(EmailConfirmationTokenProvider));
options.Tokens.EmailConfirmationTokenProvider = _tokenOptions.Name;
options.Tokens.ProviderMap[_tokenOptions.Name] = new TokenProviderDescriptor(typeof(EmailConfirmationTokenProvider));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using OrchardCore.Users.Models;

namespace OrchardCore.Users.Services;

public sealed class PasswordResetIdentityOptionsConfigurations : IConfigureOptions<IdentityOptions>
{
private readonly TokenOptions _tokenOptions;
private readonly PasswordResetTokenProviderOptions _tokenOptions;

public PasswordResetIdentityOptionsConfigurations(IOptions<TokenOptions> tokenOptions)
public PasswordResetIdentityOptionsConfigurations(IOptions<PasswordResetTokenProviderOptions> tokenOptions)
{
_tokenOptions = tokenOptions.Value;
}

public void Configure(IdentityOptions options)
{
options.Tokens.ProviderMap[_tokenOptions.PasswordResetTokenProvider] = new TokenProviderDescriptor(typeof(PasswordResetTokenProvider));
options.Tokens.PasswordResetTokenProvider = _tokenOptions.Name;
options.Tokens.ProviderMap[_tokenOptions.Name] = new TokenProviderDescriptor(typeof(PasswordResetTokenProvider));
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions src/OrchardCore/OrchardCore.Users.Core/UserConstants.cs
Original file line number Diff line number Diff line change
@@ -17,5 +17,7 @@ public class Features
public const string SmsAuthenticator = "OrchardCore.Users.2FA.Sms";

public const string UserEmailConfirmation = "OrchardCore.Users.EmailConfirmation";

public const string UserRegistration = "OrchardCore.Users.Registration";
}
}