Finalize autorenew method
This commit is contained in:
@@ -111,6 +111,7 @@ using (HttpClient client = new HttpClient()) {
|
||||
Console.WriteLine("PublicKey loaded");
|
||||
}
|
||||
|
||||
// Pull JWT out of cookie for auth
|
||||
builder.Services.AddAuthentication(options => {
|
||||
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||
@@ -197,11 +198,14 @@ app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
|
||||
// Autorenew JWT about to expire
|
||||
app.Use(async (context, next) =>{
|
||||
ClaimsPrincipal user = context.User;
|
||||
if (user.Identity?.IsAuthenticated == true) {
|
||||
string? token = context.Request.Cookies["mistox_session"];
|
||||
Claim? expClaim = user.FindFirst(JwtRegisteredClaimNames.Exp);
|
||||
Claim? staySignedIn = user.FindFirst(ClaimTypes.IsPersistent);
|
||||
if (staySignedIn != null && bool.TryParse(staySignedIn.Value, out bool sli) && sli == true) {
|
||||
Claim? expClaim = user.FindFirst(ClaimTypes.Expiration);
|
||||
if (expClaim != null && long.TryParse(expClaim.Value, out long expUnix)) {
|
||||
DateTimeOffset expTime = DateTimeOffset.FromUnixTimeSeconds(expUnix);
|
||||
if ((expTime - DateTimeOffset.UtcNow) < TimeSpan.FromDays(3)) {
|
||||
@@ -220,6 +224,9 @@ app.Use(async (context, next) =>{
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
context.Response.Cookies.Delete("mistox_session");
|
||||
}
|
||||
|
||||
await next();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user