Skip to content

Commit

Permalink
Update for latest Fastify
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Dec 29, 2017
1 parent 16e15b3 commit 4138ec4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js

node_js:
- "9"
- "8"
- "6"

script:
- npm run lint-ci
- npm run test-ci

notifications:
email:
on_success: never
on_failure: always
13 changes: 4 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*fastify-bearer-auth* provides a simple request hook for the [Fastify][fastify]
web framework.

[fastify]: https://github.com/fastify/fastify
[fastify]: https://fastify.io/

## Example

Expand All @@ -14,22 +14,17 @@ const fastify = require('fastify')
const bearerAuthPlugin = require('fastify-bearer-auth')
const keys = new Set(['a-super-secret-key', 'another-super-secret-key'])

fastify.register(bearerAuthPlugin, {keys}, (err) => {
if (err) {
console.error(err.message)
process.exit(1)
}
})
fastify.register(bearerAuthPlugin, {keys})
fastify.get('/foo', (req, reply) => {
reply({authenticated: true})
})

fastify.listen({port: 8000}, (err) => {
if (err) {
console.error(err.message)
fastify.log.error(err.message)
process.exit(1)
}
console.log.info('http://127.0.0.1:8000/foo')
fastify.log.info('http://127.0.0.1:8000/foo')
})
```

Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
"description": "An authentication plugin for Fastify",
"main": "plugin.js",
"scripts": {
"test": "tap 'test/**/*.js'",
"lint": "standard"
"test": "tap 'test/**/*.test.js'",
"test-ci": "tap --cov 'test/**/*.test.js'",
"lint": "standard | snazzy",
"lint-ci": "standard"
},
"precommit": [
"lint",
Expand All @@ -26,14 +28,14 @@
},
"homepage": "https://github.com/fastify/fastify-bearer-auth#readme",
"devDependencies": {
"abstract-logging": "^1.0.0",
"fastify": "^0.27.0",
"fastify": "^0.37.0",
"pre-commit": "^1.2.2",
"request": "^2.81.0",
"snazzy": "^7.0.0",
"standard": "^10.0.1",
"tap": "^10.3.2"
"tap": "^11.0.1"
},
"dependencies": {
"fastify-plugin": "^0.1.0"
"fastify-plugin": "^0.2.1"
}
}
10 changes: 5 additions & 5 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ internals.factory = function (options) {
},
contentType: undefined
}
const _options = Object.assign(defaultOptions, options || {})
const keys = _options.keys
const errorResponse = _options.errorResponse
const contentType = _options.contentType
const _options = Object.assign({}, defaultOptions, options || {})
const {keys, errorResponse, contentType} = _options

function bearerAuthHook (fastifyReq, fastifyRes, next) {
const header = fastifyReq.req.headers['authorization']
Expand Down Expand Up @@ -47,5 +45,7 @@ function plugin (fastify, options, next) {
next()
}

module.exports = fp(plugin, '>=0.15.0')
module.exports = fp(plugin, {
fastify: '>=0.37.0'
})
module.exports.internals = internals
31 changes: 14 additions & 17 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

const tap = require('tap')
const test = tap.test
const fastify = require('fastify')({logger: require('abstract-logging')})
const fastify = require('fastify')()
const request = require('request')
const plugin = require('../')

fastify.register(
plugin,
{keys: new Set(['123456'])},
(err) => {
if (err) tap.error(err)
}
)
fastify.register(plugin, {keys: new Set(['123456'])})

fastify.get('/test1', (req, res) => {
res.send({hello: 'world'})
})

fastify.get('/test2', (req, res) => {
res.send({hello: 'world'})
})

fastify.get('/test3', (req, res) => {
res.send({hello: 'world'})
})

fastify.listen(0, (err) => {
if (err) tap.error(err)
Expand All @@ -26,9 +32,6 @@ fastify.listen(0, (err) => {

test('success route succeeds', (t) => {
t.plan(3)
fastify.get('/test1', (req, res) => {
res.send({hello: 'world'})
})
req({uri: '/test1', auth: {bearer: '123456'}}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
Expand All @@ -38,9 +41,6 @@ fastify.listen(0, (err) => {

test('invalid key route fails correctly', (t) => {
t.plan(3)
fastify.get('/test2', (req, res) => {
res.send({hello: 'world'})
})
req({uri: '/test2', auth: {bearer: '654321'}}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 401)
Expand All @@ -50,9 +50,6 @@ fastify.listen(0, (err) => {

test('missing header route fails correctly', (t) => {
t.plan(3)
fastify.get('/test3', (req, res) => {
res.send({hello: 'world'})
})
req({uri: '/test3'}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 401)
Expand Down

0 comments on commit 4138ec4

Please sign in to comment.