mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-05 18:48:41 +00:00
refactor(backend:auth): improve banned user checks and simplify claim retrieval
1,000 commits in 270 days 🎉
This commit is contained in:
@@ -25,7 +25,11 @@ namespace SharpbinV3.Server.Authorization
|
||||
if (_requireAuth && !isAuthenticated)
|
||||
return Task.CompletedTask;
|
||||
|
||||
var isBanned = context.User.Claims.Any(c => (c.Type == ClaimTypes.Role || c.Type == "role") && c.Value == "403");
|
||||
var isBanned = bool.TryParse(context.User.Claims.FirstOrDefault(c => c.Type == "is_banned")?.Value, out var parsedIsBanned)
|
||||
&& parsedIsBanned;
|
||||
|
||||
if (!isBanned && httpContext?.GetJwtUser()?.IsBanned == true)
|
||||
isBanned = true;
|
||||
|
||||
if (httpContext?.GetApiKeyFromContext()?.User?.IsBanned == true)
|
||||
isBanned = true;
|
||||
|
||||
@@ -42,7 +42,8 @@ namespace SharpbinV3.Server.DTOs
|
||||
}
|
||||
if (Roles != null && Roles.HasValue)
|
||||
{
|
||||
if (!Enum.IsDefined(typeof(Role), Roles.Value))
|
||||
int validBits = Enum.GetValues(typeof(Role)).Cast<Role>().Aggregate(0, (acc, role) => acc | (int)role);
|
||||
if ((((int)Roles.Value) & validBits) != (int)Roles.Value || Roles.Value == 0)
|
||||
results.Add(new ValidationResult($"Invalid role: {Roles.Value}.", [nameof(Roles)]));
|
||||
}
|
||||
return results;
|
||||
|
||||
@@ -19,6 +19,18 @@ namespace SharpbinV3.Server.Extensions
|
||||
|
||||
private const string ApiKeyContextKey = "ApiKey";
|
||||
|
||||
private static string? GetClaimValue(IEnumerable<Claim> claims, params string[] claimTypes)
|
||||
{
|
||||
foreach (var claimType in claimTypes)
|
||||
{
|
||||
var value = claims.FirstOrDefault(c => c.Type == claimType)?.Value;
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
return value;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string? GetApiKey(this HttpContext context)
|
||||
{
|
||||
var apiKeyHeader = context.Request.Headers["X-API-Key"].FirstOrDefault();
|
||||
@@ -54,23 +66,20 @@ namespace SharpbinV3.Server.Extensions
|
||||
if (context.User == null || !context.User.Identity?.IsAuthenticated == true)
|
||||
return null;
|
||||
|
||||
var claims = context.User.Claims;
|
||||
var claims = context.User.Claims.ToArray();
|
||||
|
||||
var uuidClaim = claims.FirstOrDefault(c => c.Type == "uuid")?.Value;
|
||||
var uuidClaim = GetClaimValue(claims, "uuid");
|
||||
if (string.IsNullOrEmpty(uuidClaim) || !Guid.TryParse(uuidClaim, out var uuid))
|
||||
return null;
|
||||
|
||||
var username = claims.FirstOrDefault(c => c.Type == "username")?.Value ?? "";
|
||||
var displayName = claims.FirstOrDefault(c => c.Type == "displayname")?.Value ?? "";
|
||||
var totpEnabled = claims.FirstOrDefault(c => c.Type == "totp_enabled")?.Value == "True";
|
||||
var isBanned = claims.FirstOrDefault(c => c.Type == "is_banned")?.Value == "True";
|
||||
Role roles = claims.FirstOrDefault(c => c.Type == "roles")?.Value switch
|
||||
{
|
||||
string r when int.TryParse(r, out var roleInt) => (Role)roleInt,
|
||||
_ => Role.User,
|
||||
};
|
||||
var username = GetClaimValue(claims, "username") ?? "";
|
||||
var displayName = GetClaimValue(claims, "displayname") ?? "";
|
||||
var totpEnabled = bool.TryParse(GetClaimValue(claims, "totp_enabled"), out var parsedTotpEnabled) && parsedTotpEnabled;
|
||||
var isBanned = bool.TryParse(GetClaimValue(claims, "is_banned"), out var parsedIsBanned) && parsedIsBanned;
|
||||
var rolesClaim = GetClaimValue(claims, "roles", "role", ClaimTypes.Role);
|
||||
var roles = int.TryParse(rolesClaim, out var roleInt) ? (Role)roleInt : Role.User;
|
||||
|
||||
var expClaim = claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Exp)?.Value;
|
||||
var expClaim = GetClaimValue(claims, JwtRegisteredClaimNames.Exp, ClaimTypes.Expiration, "exp");
|
||||
var expires =
|
||||
expClaim != null && long.TryParse(expClaim, out var expUnix)
|
||||
? DateTimeOffset.FromUnixTimeSeconds(expUnix).UtcDateTime
|
||||
|
||||
Reference in New Issue
Block a user