-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict' | ||
|
||
const fp = require('fastify-plugin') | ||
const JWT = require('jsonwebtoken') | ||
const assert = require('assert') | ||
|
||
function fastifyJWT (fastify, opts, next) { | ||
if (!opts.secret) { | ||
return next(new Error('missing secret')) | ||
} | ||
|
||
const secret = opts.secret | ||
|
||
fastify.decorate('jwt', { | ||
sign: sign, | ||
verify: verify, | ||
decode: decode, | ||
secret: secret | ||
}) | ||
|
||
next() | ||
|
||
function sign (payload, options, callback) { | ||
assert(payload, 'missing payload') | ||
options = options || {} | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
if (typeof callback === 'function') { | ||
JWT.sign(payload, secret, options, callback) | ||
} else { | ||
return JWT.sign(payload, secret, options) | ||
} | ||
} | ||
|
||
function verify (token, options, callback) { | ||
assert(token, 'missing token') | ||
assert(secret, 'missing secret') | ||
options = options || {} | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = {} | ||
} | ||
|
||
if (typeof callback === 'function') { | ||
JWT.verify(token, secret, options, callback) | ||
} else { | ||
return JWT.verify(token, secret, options) | ||
} | ||
} | ||
|
||
function decode (token, options) { | ||
assert(token, 'missing token') | ||
options = options || {} | ||
return JWT.decode(token, options) | ||
} | ||
} | ||
|
||
module.exports = fp(fastifyJWT, '>=0.13.1') |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "fastify-jwt", | ||
"version": "0.1.0", | ||
"description": "JWT utils for Fastify", | ||
"main": "jwt.js", | ||
"scripts": { | ||
"test": "standard && tap test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fastify/fastify-jwt.git" | ||
}, | ||
"keywords": [ | ||
"jwt", | ||
"json", | ||
"token", | ||
"jsonwebtoken", | ||
"fastify" | ||
], | ||
"author": "Tomas Della Vedova - @delvedor (http://delved.org)", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/fastify/fastify-jwt/issues" | ||
}, | ||
"homepage": "https://github.com/fastify/fastify-jwt#readme", | ||
"dependencies": { | ||
"fastify-plugin": "^0.1.0", | ||
"jsonwebtoken": "^7.4.1" | ||
}, | ||
"devDependencies": { | ||
"fastify": "^0.21.0", | ||
"standard": "^10.0.2", | ||
"tap": "^10.3.3" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const Fastify = require('fastify') | ||
const jwt = require('./jwt') | ||
|
||
test('fastify-jwt should expose jwt methods', t => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
t.ok(fastify.jwt.sign) | ||
t.ok(fastify.jwt.verify) | ||
t.ok(fastify.jwt.decode) | ||
t.ok(fastify.jwt.secret) | ||
}) | ||
}) | ||
|
||
test('jwt.secret should be the same as the one given as option', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
t.is(fastify.jwt.secret, 'supersecret') | ||
}) | ||
}) | ||
|
||
test('sync sign and verify', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
const token = fastify.jwt.sign({ hello: 'world' }) | ||
t.ok(token) | ||
t.equal(fastify.jwt.verify(token).hello, 'world') | ||
}) | ||
}) | ||
|
||
test('async sign and verify', t => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
fastify.jwt.sign({ hello: 'world' }, (err, token) => { | ||
t.error(err) | ||
t.ok(token) | ||
fastify.jwt.verify(token, (err, payload) => { | ||
t.error(err) | ||
t.equal(payload.hello, 'world') | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
test('should throw if no secret is given as option', t => { | ||
t.plan(1) | ||
const fastify = Fastify() | ||
fastify.register(jwt, err => { | ||
t.is(err.message, 'missing secret') | ||
}) | ||
}) | ||
|
||
test('sign should throw if the payload is missing', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
try { | ||
fastify.jwt.sign() | ||
t.fail() | ||
} catch (err) { | ||
t.is(err.message, 'missing payload') | ||
} | ||
}) | ||
}) | ||
|
||
test('verify should throw if the token is missing', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify | ||
.register(jwt, { secret: 'supersecret' }, t.error) | ||
.after(() => { | ||
try { | ||
fastify.jwt.verify() | ||
t.fail() | ||
} catch (err) { | ||
t.is(err.message, 'missing token') | ||
} | ||
}) | ||
}) |