Skip to content

Commit

Permalink
feature: formatUser option (#269)
Browse files Browse the repository at this point in the history
* formatUser

* working test for formatting user

* fix nearform action

* revert nearform github action

* improve types for formatUser

* revert describe.only

* fix tests

* fix merge

* chore: lint

---------

Co-authored-by: Rômulo Vitoi <[email protected]>
  • Loading branch information
taylorjames and RomuloVitoi authored Mar 21, 2023
1 parent 0e7f24a commit 97cd5ab
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FastifyPluginCallback, FastifyReply, FastifyRequest } from 'fastify'
import '@fastify/jwt'
import { UserType, SignPayloadType } from '@fastify/jwt'

import NodeCache from 'node-cache'

Expand Down Expand Up @@ -51,6 +51,10 @@ export interface FastifyAuth0VerifyOptions {
*/
signed?: boolean
}
/**
* You may customize the request.user object setting a custom sync function as parameter:
*/
readonly formatUser?: (payload: SignPayloadType) => UserType
}

export interface Auth0Verify extends Pick<FastifyAuth0VerifyOptions, 'domain' | 'audience' | 'secret'> {
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ function fastifyAuth0Verify(instance, options, done) {
verify: auth0Options.verify,
cookie: options.cookie,
secret: getSecret,
jwtDecode: 'jwtDecode'
jwtDecode: 'jwtDecode',
formatUser: options.formatUser
})

// Setup our decorators
Expand Down
6 changes: 6 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ fastify.register(fastifyAuth0Verify, {
signed: true
}
})
fastify.register(fastifyAuth0Verify, {
domain: '<auth0 auth domain>',
issuer: '<auth0 issuer>',
audience: '<auth0 app audience>',
formatUser: () => ({ foo: 'bar' }),
})

fastify.register(function (instance, _options, done) {
instance.get('/verify', {
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,38 @@ describe('JWT cookie token decoding', function () {
})
})

describe('format decoded token', function () {
let server

beforeAll(async function () {
server = await buildServer({
secret: 'secret',
token: 'token',
cookie: { cookieName: 'token' },
formatUser: user => {
return {
sub: user.sub,
username: user.name,
admin: user.admin
}
}
})
})

afterAll(() => server.close())

it('should format verified user', async function () {
const response = await server.inject({
method: 'GET',
url: '/verify',
headers: { Authorization: `Bearer ${tokens.hs256Valid}` }
})

expect(response.statusCode).toEqual(200)
expect(response.json()).toEqual({ sub: '1234567890', username: 'John Doe', admin: true })
})
})

describe('HS256 JWT token validation', function () {
let server

Expand Down

0 comments on commit 97cd5ab

Please sign in to comment.