New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/auth

Package Overview
Dependencies
Maintainers
0
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/auth - npm Package Compare versions

Comparing version 5.0.1 to 5.0.2

6

auth.js

@@ -40,9 +40,7 @@ 'use strict'

/* eslint-disable-next-line no-var */
for (var i = 0; i < functions.length; i++) {
for (let i = 0; i < functions.length; i++) {
if (Array.isArray(functions[i]) === false) {
functions[i] = functions[i].bind(this)
} else {
/* eslint-disable-next-line no-var */
for (var j = 0; j < functions[i].length; j++) {
for (let j = 0; j < functions[i].length; j++) {
if (Array.isArray(functions[i][j])) {

@@ -49,0 +47,0 @@ throw new Error('Nesting sub-arrays is not supported')

{
"name": "@fastify/auth",
"version": "5.0.1",
"version": "5.0.2",
"description": "Run multiple auth functions in Fastify",

@@ -9,2 +9,16 @@ "repository": {

},
"bugs": {
"url": "https://github.com/fastify/fastify-auth/issues"
},
"homepage": "https://github.com/fastify/fastify-auth#readme",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/fastify"
},
{
"type": "opencollective",
"url": "https://opencollective.com/fastify"
}
],
"main": "auth.js",

@@ -19,3 +33,3 @@ "type": "commonjs",

"test:typescript": "tsd",
"test:unit": "tap"
"test:unit": "c8 --100 node --test"
},

@@ -32,2 +46,15 @@ "keywords": [

"email": "hello@matteocollina.com"
},
{
"name": "Manuel Spigolon",
"email": "behemoth89@gmail.com"
},
{
"name": "Aras Abbasi",
"email": "aras.abbasi@gmail.com"
},
{
"name": "Frazer Smith",
"email": "frazer.dev@icloud.com",
"url": "https://github.com/fdawgs"
}

@@ -43,6 +70,6 @@ ],

"@types/node": "^22.0.0",
"c8": "^10.1.2",
"fastify": "^5.0.0",
"rimraf": "^6.0.1",
"standard": "^17.1.0",
"tap": "^18.7.1",
"tsd": "^0.31.1"

@@ -49,0 +76,0 @@ },

# @fastify/auth
![CI](https://github.com/fastify/fastify-auth/workflows/CI/badge.svg)
[![CI](https://github.com/fastify/fastify-auth/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/fastify/fastify-auth/actions/workflows/ci.yml)
[![NPM version](https://img.shields.io/npm/v/@fastify/auth.svg?style=flat)](https://www.npmjs.com/package/@fastify/auth)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
This module does not provide an authentication strategy, but it provides a very fast utility to handle authentication (and multiple strategies) in your routes, without adding overhead.
This module does not provide an authentication strategy, but it provides a very fast utility to handle authentication (and multiple strategies) in your routes, without adding overhead.
Check out a complete example [here](test/example.js).

@@ -15,2 +15,16 @@

### Compatibility
| Plugin version | Fastify version |
| ---------------|-----------------|
| `^5.x` | `^5.x` |
| `^3.x` | `^4.x` |
| `^1.x` | `^3.x` |
| `^0.x` | `^2.x` |
| `^0.x` | `^1.x` |
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin
in the table above.
See [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.
## Usage

@@ -115,6 +129,6 @@ As said above, `@fastify/auth` does not provide an authentication strategy, so you must provide authentication strategies yourself, with a decorator or another plugin.

| auth code | resulting logical expression |
| ------------- |:-------------:|
| `fastify.auth([f1, f2, [f3, f4]], { relation: 'or' })` | `f1 OR f2 OR (f3 AND f4)` |
| `fastify.auth([f1, f2, [f3, f4]], { relation: 'and' })` | `f1 AND f2 AND (f3 OR f4)` |
| auth code | resulting logical expression |
| ------------- |:-------------:|
| `fastify.auth([f1, f2, [f3, f4]], { relation: 'or' })` | `f1 OR f2 OR (f3 AND f4)` |
| `fastify.auth([f1, f2, [f3, f4]], { relation: 'and' })` | `f1 AND f2 AND (f3 OR f4)` |

@@ -121,0 +135,0 @@

'use strict'
const { test } = require('tap')
const { test } = require('node:test')
const Fastify = require('fastify')
const fastifyAuth = require('../auth')
test('registering plugin with invalid default relation', async (t) => {
test('registering plugin with invalid default relation', (t, done) => {
t.plan(2)

@@ -14,8 +14,9 @@

fastify.ready((err) => {
t.ok(err)
t.equal(err.message, 'The value of default relation should be one of [\'or\', \'and\']')
t.assert.ok(err)
t.assert.strictEqual(err.message, 'The value of default relation should be one of [\'or\', \'and\']')
done()
})
})
test('Clean status code through auth pipeline', t => {
test('Clean status code through auth pipeline', (t, done) => {
t.plan(3)

@@ -37,5 +38,6 @@

}, (err, res) => {
t.error(err)
t.equal(res.payload, '42')
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '42')
t.assert.strictEqual(res.statusCode, 200)
done()
})

@@ -73,3 +75,3 @@ })

})
t.equal(response.statusCode, 502)
t.assert.strictEqual(response.statusCode, 502)

@@ -83,6 +85,6 @@ const res = await app.inject({

})
t.equal(res.statusCode, 200)
t.assert.strictEqual(res.statusCode, 200)
})
test('Options: non-array functions input', t => {
test('Options: non-array functions input', (t, done) => {
t.plan(4)

@@ -96,4 +98,4 @@

} catch (error) {
t.ok(error)
t.equal(error.message, 'You must give an array of functions to the auth function')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'You must give an array of functions to the auth function')
}

@@ -106,8 +108,9 @@ })

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
test('Options: empty array functions input', t => {
test('Options: empty array functions input', (t, done) => {
t.plan(4)

@@ -121,4 +124,4 @@

} catch (error) {
t.ok(error)
t.equal(error.message, 'Missing auth functions')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'Missing auth functions')
}

@@ -131,8 +134,9 @@ })

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
test('Options: faulty relation', t => {
test('Options: faulty relation', (t, done) => {
t.plan(4)

@@ -146,4 +150,4 @@

} catch (error) {
t.ok(error)
t.equal(error.message, 'The value of options.relation should be one of [\'or\', \'and\']')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.relation should be one of [\'or\', \'and\']')
}

@@ -156,8 +160,9 @@ })

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
test('Options: faulty run', t => {
test('Options: faulty run', (t, done) => {
t.plan(4)

@@ -171,4 +176,4 @@

} catch (error) {
t.ok(error)
t.equal(error.message, 'The value of options.run must be \'all\'')
t.assert.ok(error)
t.assert.strictEqual(error.message, 'The value of options.run must be \'all\'')
}

@@ -181,8 +186,9 @@ })

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 404)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 404)
done()
})
})
test('Avoid status code overwriting', t => {
test('Avoid status code overwriting', (t, done) => {
t.plan(3)

@@ -204,9 +210,10 @@

}, (err, res) => {
t.error(err)
t.equal(res.payload, '42')
t.equal(res.statusCode, 202)
t.assert.ifError(err)
t.assert.strictEqual(res.payload, '42')
t.assert.strictEqual(res.statusCode, 202)
done()
})
})
test('Last win when all failures', t => {
test('Last win when all failures', (t, done) => {
t.plan(2)

@@ -228,8 +235,9 @@

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 502)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 502)
done()
})
})
test('First success win', t => {
test('First success win', (t, done) => {
t.plan(2)

@@ -251,4 +259,5 @@

}, (err, res) => {
t.error(err)
t.equal(res.statusCode, 202)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 202)
done()
})

@@ -255,0 +264,0 @@ })

@@ -135,13 +135,2 @@ 'use strict'

if (require.main === module) {
const fastify = build({
logger: {
level: 'info'
}
})
fastify.listen({ port: 3000, host: '0.0.0.0' }, err => {
if (err) throw err
})
}
module.exports = build
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const { rimrafSync } = require('rimraf')

@@ -11,3 +10,3 @@ const build = require('./example-async')

t.before(() => {
test.before(() => {
rimrafSync('./authdb')

@@ -17,3 +16,3 @@ fastify = build()

t.teardown(async () => {
test.after(async () => {
await fastify.close()

@@ -23,3 +22,3 @@ rimrafSync('./authdb')

test('Route without auth', t => {
test('Route without auth', (t, done) => {
t.plan(2)

@@ -31,9 +30,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Missing header', t => {
test('Missing header', (t, done) => {
t.plan(2)

@@ -46,5 +46,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -54,6 +54,7 @@ message: 'Missing token header',

})
done()
})
})
test('Register user', t => {
test('Register user', (t, done) => {
t.plan(3)

@@ -69,11 +70,12 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 200)
t.assert.strictEqual(res.statusCode, 200)
token = payload.token
t.equal(typeof payload.token, 'string')
t.assert.strictEqual(typeof payload.token, 'string')
done()
})
})
test('Auth succesful', t => {
test('Auth successful', (t, done) => {
t.plan(2)

@@ -88,9 +90,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Auth succesful (multiple)', t => {
test('Auth successful (multiple)', (t, done) => {
t.plan(2)

@@ -106,9 +109,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Auth not succesful', t => {
test('Auth not successful', (t, done) => {
t.plan(2)

@@ -123,5 +127,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -131,6 +135,7 @@ message: 'Token not valid',

})
done()
})
})
test('Auth not succesful (multiple)', t => {
test('Auth not successful (multiple)', (t, done) => {
t.plan(2)

@@ -146,5 +151,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -154,6 +159,7 @@ message: 'Password not valid',

})
done()
})
})
test('Failure with explicit reply', t => {
test('Failure with explicit reply', (t, done) => {
t.plan(3)

@@ -170,7 +176,8 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 401)
t.same(payload, { error: 'Unauthorized' })
t.assert.strictEqual(res.statusCode, 401)
t.assert.deepStrictEqual(payload, { error: 'Unauthorized' })
done()
})
})

@@ -306,13 +306,2 @@ 'use strict'

if (require.main === module) {
const fastify = build({
logger: {
level: 'info'
}
})
fastify.listen({ port: 3000, host: '0.0.0.0' }, err => {
if (err) throw err
})
}
module.exports = build
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const build = require('./example-composited')

@@ -9,11 +8,11 @@

t.teardown(async () => {
test.after(async () => {
await fastify.close()
})
t.before(() => {
test.before(() => {
fastify = build()
})
test('And Relation success for single case', t => {
test('And Relation success for single case', (t, done) => {
t.plan(2)

@@ -28,9 +27,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('And Relation failed for single case', t => {
test('And Relation failed for single case', (t, done) => {
t.plan(2)

@@ -45,5 +45,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -53,6 +53,7 @@ message: '`n` is not odd',

})
done()
})
})
test('And Relation sucess for single [Array] case', t => {
test('And Relation sucess for single [Array] case', (t, done) => {
t.plan(2)

@@ -67,9 +68,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('And Relation failed for single [Array] case', t => {
test('And Relation failed for single [Array] case', (t, done) => {
t.plan(2)

@@ -84,5 +86,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -92,6 +94,7 @@ message: '`n` is not odd',

})
done()
})
})
test('Or Relation success for single case', t => {
test('Or Relation success for single case', (t, done) => {
t.plan(2)

@@ -106,9 +109,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Or Relation failed for single case', t => {
test('Or Relation failed for single case', (t, done) => {
t.plan(2)

@@ -123,5 +127,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -131,6 +135,7 @@ message: '`n` is not odd',

})
done()
})
})
test('Or Relation success for single [Array] case', t => {
test('Or Relation success for single [Array] case', (t, done) => {
t.plan(2)

@@ -145,9 +150,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Or Relation failed for single [Array] case', t => {
test('Or Relation failed for single [Array] case', (t, done) => {
t.plan(2)

@@ -162,5 +168,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -170,6 +176,7 @@ message: '`n` is not odd',

})
done()
})
})
test('And Relation failed for first check', t => {
test('And Relation failed for first check', (t, done) => {
t.plan(2)

@@ -184,5 +191,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -192,6 +199,7 @@ message: 'type of `n` is not `number`',

})
done()
})
})
test('And Relation failed for first check', t => {
test('And Relation failed for first check', (t, done) => {
t.plan(2)

@@ -206,5 +214,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -214,6 +222,7 @@ message: 'type of `n` is not `number`',

})
done()
})
})
test('And Relation failed for second check', t => {
test('And Relation failed for second check', (t, done) => {
t.plan(2)

@@ -228,5 +237,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -236,6 +245,7 @@ message: '`n` is not odd',

})
done()
})
})
test('And Relation success', t => {
test('And Relation success', (t, done) => {
t.plan(3)

@@ -250,10 +260,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('[Array] notation And Relation success', t => {
test('[Array] notation And Relation success', (t, done) => {
t.plan(3)

@@ -268,10 +279,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('And Relation with Or relation inside sub-array success', t => {
test('And Relation with Or relation inside sub-array success', (t, done) => {
t.plan(3)

@@ -286,10 +298,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('And Relation with Or relation inside sub-array failed', t => {
test('And Relation with Or relation inside sub-array failed', (t, done) => {
t.plan(2)

@@ -304,5 +317,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -312,6 +325,7 @@ message: '`n` is not big',

})
done()
})
})
test('And Relation with Or relation inside sub-array with async functions success', t => {
test('And Relation with Or relation inside sub-array with async functions success', (t, done) => {
t.plan(3)

@@ -326,10 +340,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('And Relation with Or relation inside sub-array with async functions failed', t => {
test('And Relation with Or relation inside sub-array with async functions failed', (t, done) => {
t.plan(2)

@@ -344,5 +359,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -352,6 +367,7 @@ message: '`n` is not big',

})
done()
})
})
test('Or Relation success under first case', t => {
test('Or Relation success under first case', (t, done) => {
t.plan(3)

@@ -366,10 +382,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('[Array] notation Or Relation success under first case', t => {
test('[Array] notation Or Relation success under first case', (t, done) => {
t.plan(3)

@@ -384,10 +401,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('Or Relation success under second case', t => {
test('Or Relation success under second case', (t, done) => {
t.plan(3)

@@ -402,10 +420,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('[Array] notation Or Relation success under second case', t => {
test('[Array] notation Or Relation success under second case', (t, done) => {
t.plan(3)

@@ -420,10 +439,11 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.equal(res.statusCode, 200)
t.assert.deepStrictEqual(payload, { hello: 'world' })
t.assert.strictEqual(res.statusCode, 200)
done()
})
})
test('Or Relation failed for both case', t => {
test('Or Relation failed for both case', (t, done) => {
t.plan(2)

@@ -438,5 +458,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -446,6 +466,7 @@ message: '`n` is not big',

})
done()
})
})
test('[Array] notation Or Relation failed for both case', t => {
test('[Array] notation Or Relation failed for both case', (t, done) => {
t.plan(2)

@@ -460,5 +481,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -468,6 +489,7 @@ message: '`n` is not big',

})
done()
})
})
test('single [Array] And Relation success', t => {
test('single [Array] And Relation success', (t, done) => {
t.plan(2)

@@ -482,9 +504,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('single [Array] And Relation failed', t => {
test('single [Array] And Relation failed', (t, done) => {
t.plan(2)

@@ -499,5 +522,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -507,6 +530,7 @@ message: '`n` is not odd',

})
done()
})
})
test('Two sub-arrays Or Relation success', t => {
test('Two sub-arrays Or Relation success', (t, done) => {
t.plan(2)

@@ -521,9 +545,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Two sub-arrays Or Relation called sequentially', t => {
test('Two sub-arrays Or Relation called sequentially', (t, done) => {
t.plan(2)

@@ -538,13 +563,14 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
verifyBigAsyncCalled: true,
verifyOddAsyncCalled: false
})
done()
})
})
test('Two sub-arrays Or Relation fail', t => {
test('Two sub-arrays Or Relation fail', (t, done) => {
t.plan(2)

@@ -559,5 +585,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -567,6 +593,7 @@ message: '`n` is not odd',

})
done()
})
})
test('[Array] notation & single case Or Relation success under first case', t => {
test('[Array] notation & single case Or Relation success under first case', (t, done) => {
t.plan(2)

@@ -581,9 +608,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('[Array] notation & single case Or Relation success under second case', t => {
test('[Array] notation & single case Or Relation success under second case', (t, done) => {
t.plan(2)

@@ -598,9 +626,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('[Array] notation & single case Or Relation failed', t => {
test('[Array] notation & single case Or Relation failed', (t, done) => {
t.plan(2)

@@ -615,5 +644,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -623,6 +652,7 @@ message: '`n` is not big',

})
done()
})
})
test('And Relation with Or relation inside sub-array with run: all', t => {
test('And Relation with Or relation inside sub-array with run: all', (t, done) => {
t.plan(2)

@@ -637,5 +667,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
odd: true,

@@ -645,6 +675,7 @@ big: false,

})
done()
})
})
test('Or Relation with And relation inside sub-array with run: all', t => {
test('Or Relation with And relation inside sub-array with run: all', (t, done) => {
t.plan(2)

@@ -659,5 +690,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
odd: false,

@@ -667,6 +698,7 @@ big: true,

})
done()
})
})
test('Check run all line fail with AND', t => {
test('Check run all line fail with AND', (t, done) => {
t.plan(8)

@@ -681,7 +713,7 @@

preHandler: fastify.auth([
(request, reply, done) => { t.pass('executed 1'); done() },
(request, reply, done) => { t.pass('executed 2'); done(new Error('second')) },
(request, reply, done) => { t.pass('executed 3'); done() },
(request, reply, done) => { t.pass('executed 4'); done() },
(request, reply, done) => { t.pass('executed 5'); done(new Error('fifth')) }
(request, reply, done) => { t.assert.ok('executed 1'); done() },
(request, reply, done) => { t.assert.ok('executed 2'); done(new Error('second')) },
(request, reply, done) => { t.assert.ok('executed 3'); done() },
(request, reply, done) => { t.assert.ok('executed 4'); done() },
(request, reply, done) => { t.assert.ok('executed 5'); done(new Error('fifth')) }
], { relation: 'and', run: 'all' }),

@@ -693,6 +725,6 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-pipe', (err, res) => {
t.error(err)
t.equal(res.statusCode, 401)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 401)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -702,6 +734,7 @@ message: 'second',

})
done()
})
})
test('Check run all line with AND', t => {
test('Check run all line with AND', (t, done) => {
t.plan(8)

@@ -716,7 +749,7 @@

preHandler: fastify.auth([
(request, reply, done) => { t.pass('executed 1'); done() },
(request, reply, done) => { t.pass('executed 2'); done() },
(request, reply, done) => { t.pass('executed 3'); done() },
(request, reply, done) => { t.pass('executed 4'); done() },
(request, reply, done) => { t.pass('executed 5'); done() }
(request, reply, done) => { t.assert.ok('executed 1'); done() },
(request, reply, done) => { t.assert.ok('executed 2'); done() },
(request, reply, done) => { t.assert.ok('executed 3'); done() },
(request, reply, done) => { t.assert.ok('executed 4'); done() },
(request, reply, done) => { t.assert.ok('executed 5'); done() }
], { relation: 'and', run: 'all' }),

@@ -728,10 +761,11 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-pipe', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Check run all line with OR', t => {
test('Check run all line with OR', (t, done) => {
t.plan(8)

@@ -746,7 +780,7 @@

preHandler: fastify.auth([
(req, reply, done) => { t.pass('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.pass('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.pass('executed 3'); done() },
(req, reply, done) => { t.pass('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.pass('executed 5'); done() }
(req, reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.assert.ok('executed 3'); done() },
(req, reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.assert.ok('executed 5'); done() }
], { relation: 'or', run: 'all' }),

@@ -758,10 +792,11 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-pipe', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Check run all fail line with OR', t => {
test('Check run all fail line with OR', (t, done) => {
t.plan(8)

@@ -776,7 +811,7 @@

preHandler: fastify.auth([
(req, reply, done) => { t.pass('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.pass('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.pass('executed 3'); done(new Error('terzo')) },
(req, reply, done) => { t.pass('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.pass('executed 5'); done(new Error('quinto')) }
(req, reply, done) => { t.assert.ok('executed 1'); done(new Error('primo')) },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('secondo')) },
(req, reply, done) => { t.assert.ok('executed 3'); done(new Error('terzo')) },
(req, reply, done) => { t.assert.ok('executed 4'); done(new Error('quarto')) },
(req, reply, done) => { t.assert.ok('executed 5'); done(new Error('quinto')) }
], { relation: 'or', run: 'all' }),

@@ -788,6 +823,6 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-pipe', (err, res) => {
t.error(err)
t.equal(res.statusCode, 401)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 401)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -797,6 +832,7 @@ message: 'quinto',

})
done()
})
})
test('Ignore last status', t => {
test('Ignore last status', (t, done) => {
t.plan(5)

@@ -811,4 +847,4 @@

preHandler: fastify.auth([
(req, reply, done) => { t.pass('executed 1'); done() },
(req, reply, done) => { t.pass('executed 2'); done(new Error('last')) }
(req, reply, done) => { t.assert.ok('executed 1'); done() },
(req, reply, done) => { t.assert.ok('executed 2'); done(new Error('last')) }
], { relation: 'or', run: 'all' }),

@@ -820,10 +856,11 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-status', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Or Relation run all', t => {
test('Or Relation run all', (t, done) => {
t.plan(2)

@@ -838,5 +875,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
odd: true,

@@ -846,6 +883,7 @@ big: false,

})
done()
})
})
test('Or Relation run all fail', t => {
test('Or Relation run all fail', (t, done) => {
t.plan(2)

@@ -860,5 +898,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -868,6 +906,7 @@ message: 'type of `n` is not `number`',

})
done()
})
})
test('Nested sub-arrays not supported', t => {
test('Nested sub-arrays not supported', (t, done) => {
t.plan(1)

@@ -877,7 +916,8 @@ try {

} catch (err) {
t.same(err.message, 'Nesting sub-arrays is not supported')
t.assert.deepStrictEqual(err.message, 'Nesting sub-arrays is not supported')
done()
}
})
test('And Relation run all', t => {
test('And Relation run all', (t, done) => {
t.plan(2)

@@ -892,5 +932,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
odd: true,

@@ -900,6 +940,7 @@ big: true,

})
done()
})
})
test('Clean status code settle by user', t => {
test('Clean status code settle by user', (t, done) => {
t.plan(5)

@@ -914,4 +955,4 @@

preHandler: fastify.auth([
(req, reply, done) => { t.pass('executed 1'); done() },
(req, reply, done) => { t.pass('executed 2'); reply.code(400); done(new Error('last')) }
(req, reply, done) => { t.assert.ok('executed 1'); done() },
(req, reply, done) => { t.assert.ok('executed 2'); reply.code(400); done(new Error('last')) }
], { relation: 'or', run: 'all' }),

@@ -923,7 +964,8 @@ handler: (req, reply) => { reply.send({ hello: 'world' }) }

fastify.inject('/run-all-status', (err, res) => {
t.error(err)
t.equal(res.statusCode, 200)
t.assert.ifError(err)
t.assert.strictEqual(res.statusCode, 200)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})

@@ -167,13 +167,2 @@ 'use strict'

if (require.main === module) {
const fastify = build({
logger: {
level: 'info'
}
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
}
module.exports = build
'use strict'
const t = require('tap')
const test = t.test
const { test } = require('node:test')
const { rimrafSync } = require('rimraf')

@@ -11,3 +10,3 @@ const build = require('./example')

t.before(() => {
test.before(() => {
rimrafSync('./authdb')

@@ -17,3 +16,3 @@ fastify = build()

t.teardown(async () => {
test.after(async () => {
await fastify.close()

@@ -23,3 +22,3 @@ rimrafSync('./authdb')

test('Route without auth', t => {
test('Route without auth', (t, done) => {
t.plan(2)

@@ -31,9 +30,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Missing header', t => {
test('Missing header', (t, done) => {
t.plan(2)

@@ -46,5 +46,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -54,6 +54,7 @@ message: 'Missing token header',

})
done()
})
})
test('Register user', t => {
test('Register user', (t, done) => {
t.plan(3)

@@ -69,11 +70,12 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 200)
t.assert.strictEqual(res.statusCode, 200)
token = payload.token
t.equal(typeof payload.token, 'string')
t.assert.strictEqual(typeof payload.token, 'string')
done()
})
})
test('Auth successful', t => {
test('Auth successful', (t, done) => {
t.plan(2)

@@ -88,9 +90,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Auth not successful', t => {
test('Auth not successful', (t, done) => {
t.plan(2)

@@ -105,5 +108,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
code: 'FAST_JWT_MALFORMED',

@@ -114,6 +117,7 @@ error: 'Unauthorized',

})
done()
})
})
test('Auth successful (multiple)', t => {
test('Auth successful (multiple)', (t, done) => {
t.plan(2)

@@ -129,9 +133,10 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, { hello: 'world' })
t.assert.deepStrictEqual(payload, { hello: 'world' })
done()
})
})
test('Auth not successful (multiple)', t => {
test('Auth not successful (multiple)', (t, done) => {
t.plan(2)

@@ -147,5 +152,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -155,6 +160,7 @@ message: 'Password not valid',

})
done()
})
})
test('Failure with missing user', t => {
test('Failure with missing user', (t, done) => {
t.plan(2)

@@ -169,5 +175,5 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
t.assert.deepStrictEqual(payload, {
error: 'Unauthorized',

@@ -177,6 +183,7 @@ message: 'Missing user in request body',

})
done()
})
})
test('Failure with explicit reply', t => {
test('Failure with explicit reply', (t, done) => {
t.plan(3)

@@ -193,7 +200,8 @@

}, (err, res) => {
t.error(err)
t.assert.ifError(err)
const payload = JSON.parse(res.payload)
t.equal(res.statusCode, 401)
t.same(payload, { error: 'Unauthorized' })
t.assert.strictEqual(res.statusCode, 401)
t.assert.deepStrictEqual(payload, { error: 'Unauthorized' })
done()
})
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc