@@ -2,6 +2,7 @@ import { FastifyPluginAsync } from "fastify";
22import rateLimiter from "api/plugins/rateLimiter.js" ;
33import { withRoles , withTags } from "api/components/index.js" ;
44import { clearAuthCache } from "api/functions/authorization.js" ;
5+ import { setKey } from "api/functions/redisCache.js" ;
56
67const clearSessionPlugin : FastifyPluginAsync = async ( fastify , _options ) => {
78 fastify . register ( rateLimiter , {
@@ -26,7 +27,25 @@ const clearSessionPlugin: FastifyPluginAsync = async (fastify, _options) => {
2627 const username = [ request . username ! ] ;
2728 const { redisClient } = fastify ;
2829 const { log : logger } = fastify ;
30+
2931 await clearAuthCache ( { redisClient, username, logger } ) ;
32+ if ( ! request . tokenPayload ) {
33+ return ;
34+ }
35+ const now = Date . now ( ) / 1000 ;
36+ const tokenExpiry = request . tokenPayload . exp ;
37+ const expiresIn = Math . ceil ( tokenExpiry - now ) ;
38+ const tokenId = request . tokenPayload . uti ;
39+ // if the token expires more than 10 seconds after now, add to a revoke list
40+ if ( expiresIn > 10 ) {
41+ await setKey ( {
42+ redisClient,
43+ key : `tokenRevocationList:${ tokenId } ` ,
44+ data : JSON . stringify ( { isInvalid : true } ) ,
45+ logger,
46+ expiresIn,
47+ } ) ;
48+ }
3049 } ,
3150 ) ;
3251} ;
0 commit comments