2019-01-06 00:50:05 +00:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import jwt from 'jsonwebtoken';
|
2018-02-18 04:21:19 +00:00
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
import data from './data';
|
2019-01-26 23:20:15 +00:00
|
|
|
import { privateKey, publicKey } from './secret';
|
2017-11-11 20:18:13 +00:00
|
|
|
|
|
|
|
function getCurrentSeconds() {
|
2018-02-18 02:00:56 +00:00
|
|
|
return Math.floor(Date.now() / 1000);
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createTokenId() {
|
2018-12-17 17:38:05 +00:00
|
|
|
return crypto.randomBytes(16).toString('hex');
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function createToken(scopes = {}) {
|
2017-11-11 20:18:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const payload = {
|
|
|
|
jti: createTokenId(),
|
2018-12-17 17:38:05 +00:00
|
|
|
iss: 'https://unpkg.com',
|
2017-11-11 20:18:13 +00:00
|
|
|
iat: getCurrentSeconds(),
|
|
|
|
scopes
|
2018-02-18 02:00:56 +00:00
|
|
|
};
|
2017-11-11 20:18:13 +00:00
|
|
|
|
2019-01-26 23:20:15 +00:00
|
|
|
jwt.sign(payload, privateKey, { algorithm: 'RS256' }, (error, token) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve(token);
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
2019-01-26 23:20:15 +00:00
|
|
|
});
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 17:38:05 +00:00
|
|
|
const revokedTokensSet = 'revoked-tokens';
|
2017-11-11 20:18:13 +00:00
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function verifyToken(token) {
|
2017-11-11 20:18:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2018-12-17 17:38:05 +00:00
|
|
|
const options = { algorithms: ['RS256'] };
|
2017-11-11 20:18:13 +00:00
|
|
|
|
2019-01-26 23:20:15 +00:00
|
|
|
jwt.verify(token, publicKey, options, (error, payload) => {
|
2017-11-11 20:18:13 +00:00
|
|
|
if (error) {
|
2018-02-18 02:00:56 +00:00
|
|
|
reject(error);
|
2017-11-11 20:18:13 +00:00
|
|
|
} else {
|
|
|
|
if (payload.jti) {
|
2019-01-06 00:50:05 +00:00
|
|
|
data.sismember(revokedTokensSet, payload.jti, (error, value) => {
|
2017-11-11 20:18:13 +00:00
|
|
|
if (error) {
|
2018-02-18 02:00:56 +00:00
|
|
|
reject(error);
|
2017-11-11 20:18:13 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
resolve(value === 0 ? payload : null);
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
resolve(null);
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function revokeToken(token) {
|
2017-11-11 20:18:13 +00:00
|
|
|
return verifyToken(token).then(payload => {
|
|
|
|
if (payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-06 00:50:05 +00:00
|
|
|
data.sadd(revokedTokensSet, payload.jti, error => {
|
2017-11-11 20:18:13 +00:00
|
|
|
if (error) {
|
2018-02-18 02:00:56 +00:00
|
|
|
reject(error);
|
2017-11-11 20:18:13 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
resolve();
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 00:50:05 +00:00
|
|
|
export function removeAllRevokedTokens() {
|
2017-11-11 20:18:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-01-06 00:50:05 +00:00
|
|
|
data.del(revokedTokensSet, error => {
|
2017-11-11 20:18:13 +00:00
|
|
|
if (error) {
|
2018-02-18 02:00:56 +00:00
|
|
|
reject(error);
|
2017-11-11 20:18:13 +00:00
|
|
|
} else {
|
2018-02-18 02:00:56 +00:00
|
|
|
resolve();
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|
2018-02-18 02:00:56 +00:00
|
|
|
});
|
|
|
|
});
|
2017-11-11 20:18:13 +00:00
|
|
|
}
|