Kavita+ Overhaul & New Changelog (#3507)

This commit is contained in:
Joe Milazzo 2025-01-20 08:14:57 -06:00 committed by GitHub
parent d880c1690c
commit a5707617f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
249 changed files with 14775 additions and 2300 deletions

40
API/Helpers/JwtHelper.cs Normal file
View file

@ -0,0 +1,40 @@
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
namespace API.Helpers;
public static class JwtHelper
{
/// <summary>
/// Extracts the expiration date from a JWT token.
/// </summary>
public static DateTime GetTokenExpiry(string jwtToken)
{
if (string.IsNullOrEmpty(jwtToken))
return DateTime.MinValue;
// Parse the JWT and extract the expiry claim
var jwtHandler = new JwtSecurityTokenHandler();
var token = jwtHandler.ReadJwtToken(jwtToken);
var exp = token.Claims.FirstOrDefault(c => c.Type == "exp")?.Value;
if (long.TryParse(exp, out var expSeconds))
{
return DateTimeOffset.FromUnixTimeSeconds(expSeconds).UtcDateTime;
}
return DateTime.MinValue;
}
/// <summary>
/// Checks if a JWT token is valid based on its expiry date.
/// </summary>
public static bool IsTokenValid(string jwtToken)
{
if (string.IsNullOrEmpty(jwtToken)) return false;
var expiry = GetTokenExpiry(jwtToken);
return expiry > DateTime.UtcNow;
}
}