diff --git a/CHANGELOG.md b/CHANGELOG.md index 457648cd..a9917bdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Change Log +- csharp: jwt token lifetime from config - golang: jwt token lifetime from config ## 1.13.0 diff --git a/web/documentserver-example/csharp/JwtManager.cs b/web/documentserver-example/csharp/JwtManager.cs index 28e5214c..dbd2678e 100644 --- a/web/documentserver-example/csharp/JwtManager.cs +++ b/web/documentserver-example/csharp/JwtManager.cs @@ -20,6 +20,7 @@ using JWT; using JWT.Algorithms; using JWT.Builder; using JWT.Serializers; +using System; using System.Collections.Generic; using System.Web.Configuration; @@ -28,6 +29,7 @@ namespace OnlineEditorsExample public static class JwtManager { private static readonly string Secret; + private static readonly int ExpiresIn; public static readonly bool Enabled; public static readonly bool SignatureUseForRequest; @@ -35,12 +37,19 @@ namespace OnlineEditorsExample { Secret = WebConfigurationManager.AppSettings["files.docservice.secret"] ?? ""; // get token secret from the config parameters Enabled = !string.IsNullOrEmpty(Secret); // check if the token is enabled + ExpiresIn = int.Parse(WebConfigurationManager.AppSettings["files.docservice.token.expires-in"]); SignatureUseForRequest = bool.Parse(WebConfigurationManager.AppSettings["files.docservice.token.useforrequest"]); } // encode a payload object into a token using a secret key public static string Encode(IDictionary payload) { + var now = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + var expires = now + ExpiresIn * 60; + + payload["iat"] = now; + payload["exp"] = expires; + var encoder = new JwtEncoder(new HMACSHA256Algorithm(), new JsonNetSerializer(), new JwtBase64UrlEncoder()); diff --git a/web/documentserver-example/csharp/settings.config b/web/documentserver-example/csharp/settings.config index 21a8911d..67965096 100644 --- a/web/documentserver-example/csharp/settings.config +++ b/web/documentserver-example/csharp/settings.config @@ -14,6 +14,7 @@ +