Skip to content

Commit

Permalink
fix(deps): Replace fastify-auth0-verify with fastify-jwt-jwks (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea authored May 19, 2023
1 parent 22265d5 commit 1c974b9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 25 deletions.
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"date-fns": "^2.29.3",
"env-var": "^7.3.0",
"fastify": "^4.14.0",
"fastify-auth0-verify": "github:relaycorp/fastify-auth0-verify#auth0-agnostic-generated",
"fastify-jwt-jwks": "^1.1.3",
"fastify-plugin": "^4.5.0",
"is-valid-domain": "^0.1.6",
"json-schema-to-ts": "^2.8.0",
Expand Down
14 changes: 7 additions & 7 deletions src/utilities/fastify/plugins/jwksAuthentication.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { jest } from '@jest/globals';
import type { FastifyInstance } from 'fastify';
import envVar from 'env-var';
import fastifyOauth2Verify from 'fastify-auth0-verify';
import fastifyJwtJwks from 'fastify-jwt-jwks';

import { mockSpy } from '../../../testUtils/jest.js';
import {
Expand Down Expand Up @@ -34,12 +34,12 @@ describe('jwks-authentication', () => {
);
});

test('OAUTH2_JWKS_URL should be used as the domain', async () => {
test('OAUTH2_JWKS_URL should be honoured', async () => {
await jwksPlugin(mockFastify, {});

expect(mockFastify.register).toHaveBeenCalledWith(
fastifyOauth2Verify,
expect.objectContaining({ domain: OAUTH2_JWKS_URL }),
fastifyJwtJwks,
expect.objectContaining({ jwksUrl: OAUTH2_JWKS_URL }),
);
});

Expand Down Expand Up @@ -85,7 +85,7 @@ describe('jwks-authentication', () => {
await jwksPlugin(mockFastify, {});

expect(mockFastify.register).toHaveBeenCalledWith(
fastifyOauth2Verify,
fastifyJwtJwks,
expect.objectContaining({ issuer: OAUTH2_TOKEN_ISSUER }),
);
});
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('jwks-authentication', () => {
await jwksPlugin(mockFastify, {});

expect(mockFastify.register).toHaveBeenCalledWith(
fastifyOauth2Verify,
fastifyJwtJwks,
expect.objectContaining({ issuer: new RegExp(issuerRegex, 'u') }),
);
});
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('jwks-authentication', () => {
await jwksPlugin(mockFastify, {});

expect(mockFastify.register).toHaveBeenCalledWith(
fastifyOauth2Verify,
fastifyJwtJwks,
expect.objectContaining({ audience: OAUTH2_TOKEN_AUDIENCE }),
);
});
Expand Down
14 changes: 5 additions & 9 deletions src/utilities/fastify/plugins/jwksAuthentication.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import fastifyAuth0Verify, { type FastifyAuth0VerifyOptions } from 'fastify-auth0-verify';
import fastifyJwtJwks, { type FastifyJwtJwksOptions } from 'fastify-jwt-jwks';
import env from 'env-var';

function getOauth2PluginOptions(): FastifyAuth0VerifyOptions {
function getOauth2PluginOptions(): FastifyJwtJwksOptions {
const audience = env.get('OAUTH2_TOKEN_AUDIENCE').required().asString();
const domain = env.get('OAUTH2_JWKS_URL').required().asUrlString();
const jwksUrl = env.get('OAUTH2_JWKS_URL').required().asUrlString();

const issuer = env.get('OAUTH2_TOKEN_ISSUER').asUrlString();
const issuerRegex = env.get('OAUTH2_TOKEN_ISSUER_REGEX').asRegExp('u');
Expand All @@ -16,16 +16,12 @@ function getOauth2PluginOptions(): FastifyAuth0VerifyOptions {
throw new Error('Either OAUTH2_TOKEN_ISSUER or OAUTH2_TOKEN_ISSUER_REGEX should be set');
}

return {
audience,
domain,
issuer: issuer ?? (issuerRegex as any),
};
return { audience, jwksUrl, issuer: issuer ?? issuerRegex };
}

async function registerJwksPlugin(fastify: FastifyInstance): Promise<void> {
const options = getOauth2PluginOptions();
await fastify.register(fastifyAuth0Verify, options);
await fastify.register(fastifyJwtJwks, options);
}

const jwksPlugin = fastifyPlugin(registerJwksPlugin, {
Expand Down

0 comments on commit 1c974b9

Please sign in to comment.