From a64a43c2bd4e470e8f46239e42a8e06a31ec5f41 Mon Sep 17 00:00:00 2001 From: Theresa Kamerman Date: Fri, 21 Jun 2024 15:47:40 -0700 Subject: [PATCH] Allow specifying expiry when generating a JWT --- src/packages/auth/functions.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/packages/auth/functions.ts b/src/packages/auth/functions.ts index 0f70e2d..8f616f0 100644 --- a/src/packages/auth/functions.ts +++ b/src/packages/auth/functions.ts @@ -129,11 +129,16 @@ export function decodeJwt(authorizationHeader: string | undefined): JwtDecode { } } -export function generateJwt(username: string, defaultRole: string, allowedRoles: string[]): string | null { +export function generateJwt( + username: string, + defaultRole: string, + allowedRoles: string[], + expiry: string = getEnv().JWT_EXPIRATION, +): string | null { try { - const { HASURA_GRAPHQL_JWT_SECRET, JWT_EXPIRATION } = getEnv(); + const { HASURA_GRAPHQL_JWT_SECRET } = getEnv(); const { key, type }: JwtSecret = JSON.parse(HASURA_GRAPHQL_JWT_SECRET); - const options: jwt.SignOptions = { algorithm: type as Algorithm, expiresIn: JWT_EXPIRATION }; + const options: jwt.SignOptions = { algorithm: type as Algorithm, expiresIn: expiry }; const payload: JwtPayload = { 'https://hasura.io/jwt/claims': { 'x-hasura-allowed-roles': allowedRoles,