/** * Example to refresh tokens using https://github.com/auth0/node-jsonwebtoken * It was requested to be introduced at as part of the jsonwebtoken library, * since we don't think a JWT should be auto-refreshed it should be there we won't include it. * * I create this gist just to help those who want to auto-refresh JWTs. */ const jwt = require('jwt'); function TokenGenerator (secretOrPrivateKey, secretOrPublicKey, options) { this.secretOrPrivateKey = secretOrPrivateKey; this.secretOrPublicKey = secretOrPublicKey; this.options = options; //algorithm + keyid + noTimestamp + expiresIn + notBefore } TokenGenerator.prototype.sign = function(payload, signOptions) { const jwtSignOptions = Object.assign({}, signOptions, this.options); return jwt.sign(payload, this.secretOrPrivateKey, jwtSignOptions); } // refreshOptions.verify = options you would use with verify function // refreshOptions.jwtid = contains the id for the new token TokenGenerator.prototype.refresh = function(token, refreshOptions) { const payload = jwt.verify(token, this.secretOrPrivateKey, refreshOptions.verify); delete payload.iat; delete payload.exp; delete payload.nbf; delete payload.jti; //We are generating a new token, if you are using jwtid during signing, // Since the first signing converted all neede options into claims, they are already in the payload const jwtSignOptions = Object.assign({ jwtid: refreshOptions.jwtid }, this.options); return jwt.sign(payload, this.secretOrPrivateKey, jwtSignOptions); } module.exports = TokenGenerator;