Skip to content

Commit

Permalink
Docs update - JWKS example (#51)
Browse files Browse the repository at this point in the history
* JWKS example updated in docs
* fast-jwt integration test updated
Co-authored-by: Sameer Srivastava <sameer.srivastava@nearform.com>
  • Loading branch information
sameer-coder committed Apr 26, 2021
1 parent c30b1b5 commit 4c60102
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
31 changes: 14 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,26 @@ fastify.listen(3000)
The following example shows how to use JWKS in fast-jwt via get-jwks.

```js
const { createDecoder, createVerifier } = require('fast-jwt')
const { createVerifier } = require('fast-jwt')
const buildGetJwks = require('get-jwks')

// JWT signed with JWKS
const token = '...'

// well known url of the token issuer
// often encoded as the `iss` property of the token payload
const domain = 'https://...'

// complete is necessary to get the header
const decode = createDecoder({ complete: true })

// decode the token and extract the header
const {
header: { kid, alg },
} = decode(token)

const getJwks = buildGetJwks()
const publicKey = await getJwks.getPublicKey({ kid, domain, alg })

const verifyWithPromise = createVerifier({ key: publicKey })
const getJwks = buildGetJwks({ allowedDomains: [...]})

// create a verifier function with key as a function
const verifyWithPromise = createVerifier({
key: async function (token) {
const publicKey = await getJwks.getPublicKey({
kid: token.kid,
alg: token.alg,
domain,
})
return publicKey
},
})

// verify the token via the public key
const payload = await verifyWithPromise(token)
```
25 changes: 13 additions & 12 deletions test/fast-jwt-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const t = require('tap')
const nock = require('nock')
const { createDecoder, createVerifier } = require('fast-jwt')
const { createVerifier } = require('fast-jwt')

const { jwks, token } = require('./constants')
const buildGetJwks = require('../src/get-jwks')
Expand All @@ -17,19 +17,20 @@ t.afterEach(async () => {
})

t.test('fast-jwt integration tests', async t => {
const myDomain = 'https://localhost/'
nock(myDomain).get('/.well-known/jwks.json').reply(200, jwks)

const decodeComplete = createDecoder({ complete: true })
const sections = decodeComplete(token)
const {
header: { kid, alg },
} = sections
const domain = 'https://localhost/'
nock(domain).get('/.well-known/jwks.json').reply(200, jwks)

const getJwks = buildGetJwks()
const publicKey = await getJwks.getPublicKey({ kid, domain: myDomain, alg })

const verifyWithPromise = createVerifier({ key: publicKey })
const verifyWithPromise = createVerifier({
key: async function (token) {
const publicKey = await getJwks.getPublicKey({
kid: token.kid,
alg: token.alg,
domain,
})
return publicKey
},
})
const payload = await verifyWithPromise(token)

t.equal(payload.name, 'Jane Doe')
Expand Down

0 comments on commit 4c60102

Please sign in to comment.