Skip to content

Commit

Permalink
Call securityHandlers and securityErrorMapper with req and operation
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Oct 14, 2024
1 parent 4daa4a9 commit 6b614cd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ await fastify.register(import('@fastify/fastify-openapi-router-plugin'), {
}
},
securityHandlers: {
OAuth2: async (token, request) => {
OAuth2: async (token, request, operation) => {
// Validate and decode token.
const { userId } = verifyToken(token);

Expand Down Expand Up @@ -154,7 +154,7 @@ await fastify.register(import('@fastify/fastify-openapi-router-plugin'), {
// ...
}
},
securityErrorMapper: (unauthorizedError) => {
securityErrorMapper: (unauthorizedError, request, operation) => {
// Use `unauthorizedError.securityReport` to perform logic and return a custom error.
return MyUnauthorizedError();
},
Expand Down
4 changes: 2 additions & 2 deletions src/parser/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const applySecurity = (operation, spec, securityHandlers, securityErrorMa
let promise = promisesCache.get(name);

if (!promise) {
promise = new Promise(resolve => resolve(securityHandlers[name](value, request)));
promise = new Promise(resolve => resolve(securityHandlers[name](value, request, operation)));
promisesCache.set(name, promise);
}

Expand Down Expand Up @@ -95,7 +95,7 @@ export const applySecurity = (operation, spec, securityHandlers, securityErrorMa
if (!lastResult.ok) {
const error = createUnauthorizedError(report);

throw securityErrorMapper?.(error) ?? error;
throw securityErrorMapper?.(error, request, operation) ?? error;
}

// Otherwise, we can safely use the last result to decorate the request.
Expand Down
10 changes: 5 additions & 5 deletions src/parser/security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ describe('applySecurity()', () => {
await onRequest(request);

expect(securityHandlers.ApiKey).toHaveBeenCalledTimes(1);
expect(securityHandlers.ApiKey).toHaveBeenCalledWith('api key', request);
expect(securityHandlers.ApiKey).toHaveBeenCalledWith('api key', request, operation);
expect(securityHandlers.OAuth2).toHaveBeenCalledTimes(1);
expect(securityHandlers.OAuth2).toHaveBeenCalledWith('bearer token', request);
expect(securityHandlers.OAuth2).toHaveBeenCalledWith('bearer token', request, operation);
expect(securityHandlers.ApiKey2).not.toHaveBeenCalled();
expect(request[DECORATOR_NAME].security).toMatchObject({ ApiKey: 'ApiKey data', OAuth2: 'OAuth2 data' });
expect(request[DECORATOR_NAME].securityReport).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -185,9 +185,9 @@ describe('applySecurity()', () => {
await onRequest(request);

expect(securityHandlers.ApiKey).toHaveBeenCalledTimes(1);
expect(securityHandlers.ApiKey).toHaveBeenCalledWith('api key', request);
expect(securityHandlers.ApiKey).toHaveBeenCalledWith('api key', request, operation);
expect(securityHandlers.OAuth2).toHaveBeenCalledTimes(1);
expect(securityHandlers.OAuth2).toHaveBeenCalledWith('bearer token', request);
expect(securityHandlers.OAuth2).toHaveBeenCalledWith('bearer token', request, operation);
expect(request[DECORATOR_NAME].security).toMatchObject({ OAuth2: 'OAuth2 data' });
expect(request[DECORATOR_NAME].securityReport).toMatchInlineSnapshot(`
[
Expand Down Expand Up @@ -554,7 +554,7 @@ describe('applySecurity()', () => {
} catch (err) {
expect(err).toBe(customError);
expect(securityErrorMapper).toHaveBeenCalledTimes(1);
expect(securityErrorMapper.mock.calls[0][0]).toBeInstanceOf(errors.UnauthorizedError);
expect(securityErrorMapper).toHaveBeenCalledWith(expect.any(errors.UnauthorizedError), request, operation);
}
});
});

0 comments on commit 6b614cd

Please sign in to comment.