fix: only query user ID in /api/internal/request-data

This commit is contained in:
sam 2024-09-11 16:34:08 +02:00
parent 2682cabfb0
commit 4ac0001795
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
3 changed files with 16 additions and 2 deletions

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectTasksOptions" suppressed-tasks="SCSS" />
</project>

View file

@ -43,8 +43,8 @@ public partial class InternalController(DatabaseContext db, IClock clock) : Cont
if (!AuthUtils.TryParseToken(req.Token, out var rawToken)) if (!AuthUtils.TryParseToken(req.Token, out var rawToken))
return Ok(new RequestDataResponse(null, template)); return Ok(new RequestDataResponse(null, template));
var oauthToken = await db.GetToken(rawToken); var userId = await db.GetTokenUserId(rawToken);
return Ok(new RequestDataResponse(oauthToken?.UserId, template)); return Ok(new RequestDataResponse(userId, template));
} }
public record RequestDataRequest(string? Token, string Method, string Path); public record RequestDataRequest(string? Token, string Method, string Path);

View file

@ -110,6 +110,7 @@ public static class DatabaseQueryExtensions
CancellationToken ct = default) CancellationToken ct = default)
{ {
var hash = SHA512.HashData(rawToken); var hash = SHA512.HashData(rawToken);
var oauthToken = await context.Tokens var oauthToken = await context.Tokens
.Include(t => t.Application) .Include(t => t.Application)
.Include(t => t.User) .Include(t => t.User)
@ -119,4 +120,13 @@ public static class DatabaseQueryExtensions
return oauthToken; return oauthToken;
} }
public static async Task<Snowflake?> GetTokenUserId(this DatabaseContext context, byte[] rawToken,
CancellationToken ct = default)
{
var hash = SHA512.HashData(rawToken);
return await context.Tokens
.Where(t => t.Hash == hash && t.ExpiresAt > SystemClock.Instance.GetCurrentInstant() && !t.ManuallyExpired)
.Select(t => t.UserId).FirstOrDefaultAsync(ct);
}
} }