fix: only query user ID in /api/internal/request-data
This commit is contained in:
parent
2682cabfb0
commit
4ac0001795
3 changed files with 16 additions and 2 deletions
4
.idea/.idea.Foxnouns.NET/.idea/watcherTasks.xml
Normal file
4
.idea/.idea.Foxnouns.NET/.idea/watcherTasks.xml
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectTasksOptions" suppressed-tasks="SCSS" />
|
||||
</project>
|
|
@ -43,8 +43,8 @@ public partial class InternalController(DatabaseContext db, IClock clock) : Cont
|
|||
if (!AuthUtils.TryParseToken(req.Token, out var rawToken))
|
||||
return Ok(new RequestDataResponse(null, template));
|
||||
|
||||
var oauthToken = await db.GetToken(rawToken);
|
||||
return Ok(new RequestDataResponse(oauthToken?.UserId, template));
|
||||
var userId = await db.GetTokenUserId(rawToken);
|
||||
return Ok(new RequestDataResponse(userId, template));
|
||||
}
|
||||
|
||||
public record RequestDataRequest(string? Token, string Method, string Path);
|
||||
|
|
|
@ -110,6 +110,7 @@ public static class DatabaseQueryExtensions
|
|||
CancellationToken ct = default)
|
||||
{
|
||||
var hash = SHA512.HashData(rawToken);
|
||||
|
||||
var oauthToken = await context.Tokens
|
||||
.Include(t => t.Application)
|
||||
.Include(t => t.User)
|
||||
|
@ -119,4 +120,13 @@ public static class DatabaseQueryExtensions
|
|||
|
||||
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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue