Skip to content
Snippets Groups Projects
Commit be989213 authored by Petar Hristov's avatar Petar Hristov :speech_balloon: Committed by L. Ellenbeck
Browse files

Fix: JWT token now uses UTC and not local time

parent ec87e87f
No related branches found
No related tags found
2 merge requests!15Release: Sprint/2022 04 :robot:,!14Fix: JWT token now uses UTC and not local time
...@@ -15,8 +15,10 @@ namespace Coscine.JwtHandler ...@@ -15,8 +15,10 @@ namespace Coscine.JwtHandler
private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler; private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private readonly SymmetricSecurityKey _symmetricSecurityKey; private readonly SymmetricSecurityKey _symmetricSecurityKey;
private readonly DateTime _centuryBegin; private readonly DateTime _centuryBegin;
// How long the default token is valid (in minutes). // How long the default token is valid (in minutes).
private readonly double _defaultExpiration; private readonly double _defaultExpiration;
private readonly string _issuer; private readonly string _issuer;
private readonly string _audience; private readonly string _audience;
...@@ -25,8 +27,8 @@ namespace Coscine.JwtHandler ...@@ -25,8 +27,8 @@ namespace Coscine.JwtHandler
Configuration = configuration; Configuration = configuration;
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); _jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
_symmetricSecurityKey = GetSecurityKey(); _symmetricSecurityKey = GetSecurityKey();
_centuryBegin = new DateTime(1970, 1, 1); _centuryBegin = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
_defaultExpiration = 30; _defaultExpiration = 60;
_issuer = "https://coscine.rwth-aachen.de"; _issuer = "https://coscine.rwth-aachen.de";
_audience = "https://coscine.rwth-aachen.de"; _audience = "https://coscine.rwth-aachen.de";
} }
...@@ -52,7 +54,9 @@ namespace Coscine.JwtHandler ...@@ -52,7 +54,9 @@ namespace Coscine.JwtHandler
ValidateIssuerSigningKey = true, ValidateIssuerSigningKey = true,
IssuerSigningKey = _symmetricSecurityKey, IssuerSigningKey = _symmetricSecurityKey,
ValidateIssuer = false, ValidateIssuer = false,
ValidateAudience = false ValidateAudience = false,
// set clockskew to zero so tokens expire exactly at token expiration time (instead of 5 minutes later)
ClockSkew = TimeSpan.Zero
}; };
_jwtSecurityTokenHandler.ValidateToken(token, tokenValidationParameters, out _); _jwtSecurityTokenHandler.ValidateToken(token, tokenValidationParameters, out _);
...@@ -66,7 +70,7 @@ namespace Coscine.JwtHandler ...@@ -66,7 +70,7 @@ namespace Coscine.JwtHandler
public string GenerateJwtToken(JwtPayload payload, string signatureAlgorithm = "HS256") public string GenerateJwtToken(JwtPayload payload, string signatureAlgorithm = "HS256")
{ {
var issuedAt = DateTime.Now; var issuedAt = DateTime.UtcNow;
var expires = issuedAt.AddMinutes(_defaultExpiration); var expires = issuedAt.AddMinutes(_defaultExpiration);
return GenerateJwtToken(payload, _issuer, _audience, issuedAt, expires, signatureAlgorithm); return GenerateJwtToken(payload, _issuer, _audience, issuedAt, expires, signatureAlgorithm);
} }
...@@ -107,6 +111,5 @@ namespace Coscine.JwtHandler ...@@ -107,6 +111,5 @@ namespace Coscine.JwtHandler
return GenerateJwtToken(payload, issuer, audience, issuedAt, expires, signatureAlgorithm); return GenerateJwtToken(payload, issuer, audience, issuedAt, expires, signatureAlgorithm);
} }
} }
} }
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<RootNamespace>Coscine.JwtHandler</RootNamespace> <RootNamespace>Coscine.JwtHandler</RootNamespace>
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<PropertyGroup> <PropertyGroup>
<Authors>RWTH Aachen University</Authors> <Authors>RWTH Aachen University</Authors>
<Company>IT Center, RWTH Aachen University</Company> <Company>IT Center, RWTH Aachen University</Company>
<Copyright>2021 IT Center, RWTH Aachen University</Copyright> <Copyright>2022 IT Center, RWTH Aachen University</Copyright>
<Description>JwtHandler is a part of the Coscine group.</Description> <Description>JwtHandler is a part of the Coscine group.</Description>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://git.rwth-aachen.de/coscine/backend/libraries/Jwt-Handler</PackageProjectUrl> <PackageProjectUrl>https://git.rwth-aachen.de/coscine/backend/libraries/Jwt-Handler</PackageProjectUrl>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment