2020-10-13 12:49:08 -07:00
|
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
class Jwt {
|
|
|
|
constructor(privateKey, publicKey) {
|
|
|
|
this.privateKey = fs.readFileSync(privateKey);
|
|
|
|
this.publicKey = fs.readFileSync(publicKey);
|
|
|
|
}
|
|
|
|
|
2021-08-28 14:44:30 -07:00
|
|
|
sign(payload, expiresIn = '365d') {
|
2020-10-13 12:49:08 -07:00
|
|
|
return jwt.sign(payload, this.privateKey, {
|
|
|
|
expiresIn,
|
|
|
|
algorithm: 'RS256',
|
2021-08-22 14:53:22 -07:00
|
|
|
audience: process.env.ALL_LOCALES_URLS.split(','),
|
2020-10-13 12:49:08 -07:00
|
|
|
issuer: process.env.BASE_URL,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
validate(token) {
|
|
|
|
try {
|
|
|
|
return jwt.verify(token, this.publicKey, {
|
|
|
|
algorithm: 'RS256',
|
2021-08-22 14:53:22 -07:00
|
|
|
audience: process.env.ALL_LOCALES_URLS.split(','),
|
|
|
|
issuer: process.env.ALL_LOCALES_URLS.split(','),
|
2020-10-13 12:49:08 -07:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Jwt(__dirname + '/../keys/private.pem', __dirname + '/../keys/public.pem');
|