2023-09-10 11:49:40 +02:00
|
|
|
const {expressjwt: expressJWT} = require('express-jwt');
|
2020-08-08 23:59:13 +02:00
|
|
|
|
|
|
|
|
const cfg = require("../../../lib/config").jwt;
|
|
|
|
|
|
|
|
|
|
const getToken = function (req) {
|
|
|
|
|
if (req.headers.authorization && req.headers.authorization.split(' ')[0] == 'Bearer') {
|
|
|
|
|
return req.headers.authorization.split(' ')[1];
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-26 10:56:23 +02:00
|
|
|
const onExpired = async function (req, err) {
|
|
|
|
|
// If it's not too badly expired, let it through
|
|
|
|
|
// and hope that a new token will be issued soon.
|
|
|
|
|
const elapsed = new Date() - err.inner.expiredAt;
|
|
|
|
|
// TODO: Add proper logging
|
|
|
|
|
// console.log("Expiry details (elapsed, gracePeriod)", elapsed, cfg.gracePeriod*1000);
|
|
|
|
|
if (elapsed < cfg.gracePeriod*1000) {
|
|
|
|
|
// console.log("JWT within grace period");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 23:59:13 +02:00
|
|
|
const options = {
|
|
|
|
|
secret: cfg.secret,
|
|
|
|
|
credentialsRequired: false,
|
|
|
|
|
algorithms: ['HS256'],
|
2023-09-10 14:09:01 +02:00
|
|
|
requestProperty: "user",
|
2025-07-26 10:56:23 +02:00
|
|
|
getToken,
|
|
|
|
|
onExpired
|
2020-08-08 23:59:13 +02:00
|
|
|
};
|
|
|
|
|
|
2023-11-02 23:49:42 +01:00
|
|
|
module.exports = expressJWT(options);
|