fix: revalidate tokens created prior to server startup
This commit is contained in:
parent
92b29d1f06
commit
98f21b9f6a
@ -115,27 +115,30 @@ module.exports = {
|
|||||||
let mustRevalidate = false
|
let mustRevalidate = false
|
||||||
|
|
||||||
// Expired but still valid within N days, just renew
|
// Expired but still valid within N days, just renew
|
||||||
if (info instanceof Error && info.name === 'TokenExpiredError' && DateTime.utc().minus(ms(WIKI.config.auth.tokenRenewal)) < DateTime.fromSeconds(info.expiredAt)) {
|
if (info instanceof Error && info.name === 'TokenExpiredError' && DateTime.utc().minus(ms(WIKI.config.auth.tokenRenewal)) < DateTime.fromISO(info.expiredAt)) {
|
||||||
mustRevalidate = true
|
mustRevalidate = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if user / group is in revokation list
|
// Check if user / group is in revokation list
|
||||||
if (user) {
|
if (user && !mustRevalidate) {
|
||||||
const uRevalidate = WIKI.auth.revokationList.get(`u${_.toString(user.id)}`)
|
const uRevalidate = WIKI.auth.revokationList.get(`u${_.toString(user.id)}`)
|
||||||
if (uRevalidate && user.iat < uRevalidate) {
|
if (uRevalidate && user.iat < uRevalidate) {
|
||||||
mustRevalidate = true
|
mustRevalidate = true
|
||||||
}
|
} else if (DateTime.fromSeconds(user.iat) <= WIKI.startedAt) { // Prevent new / restarted instance from allowing revoked tokens
|
||||||
|
mustRevalidate = true
|
||||||
|
} else {
|
||||||
for (const gid of user.groups) {
|
for (const gid of user.groups) {
|
||||||
const gRevalidate = WIKI.auth.revokationList.get(`g${_.toString(gid)}`)
|
const gRevalidate = WIKI.auth.revokationList.get(`g${_.toString(gid)}`)
|
||||||
if (gRevalidate && user.iat < gRevalidate) {
|
if (gRevalidate && user.iat < gRevalidate) {
|
||||||
mustRevalidate = true
|
mustRevalidate = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revalidate and renew token
|
// Revalidate and renew token
|
||||||
if (mustRevalidate) {
|
if (mustRevalidate) {
|
||||||
console.info('MUST REVALIDATE')
|
|
||||||
const jwtPayload = jwt.decode(securityHelper.extractJWT(req))
|
const jwtPayload = jwt.decode(securityHelper.extractJWT(req))
|
||||||
try {
|
try {
|
||||||
const newToken = await WIKI.models.users.refreshToken(jwtPayload.id)
|
const newToken = await WIKI.models.users.refreshToken(jwtPayload.id)
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const { nanoid } = require('nanoid')
|
const { nanoid } = require('nanoid')
|
||||||
|
const { DateTime } = require('luxon')
|
||||||
|
|
||||||
let WIKI = {
|
let WIKI = {
|
||||||
IS_DEBUG: process.env.NODE_ENV === 'development',
|
IS_DEBUG: process.env.NODE_ENV === 'development',
|
||||||
@ -14,7 +15,8 @@ let WIKI = {
|
|||||||
SERVERPATH: path.join(process.cwd(), 'server'),
|
SERVERPATH: path.join(process.cwd(), 'server'),
|
||||||
Error: require('./helpers/error'),
|
Error: require('./helpers/error'),
|
||||||
configSvc: require('./core/config'),
|
configSvc: require('./core/config'),
|
||||||
kernel: require('./core/kernel')
|
kernel: require('./core/kernel'),
|
||||||
|
startedAt: DateTime.utc()
|
||||||
}
|
}
|
||||||
global.WIKI = WIKI
|
global.WIKI = WIKI
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user