Fully impliment JWT auto renew

This commit is contained in:
2025-09-22 19:02:03 -07:00
parent 0a48fb359f
commit e94f77d56d
3 changed files with 18 additions and 13 deletions
-1
View File
@@ -1,6 +1,5 @@
Server: Server:
Auth: Auth:
Make sure autorenew works
Make sure rate limiting isnt being broken by cloudflare Make sure rate limiting isnt being broken by cloudflare
Auth-Key-Value-Storage: Auth-Key-Value-Storage:
+5
View File
@@ -0,0 +1,5 @@
namespace BoredCareers.Entities {
public class JWTRenewRequest {
public string JWT { get; set; } = "";
}
}
+6 -5
View File
@@ -9,6 +9,7 @@ using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.Security.Cryptography; using System.Security.Cryptography;
using BoredCareers.Services.TimerService; using BoredCareers.Services.TimerService;
using BoredCareers.Entities;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -203,22 +204,22 @@ app.Use(async (context, next) =>{
Claim? expClaim = user.FindFirst(JwtRegisteredClaimNames.Exp); Claim? expClaim = user.FindFirst(JwtRegisteredClaimNames.Exp);
if (expClaim != null && long.TryParse(expClaim.Value, out long expUnix)) { if (expClaim != null && long.TryParse(expClaim.Value, out long expUnix)) {
DateTimeOffset expTime = DateTimeOffset.FromUnixTimeSeconds(expUnix); DateTimeOffset expTime = DateTimeOffset.FromUnixTimeSeconds(expUnix);
if ((expTime - DateTimeOffset.UtcNow) < TimeSpan.FromMinutes(2)) { if ((expTime - DateTimeOffset.UtcNow) < TimeSpan.FromDays(3)) {
IHttpClientFactory clientFactory = context.RequestServices.GetRequiredService<IHttpClientFactory>(); using (HttpClient client = new HttpClient()) {
HttpClient client = clientFactory.CreateClient(); HttpResponseMessage response = await client.PostAsJsonAsync("https://auth.mistox.com/api/auth/renew", new JWTRenewRequest() { JWT = token });
HttpResponseMessage response = await client.PostAsync("https://auth.mistox.com/api/auth/renew", new StringContent(token));
if (response.IsSuccessStatusCode) { if (response.IsSuccessStatusCode) {
string newJwt = await response.Content.ReadAsStringAsync(); string newJwt = await response.Content.ReadAsStringAsync();
context.Response.Cookies.Append("mistox_session", newJwt, new CookieOptions { context.Response.Cookies.Append("mistox_session", newJwt, new CookieOptions {
HttpOnly = true, HttpOnly = true,
Secure = true, Secure = true,
SameSite = SameSiteMode.Strict, SameSite = SameSiteMode.Strict,
Expires = DateTimeOffset.UtcNow.AddYears(1) Expires = DateTimeOffset.UtcNow.AddYears(3)
}); });
} }
} }
} }
} }
}
await next(); await next();
}); });