29 lines
1.2 KiB
JavaScript
29 lines
1.2 KiB
JavaScript
const jwt = require('./')
|
|
|
|
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;
|
|
|