mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
@@ -12,11 +12,7 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
private readonly IDataProtector _protector;
|
||||
private readonly IMemoryCache _cache;
|
||||
|
||||
public TotpVerificationProvider(
|
||||
AppDbContext db,
|
||||
IDataProtectionProvider dataProtectionProvider,
|
||||
IMemoryCache cache
|
||||
)
|
||||
public TotpVerificationProvider(AppDbContext db, IDataProtectionProvider dataProtectionProvider, IMemoryCache cache)
|
||||
{
|
||||
_db = db;
|
||||
_protector = dataProtectionProvider.CreateProtector("TotpSecret-v1");
|
||||
@@ -28,16 +24,11 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
|
||||
public async Task<bool> VerifyAsync(VerificationContext ctx)
|
||||
{
|
||||
if (ctx.UserUUID is null)
|
||||
return false;
|
||||
if (string.IsNullOrWhiteSpace(ctx.Code))
|
||||
return false;
|
||||
if (ctx.UserUUID is null) return false;
|
||||
if (string.IsNullOrWhiteSpace(ctx.Code)) return false;
|
||||
|
||||
var userTotp = await _db.UserTotps.FirstOrDefaultAsync(t =>
|
||||
t.UserUUID == ctx.UserUUID.Value
|
||||
);
|
||||
if (userTotp is null)
|
||||
return false;
|
||||
var userTotp = await _db.UserTotps.FirstOrDefaultAsync(t => t.UserUUID == ctx.UserUUID.Value);
|
||||
if (userTotp is null) return false;
|
||||
|
||||
var secret = _protector.Unprotect(userTotp.EncryptedSecret);
|
||||
if (!VerifyCode(secret, ctx.Code, out var step))
|
||||
@@ -53,58 +44,30 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
|
||||
public string ToBase32(byte[] data) => Base32Encoding.ToString(data);
|
||||
|
||||
public byte[] FromBase32(string base32) =>
|
||||
string.IsNullOrWhiteSpace(base32) ? [] : Base32Encoding.ToBytes(base32);
|
||||
public byte[] FromBase32(string base32) => string.IsNullOrWhiteSpace(base32) ? [] : Base32Encoding.ToBytes(base32);
|
||||
|
||||
public string BuildOtpAuthUri(
|
||||
string issuer,
|
||||
string accountName,
|
||||
string secretBase32,
|
||||
int digits = 6,
|
||||
int period = 30,
|
||||
string algorithm = "SHA1"
|
||||
)
|
||||
public string BuildOtpAuthUri(string issuer, string accountName, string secretBase32, int digits = 6, int period = 30, string algorithm = "SHA1")
|
||||
{
|
||||
var label = Uri.EscapeDataString($"{issuer}:{accountName}");
|
||||
var query =
|
||||
$"secret={secretBase32}&issuer={Uri.EscapeDataString(issuer)}&algorithm={algorithm}&digits={digits}&period={period}";
|
||||
var query = $"secret={secretBase32}&issuer={Uri.EscapeDataString(issuer)}&algorithm={algorithm}&digits={digits}&period={period}";
|
||||
return $"otpauth://totp/{label}?{query}";
|
||||
}
|
||||
|
||||
public byte[] Protect(byte[] secret) => _protector.Protect(secret);
|
||||
|
||||
public byte[] Unprotect(byte[] protectedSecret) => _protector.Unprotect(protectedSecret);
|
||||
|
||||
public bool VerifyWithSecretBase32(
|
||||
string secretBase32,
|
||||
string code,
|
||||
int digits = 6,
|
||||
int period = 30,
|
||||
int window = 1
|
||||
)
|
||||
public bool VerifyWithSecretBase32(string secretBase32, string code, int digits = 6, int period = 30, int window = 1)
|
||||
{
|
||||
var secret = FromBase32(secretBase32);
|
||||
return VerifyCode(secret, code, out _, digits, period, window);
|
||||
}
|
||||
|
||||
private bool VerifyCode(
|
||||
byte[] secret,
|
||||
string code,
|
||||
out long matchedStep,
|
||||
int digits = 6,
|
||||
int period = 30,
|
||||
int window = 1
|
||||
)
|
||||
private bool VerifyCode(byte[] secret, string code, out long matchedStep, int digits = 6, int period = 30, int window = 1)
|
||||
{
|
||||
var totp = new Totp(secret, step: period, mode: OtpHashMode.Sha1, totpSize: digits);
|
||||
return totp.VerifyTotp(
|
||||
code,
|
||||
out matchedStep,
|
||||
new VerificationWindow(previous: window, future: window)
|
||||
);
|
||||
return totp.VerifyTotp(code, out matchedStep, new VerificationWindow(previous: window, future: window));
|
||||
}
|
||||
|
||||
private string ComputeCode(byte[] key, long timestep, int digits) =>
|
||||
new Totp(key, totpSize: digits).ComputeTotp(DateTime.UtcNow);
|
||||
private string ComputeCode(byte[] key, long timestep, int digits) => new Totp(key, totpSize: digits).ComputeTotp(DateTime.UtcNow);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.DTOs.Auth;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
{
|
||||
public sealed class TurnstileVerificationProvider(
|
||||
IOptions<AuthSettings> options,
|
||||
HttpClient httpClient
|
||||
) : IVerificationProvider
|
||||
public sealed class TurnstileVerificationProvider(IOptions<AuthSettings> options, HttpClient httpClient) : IVerificationProvider
|
||||
{
|
||||
private readonly AuthSettings _authSettings = options.Value;
|
||||
private const string SiteverifyUrl =
|
||||
"https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
||||
private const string SiteverifyUrl = "https://challenges.cloudflare.com/turnstile/v0/siteverify";
|
||||
|
||||
public int Priority => 1;
|
||||
public bool IsConfigured =>
|
||||
!string.IsNullOrEmpty(_authSettings.CF_Turnstile_SecretKey)
|
||||
&& !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SiteKey);
|
||||
public bool IsConfigured => !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SecretKey) && !string.IsNullOrEmpty(_authSettings.CF_Turnstile_SiteKey);
|
||||
|
||||
public async Task<bool> VerifyAsync(VerificationContext ctx)
|
||||
{
|
||||
@@ -29,7 +23,7 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
var parameters = new Dictionary<string, string>
|
||||
{
|
||||
{ "secret", _authSettings.CF_Turnstile_SecretKey },
|
||||
{ "response", ctx.Token },
|
||||
{ "response", ctx.Token }
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(ctx.Ip))
|
||||
|
||||
Reference in New Issue
Block a user