Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fastify v3 #15

Merged
merged 10 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dependabot/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: 1
update_configs:
- package_manager: "javascript"
directory: "/"
update_schedule: "daily"
29 changes: 29 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Node CI

on: [push, pull_request]

jobs:
test:
name: Test
runs-on: ${{ matrix.os }}

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
npm install

- name: Test
run: |
npm test
39 changes: 19 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# fastify-basic-auth

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
[![Build Status](https://dev.azure.com/fastify/fastify/_apis/build/status/fastify.fastify-basic-auth?branchName=master)](https://dev.azure.com/fastify/fastify/_build/latest?definitionId=7&branchName=master)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/) ![Node CI](https://github.com/fastify/fastify-basic-auth/workflows/Node%20CI/badge.svg)

A simple basic auth plugin for Fastify.

Expand All @@ -10,7 +9,7 @@
npm i fastify-basic-auth
```
## Usage
This plugin decorates the fastify instance with a `basicAuth` function, which you can use inside a `preHandler` hook, in a `beforeHandler` or with [`fastify-auth`](https://github.com/fastify/fastify-auth).
This plugin decorates the fastify instance with a `basicAuth` function, which you can use inside any hook before your route handler, or with [`fastify-auth`](https://github.com/fastify/fastify-auth).

```js
const fastify = require('fastify')()
Expand All @@ -26,7 +25,7 @@ function validate (username, password, req, reply, done) {
}

fastify.after(() => {
fastify.addHook('preHandler', fastify.basicAuth)
fastify.addHook('onRequest', fastify.basicAuth)

fastify.get('/', (req, reply) => {
reply.send({ hello: 'world' })
Expand All @@ -46,7 +45,7 @@ async function validate (username, password, req, reply) {
}
```

Use with `preHandler`:
Use with `onRequest`:
```js
const fastify = require('fastify')()
const authenticate = {realm: 'Westeros'}
Expand All @@ -61,7 +60,7 @@ fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
preHandler: fastify.basicAuth,
onRequest: fastify.basicAuth,
handler: async (req, reply) => {
return { hello: 'world' }
}
Expand All @@ -88,8 +87,8 @@ fastify.after(() => {
fastify.route({
method: 'GET',
url: '/',
// use beforeHanderto authenticatejust this one
preHandler: fastify.auth([fastify.basicAuth]),
// use onRequest to authenticate just this one
onRequest: fastify.auth([fastify.basicAuth]),
handler: async (req, reply) => {
return { hello: 'world' }
}
Expand Down Expand Up @@ -121,34 +120,34 @@ fastify.setErrorHandler(function (err, req, reply) {

### `validate` <Function> (required)

The `validate` function is called on each request made,
and is passed the `username`, `password`, `req` and `reply`
The `validate` function is called on each request made,
and is passed the `username`, `password`, `req` and `reply`
parameters in that order. An optional fifth parameter, `done` may be
used to signify a valid request when called with no arguments,
or an invalid request when called with an `Error` object. Alternatively,
the `validate` function may return a promise, resolving for valid
used to signify a valid request when called with no arguments,
or an invalid request when called with an `Error` object. Alternatively,
the `validate` function may return a promise, resolving for valid
requests and rejecting for invalid. This can also be achieved using
an `async/await` function, and throwing for invalid requests.

See code above for examples.

### `authenticate` <Boolean|Object> (optional, default: false)

When supplied, the `authenticate` option will cause the
When supplied, the `authenticate` option will cause the
[`WWW-Authenticate` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/WWW-Authenticate) to be added. It may also be used to set the `realm` value.

This can be useful in situations where we want to trigger client-side authentication interfaces - for instance the browser authentication dialog.

As a boolean setting `authenticate` to `true` will set a header like so: `WWW-Authenticate: Basic`. When `false`, no header is added. This is the default.

```js
fastify.register(require('fastify-basic-auth'), {
validate,
fastify.register(require('fastify-basic-auth'), {
validate,
authenticate: true // WWW-Authenticate: Basic
})

fastify.register(require('fastify-basic-auth'), {
validate,
fastify.register(require('fastify-basic-auth'), {
validate,
authenticate: false // no authenticate header, same as omitting authenticate option
})
```
Expand All @@ -158,8 +157,8 @@ When supplied as an object the `authenticate` option may have a `realm` key.
If the `realm` key is supplied, it will be appended to the header value:

```js
fastify.register(require('fastify-basic-auth'), {
validate,
fastify.register(require('fastify-basic-auth'), {
validate,
authenticate: {realm: 'example'} // WWW-Authenticate: Basic realm="example"
})
```
Expand Down
25 changes: 0 additions & 25 deletions azure-pipelines.yml

This file was deleted.

47 changes: 26 additions & 21 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import fastify = require('fastify');

import { Server, IncomingMessage, ServerResponse } from 'http';
import {
FastifyRequest,
FastifyPlugin,
FastifyReply,
onRequestHookHandler,
preParsingHookHandler,
preValidationHookHandler,
preHandlerHookHandler
} from 'fastify'

declare module 'fastify' {
interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> {
basicAuth: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>;
interface FastifyInstance {
basicAuth: onRequestHookHandler |
preParsingHookHandler |
preValidationHookHandler |
preHandlerHookHandler
}
}

declare const fastifyBasicAuth: fastify.Plugin<
Server,
IncomingMessage,
ServerResponse,
{
validate: (
username: string,
password: string,
req: fastify.FastifyRequest,
reply: fastify.FastifyReply<ServerResponse>,
done: (err?: Error) => void
) => void;
authenticate?: boolean | { realm: string };
}
>;
export interface FastifyBasicAuthOptions {
validate(
username: string,
password: string,
req: FastifyRequest,
reply: FastifyReply,
done: (err?: Error) => void
): void | Promise<void>;
authenticate?: boolean | { realm: string };
}

export = fastifyBasicAuth;
declare const fastifyBasicAuth: FastifyPlugin<FastifyBasicAuthOptions>
export default fastifyBasicAuth;
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ function getAuthenticateHeader (authenticate) {
}

module.exports = fp(basicPlugin, {
fastify: '>=1.0.0',
fastify: '3.x',
name: 'fastify-basic-auth'
})
36 changes: 36 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expectType, expectAssignable } from 'tsd'
import fastify, {
FastifyRequest,
FastifyReply,
onRequestHookHandler,
preParsingHookHandler,
preValidationHookHandler,
preHandlerHookHandler
} from 'fastify'
import fastifyBasicAuth from '.'

const app = fastify()

app.register(fastifyBasicAuth, {
validate: async function validatePromise (username, password, req, reply) {
expectType<string>(username)
expectType<string>(password)
expectType<FastifyRequest>(req)
expectType<FastifyReply>(reply)
}
})

app.register(fastifyBasicAuth, {
validate: function validateCallback (username, password, req, reply, done) {
expectType<string>(username)
expectType<string>(password)
expectType<FastifyRequest>(req)
expectType<FastifyReply>(reply)
expectAssignable<(err?: Error) => void>(done)
}
})

expectAssignable<onRequestHookHandler>(app.basicAuth)
expectAssignable<preParsingHookHandler>(app.basicAuth)
expectAssignable<preValidationHookHandler>(app.basicAuth)
expectAssignable<preHandlerHookHandler>(app.basicAuth)
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"typescript": "tsc --project ./test/types/tsconfig.json",
"test": "standard && tap test/*.test.js && npm run typescript"
"test": "standard && tap test.js && tsd"
},
"repository": {
"type": "git",
Expand All @@ -26,15 +25,18 @@
},
"homepage": "https://github.com/fastify/fastify-basic-auth#readme",
"devDependencies": {
"@types/node": "^12.7.12",
"fastify": "^2.0.0",
"fastify-auth": "^0.3.0",
"standard": "^11.0.1",
"tap": "^12.0.1"
"fastify": "^3.0.0-rc.1",
"fastify-auth": "^1.0.0",
"standard": "^14.3.3",
"tap": "^14.10.7",
"tsd": "^0.11.0"
},
"dependencies": {
"basic-auth": "^2.0.0",
"fastify-plugin": "^1.0.1",
"http-errors": "^1.7.2"
"basic-auth": "^2.0.1",
"fastify-plugin": "^2.0.0",
"http-errors": "^1.7.3"
},
"engines": {
"node": ">=10.0.0"
}
}
Loading