mirror of
https://github.com/streetwriters/notesnook-sync-server.git
synced 2026-02-12 19:22:45 +00:00
identity: validate mfa method against user's mfa methods
This commit is contained in:
@@ -34,6 +34,7 @@ namespace Streetwriters.Identity.Interfaces
|
||||
string GetSecondaryMethod(User user);
|
||||
Task<int> GetRemainingValidCodesAsync(User user);
|
||||
bool IsValidMFAMethod(string method);
|
||||
bool IsValidMFAMethod(string method, User user);
|
||||
Task<AuthenticatorDetails> GetAuthenticatorDetailsAsync(User user, IClient client);
|
||||
Task SendOTPAsync(User user, IClient client, MultiFactorSetupForm form, bool isSetup = false);
|
||||
Task<bool> VerifyOTPAsync(User user, string code, string method);
|
||||
|
||||
@@ -121,6 +121,13 @@ namespace Streetwriters.Identity.Services
|
||||
return method == MFAMethods.App || method == MFAMethods.Email || method == MFAMethods.SMS || method == MFAMethods.RecoveryCode;
|
||||
}
|
||||
|
||||
public bool IsValidMFAMethod(string method, User user)
|
||||
{
|
||||
var primaryMethod = GetPrimaryMethod(user);
|
||||
var secondaryMethod = GetSecondaryMethod(user);
|
||||
return IsValidMFAMethod(method) && (method == primaryMethod || method == secondaryMethod);
|
||||
}
|
||||
|
||||
private Task RemoveSecondaryMethodAsync(User user)
|
||||
{
|
||||
return this.RemoveClaimAsync(user, MFAService.SECONDARY_METHOD_CLAIM);
|
||||
@@ -157,8 +164,8 @@ namespace Streetwriters.Identity.Services
|
||||
public async Task SendOTPAsync(User user, IClient client, MultiFactorSetupForm form, bool isSetup = false)
|
||||
{
|
||||
var method = form.Type;
|
||||
if (method != MFAMethods.Email && method != MFAMethods.SMS) throw new Exception("Invalid method.");
|
||||
|
||||
if ((method != MFAMethods.Email && method != MFAMethods.SMS) || !IsValidMFAMethod(method, user))
|
||||
throw new Exception("Invalid method.");
|
||||
|
||||
if (isSetup &&
|
||||
method == MFAMethods.SMS &&
|
||||
|
||||
@@ -101,7 +101,7 @@ namespace Streetwriters.Identity.Validation
|
||||
context.Result.ErrorDescription = "Please provide a valid multi-factor authentication code.";
|
||||
|
||||
if (string.IsNullOrEmpty(mfaCode)) return;
|
||||
if (string.IsNullOrEmpty(mfaMethod) || !MFAService.IsValidMFAMethod(mfaMethod))
|
||||
if (string.IsNullOrEmpty(mfaMethod) || !MFAService.IsValidMFAMethod(mfaMethod, user))
|
||||
{
|
||||
context.Result.ErrorDescription = "Please provide a valid multi-factor authentication method.";
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user