@@ -19,6 +19,7 @@ module.exports = {
1919 } ,
2020 groups : { } ,
2121 validApiKeys : [ ] ,
22+ revokationList : require ( './cache' ) . init ( ) ,
2223
2324 /**
2425 * Initialize the authentication module
@@ -111,10 +112,28 @@ module.exports = {
111112 authenticate ( req , res , next ) {
112113 WIKI . auth . passport . authenticate ( 'jwt' , { session : false } , async ( err , user , info ) => {
113114 if ( err ) { return next ( ) }
115+ let mustRevalidate = false
114116
115117 // Expired but still valid within N days, just renew
116- if ( info instanceof Error && info . name === 'TokenExpiredError' &&
117- moment ( ) . subtract ( ms ( WIKI . config . auth . tokenRenewal ) , 'ms' ) . isBefore ( info . expiredAt ) ) {
118+ if ( info instanceof Error && info . name === 'TokenExpiredError' && moment ( ) . subtract ( ms ( WIKI . config . auth . tokenRenewal ) , 'ms' ) . isBefore ( info . expiredAt ) ) {
119+ mustRevalidate = true
120+ }
121+
122+ // Check if user / group is in revokation list
123+ if ( user ) {
124+ if ( WIKI . auth . revokationList . has ( `u${ _ . toString ( user . id ) } ` ) ) {
125+ mustRevalidate = true
126+ }
127+ for ( const gid of user . groups ) {
128+ if ( WIKI . auth . revokationList . has ( `g${ _ . toString ( gid ) } ` ) ) {
129+ mustRevalidate = true
130+ }
131+ }
132+ }
133+
134+ // Revalidate and renew token
135+ if ( mustRevalidate ) {
136+ console . info ( 'MUST REVALIDATE' )
118137 const jwtPayload = jwt . decode ( securityHelper . extractJWT ( req ) )
119138 try {
120139 const newToken = await WIKI . models . users . refreshToken ( jwtPayload . id )
@@ -380,6 +399,9 @@ module.exports = {
380399 WIKI . events . inbound . on ( 'reloadAuthStrategies' , ( ) => {
381400 WIKI . auth . activateStrategies ( )
382401 } )
402+ WIKI . events . inbound . on ( 'addAuthRevoke' , ( args ) => {
403+ WIKI . auth . revokeUserTokens ( args )
404+ } )
383405 } ,
384406
385407 /**
@@ -410,5 +432,13 @@ module.exports = {
410432 manage : WIKI . auth . checkAccess ( req . user , [ 'manage:system' ] , page )
411433 }
412434 }
435+ } ,
436+
437+ /**
438+ * Add user / group ID to JWT revokation list, forcing all requests to be validated against the latest permissions
439+ */
440+ revokeUserTokens ( { id, kind = 'u' } ) {
441+ console . info ( Math . ceil ( ms ( WIKI . config . auth . tokenRenewal ) / 1000 ) )
442+ WIKI . auth . revokationList . set ( `${ kind } ${ _ . toString ( id ) } ` , true , Math . ceil ( ms ( WIKI . config . auth . tokenRenewal ) / 1000 ) )
413443 }
414444}
0 commit comments