🐛 Bug fixes and improvements

This commit is contained in:
2025-05-01 00:47:26 +08:00
parent 758186f674
commit 84a88222bd
13 changed files with 111 additions and 59 deletions

View File

@ -20,8 +20,9 @@ public class AuthController(
{
public class ChallengeRequest
{
[Required] public ChallengePlatform Platform { get; set; }
[Required] [MaxLength(256)] public string Account { get; set; } = string.Empty;
[MaxLength(512)] public string? DeviceId { get; set; }
[Required] [MaxLength(512)] public string DeviceId { get; set; }
public List<string> Audiences { get; set; } = new();
public List<string> Scopes { get; set; } = new();
}
@ -52,6 +53,7 @@ public class AuthController(
Account = account,
ExpiredAt = Instant.FromDateTimeUtc(DateTime.UtcNow.AddHours(1)),
StepTotal = 1,
Platform = request.Platform,
Audiences = request.Audiences,
Scopes = request.Scopes,
IpAddress = ipAddress,
@ -125,6 +127,8 @@ public class AuthController(
}
catch
{
challenge.FailedAttempts++;
await db.SaveChangesAsync();
return BadRequest();
}

View File

@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages;
using NodaTime;
namespace DysonNetwork.Sphere.Auth;
@ -8,6 +9,7 @@ namespace DysonNetwork.Sphere.Auth;
public class Session : ModelBase
{
public Guid Id { get; set; } = Guid.NewGuid();
[MaxLength(1024)] public string? Label { get; set; }
public Instant? LastGrantedAt { get; set; }
public Instant? ExpiredAt { get; set; }
@ -15,6 +17,23 @@ public class Session : ModelBase
[JsonIgnore] public Challenge Challenge { get; set; } = null!;
}
public enum ChallengeType
{
Login,
OAuth
}
public enum ChallengePlatform
{
Unidentified,
Web,
Ios,
Android,
MacOs,
Windows,
Linux
}
public class Challenge : ModelBase
{
public Guid Id { get; set; } = Guid.NewGuid();
@ -22,6 +41,8 @@ public class Challenge : ModelBase
public int StepRemain { get; set; }
public int StepTotal { get; set; }
public int FailedAttempts { get; set; }
public ChallengePlatform Platform { get; set; } = ChallengePlatform.Unidentified;
public ChallengeType Type { get; set; } = ChallengeType.Login;
[Column(TypeName = "jsonb")] public List<long> BlacklistFactors { get; set; } = new();
[Column(TypeName = "jsonb")] public List<string> Audiences { get; set; } = new();
[Column(TypeName = "jsonb")] public List<string> Scopes { get; set; } = new();

View File

@ -7,29 +7,30 @@ public class UserInfoMiddleware(RequestDelegate next, IMemoryCache cache)
{
public async Task InvokeAsync(HttpContext context, AppDatabase db)
{
var userIdClaim = context.User.FindFirst("user_id")?.Value;
if (userIdClaim is not null && long.TryParse(userIdClaim, out var userId))
var sessionIdClaim = context.User.FindFirst("session_id")?.Value;
if (sessionIdClaim is not null && Guid.TryParse(sessionIdClaim, out var sessionId))
{
if (!cache.TryGetValue($"user_{userId}", out Account.Account? user))
if (!cache.TryGetValue($"dyn_auth_{sessionId}", out Session? session))
{
user = await db.Accounts
.Include(e => e.Profile)
.Include(e => e.Profile.Picture)
.Include(e => e.Profile.Background)
.Where(e => e.Id == userId)
session = await db.AuthSessions
.Include(e => e.Challenge)
.Include(e => e.Account)
.Include(e => e.Account.Profile)
.Include(e => e.Account.Profile.Picture)
.Include(e => e.Account.Profile.Background)
.Where(e => e.Id == sessionId)
.FirstOrDefaultAsync();
if (user is not null)
if (session is not null)
{
cache.Set($"user_{userId}", user, TimeSpan.FromMinutes(10));
cache.Set($"dyn_auth_{sessionId}", session, TimeSpan.FromHours(1));
}
}
if (user is not null)
if (session is not null)
{
context.Items["CurrentUser"] = user;
var prefix = user.IsSuperuser ? "super:" : "";
context.Items["CurrentIdentity"] = $"{prefix}{userId}";
context.Items["CurrentUser"] = session.Account;
context.Items["CurrentSession"] = session;
}
}