From c95260a22872dd7df06d63376876592a28af0aa4 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Wed, 12 Jun 2024 14:13:52 +0530 Subject: [PATCH] Reduce account jwt token validity to 30mins --- server/pkg/controller/user/jwt.go | 6 +++++- server/pkg/utils/time/time.go | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/server/pkg/controller/user/jwt.go b/server/pkg/controller/user/jwt.go index d920e36b0b..a3d02b2eff 100644 --- a/server/pkg/controller/user/jwt.go +++ b/server/pkg/controller/user/jwt.go @@ -13,11 +13,15 @@ import ( const ValidForDays = 1 func (c *UserController) GetJWTToken(userID int64, scope enteJWT.ClaimScope) (string, error) { + tokenExpirty := time.NDaysFromNow(1) + if scope == enteJWT.ACCOUNTS { + tokenExpirty = time.NMinFromNow(30) + } // Create a new token object, specifying signing method and the claims // you would like it to contain. token := jwt.NewWithClaims(jwt.SigningMethodHS256, &enteJWT.WebCommonJWTClaim{ UserID: userID, - ExpiryTime: time.NDaysFromNow(1), + ExpiryTime: tokenExpirty, ClaimScope: &scope, }) // Sign and get the complete encoded token as a string using the secret diff --git a/server/pkg/utils/time/time.go b/server/pkg/utils/time/time.go index c03f97696d..a07df4b262 100644 --- a/server/pkg/utils/time/time.go +++ b/server/pkg/utils/time/time.go @@ -48,6 +48,11 @@ func NDaysFromNow(n int) int64 { return time.Now().AddDate(0, 0, n).UnixNano() / 1000 } +// NMinFromNow returns the time n min from now in micro seconds +func NMinFromNow(n int64) int64 { + return time.Now().Add(time.Minute*time.Duration(n)).UnixNano() / 1000 +} + // MicrosecondsBeforeMinutes returns the unix time n minutes before now in micro seconds func MicrosecondsBeforeMinutes(noOfMinutes int64) int64 { return Microseconds() - (MicroSecondsInOneMinute * noOfMinutes)