@zhaochy/egg-oauth2-server
Version:
koa-oauth-server(node-oauth2-server) plugin for egg
45 lines (38 loc) • 1.14 kB
JavaScript
'use strict';
const jwt = require('jsonwebtoken');
const JWT = Symbol('Application#jwt');
module.exports = {
get jwt() {
if (!this[JWT]) {
const config = this.config.oAuth2Server;
// const secret = config.jwt.secret;
this[JWT] = {};
this[JWT].sign = (payload, privateKey, options = {}, callback) => {
if (this[JWT].privateKey) {
privateKey = this[JWT].privateKey;
}
return jwt.sign(
payload,
privateKey,
Object.assign({}, config.sign || {}, options),
callback
);
};
this[JWT].verify = (token, publicKey, options, callback) => {
if (this[JWT].publicKey) {
publicKey = this[JWT].publicKey;
}
return jwt.verify(
token,
publicKey,
Object.assign({}, config.verify || {}, options),
callback
);
};
this[JWT].decode = jwt.decode;
this[JWT].setPrivateKey = (privateKey) => this[JWT].privateKey = privateKey;
this[JWT].setPublicKey = (publicKey) => this[JWT].publicKey = publicKey;
}
return this[JWT];
},
};