refactor: add DatabaseContext.GetToken method

This commit is contained in:
sam 2024-09-11 16:23:45 +02:00
parent be34c4c77e
commit 2682cabfb0
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
6 changed files with 51 additions and 35 deletions

View file

@ -105,4 +105,18 @@ public static class DatabaseQueryExtensions
await context.SaveChangesAsync(ct);
return app;
}
public static async Task<Token?> GetToken(this DatabaseContext context, byte[] rawToken,
CancellationToken ct = default)
{
var hash = SHA512.HashData(rawToken);
var oauthToken = await context.Tokens
.Include(t => t.Application)
.Include(t => t.User)
.FirstOrDefaultAsync(
t => t.Hash == hash && t.ExpiresAt > SystemClock.Instance.GetCurrentInstant() && !t.ManuallyExpired,
ct);
return oauthToken;
}
}