Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/typedoc-0.24.4
Browse files Browse the repository at this point in the history
  • Loading branch information
adamjmcgrath authored Apr 17, 2023
2 parents 859f0f5 + 472e57e commit 776ff64
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/auth0-session/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ export interface SessionConfig {
*
* **IMPORTANT** You must use a suitably unique value to prevent collisions.
*/
genId?: <Req = any>(req: Req) => string | Promise<string>;
genId?: <Req = any, SessionType extends { [key: string]: any } = { [key: string]: any }>(
req: Req,
session: SessionType
) => string | Promise<string>;

/**
* If you want your session duration to be rolling, resetting everytime the
Expand Down
2 changes: 1 addition & 1 deletion src/auth0-session/get-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const paramsSchema = Joi.object({
autoSave: Joi.boolean().optional().default(true),
name: Joi.string().token().optional().default('appSession'),
store: Joi.object().optional(),
genId: Joi.function().maxArity(1).when(Joi.ref('store'), { then: Joi.required() }),
genId: Joi.function().maxArity(2).when(Joi.ref('store'), { then: Joi.required() }),
storeIDToken: Joi.boolean().optional().default(true),
cookie: Joi.object({
domain: Joi.string().optional(),
Expand Down
2 changes: 1 addition & 1 deletion src/auth0-session/session/stateful-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class StatefulSession<
}

if (!sessionId) {
sessionId = await genId!(req);
sessionId = await genId!(req, session);
debug('generated new session id %o', sessionId);
}
debug('set session %o', sessionId);
Expand Down
5 changes: 4 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,10 @@ export interface SessionConfig {
* **IMPORTANT** If you override this, you must use a suitable value from your platform to
* prevent collisions. For example, for Node: `require('crypto').randomBytes(16).toString('hex')`.
*/
genId?: <Req = any>(req: Req) => string | Promise<string>;
genId?: <Req = any, SessionType extends { [key: string]: any } = { [key: string]: any }>(
req: Req,
session: SessionType
) => string | Promise<string>;

/**
* If you want your session duration to be rolling, resetting everytime the
Expand Down
11 changes: 11 additions & 0 deletions tests/auth0-session/session/stateful-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ describe('StatefulSession', () => {
expect(cookieJar.getCookieStringSync(baseURL)).toMatch(/^appSession=foobar\..+/);
});

it('should provide current user session to custom session id generator', async () => {
const genId = jest.fn().mockImplementation((_req, session) => session.state);
const baseURL = await setup({ ...config, session: { ...config.session, genId } });
const cookieJar = await login(baseURL);
const genIdParams = genId.mock.calls.at(0);
expect(genIdParams.length).toEqual(2);
expect('id_token' in genIdParams.at(1)).toBeTruthy();
const regex = `^appSession=${genIdParams.at(1).state}\..+`;
expect(cookieJar.getCookieStringSync(baseURL)).toMatch(new RegExp(regex));
});

it('should regenerate the session when a new user is logging in over an existing user', async () => {
await store.set('foo', await getPayload());
const baseURL = await setup(config);
Expand Down
11 changes: 11 additions & 0 deletions tests/stateful-session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,15 @@ describe('next stateful session', () => {
expect(Object.keys(store)).toHaveLength(1);
expect(cookieJar.getCookieStringSync(baseUrl)).toMatch(/^appSession=foo\..+/);
});

it('should provide current user session to custom session id generator', async () => {
const genId = jest.fn().mockImplementation((_req, session) => session.user.nickname);
const baseURL = await setup({ ...config, session: { ...config.session, genId } });
const cookieJar = await login(baseURL);
const genIdParams = genId.mock.calls.at(0);
expect(genIdParams.length).toEqual(2);
expect('idToken' in genIdParams.at(1)).toBeTruthy();
expect('user' in genIdParams.at(1)).toBeTruthy();
expect(cookieJar.getCookieStringSync(baseURL)).toMatch(/^appSession=__test_nickname__\..+/);
});
});

0 comments on commit 776ff64

Please sign in to comment.