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:
@@ -1,13 +1,13 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
using SharpbinV3.Server.DTOs.Auth;
|
||||
using SharpbinV3.Server.Settings;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Bcrypt = BCrypt.Net.BCrypt;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
@@ -21,7 +21,13 @@ namespace SharpbinV3.Server.Services
|
||||
public int[] Roles { get; set; } = [];
|
||||
public DateTime Expires { get; set; }
|
||||
}
|
||||
public sealed class AuthService(AppDbContext db, IOptions<JWTSettings> jwtOptions, IOptions<AuthSettings> authOptions, ILogger<AuthService> logger)
|
||||
|
||||
public sealed class AuthService(
|
||||
AppDbContext db,
|
||||
IOptions<JWTSettings> jwtOptions,
|
||||
IOptions<AuthSettings> authOptions,
|
||||
ILogger<AuthService> logger
|
||||
)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly JWTSettings _jwtSettings = jwtOptions.Value;
|
||||
@@ -36,7 +42,7 @@ namespace SharpbinV3.Server.Services
|
||||
PasswordHash = Bcrypt.HashPassword(password),
|
||||
Email = email,
|
||||
DisplayName = displayName,
|
||||
UUID = Guid.CreateVersion7()
|
||||
UUID = Guid.CreateVersion7(),
|
||||
};
|
||||
|
||||
if (_authSettings.First_User_Admin && !await _db.Users.AnyAsync())
|
||||
@@ -46,6 +52,7 @@ namespace SharpbinV3.Server.Services
|
||||
await _db.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task<CreateJWT> GenerateJWTToken(User user)
|
||||
{
|
||||
var jwtHandler = new JwtSecurityTokenHandler();
|
||||
@@ -58,7 +65,7 @@ namespace SharpbinV3.Server.Services
|
||||
new("username", user.Username),
|
||||
new("displayname", user.DisplayName ?? ""),
|
||||
new("totp_enabled", totpEnabled.ToString()),
|
||||
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
|
||||
new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
|
||||
};
|
||||
|
||||
if (user.Roles != null)
|
||||
@@ -77,10 +84,7 @@ namespace SharpbinV3.Server.Services
|
||||
Issuer = _jwtSettings.Issuer,
|
||||
Audience = _jwtSettings.Audience,
|
||||
Expires = DateTime.UtcNow.AddMinutes(15),
|
||||
SigningCredentials = new SigningCredentials(
|
||||
new SymmetricSecurityKey(key),
|
||||
SecurityAlgorithms.HmacSha256Signature
|
||||
)
|
||||
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature),
|
||||
};
|
||||
|
||||
var token = jwtHandler.CreateToken(descriptor);
|
||||
@@ -97,7 +101,7 @@ namespace SharpbinV3.Server.Services
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
ExpiresAt = DateTimeOffset.UtcNow.AddMonths(6),
|
||||
Used = false,
|
||||
Revoked = false
|
||||
Revoked = false,
|
||||
};
|
||||
|
||||
await _db.RefreshTokens.AddAsync(refreshToken);
|
||||
@@ -107,7 +111,7 @@ namespace SharpbinV3.Server.Services
|
||||
{
|
||||
Token = jwtToken,
|
||||
Success = true,
|
||||
RefreshToken = rawRefreshToken
|
||||
RefreshToken = rawRefreshToken,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -118,38 +122,39 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
try
|
||||
{
|
||||
var principal = jwtHandler.ValidateToken(token, new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = _jwtSettings.Issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = _jwtSettings.Audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateLifetime = false,
|
||||
RequireSignedTokens = true,
|
||||
ValidAlgorithms = [SecurityAlgorithms.HmacSha256]
|
||||
}, out SecurityToken validatedToken);
|
||||
var principal = jwtHandler.ValidateToken(
|
||||
token,
|
||||
new TokenValidationParameters
|
||||
{
|
||||
ValidateIssuerSigningKey = true,
|
||||
IssuerSigningKey = new SymmetricSecurityKey(key),
|
||||
ValidateIssuer = true,
|
||||
ValidIssuer = _jwtSettings.Issuer,
|
||||
ValidateAudience = true,
|
||||
ValidAudience = _jwtSettings.Audience,
|
||||
ClockSkew = TimeSpan.Zero,
|
||||
ValidateLifetime = false,
|
||||
RequireSignedTokens = true,
|
||||
ValidAlgorithms = [SecurityAlgorithms.HmacSha256],
|
||||
},
|
||||
out SecurityToken validatedToken
|
||||
);
|
||||
|
||||
var jti = principal.Claims.First(c => c.Type == JwtRegisteredClaimNames.Jti).Value;
|
||||
|
||||
var tokenHash = Utilities.ComputeSha256(refreshToken);
|
||||
|
||||
var storedToken = await _db.RefreshTokens
|
||||
.FirstOrDefaultAsync(rt => rt.TokenHash == tokenHash);
|
||||
var storedToken = await _db.RefreshTokens.FirstOrDefaultAsync(rt => rt.TokenHash == tokenHash);
|
||||
|
||||
if (storedToken == null
|
||||
if (
|
||||
storedToken == null
|
||||
|| storedToken.Used
|
||||
|| storedToken.Revoked
|
||||
|| storedToken.ExpiresAt < DateTimeOffset.UtcNow
|
||||
|| storedToken.JwtId != jti)
|
||||
|| storedToken.JwtId != jti
|
||||
)
|
||||
{
|
||||
return new CreateJWT
|
||||
{
|
||||
Success = false,
|
||||
Errors = ["Invalid or expired refresh token."]
|
||||
};
|
||||
return new CreateJWT { Success = false, Errors = ["Invalid or expired refresh token."] };
|
||||
}
|
||||
|
||||
storedToken.Used = true;
|
||||
@@ -172,7 +177,6 @@ namespace SharpbinV3.Server.Services
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<User> UpdateUser(User user)
|
||||
{
|
||||
var existingUser = await _db.Users.FirstOrDefaultAsync(u => u.UUID == user.UUID) ?? throw new Exception("User not found");
|
||||
@@ -186,6 +190,7 @@ namespace SharpbinV3.Server.Services
|
||||
await _db.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
|
||||
public async Task UpdateLoginTime(User user)
|
||||
{
|
||||
var existingUser = await _db.Users.FirstOrDefaultAsync(u => u.UUID == user.UUID) ?? throw new Exception("User not found");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System.IO.Compression;
|
||||
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public class CompressionService : ICompressionService
|
||||
@@ -21,11 +20,13 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public byte[] Compress(string data)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(data);
|
||||
return Compress(System.Text.Encoding.UTF8.GetBytes(data));
|
||||
}
|
||||
|
||||
public byte[] Decompress(byte[] compressedData)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(compressedData);
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace SharpbinV3.Server.Services
|
||||
public class PasteCleanUpService(IServiceProvider serviceProvider, ILogger<PasteCleanUpService> logger) : BackgroundService
|
||||
{
|
||||
private readonly TimeSpan _period = TimeSpan.FromMinutes(5);
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(_period);
|
||||
|
||||
@@ -9,12 +9,7 @@ using SharpbinV3.Server.Settings;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class PasteService(
|
||||
AppDbContext db,
|
||||
ICompressionService cs,
|
||||
IMemoryCache cache,
|
||||
IOptions<PasteSettings> options
|
||||
)
|
||||
public sealed class PasteService(AppDbContext db, ICompressionService cs, IMemoryCache cache, IOptions<PasteSettings> options)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
private readonly ICompressionService _cs = cs;
|
||||
@@ -114,10 +109,7 @@ namespace SharpbinV3.Server.Services
|
||||
return result > 0;
|
||||
}
|
||||
|
||||
public async Task<(Paste? paste, bool alreadyExists)> RecordView(
|
||||
Paste paste,
|
||||
HttpContext context
|
||||
)
|
||||
public async Task<(Paste? paste, bool alreadyExists)> RecordView(Paste paste, HttpContext context)
|
||||
{
|
||||
if (paste == null)
|
||||
return (null, false);
|
||||
@@ -132,21 +124,11 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
string viewerHash;
|
||||
if (viewerUser != null)
|
||||
viewerHash = Utilities.ComputeHmacSha256(
|
||||
_pasteSettings.View_HMAC_Secret,
|
||||
viewerUser.UUID.ToString()
|
||||
);
|
||||
viewerHash = Utilities.ComputeHmacSha256(_pasteSettings.View_HMAC_Secret, viewerUser.UUID.ToString());
|
||||
else
|
||||
viewerHash = Utilities.ComputeHmacSha256(
|
||||
_pasteSettings.View_HMAC_Secret,
|
||||
viewerIp + viewerUserAgent
|
||||
);
|
||||
viewerHash = Utilities.ComputeHmacSha256(_pasteSettings.View_HMAC_Secret, viewerIp + viewerUserAgent);
|
||||
|
||||
if (
|
||||
await _db.PasteViews.AnyAsync(pv =>
|
||||
pv.PastePID == paste.PID && pv.ViewerHash == viewerHash
|
||||
)
|
||||
)
|
||||
if (await _db.PasteViews.AnyAsync(pv => pv.PastePID == paste.PID && pv.ViewerHash == viewerHash))
|
||||
return (null, true);
|
||||
|
||||
_db.PasteViews.Add(
|
||||
@@ -176,9 +158,7 @@ namespace SharpbinV3.Server.Services
|
||||
async entry =>
|
||||
{
|
||||
entry.SetSlidingExpiration(TimeSpan.FromMinutes(10));
|
||||
return await _db
|
||||
.Pastes.Include(p => p.User)
|
||||
.FirstOrDefaultAsync(p => p.ID == id);
|
||||
return await _db.Pastes.Include(p => p.User).FirstOrDefaultAsync(p => p.ID == id);
|
||||
}
|
||||
);
|
||||
return paste ?? null;
|
||||
@@ -201,12 +181,7 @@ namespace SharpbinV3.Server.Services
|
||||
if (publicOnly)
|
||||
query = query.Where(p => p.Visibility == 0);
|
||||
|
||||
return await query
|
||||
.OrderByDescending(p => p.PID)
|
||||
.Skip(offset)
|
||||
.Take(count)
|
||||
.Include(p => p.User)
|
||||
.ToListAsync();
|
||||
return await query.OrderByDescending(p => p.PID).Skip(offset).Take(count).Include(p => p.User).ToListAsync();
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -3,21 +3,16 @@ using SharpbinV3.Server.Data;
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class PasteViewCleanUpService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<PasteViewCleanUpService> logger
|
||||
) : BackgroundService
|
||||
public sealed class PasteViewCleanUpService(IServiceProvider serviceProvider, ILogger<PasteViewCleanUpService> logger) : BackgroundService
|
||||
{
|
||||
private static readonly TimeSpan Period = TimeSpan.FromHours(1);
|
||||
private static readonly long RetentionMs =
|
||||
(long)TimeSpan.FromDays(7).TotalMilliseconds;
|
||||
private static readonly long RetentionMs = (long)TimeSpan.FromDays(7).TotalMilliseconds;
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
using var timer = new PeriodicTimer(Period);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested &&
|
||||
await timer.WaitForNextTickAsync(stoppingToken))
|
||||
while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken))
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -35,16 +30,12 @@ namespace SharpbinV3.Server.Services
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
|
||||
var cutoff =
|
||||
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - RetentionMs;
|
||||
var cutoff = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - RetentionMs;
|
||||
|
||||
var deleted = await db.PasteViews
|
||||
.Where(v => v.ViewedAt < cutoff)
|
||||
.ExecuteDeleteAsync(token);
|
||||
var deleted = await db.PasteViews.Where(v => v.ViewedAt < cutoff).ExecuteDeleteAsync(token);
|
||||
|
||||
if (deleted > 0)
|
||||
logger.LogInformation("Deleted {Count} stale paste views", deleted);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ namespace SharpbinV3.Server.Services
|
||||
public class ReportService(AppDbContext db)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
private sealed record UserInfo(string Username, string? DisplayName);
|
||||
|
||||
public async Task<Report> CreateReport(Guid reporterUUID, ReportTargetType targetType, Guid userUUID, ReportType type, string description)
|
||||
{
|
||||
var report = new Report
|
||||
@@ -17,12 +19,13 @@ namespace SharpbinV3.Server.Services
|
||||
TargetType = targetType,
|
||||
UserUUID = userUUID,
|
||||
Type = type,
|
||||
Description = description
|
||||
Description = description,
|
||||
};
|
||||
await _db.Reports.AddAsync(report);
|
||||
await _db.SaveChangesAsync();
|
||||
return report;
|
||||
}
|
||||
|
||||
public async Task<Report> CreateReport(Guid reporterUUID, ReportTargetType targetType, int? pastePID, ReportType type, string description)
|
||||
{
|
||||
var report = new Report
|
||||
@@ -53,10 +56,7 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
if (query.PasteId != null)
|
||||
{
|
||||
q = q.Where(r => _db.Pastes
|
||||
.Where(p => p.ID == query.PasteId)
|
||||
.Select(p => (int?)p.PID)
|
||||
.Contains(r.PastePID));
|
||||
q = q.Where(r => _db.Pastes.Where(p => p.ID == query.PasteId).Select(p => (int?)p.PID).Contains(r.PastePID));
|
||||
}
|
||||
|
||||
if (query.TargetType != null)
|
||||
@@ -71,25 +71,16 @@ namespace SharpbinV3.Server.Services
|
||||
return q;
|
||||
}
|
||||
|
||||
|
||||
public async Task<List<ReportResponseDto>> GetPasteReports(ReportQuery query)
|
||||
{
|
||||
query = query with
|
||||
{
|
||||
TargetType = ReportTargetType.Paste,
|
||||
UserUUID = null
|
||||
};
|
||||
query = query with { TargetType = ReportTargetType.Paste, UserUUID = null };
|
||||
|
||||
return await GetReports(query);
|
||||
}
|
||||
|
||||
public async Task<List<ReportResponseDto>> GetUserReports(ReportQuery query)
|
||||
{
|
||||
query = query with
|
||||
{
|
||||
TargetType = ReportTargetType.User,
|
||||
PastePID = null
|
||||
};
|
||||
query = query with { TargetType = ReportTargetType.User, PastePID = null };
|
||||
|
||||
return await GetReports(query);
|
||||
}
|
||||
@@ -109,27 +100,22 @@ namespace SharpbinV3.Server.Services
|
||||
CreatedAt = r.CreatedAt,
|
||||
UpdatedAt = r.UpdatedAt,
|
||||
ReporterUUID = r.ReporterUUID,
|
||||
ReporterUsername = _db.Users
|
||||
.Where(u => u.UUID == r.ReporterUUID)
|
||||
.Select(u => u.Username)
|
||||
.FirstOrDefault(),
|
||||
ReporterDisplayName = _db.Users
|
||||
.Where(u => u.UUID == r.ReporterUUID)
|
||||
.Select(u => u.DisplayName)
|
||||
.FirstOrDefault(),
|
||||
ReporterUsername = _db.Users.Where(u => u.UUID == r.ReporterUUID).Select(u => u.Username).FirstOrDefault(),
|
||||
ReporterDisplayName = _db.Users.Where(u => u.UUID == r.ReporterUUID).Select(u => u.DisplayName).FirstOrDefault(),
|
||||
TargetType = r.TargetType,
|
||||
PasteId = r.Paste != null ? r.Paste.ID : null,
|
||||
PasteTitle = r.Paste != null ? r.Paste.Title : null,
|
||||
UserUUID = r.UserUUID,
|
||||
TargetUsername = r.User != null ? r.User.Username : null,
|
||||
TargetDisplayName = r.User != null ? r.User.DisplayName : null
|
||||
TargetDisplayName = r.User != null ? r.User.DisplayName : null,
|
||||
})
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<(string? Username, string? DisplayName)> GetReporterInfo(Guid reporterUUID)
|
||||
{
|
||||
var info = await _db.Users.AsNoTracking()
|
||||
var info = await _db
|
||||
.Users.AsNoTracking()
|
||||
.Where(u => u.UUID == reporterUUID)
|
||||
.Select(u => new UserInfo(u.Username, u.DisplayName))
|
||||
.FirstOrDefaultAsync();
|
||||
@@ -137,25 +123,19 @@ namespace SharpbinV3.Server.Services
|
||||
return info is null ? (null, null) : (info.Username, info.DisplayName);
|
||||
}
|
||||
|
||||
|
||||
public async Task<int> GetReportCount(ReportTargetType targetType)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Where(r => r.TargetType == targetType)
|
||||
.CountAsync();
|
||||
return await _db.Reports.Where(r => r.TargetType == targetType).CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetReportCountByReporter(Guid reporterUUID)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Where(r => r.ReporterUUID == reporterUUID)
|
||||
.CountAsync();
|
||||
return await _db.Reports.Where(r => r.ReporterUUID == reporterUUID).CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetReportCount()
|
||||
{
|
||||
return await _db.Reports
|
||||
.CountAsync();
|
||||
return await _db.Reports.CountAsync();
|
||||
}
|
||||
|
||||
public async Task<int> GetReportCount(ReportQuery query)
|
||||
@@ -165,10 +145,7 @@ namespace SharpbinV3.Server.Services
|
||||
|
||||
public async Task<Report?> GetReportByID(int reportID)
|
||||
{
|
||||
return await _db.Reports
|
||||
.Include(r => r.User)
|
||||
.Include(r => r.Paste)
|
||||
.FirstOrDefaultAsync(r => r.ReportID == reportID);
|
||||
return await _db.Reports.Include(r => r.User).Include(r => r.Paste).FirstOrDefaultAsync(r => r.ReportID == reportID);
|
||||
}
|
||||
|
||||
public async Task<Report> UpdateReport(Report report)
|
||||
@@ -177,6 +154,7 @@ namespace SharpbinV3.Server.Services
|
||||
await _db.SaveChangesAsync();
|
||||
return report;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteReport(Report report)
|
||||
{
|
||||
_db.Reports.Remove(report);
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
using SharpbinV3.Server.Data;
|
||||
using SharpbinV3.Server.Data.Entities;
|
||||
|
||||
|
||||
namespace SharpbinV3.Server.Services
|
||||
{
|
||||
public sealed class UserService(AppDbContext db)
|
||||
{
|
||||
private readonly AppDbContext _db = db;
|
||||
|
||||
public Task<User?> GetByUID(int uid, bool withPastes = false)
|
||||
{
|
||||
IQueryable<User> query = _db.Users;
|
||||
|
||||
@@ -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