function JWTError(message) { Error.call(this) Error.captureStackTrace(this, this.constructor) this.name = this.constructor.name this.message = message }
n/a
function Error() { [native code] }
n/a
function decode(key, token, cb) { if (paramsAreFalsy(key, token)) { return prcResult('The key and token are mandatory!', null, cb) } const parts = token.split('.') // check all parts're present if (parts.length !== 3) { return prcResult( 'The JWT should consist of three parts!', null, cb ) } // base64 decode and parse JSON const header = JSONParse(b64url.decode(parts[0])) const payload = JSONParse(b64url.decode(parts[1])) // get algorithm hash and type and check if is valid const algorithm = algorithms[header.alg] if (!algorithm) { return prcResult('The algorithm is not supported!', null, cb) } // verify the signature const res = verify( algorithm, key, parts.slice(0, 2).join('.'), parts[2] ) return prcResult(!res && 'Invalid key!' || null, payload, header, cb) }
...
jwt.encode(secret, payload, function (err, token) {
if (err) {
console.error(err.name, err.message);
} else {
console.log(token);
// decode
jwt.decode(secret, token, function (err_, decodedPayload, decodedHeader) {
if (err) {
console.error(err.name, err.message);
} else {
console.log(decodedPayload, decodedHeader);
}
});
}
...
function encode(key, data, algorithm, cb) { if (paramIsValid(algorithm, 'function')) { cb = algorithm algorithm = 'HS256' } var defaultHeader = {typ: 'JWT', alg: algorithm} var payload = isObject(data) && data.payload ? data.payload : data var header = isObject(data) && data.header ? extend(data.header, defaultHeader) : defaultHeader const validationError = encodeValidations(key, payload, algorithm) if (validationError) { return prcResult(validationError, null, cb) } const parts = b64url.encode(JSON.stringify(header)) + '.' + b64url.encode(JSON.stringify(payload)) return prcResult( null, parts + '.' + sign(algorithms[algorithm], key, parts), cb ) }
...
"status": "SUCCESS"
}
};
var secret = 'TOPSECRETTTTT';
// encode
jwt.encode(secret, payload, function (err, token) {
if (err) {
console.error(err.name, err.message);
} else {
console.log(token);
// decode
jwt.decode(secret, token, function (err_, decodedPayload, decodedHeader) {
...
function getAlgorithms() { return Object.keys(algorithms) }
n/a
function JWTError(message) { Error.call(this) Error.captureStackTrace(this, this.constructor) this.name = this.constructor.name this.message = message }
n/a
function Error() { [native code] }
n/a
function Error() { [native code] }
n/a
function captureStackTrace() { [native code] }
...
//
// JWT token error
//
function JWTError (message) {
Error.call(this)
Error.captureStackTrace(this, this.constructor)
this.name = this.constructor.name
this.message = message
}
inherits(JWTError, Error)
//
...