-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use MinimalAPI for two-factor authentication code request (#16174)
--------- Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
1 parent
8bf9641
commit 4d10d15
Showing
6 changed files
with
258 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/OrchardCore.Modules/OrchardCore.Users/Endpoints/EmailAuthenticator/SendCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Fluid.Values; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Localization; | ||
using OrchardCore.Email; | ||
using OrchardCore.Liquid; | ||
using OrchardCore.Settings; | ||
using OrchardCore.Users.Models; | ||
|
||
namespace OrchardCore.Users.Endpoints.EmailAuthenticator; | ||
|
||
public static class SendCode | ||
{ | ||
public const string RouteName = "EmailAuthenticatorSendCode"; | ||
|
||
public static IEndpointRouteBuilder AddEmailSendCodeEndpoint<T>(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapPost("TwoFactor-Authenticator/EmailSendCode", HandleAsync<T>) | ||
.AllowAnonymous() | ||
.WithName(RouteName) | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
private static async Task<IResult> HandleAsync<T>( | ||
SignInManager<IUser> signInManager, | ||
UserManager<IUser> userManager, | ||
ISiteService siteService, | ||
IEmailService emailService, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder, | ||
IStringLocalizer<T> S) | ||
{ | ||
var user = await signInManager.GetTwoFactorAuthenticationUserAsync(); | ||
var errorMessage = S["The email could not be sent. Please attempt to request the code at a later time."]; | ||
|
||
if (user == null) | ||
{ | ||
return TypedResults.BadRequest(new | ||
{ | ||
success = false, | ||
message = errorMessage.Value, | ||
}); | ||
} | ||
|
||
var settings = (await siteService.GetSiteSettingsAsync()).As<EmailAuthenticatorLoginSettings>(); | ||
var code = await userManager.GenerateTwoFactorTokenAsync(user, TokenOptions.DefaultEmailProvider); | ||
|
||
var to = await userManager.GetEmailAsync(user); | ||
var subject = await GetSubjectAsync(settings, user, code, liquidTemplateManager, htmlEncoder); | ||
var body = await GetBodyAsync(settings, user, code, liquidTemplateManager, htmlEncoder); | ||
var result = await emailService.SendAsync(to, subject, body); | ||
|
||
return TypedResults.Ok(new | ||
{ | ||
success = result.Succeeded, | ||
message = result.Succeeded | ||
? S["A verification code has been sent via email. Please check your email for the code."].Value | ||
: errorMessage.Value, | ||
}); | ||
} | ||
|
||
private static Task<string> GetSubjectAsync( | ||
EmailAuthenticatorLoginSettings settings, | ||
IUser user, | ||
string code, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder) | ||
{ | ||
var message = string.IsNullOrWhiteSpace(settings.Subject) | ||
? EmailAuthenticatorLoginSettings.DefaultSubject | ||
: settings.Subject; | ||
|
||
return GetContentAsync(message, user, code, liquidTemplateManager, htmlEncoder); | ||
} | ||
|
||
private static Task<string> GetBodyAsync( | ||
EmailAuthenticatorLoginSettings settings, | ||
IUser user, | ||
string code, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder) | ||
{ | ||
var message = string.IsNullOrWhiteSpace(settings.Body) | ||
? EmailAuthenticatorLoginSettings.DefaultBody | ||
: settings.Body; | ||
|
||
return GetContentAsync(message, user, code, liquidTemplateManager, htmlEncoder); | ||
} | ||
|
||
private static async Task<string> GetContentAsync( | ||
string message, | ||
IUser user, | ||
string code, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder) | ||
{ | ||
var result = await liquidTemplateManager.RenderHtmlContentAsync(message, htmlEncoder, null, | ||
new Dictionary<string, FluidValue>() | ||
{ | ||
["User"] = new ObjectValue(user), | ||
["Code"] = new StringValue(code), | ||
}); | ||
|
||
using var writer = new StringWriter(); | ||
result.WriteTo(writer, htmlEncoder); | ||
|
||
return writer.ToString(); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/OrchardCore.Modules/OrchardCore.Users/Endpoints/SmsAuthenticator/SendCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Fluid.Values; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Routing; | ||
using Microsoft.Extensions.Localization; | ||
using Microsoft.Extensions.Options; | ||
using OrchardCore.Liquid; | ||
using OrchardCore.Settings; | ||
using OrchardCore.Sms; | ||
using OrchardCore.Users.Models; | ||
|
||
namespace OrchardCore.Users.Endpoints.SmsAuthenticator; | ||
|
||
public static class SendCode | ||
{ | ||
public const string RouteName = "SmsAuthenticatorSendCode"; | ||
|
||
public static IEndpointRouteBuilder AddSmsSendCodeEndpoint<T>(this IEndpointRouteBuilder builder) | ||
{ | ||
builder.MapPost("TwoFactor-Authenticator/SmsSendCode", HandleAsync<T>) | ||
.AllowAnonymous() | ||
.WithName(RouteName) | ||
.DisableAntiforgery(); | ||
|
||
return builder; | ||
} | ||
|
||
private static async Task<IResult> HandleAsync<T>( | ||
SignInManager<IUser> signInManager, | ||
UserManager<IUser> userManager, | ||
ISiteService siteService, | ||
ISmsService smsService, | ||
IOptions<IdentityOptions> identityOptions, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder, | ||
IStringLocalizer<T> S) | ||
{ | ||
var user = await signInManager.GetTwoFactorAuthenticationUserAsync(); | ||
var errorMessage = S["The SMS message could not be sent. Please attempt to request the code at a later time."]; | ||
|
||
if (user == null) | ||
{ | ||
return TypedResults.BadRequest(new | ||
{ | ||
success = false, | ||
message = errorMessage.Value, | ||
}); | ||
} | ||
|
||
var settings = (await siteService.GetSiteSettingsAsync()).As<SmsAuthenticatorLoginSettings>(); | ||
var code = await userManager.GenerateTwoFactorTokenAsync(user, identityOptions.Value.Tokens.ChangePhoneNumberTokenProvider); | ||
|
||
var message = new SmsMessage() | ||
{ | ||
To = await userManager.GetPhoneNumberAsync(user), | ||
Body = await GetBodyAsync(settings, user, code, liquidTemplateManager, htmlEncoder), | ||
}; | ||
|
||
var result = await smsService.SendAsync(message); | ||
|
||
return TypedResults.Ok(new | ||
{ | ||
success = result.Succeeded, | ||
message = result.Succeeded ? S["A verification code has been sent to your phone number. Please check your device for the code."].Value | ||
: errorMessage.Value, | ||
}); | ||
} | ||
|
||
private static Task<string> GetBodyAsync( | ||
SmsAuthenticatorLoginSettings settings, | ||
IUser user, | ||
string code, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder) | ||
{ | ||
var message = string.IsNullOrWhiteSpace(settings.Body) | ||
? EmailAuthenticatorLoginSettings.DefaultBody | ||
: settings.Body; | ||
|
||
return GetContentAsync(message, user, code, liquidTemplateManager, htmlEncoder); | ||
} | ||
|
||
private static async Task<string> GetContentAsync( | ||
string message, | ||
IUser user, | ||
string code, | ||
ILiquidTemplateManager liquidTemplateManager, | ||
HtmlEncoder htmlEncoder) | ||
{ | ||
var result = await liquidTemplateManager.RenderHtmlContentAsync(message, htmlEncoder, null, | ||
new Dictionary<string, FluidValue>() | ||
{ | ||
["User"] = new ObjectValue(user), | ||
["Code"] = new StringValue(code), | ||
}); | ||
|
||
using var writer = new StringWriter(); | ||
result.WriteTo(writer, htmlEncoder); | ||
|
||
return writer.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 4 additions & 11 deletions
15
src/OrchardCore.Modules/OrchardCore.Users/Views/EmailAuthenticatorValidation.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 4 additions & 11 deletions
15
src/OrchardCore.Modules/OrchardCore.Users/Views/SmsAuthenticatorValidation.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters