Fix bug where redirect handler would fetch wrong token

This commit is contained in:
Slendy 2022-12-14 01:14:58 -06:00
parent 6024d44508
commit 28ff0ddc1d
No known key found for this signature in database
GPG key ID: 7288D68361B91428
2 changed files with 10 additions and 5 deletions

View file

@ -13,14 +13,19 @@ public class UserRequiredRedirectMiddleware : MiddlewareDBContext
public override async Task InvokeAsync(HttpContext ctx, Database database)
{
User? user = database.UserFromWebRequest(ctx.Request);
if (user == null || pathContains(ctx, "/logout"))
WebToken? token = database.WebTokenFromRequest(ctx.Request);
if (token == null || pathContains(ctx, "/logout"))
{
await this.next(ctx);
return;
}
WebToken token = await database.WebTokens.FirstAsync(t => t.UserId == user.UserId);
User? user = await database.Users.FirstOrDefaultAsync(u => u.UserId == token.UserId);
if (user == null)
{
await this.next(ctx);
return;
}
// Request ends with a path (e.g. /css/style.css)
if (!string.IsNullOrEmpty(Path.GetExtension(ctx.Request.Path)) || pathContains(ctx, "/gameAssets"))

View file

@ -431,14 +431,14 @@ public class Database : DbContext
public User? UserFromWebRequest(HttpRequest request)
{
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken) || lighthouseToken == null) return null;
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken)) return null;
return this.UserFromLighthouseToken(lighthouseToken);
}
public WebToken? WebTokenFromRequest(HttpRequest request)
{
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken) || lighthouseToken == null) return null;
if (!request.Cookies.TryGetValue("LighthouseToken", out string? lighthouseToken)) return null;
WebToken? token = this.WebTokens.FirstOrDefault(t => t.UserToken == lighthouseToken);
if (token == null) return null;