41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using Heimdall.Application.Services;
|
|||
|
|
using Heimdall.Infrastructure.Data;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
|
||
|
|
namespace Heimdall.Infrastructure.Security;
|
||
|
|
|
||
|
|
public class AuthDataService : IAuthDataService
|
||
|
|
{
|
||
|
|
private readonly AppDbContext _db;
|
||
|
|
|
||
|
|
public AuthDataService(AppDbContext db)
|
||
|
|
{
|
||
|
|
_db = db;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<string?> GetSettingValueAsync(string key, CancellationToken ct)
|
||
|
|
{
|
||
|
|
var setting = await _db.AppSettings
|
||
|
|
.AsNoTracking()
|
||
|
|
.FirstOrDefaultAsync(s => s.Key == key, ct);
|
||
|
|
return setting?.Value;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> IsFirstUserAsync(CancellationToken ct)
|
||
|
|
{
|
||
|
|
return await _db.Users.AnyAsync(ct);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> IsRefreshTokenRevokedAsync(string tokenHash, CancellationToken ct)
|
||
|
|
{
|
||
|
|
return await _db.RevokedRefreshTokens
|
||
|
|
.AnyAsync(r => r.TokenHash == tokenHash, ct);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task RevokeRefreshTokenAsync(string refreshToken, Guid userId, CancellationToken ct)
|
||
|
|
{
|
||
|
|
_db.RevokedRefreshTokens.Add(new Heimdall.Domain.Entities.RevokedRefreshToken(
|
||
|
|
refreshToken, userId, DateTime.UtcNow.AddDays(7)));
|
||
|
|
await _db.SaveChangesAsync(ct);
|
||
|
|
}
|
||
|
|
}
|