Skip to content

Commit 00559fc

Browse files
committed
Fixed an issue where clients could get logged out on token refresh.
1 parent 1f57661 commit 00559fc

1 file changed

Lines changed: 7 additions & 11 deletions

File tree

Kavita.Services/TokenService.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,22 @@ public async Task<string> CreateRefreshToken(AppUser user, CancellationToken ct
9090
IssuerSigningKey = _key,
9191
ValidateIssuer = false,
9292
ValidateAudience = false,
93+
ValidateLifetime = false, // JWT may be expired
9394
ValidIssuer = "Kavita",
9495
NameClaimType = JwtRegisteredClaimNames.Name,
95-
RoleClaimType = "role",
96+
RoleClaimType = "role"
9697
};
9798

98-
var principal = tokenHandler.ValidateToken(request.Token, tokenValidationParams, out var tokenContent);
99-
var username = principal.Claims.FirstOrDefault(q => q.Type == JwtRegisteredClaimNames.Name)?.Value;
99+
var principal = tokenHandler.ValidateToken(request.Token, tokenValidationParams, out _);
100+
var userId = principal.Claims.FirstOrDefault(q => q.Type == NameIdentifier)?.Value;
100101

101-
if (string.IsNullOrEmpty(username))
102+
if (string.IsNullOrEmpty(userId))
102103
{
103-
logger.LogDebug("[RefreshToken] failed to validate due to not finding user in RefreshToken");
104+
logger.LogDebug("[RefreshToken] JWT did not include a valid userId");
104105
return null;
105106
}
106107

107-
var user = await userManager.FindByNameAsync(username);
108+
var user = await userManager.FindByIdAsync(userId);
108109
if (user == null)
109110
{
110111
logger.LogDebug("[RefreshToken] failed to validate due to not finding user in DB");
@@ -119,11 +120,6 @@ public async Task<string> CreateRefreshToken(AppUser user, CancellationToken ct
119120
return null;
120121
}
121122

122-
if (tokenContent.ValidTo <= DateTime.UtcNow.Add(TimeSpan.FromHours(1)))
123-
{
124-
return null;
125-
}
126-
127123
// CreateRefreshToken removes the old refresh token and writes the new one under RefreshTokenLock.
128124
return new TokenRequestDto()
129125
{

0 commit comments

Comments
 (0)