Token fix
This commit is contained in:
+22
-18
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Server.Data;
|
||||
using Server.Services.Auth;
|
||||
using VoidScavengers.Services;
|
||||
|
||||
namespace VoidScavengers.Api;
|
||||
|
||||
@@ -73,13 +74,13 @@ public class AdminController : ControllerBase
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(username))
|
||||
query = query.Where(a => a.Username.Contains(username));
|
||||
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(email))
|
||||
query = query.Where(a => a.Email != null && a.Email.Contains(email));
|
||||
|
||||
|
||||
if (role.HasValue)
|
||||
query = query.Where(a => a.Role == role.Value);
|
||||
|
||||
|
||||
if (isBanned.HasValue)
|
||||
query = query.Where(a => a.IsBanned == isBanned.Value);
|
||||
|
||||
@@ -131,7 +132,7 @@ public class AdminController : ControllerBase
|
||||
|
||||
// Permission checks
|
||||
var caller = auth.Account!;
|
||||
|
||||
|
||||
// Can't modify your own role
|
||||
if (caller.Id == accountId)
|
||||
return BadRequest(new { error = "Cannot modify your own role" });
|
||||
@@ -152,11 +153,12 @@ public class AdminController : ControllerBase
|
||||
targetAccount.Role = newRole;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Ok(new {
|
||||
message = "Role updated",
|
||||
accountId,
|
||||
oldRole = oldRole.ToString(),
|
||||
newRole = newRole.ToString()
|
||||
return Ok(new
|
||||
{
|
||||
message = "Role updated",
|
||||
accountId,
|
||||
oldRole = oldRole.ToString(),
|
||||
newRole = newRole.ToString()
|
||||
});
|
||||
}
|
||||
|
||||
@@ -184,15 +186,15 @@ public class AdminController : ControllerBase
|
||||
return BadRequest(new { error = "Cannot ban yourself" });
|
||||
|
||||
targetAccount.IsBanned = true;
|
||||
targetAccount.BannedUntil = request.Duration.HasValue
|
||||
? DateTime.UtcNow.Add(request.Duration.Value)
|
||||
targetAccount.BannedUntil = request.Duration.HasValue
|
||||
? DateTime.UtcNow.Add(request.Duration.Value)
|
||||
: null; // Permanent ban
|
||||
|
||||
// Revoke all active sessions
|
||||
var activeSessions = await _db.Sessions
|
||||
.Where(s => s.AccountId == accountId && s.RevokedAt == null)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
foreach (var session in activeSessions)
|
||||
{
|
||||
session.RevokedAt = DateTime.UtcNow;
|
||||
@@ -200,9 +202,10 @@ public class AdminController : ControllerBase
|
||||
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Ok(new {
|
||||
message = "Account banned",
|
||||
accountId,
|
||||
return Ok(new
|
||||
{
|
||||
message = "Account banned",
|
||||
accountId,
|
||||
bannedUntil = targetAccount.BannedUntil,
|
||||
permanent = !targetAccount.BannedUntil.HasValue,
|
||||
reason = request.Reason,
|
||||
@@ -265,7 +268,7 @@ public class AdminController : ControllerBase
|
||||
return BadRequest(new { error = "Admin accounts already exist. Use normal promotion flow." });
|
||||
|
||||
// Verify bootstrap key from environment
|
||||
var bootstrapKey = "ThisIsThePassw0rd!";//Environment.GetEnvironmentVariable("BOOTSTRAP_ADMIN_KEY");
|
||||
var bootstrapKey = Environment.GetEnvironmentVariable("BOOTSTRAP_ADMIN_KEY");
|
||||
if (string.IsNullOrEmpty(bootstrapKey))
|
||||
return BadRequest(new { error = "Bootstrap key not configured on server" });
|
||||
|
||||
@@ -283,8 +286,9 @@ public class AdminController : ControllerBase
|
||||
account.Role = AccountRole.SuperAdmin;
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return Ok(new {
|
||||
message = "SuperAdmin created successfully",
|
||||
return Ok(new
|
||||
{
|
||||
message = "SuperAdmin created successfully",
|
||||
accountId = account.Id,
|
||||
username = account.Username
|
||||
});
|
||||
|
||||
@@ -42,20 +42,20 @@ public class AuthApiController : ControllerBase
|
||||
|
||||
if (account.IsBanned)
|
||||
{
|
||||
var banMsg = account.BannedUntil.HasValue
|
||||
? $"Account banned until {account.BannedUntil.Value:u}"
|
||||
var banMsg = account.BannedUntil.HasValue
|
||||
? $"Account banned until {account.BannedUntil.Value:u}"
|
||||
: "Account permanently banned";
|
||||
return Unauthorized(new { error = banMsg });
|
||||
}
|
||||
|
||||
// Generate session token
|
||||
var token = GenerateToken();
|
||||
var tokenHash = HashToken(token);
|
||||
var tokenHash = HashTokenToString(token);
|
||||
|
||||
var session = new Session
|
||||
{
|
||||
AccountId = account.Id,
|
||||
TokenHash = tokenHash.ToString(),
|
||||
TokenHash = tokenHash,
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
ExpiresAt = DateTime.UtcNow.AddDays(7), // 7 day expiry for API tokens
|
||||
CreatedIp = HttpContext.Connection.RemoteIpAddress?.ToString(),
|
||||
@@ -85,9 +85,9 @@ public class AuthApiController : ControllerBase
|
||||
if (string.IsNullOrEmpty(token))
|
||||
return BadRequest(new { error = "No token provided" });
|
||||
|
||||
var tokenHash = HashToken(token);
|
||||
var tokenHash = HashTokenToString(token);
|
||||
var session = await _db.Sessions
|
||||
.FirstOrDefaultAsync(s => s.TokenHash == tokenHash.ToString() && s.RevokedAt == null);
|
||||
.FirstOrDefaultAsync(s => s.TokenHash == tokenHash && s.RevokedAt == null);
|
||||
|
||||
if (session == null)
|
||||
return NotFound(new { error = "Session not found" });
|
||||
@@ -106,10 +106,11 @@ public class AuthApiController : ControllerBase
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
|
||||
private static byte[] HashToken(string token)
|
||||
private static string HashTokenToString(string token)
|
||||
{
|
||||
using var sha = System.Security.Cryptography.SHA256.Create();
|
||||
return sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(token));
|
||||
var hashBytes = sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(token));
|
||||
return Convert.ToBase64String(hashBytes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Server.Data;
|
||||
using Server.Services.Auth;
|
||||
using VoidScavengers.Game;
|
||||
using Server.Services;
|
||||
using VoidScavengers.Services;
|
||||
|
||||
namespace VoidScavengers.Api;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user