Skip to content

Commit

Permalink
docs: update references to old fastify-* modules (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Apr 29, 2022
1 parent 01d74e6 commit b71829a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
52 changes: 26 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
# fastify-jwt
# @fastify/jwt

![CI](https://github.com/fastify/fastify-jwt/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/fastify-jwt.svg?style=flat)](https://www.npmjs.com/package/fastify-jwt)
[![NPM version](https://img.shields.io/npm/v/@fastify/jwt.svg?style=flat)](https://www.npmjs.com/package/@fastify/jwt)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-jwt/badge.svg)](https://snyk.io/test/github/fastify/fastify-jwt)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)

JWT utils for Fastify, internally it uses [fast-jwt](https://github.com/nearform/fast-jwt).

**NOTE:** The plugin has been migrated from using `jsonwebtoken` to `fast-jwt`. Even though `fast-jwt` has 1:1 feature implementation with `jsonwebtoken`, some _exotic_ implementations might break. In that case please open an issue with details of your implementation. See [Upgrading notes](UPGRADING.md) for more details about what changes this migration introduced.

`fastify-jwt` supports Fastify@3.
`fastify-jwt` [v1.x](https://github.com/fastify/fastify-jwt/tree/1.x)
`@fastify/jwt` supports Fastify@3.
`@fastify/jwt` [v1.x](https://github.com/fastify/fastify-jwt/tree/1.x)
supports both Fastify@2.

## Install
```
npm i fastify-jwt --save
npm i @fastify/jwt --save
```

## Usage
Register as a plugin. This will decorate your `fastify` instance with the following methods: `decode`, `sign`, and `verify`; refer to their documentation to find how to use the utilities. It will also register `request.jwtVerify` and `reply.jwtSign`. You must pass a `secret` when registering the plugin.

```js
const fastify = require('fastify')()
fastify.register(require('fastify-jwt'), {
fastify.register(require('@fastify/jwt'), {
secret: 'supersecret'
})

Expand All @@ -42,7 +42,7 @@ For verifying & accessing the decoded token inside your services, you can use a

```js
const fastify = require('fastify')()
fastify.register(require('fastify-jwt'), {
fastify.register(require('@fastify/jwt'), {
secret: 'supersecret'
})

Expand Down Expand Up @@ -71,7 +71,7 @@ However, most of the time we want to protect only some of the routes in our appl
const fp = require("fastify-plugin")

module.exports = fp(async function(fastify, opts) {
fastify.register(require("fastify-jwt"), {
fastify.register(require("@fastify/jwt"), {
secret: "supersecret"
})

Expand Down Expand Up @@ -101,7 +101,7 @@ module.exports = async function(fastify, opts) {
}
```

Make sure that you also check [fastify-auth](https://github.com/fastify/fastify-auth) plugin for composing more complex strategies.
Make sure that you also check [@fastify/auth](https://github.com/fastify/fastify-auth) plugin for composing more complex strategies.

### Auth0 tokens verification

Expand All @@ -123,7 +123,7 @@ Function based `secret` is supported by the `request.jwtVerify()` and `reply.jwt
const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')
// secret as a string
fastify.register(jwt, { secret: 'supersecret' })
// secret as a function with callback
Expand Down Expand Up @@ -167,7 +167,7 @@ fastify.register(jwt, {
sign: { algorithm: 'ES256' }
})
```
Optionally you can define global default options that will be used by `fastify-jwt` API if you do not override them.
Optionally you can define global default options that will be used by `@fastify/jwt` API if you do not override them.

Additionally, it is also possible to reject tokens selectively (i.e. blacklisting) by providing the option `trusted` with the following signature: `(request, decodedToken) => boolean|Promise<boolean>|SignPayloadType|Promise<SignPayloadType>` where `request` is a `FastifyRequest` and `decodedToken` is the parsed (and verified) token information. Its result should be `false` or `Promise<false>` if the token should be rejected or, otherwise, be `true` or `Promise<true>` if the token should be accepted and, considering that `request.user` will be used after that, the return should be `decodedToken` itself.

Expand All @@ -176,7 +176,7 @@ Additionally, it is also possible to reject tokens selectively (i.e. blacklistin
const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')
fastify.register(jwt, {
secret: {
private: readFileSync(`${path.join(__dirname, 'certs')}/private.pem`, 'utf8')
Expand Down Expand Up @@ -244,9 +244,9 @@ fastify.listen(3000, err => {

#### Example using cookie

In some situations you may want to store a token in a cookie. This allows you to drastically reduce the attack surface of XSS on your web app with the [`httpOnly`](https://wiki.owasp.org/index.php/HttpOnly) and `secure` flags. Cookies can be susceptible to CSRF. You can mitigate this by either setting the [`sameSite`](https://www.owasp.org/index.php/SameSite) flag to `strict`, or by using a CSRF library such as [`fastify-csrf`](https://www.npmjs.com/package/fastify-csrf).
In some situations you may want to store a token in a cookie. This allows you to drastically reduce the attack surface of XSS on your web app with the [`httpOnly`](https://wiki.owasp.org/index.php/HttpOnly) and `secure` flags. Cookies can be susceptible to CSRF. You can mitigate this by either setting the [`sameSite`](https://www.owasp.org/index.php/SameSite) flag to `strict`, or by using a CSRF library such as [`@fastify/csrf`](https://www.npmjs.com/package/@fastify/csrf).

**Note:** This plugin will look for a decorated request with the `cookies` property. [`fastify-cookie`](https://www.npmjs.com/package/fastify-cookie) supports this feature, and therefore you should use it when using the cookie feature. The plugin will fallback to looking for the token in the authorization header if either of the following happens (even if the cookie option is enabled):
**Note:** This plugin will look for a decorated request with the `cookies` property. [`@fastify/cookie`](https://www.npmjs.com/package/@fastify/cookie) supports this feature, and therefore you should use it when using the cookie feature. The plugin will fallback to looking for the token in the authorization header if either of the following happens (even if the cookie option is enabled):

- The request has both the authorization and cookie header
- Cookie is empty, authorization header is present
Expand All @@ -255,7 +255,7 @@ If you are signing your cookie, you can set the `signed` boolean to `true` which

```js
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
secret: 'foobar'
Expand All @@ -266,7 +266,7 @@ fastify.register(jwt, {
})

fastify
.register(require('fastify-cookie'))
.register(require('@fastify/cookie'))

fastify.get('/cookies', async (request, reply) => {
const token = await reply.jwtSign({
Expand Down Expand Up @@ -303,7 +303,7 @@ fastify.listen(3000, err => {
```js
const fastify = require('fastify')()

fastify.register(require('fastify-jwt'), {
fastify.register(require('@fastify/jwt'), {
secret: 'foobar',
trusted: validateToken
})
Expand Down Expand Up @@ -335,7 +335,7 @@ You may customize the `request.user` object setting a custom sync function as pa

```js
const fastify = require('fastify')();
fastify.register(require('fastify-jwt'), {
fastify.register(require('@fastify/jwt'), {
formatUser: function (user) {
return {
departmentName: user.department_name,
Expand Down Expand Up @@ -405,7 +405,7 @@ const myCustomMessages = {
}
}

fastify.register(require('fastify-jwt'), {
fastify.register(require('@fastify/jwt'), {
secret: 'supersecret',
messages: myCustomMessages
})
Expand Down Expand Up @@ -479,7 +479,7 @@ For your convenience, the `decode`, `sign`, `verify` and `messages` options you
const { readFileSync } = require('fs')
const path = require('path')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')
fastify.register(jwt, {
secret: {
private: readFileSync(`${path.join(__dirname, 'certs')}/private.key`),
Expand Down Expand Up @@ -555,7 +555,7 @@ As of 3.2.0, decorated when `options.jwtDecode` is truthy. Will become non-condi

### Algorithms supported

The following algorithms are currently supported by [fast-jwt](https://github.com/nearform/fast-jwt) that is internally used by `fastify-jwt`.
The following algorithms are currently supported by [fast-jwt](https://github.com/nearform/fast-jwt) that is internally used by `@fastify/jwt`.

**Name** | **Description**
----------------|----------------------------
Expand Down Expand Up @@ -585,7 +585,7 @@ You can find the list [here](https://github.com/nearform/fast-jwt#algorithms-sup
#### Signing and verifying (jwtSign, jwtVerify)
```js
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')
const request = require('request')

fastify.register(jwt, {
Expand Down Expand Up @@ -652,7 +652,7 @@ The following example integrates the [get-jwks](https://github.com/nearform/get-
##### Example
```js
const Fastify = require('fastify')
const fjwt = require('fastify-jwt')
const fjwt = require('@fastify/jwt')
const buildGetJwks = require('get-jwks')

const fastify = Fastify()
Expand Down Expand Up @@ -684,7 +684,7 @@ This plugin has two available exports, the default plugin function `fastifyJwt`
Import them like so:

```ts
import fastifyJwt, { FastifyJWTOptions } from 'fastify-jwt'
import fastifyJwt, { FastifyJWTOptions } from '@fastify/jwt'
```


Expand All @@ -693,9 +693,9 @@ Define custom Payload Type and Attached User Type to request object
```ts
// fastify-jwt.d.ts
import "fastify-jwt"
import "@fastify/jwt"

declare module "fastify-jwt" {
declare module "@fastify/jwt" {
interface FastifyJWT {
payload: { id: number } // payload type is used for signing and verifying
user: {
Expand Down
2 changes: 1 addition & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Upgrading Notes
This document captures breaking changes between versions of `fastify-jwt`.
This document captures breaking changes between versions of `@fastify/jwt`.

### Upgrading from 3.x to 4.0

Expand Down
8 changes: 4 additions & 4 deletions example/UsingCertificates.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Code example
```js
const { readFileSync } = require('fs')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
secret: {
Expand Down Expand Up @@ -43,7 +43,7 @@ Code example
```js
const { readFileSync } = require('fs')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
secret: {
Expand Down Expand Up @@ -74,7 +74,7 @@ Code example
```js
const { readFileSync } = require('fs')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
secret: {
Expand Down Expand Up @@ -103,7 +103,7 @@ Code example
```js
const { readFileSync } = require('fs')
const fastify = require('fastify')()
const jwt = require('fastify-jwt')
const jwt = require('@fastify/jwt')

fastify.register(jwt, {
secret: {
Expand Down

0 comments on commit b71829a

Please sign in to comment.