Files
dougal-software/lib/www/server/api/middleware/auth/jwt.js

36 lines
911 B
JavaScript
Raw Normal View History

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'],
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);