Skip to content

Commit

Permalink
fix breaking change for auth interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed Oct 18, 2019
1 parent 018db23 commit 60c596c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
20 changes: 11 additions & 9 deletions src/core/server/http/cookie_session_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class ScopedCookieSessionStorage<T extends Record<string, any>> implements Sessi
) {}
public async get(): Promise<T | null> {
try {
const session = await this.server.auth.test('security-cookie', this.request);
const { credentials: session } = await this.server.auth.test('security-cookie', this.request);
// A browser can send several cookies, if it's not an array, just return the session value
if (!Array.isArray(session)) {
return (session as unknown) as T;
return session as T;
}

// If we have an array with one value, we're good also
Expand Down Expand Up @@ -101,16 +101,18 @@ export async function createCookieSessionStorageFactory<T>(
await server.register({ plugin: hapiAuthCookie });

server.auth.strategy('security-cookie', 'cookie', {
cookie: cookieOptions.name,
password: cookieOptions.encryptionKey,
cookie: {
name: cookieOptions.name,
password: cookieOptions.encryptionKey,
path: basePath,
clearInvalid: true,
isSecure: cookieOptions.isSecure,
isHttpOnly: true,
isSameSite: false,
},
validateFunc: async (req: Request, session: T) => ({
valid: await cookieOptions.validate(session),
}),
isSecure: cookieOptions.isSecure,
path: basePath,
clearInvalid: true,
isHttpOnly: true,
isSameSite: false,
});

return {
Expand Down
30 changes: 19 additions & 11 deletions src/core/server/http/cookie_sesson_storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ const cookieOptions = {
encryptionKey: 'something_at_least_32_characters',
validate: (session: Storage) => session.expires > Date.now(),
isSecure: false,
path: '/',
};
const basePath = '/';

describe('Cookie based SessionStorage', () => {
describe('#set()', () => {
Expand All @@ -116,7 +116,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
innerServer,
cookieOptions
cookieOptions,
basePath
);
await server.start();

Expand Down Expand Up @@ -154,7 +155,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
innerServer,
cookieOptions
cookieOptions,
basePath
);
await server.start();

Expand Down Expand Up @@ -186,7 +188,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
innerServer,
cookieOptions
cookieOptions,
basePath
);
await server.start();

Expand Down Expand Up @@ -218,7 +221,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
innerServer,
cookieOptions
cookieOptions,
basePath
);
await server.start();

Expand Down Expand Up @@ -248,7 +252,7 @@ describe('Cookie based SessionStorage', () => {
register: jest.fn(),
auth: {
strategy: jest.fn(),
test: jest.fn(() => ['foo', 'bar']),
test: jest.fn(() => ({ credentials: ['foo', 'bar'] })),
},
};

Expand All @@ -257,7 +261,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
mockServer as any,
cookieOptions
cookieOptions,
basePath
);

expect(mockServer.register).toBeCalledTimes(1);
Expand All @@ -279,7 +284,7 @@ describe('Cookie based SessionStorage', () => {
register: jest.fn(),
auth: {
strategy: jest.fn(),
test: jest.fn(() => ['foo']),
test: jest.fn(() => ({ credentials: ['foo'] })),
},
};

Expand All @@ -288,7 +293,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
mockServer as any,
cookieOptions
cookieOptions,
basePath
);

expect(mockServer.register).toBeCalledTimes(1);
Expand Down Expand Up @@ -317,7 +323,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
mockServer as any,
cookieOptions
cookieOptions,
basePath
);

expect(mockServer.register).toBeCalledTimes(1);
Expand Down Expand Up @@ -349,7 +356,8 @@ describe('Cookie based SessionStorage', () => {
const factory = await createCookieSessionStorageFactory(
logger.get(),
innerServer,
cookieOptions
cookieOptions,
basePath
);
await server.start();

Expand Down

0 comments on commit 60c596c

Please sign in to comment.