♻️ Refactored grpc part of shared project

This commit is contained in:
2025-07-06 22:58:34 +08:00
parent 65450e8511
commit 4b220e7ed7
5 changed files with 111 additions and 28 deletions

View File

@ -6,33 +6,21 @@ using Microsoft.EntityFrameworkCore;
using NodaTime;
using System.Text.Json;
using DysonNetwork.Shared.Models;
using Challenge = DysonNetwork.Sphere.Auth.Proto.Challenge;
using Session = DysonNetwork.Sphere.Auth.Proto.Session;
namespace DysonNetwork.Sphere.Auth;
public class AuthGrpcService : DysonNetwork.Sphere.Auth.Proto.AuthService.AuthServiceBase
public class AuthGrpcService(AppDatabase db, AccountService accounts, AuthService auth)
: DysonNetwork.Sphere.Auth.Proto.AuthService.AuthServiceBase
{
private readonly AppDatabase _db;
private readonly AccountService _accounts;
private readonly AuthService _auth;
public AuthGrpcService(AppDatabase db, AccountService accounts, AuthService auth)
{
_db = db;
_accounts = accounts;
_auth = auth;
}
public override async Task<LoginResponse> Login(LoginRequest request, ServerCallContext context)
{
var account = await _accounts.LookupAccount(request.Username);
var account = await accounts.LookupAccount(request.Username);
if (account == null)
{
throw new RpcException(new Grpc.Core.Status(StatusCode.NotFound, "Account not found."));
}
var factor = await _db.AccountAuthFactors.FirstOrDefaultAsync(f => f.AccountId == account.Id && f.Type == AccountAuthFactorType.Password);
var factor = await db.AccountAuthFactors.FirstOrDefaultAsync(f => f.AccountId == account.Id && f.Type == AccountAuthFactorType.Password);
if (factor == null || !factor.VerifyPassword(request.Password))
{
throw new RpcException(new Grpc.Core.Status(StatusCode.Unauthenticated, "Invalid credentials."));
@ -46,10 +34,10 @@ public class AuthGrpcService : DysonNetwork.Sphere.Auth.Proto.AuthService.AuthSe
Challenge = new Challenge() // Create a dummy challenge
};
_db.AuthSessions.Add(session);
await _db.SaveChangesAsync();
db.AuthSessions.Add(session);
await db.SaveChangesAsync();
var token = _auth.CreateToken(session);
var token = auth.CreateToken(session);
return new LoginResponse
{
@ -60,9 +48,9 @@ public class AuthGrpcService : DysonNetwork.Sphere.Auth.Proto.AuthService.AuthSe
public override async Task<IntrospectionResponse> IntrospectToken(IntrospectTokenRequest request, ServerCallContext context)
{
if (_auth.ValidateToken(request.Token, out var sessionId))
if (auth.ValidateToken(request.Token, out var sessionId))
{
var session = await _db.AuthSessions
var session = await db.AuthSessions
.Include(s => s.Account)
.Include(s => s.Challenge)
.FirstOrDefaultAsync(s => s.Id == sessionId);
@ -91,13 +79,13 @@ public class AuthGrpcService : DysonNetwork.Sphere.Auth.Proto.AuthService.AuthSe
if (authorizationHeader != null)
{
var token = authorizationHeader.Value.Replace("Bearer ", "");
if (_auth.ValidateToken(token, out var sessionId))
if (auth.ValidateToken(token, out var sessionId))
{
var session = await _db.AuthSessions.FindAsync(sessionId);
var session = await db.AuthSessions.FindAsync(sessionId);
if (session != null)
{
_db.AuthSessions.Remove(session);
await _db.SaveChangesAsync();
db.AuthSessions.Remove(session);
await db.SaveChangesAsync();
}
}
}