Last active
December 10, 2018 19:32
-
-
Save daliborgogic/3835e4486fed695c4f81f9631624de18 to your computer and use it in GitHub Desktop.
JWT module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const jwt = require('jsonwebtoken') | |
const { readFileSync } = require('fs') | |
const private = readFileSync(__dirname + '/private.pem') | |
const public = readFileSync(__dirname + '/public.pem') | |
const { | |
ISSUER = 'DevOops', | |
SUBJECT = '[email protected]', | |
AUDIENCE = 'https://devoops.app' | |
} = process.env | |
const options = { | |
issuer: ISSUER, | |
subject: SUBJECT, | |
audience: AUDIENCE | |
} | |
module.exports = { | |
sign: (payload, expiresIn = '12h') => { | |
options.expiresIn = expiresIn | |
options.algorithm = 'RS256' | |
return jwt.sign(payload, privateKey, options) | |
}, | |
verify: (token, aud) => { | |
try { | |
return jwt.verify(token, public, options, (err, decoded) => { | |
if (err) return false | |
const { audience } = decoded | |
return audience === aud | |
}) | |
} catch (error) { | |
console.error(error) | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
403 Forbidden
error as the audience identification fails to match. L31