mirror of
https://github.com/whoswhip/Sharpbin.git
synced 2026-12-06 02:48:43 +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)
|
if (_requireAuth && !isAuthenticated)
|
||||||
return Task.CompletedTask;
|
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)
|
if (httpContext?.GetApiKeyFromContext()?.User?.IsBanned == true)
|
||||||
isBanned = true;
|
isBanned = true;
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ namespace SharpbinV3.Server.DTOs
|
|||||||
}
|
}
|
||||||
if (Roles != null && Roles.HasValue)
|
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)]));
|
results.Add(new ValidationResult($"Invalid role: {Roles.Value}.", [nameof(Roles)]));
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
|
|||||||
@@ -19,6 +19,18 @@ namespace SharpbinV3.Server.Extensions
|
|||||||
|
|
||||||
private const string ApiKeyContextKey = "ApiKey";
|
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)
|
public static string? GetApiKey(this HttpContext context)
|
||||||
{
|
{
|
||||||
var apiKeyHeader = context.Request.Headers["X-API-Key"].FirstOrDefault();
|
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)
|
if (context.User == null || !context.User.Identity?.IsAuthenticated == true)
|
||||||
return null;
|
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))
|
if (string.IsNullOrEmpty(uuidClaim) || !Guid.TryParse(uuidClaim, out var uuid))
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var username = claims.FirstOrDefault(c => c.Type == "username")?.Value ?? "";
|
var username = GetClaimValue(claims, "username") ?? "";
|
||||||
var displayName = claims.FirstOrDefault(c => c.Type == "displayname")?.Value ?? "";
|
var displayName = GetClaimValue(claims, "displayname") ?? "";
|
||||||
var totpEnabled = claims.FirstOrDefault(c => c.Type == "totp_enabled")?.Value == "True";
|
var totpEnabled = bool.TryParse(GetClaimValue(claims, "totp_enabled"), out var parsedTotpEnabled) && parsedTotpEnabled;
|
||||||
var isBanned = claims.FirstOrDefault(c => c.Type == "is_banned")?.Value == "True";
|
var isBanned = bool.TryParse(GetClaimValue(claims, "is_banned"), out var parsedIsBanned) && parsedIsBanned;
|
||||||
Role roles = claims.FirstOrDefault(c => c.Type == "roles")?.Value switch
|
var rolesClaim = GetClaimValue(claims, "roles", "role", ClaimTypes.Role);
|
||||||
{
|
var roles = int.TryParse(rolesClaim, out var roleInt) ? (Role)roleInt : Role.User;
|
||||||
string r when int.TryParse(r, 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 =
|
var expires =
|
||||||
expClaim != null && long.TryParse(expClaim, out var expUnix)
|
expClaim != null && long.TryParse(expClaim, out var expUnix)
|
||||||
? DateTimeOffset.FromUnixTimeSeconds(expUnix).UtcDateTime
|
? DateTimeOffset.FromUnixTimeSeconds(expUnix).UtcDateTime
|
||||||
|
|||||||
Reference in New Issue
Block a user