Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
auth() wrapper: switch to options passed in .with(auth, opts)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkli committed Aug 4, 2023
1 parent 75304d9 commit 597ae6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
25 changes: 10 additions & 15 deletions packages/frontegg-auth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import { handleAuthorization } from './authorization.js';
* }
*
* export const main = wrap(run)
* .with(auth({ permissions: 'some-permission' }))
* .with(auth, { permissions: 'some-permission' })
* .with(helixStatus);
* ```
*
Expand Down Expand Up @@ -123,33 +123,28 @@ export async function handleAuth(request, context, opts = {}) {
}

/**
* A helix-shared-wrap middleware (wrapper function) that handles authentication
* & authorization for all requests by calling {@link handleAuth}.
* Wraps a function with a Frontegg authentication and authorization middleware.
* Invokes {@link handleAuth} for all requests.
*
* Note that this must be invoked before being passed to `wrap().with(fn)`, unlike
* other wrapper functions, in order to support passing of custom options:
* ```
* export const main = wrap(run)
* // minimal
* .with(auth());
* .with(auth);
*
* // alternatively with options
* .with(auth({ permissions: 'some-permission' }));
* .with(auth, { permissions: 'some-permission' });
* ```
*
* @param {UniversalFunction} fn the universal function
* @param {AuthOptions} [opts] Options
* @returns {function} wrapper for use with @adobe/helix-shared-wrap
* @returns {function} an universal function with the added middleware
*/
export function auth(opts = {}) {
if (typeof opts === 'function') {
throw new Error('Developer error: auth() must be invoked before being passed to wrap().with(*) and expects an opts object as argument: wrap().with(auth({}))');
}

return (func) => async (request, context) => {
export function auth(fn, opts = {}) {
return async (request, context) => {
// on auth failures, this will throw an error which helix-universal 4.2.0+
// will catch and turn into a 401 or 403 http response.
await handleAuth(request, context, opts);

return func(request, context);
return fn(request, context);
};
}
24 changes: 5 additions & 19 deletions packages/frontegg-auth/test/wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ describe('auth wrapper', () => {
});
});

it('should throw error if auth is passed to wrap().with() without invocation', async () => {
const universalFn = async () => new Response('ok');

assert.throws(() => {
wrap(universalFn).with(auth);
});
});

it('should not throw error if auth() is invoked before passing to wrap().with()', async () => {
const universalFn = async (request, context) => {
// check that data is properly passed through
Expand All @@ -61,9 +53,7 @@ describe('auth wrapper', () => {
};

try {
const actualFn = wrap(universalFn).with(auth({
skip: true,
}));
const actualFn = wrap(universalFn).with(auth, { skip: true });

await actualFn(new Request('http://localhost?foo=bar'), {
log,
Expand All @@ -83,9 +73,7 @@ describe('auth wrapper', () => {
wentThrough = true;
return new Response('ok');
};
const actualFn = wrap(universalFn).with(auth({
skipAuthorization: true,
}));
const actualFn = wrap(universalFn).with(auth, { skipAuthorization: true });

const payload = {
tenantIds: ['123'],
Expand Down Expand Up @@ -117,9 +105,7 @@ describe('auth wrapper', () => {
wentThrough = true;
return new Response('ok');
};
const actualFn = wrap(universalFn).with(auth({
skipAuthorization: true,
}));
const actualFn = wrap(universalFn).with(auth, { skipAuthorization: true });

const payload = {
tenantIds: ['123'],
Expand Down Expand Up @@ -149,7 +135,7 @@ describe('auth wrapper', () => {
wentThrough = true;
return new Response('ok');
};
const actualFn = wrap(universalFn).with(auth());
const actualFn = wrap(universalFn).with(auth);

mockSpace('test.findmy.media', '123');
const token = createJWT({
Expand Down Expand Up @@ -182,7 +168,7 @@ describe('auth wrapper', () => {
wentThrough = true;
return new Response('ok');
};
const actualFn = wrap(universalFn).with(auth());
const actualFn = wrap(universalFn).with(auth);

mockSpace('test.findmy.media', '123');
const token = createJWT({
Expand Down

0 comments on commit 597ae6a

Please sign in to comment.