@@ -28,6 +28,7 @@ import {
2828import { encode } from 'base-64' ;
2929import crypto from 'crypto' ;
3030import { HttpsProxyAgent } from 'https-proxy-agent' ;
31+ import jwt from 'jsonwebtoken' ;
3132import {
3233 authenticate ,
3334 AuthenticationBindings ,
@@ -56,6 +57,7 @@ import {
5657 UserTenantRepository ,
5758} from '../../../repositories' ;
5859import { ActorId , IUserActivity } from '../../../types' ;
60+ import { TokenPayload } from '../interfaces' ;
5961
6062const proxyUrl = process . env . HTTPS_PROXY ?? process . env . HTTP_PROXY ;
6163
@@ -70,6 +72,7 @@ const size = 16;
7072const SUCCESS_RESPONSE = 'Success Response' ;
7173const AUTHENTICATE_USER =
7274 'This is the access token which is required to authenticate user.' ;
75+
7376export class LogoutController {
7477 constructor (
7578 @inject ( RestBindings . Http . REQUEST ) private readonly req : Request ,
@@ -136,15 +139,21 @@ export class LogoutController {
136139 AuthenticateErrorKeys . TokenMissing ,
137140 ) ;
138141 }
139-
140142 const refreshTokenModel = await this . refreshTokenRepo . get ( req . refreshToken ) ;
141143 if ( ! refreshTokenModel ) {
142144 throw new HttpErrors . Unauthorized ( AuthErrorKeys . TokenExpired ) ;
143145 }
144146 if ( refreshTokenModel . accessToken !== token ) {
145147 throw new HttpErrors . Unauthorized ( AuthErrorKeys . TokenInvalid ) ;
146148 }
147- await this . revokedTokens . set ( token , { token} ) ;
149+ const expiry = this . decodeAndGetExpiry ( token ) ;
150+ await this . revokedTokens . set (
151+ token ,
152+ { token} ,
153+ {
154+ ttl : expiry ,
155+ } ,
156+ ) ;
148157 await this . refreshTokenRepo . delete ( req . refreshToken ) ;
149158 if ( refreshTokenModel . pubnubToken ) {
150159 await this . refreshTokenRepo . delete ( refreshTokenModel . pubnubToken ) ;
@@ -517,4 +526,27 @@ export class LogoutController {
517526 } ) ;
518527 }
519528 }
529+
530+ /**
531+ * The function decodes a JWT token and returns the expiration time in milliseconds.
532+ * @param {string } token - The `token` parameter is a string that represents a JSON Web Token (JWT).
533+ * @returns the expiry time of the token in milliseconds.
534+ */
535+ /**
536+ * Decodes the given token and retrieves the expiry timestamp.
537+ *
538+ * @param token - The token to decode.
539+ * @returns The expiry timestamp in milliseconds.
540+ */
541+ decodeAndGetExpiry ( token : string ) : number | null {
542+ const tokenData = jwt . decode ( token ) as TokenPayload | null ; // handle null result from decode
543+ const ms = 1000 ;
544+
545+ if ( tokenData ?. exp ) {
546+ return tokenData . exp * ms ;
547+ }
548+
549+ // If tokenData or exp is missing, return null to indicate no expiry
550+ return null ;
551+ }
520552}
0 commit comments