mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Implements authentication system
This commit is contained in:
1 parent
38829d64ff
commit
a6dcf65d1c
103 files changed
+2983
-454
No files matched your search
@@ -0,0 +1,34 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using OtpNet;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Infrastructure.Core.Authentication;
|
||||
|
||||
/// <summary>
|
||||
/// Service for generating and validating TOTP tokens.
|
||||
/// </summary>
|
||||
public class TotpTokenGenerator : ITotpTokenGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// Generates a new TOTP secret for a user.
|
||||
/// </summary>
|
||||
/// <returns>The generated TOTP secret.</returns>
|
||||
public byte[] GenerateSecret()
|
||||
{
|
||||
return KeyGeneration.GenerateRandomKey(); // generate a random key for TOTP
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates a TOTP token against the stored secret.
|
||||
/// </summary>
|
||||
/// <param name="secret">The user's TOTP secret.</param>
|
||||
/// <param name="token">The token to validate.</param>
|
||||
/// <returns><see langword="true"/> if valid, <see langword="false"/> otherwise.</returns>
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user