👔 Send factor code no longer requires hint

This commit is contained in:
2025-08-17 21:20:42 +08:00
parent a0fe8fd0f0
commit 8e8965eb3d
2 changed files with 5 additions and 22 deletions

View File

@@ -335,12 +335,10 @@ public class AccountService(
/// <summary> /// <summary>
/// Send the auth factor verification code to users, for factors like in-app code and email. /// Send the auth factor verification code to users, for factors like in-app code and email.
/// Sometimes it requires a hint, like a part of the user's email address to ensure the user is who own the account.
/// </summary> /// </summary>
/// <param name="account">The owner of the auth factor</param> /// <param name="account">The owner of the auth factor</param>
/// <param name="factor">The auth factor needed to send code</param> /// <param name="factor">The auth factor needed to send code</param>
/// <param name="hint">The part of the contact method for verification</param> public async Task SendFactorCode(Account account, AccountAuthFactor factor)
public async Task SendFactorCode(Account account, AccountAuthFactor factor, string? hint = null)
{ {
var code = new Random().Next(100000, 999999).ToString("000000"); var code = new Random().Next(100000, 999999).ToString("000000");
@@ -369,30 +367,16 @@ public class AccountService(
if (await _GetFactorCode(factor) is not null) if (await _GetFactorCode(factor) is not null)
throw new InvalidOperationException("A factor code has been sent and in active duration."); throw new InvalidOperationException("A factor code has been sent and in active duration.");
ArgumentNullException.ThrowIfNull(hint);
hint = hint.Replace("@", "").Replace(".", "").Replace("+", "").Replace("%", "");
if (string.IsNullOrWhiteSpace(hint))
{
logger.LogWarning(
"Unable to send factor code to #{FactorId} with hint {Hint}, due to invalid hint...",
factor.Id,
hint
);
return;
}
var contact = await db.AccountContacts var contact = await db.AccountContacts
.Where(c => c.Type == AccountContactType.Email) .Where(c => c.Type == AccountContactType.Email)
.Where(c => c.VerifiedAt != null) .Where(c => c.VerifiedAt != null)
.Where(c => EF.Functions.ILike(c.Content, $"%{hint}%")) .Where(c => c.IsPrimary)
.Include(c => c.Account) .Include(c => c.Account)
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (contact is null) if (contact is null)
{ {
logger.LogWarning( logger.LogWarning(
"Unable to send factor code to #{FactorId} with hint {Hint}, due to no contact method found according to hint...", "Unable to send factor code to #{FactorId} with, due to no contact method was found..."
factor.Id,
hint
); );
return; return;
} }

View File

@@ -120,8 +120,7 @@ public class AuthController(
[HttpPost("challenge/{id:guid}/factors/{factorId:guid}")] [HttpPost("challenge/{id:guid}/factors/{factorId:guid}")]
public async Task<ActionResult> RequestFactorCode( public async Task<ActionResult> RequestFactorCode(
[FromRoute] Guid id, [FromRoute] Guid id,
[FromRoute] Guid factorId, [FromRoute] Guid factorId
[FromBody] string? hint
) )
{ {
var challenge = await db.AuthChallenges var challenge = await db.AuthChallenges
@@ -135,7 +134,7 @@ public class AuthController(
try try
{ {
await accounts.SendFactorCode(challenge.Account, factor, hint); await accounts.SendFactorCode(challenge.Account, factor);
} }
catch (Exception ex) catch (Exception ex)
{ {