Skip to content

Commit 1ec77d1

Browse files
Fix webhook check in auth middleware
1 parent da3f024 commit 1ec77d1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/fastify/authenticationMiddleware.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ROUTES } from '../../shared/routes';
66
import { appContext } from '../applicationContext';
77
import { getPayloadUserId } from './jwt';
88

9+
const WEBHOOKS_BASE_PATH = '/api/webhooks/';
10+
911
// Middleware function
1012
export function singleUserMiddleware(req: FastifyRequest, _res: any, next: () => void): void {
1113
const user = appContext().userService.getSingleUser();
@@ -17,7 +19,7 @@ export function singleUserMiddleware(req: FastifyRequest, _res: any, next: () =>
1719

1820
export function jwtAuthMiddleware(req: FastifyRequest, reply: FastifyReply, done: () => void): void {
1921
// Skip auth for public endpoints
20-
if (req.raw.url.startsWith('/webhooks/') || req.raw.url === DEFAULT_HEALTHCHECK || req.raw.url.startsWith(ROUTES.AUTH_ROUTE_PREFIX)) {
22+
if (req.raw.url.startsWith(WEBHOOKS_BASE_PATH) || req.raw.url === DEFAULT_HEALTHCHECK || req.raw.url.startsWith(ROUTES.AUTH_ROUTE_PREFIX)) {
2123
done();
2224
return;
2325
}
@@ -51,12 +53,12 @@ export function jwtAuthMiddleware(req: FastifyRequest, reply: FastifyReply, done
5153

5254
export function googleIapMiddleware(req: FastifyRequest, reply: FastifyReply, next: () => void) {
5355
// It would be nicer if the health-check was earlier in the chain. Maybe when nextauthjs integration is done.
54-
if (req.raw.url.startsWith('/webhooks/') || req.raw.url === DEFAULT_HEALTHCHECK) {
56+
if (req.raw.url.startsWith(WEBHOOKS_BASE_PATH) || req.raw.url === DEFAULT_HEALTHCHECK) {
5557
next();
5658
return;
5759
}
5860
let email = req.headers['x-goog-authenticated-user-email'];
59-
if (!email) throw new Error('x-goog-authenticated-user-email header not found');
61+
if (!email) throw new Error(`x-goog-authenticated-user-email header not found requesting ${req.raw.url}`);
6062
if (Array.isArray(email)) email = email[0];
6163
// TODO validate the JWT https://cloud.google.com/iap/docs/signed-headers-howto#securing_iap_headers
6264

0 commit comments

Comments
 (0)