Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor committed Jun 7, 2017
1 parent 2c5af5b commit b8dbf58
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
61 changes: 61 additions & 0 deletions jwt.js
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')
35 changes: 35 additions & 0 deletions package.json
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"
}
}
95 changes: 95 additions & 0 deletions test.js
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')
}
})
})

0 comments on commit b8dbf58

Please sign in to comment.