mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +00:00
style: format via csharpier
This commit is contained in:
@@ -6,7 +6,8 @@ using SharpbinV3.Server.Data;
|
||||
|
||||
namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
{
|
||||
public sealed class TotpVerificationProvider(AppDbContext db, IDataProtectionProvider dataProtectionProvider, IMemoryCache cache) : IVerificationProvider
|
||||
public sealed class TotpVerificationProvider(AppDbContext db, IDataProtectionProvider dataProtectionProvider, IMemoryCache cache)
|
||||
: IVerificationProvider
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly IDataProtector _protector = dataProtectionProvider.CreateProtector("TotpSecret-v1");
|
||||
@@ -17,11 +18,14 @@ 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;
|
||||
if (userTotp is null)
|
||||
return false;
|
||||
|
||||
var secret = _protector.Unprotect(userTotp.EncryptedSecret);
|
||||
if (!VerifyCode(secret, ctx.Code, out var step))
|
||||
@@ -39,7 +43,14 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
|
||||
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}";
|
||||
@@ -47,6 +58,7 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SharpbinV3.Server.DTOs.Auth;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
{
|
||||
@@ -11,7 +11,8 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
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)
|
||||
{
|
||||
@@ -20,11 +21,7 @@ namespace SharpbinV3.Server.Services.Verification.Providers
|
||||
if (string.IsNullOrEmpty(ctx.Token))
|
||||
return false;
|
||||
|
||||
var parameters = new Dictionary<string, string>
|
||||
{
|
||||
{ "secret", _authSettings.CF_Turnstile_SecretKey },
|
||||
{ "response", ctx.Token }
|
||||
};
|
||||
var parameters = new Dictionary<string, string> { { "secret", _authSettings.CF_Turnstile_SecretKey }, { "response", ctx.Token } };
|
||||
|
||||
if (!string.IsNullOrEmpty(ctx.Ip))
|
||||
parameters.Add("remoteip", ctx.Ip);
|
||||
|
||||
@@ -8,5 +8,4 @@
|
||||
public Guid? UserUUID { get; init; }
|
||||
public string? Code { get; init; }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,10 +8,7 @@ namespace SharpbinV3.Server.Services.Verification
|
||||
|
||||
private IVerificationProvider? GetActiveProvider()
|
||||
{
|
||||
return _providers
|
||||
.Where(p => p.IsConfigured)
|
||||
.OrderByDescending(p => p.Priority)
|
||||
.FirstOrDefault();
|
||||
return _providers.Where(p => p.IsConfigured).OrderByDescending(p => p.Priority).FirstOrDefault();
|
||||
}
|
||||
|
||||
public async Task<bool> VerifyAsync(VerificationContext context, IVerificationProvider? provider = null)
|
||||
|
||||
Reference in New Issue
Block a user