#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Authentication;
using OtpNet;
#endregion
namespace Lumina.Infrastructure.Core.Authentication;
///
/// Service for generating and validating TOTP tokens.
///
public class TotpTokenGenerator : ITotpTokenGenerator
{
///
/// Generates a new TOTP secret for a user.
///
/// The generated TOTP secret.
public byte[] GenerateSecret()
{
return KeyGeneration.GenerateRandomKey(); // Generate a random key for TOTP.
}
///
/// Validates a TOTP token against the stored secret.
///
/// The user's TOTP secret.
/// The token to validate.
/// if valid, otherwise.
public bool ValidateToken(byte[] secret, string token)
{
Totp totp = new(secret);
// Verify the TOTP token, allowing for slight time drifts between server and client.
return totp.VerifyTotp(token, out _, VerificationWindow.RfcSpecifiedNetworkDelay);
}
}