Skip to content

Commit

Permalink
Merge branch 'main' into yuhengsh-ci-test-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhengshs authored Feb 24, 2025
2 parents 9bd6d28 + 677f466 commit 19f5fc8
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ describe('handleSignInCallbackRequest', () => {
});
expect(mockCreateTokenCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
mockOrigin,
);
expect(mockCreateSignInFlowProofCookies).toHaveBeenCalledWith({
state: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ describe('handleSignInCallbackRequest', () => {
});
expect(mockCreateTokenCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
mockOrigin,
);
expect(mockCreateSignInFlowProofCookies).toHaveBeenCalledWith({
state: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isSSLOrigin,
} from '../../../src/auth/utils';

jest.mock('../../../src/auth/utils');
Expand All @@ -28,7 +27,6 @@ const mockCreateSignUpEndpoint = jest.mocked(createSignUpEndpoint);
const mockCreateUrlSearchParamsForSignInSignUp = jest.mocked(
createUrlSearchParamsForSignInSignUp,
);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignInSignUpRequest', () => {
const mockCustomState = 'mockCustomState';
Expand All @@ -40,10 +38,6 @@ describe('handleSignInSignUpRequest', () => {
};
const mockToCodeChallenge = jest.fn(() => 'mockCodeChallenge');

beforeAll(() => {
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
mockAppendSetCookieHeaders.mockClear();
mockCreateAuthFlowProofCookiesSetOptions.mockClear();
Expand All @@ -53,7 +47,6 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignUpEndpoint.mockClear();
mockCreateUrlSearchParamsForSignInSignUp.mockClear();
mockToCodeChallenge.mockClear();
mockIsSSLOrigin.mockClear();
});

test.each(['signIn' as const, 'signUp' as const])(
Expand Down Expand Up @@ -152,17 +145,14 @@ describe('handleSignInSignUpRequest', () => {

expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
secure: true,
},
mockOrigin,
);

expect(mockAppendSetCookieHeaders).toHaveBeenCalledWith(
expect.any(Headers),
mockCreateSignInFlowProofCookiesResult,
mockCreateAuthFlowProofCookiesSetOptionsResult,
);
expect(isSSLOrigin).toHaveBeenCalledWith(mockOrigin);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isSSLOrigin,
} from '../../../src/auth/utils';
import { createMockNextApiResponse } from '../testUtils';

Expand All @@ -32,7 +31,6 @@ const mockCreateSignUpEndpoint = jest.mocked(createSignUpEndpoint);
const mockCreateUrlSearchParamsForSignInSignUp = jest.mocked(
createUrlSearchParamsForSignInSignUp,
);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignInSignUpRequest', () => {
const mockCustomState = 'mockCustomState';
Expand All @@ -53,10 +51,6 @@ describe('handleSignInSignUpRequest', () => {
mockResponse,
} = createMockNextApiResponse();

beforeAll(() => {
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
mockAppendSetCookieHeadersToNextApiResponse.mockClear();
mockCreateAuthFlowProofCookiesSetOptions.mockClear();
Expand All @@ -66,7 +60,6 @@ describe('handleSignInSignUpRequest', () => {
mockCreateSignUpEndpoint.mockClear();
mockCreateUrlSearchParamsForSignInSignUp.mockClear();
mockToCodeChallenge.mockClear();
mockIsSSLOrigin.mockClear();

mockResponseAppendHeader.mockClear();
mockResponseEnd.mockClear();
Expand Down Expand Up @@ -177,17 +170,14 @@ describe('handleSignInSignUpRequest', () => {

expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
secure: true,
},
mockOrigin,
);

expect(mockAppendSetCookieHeadersToNextApiResponse).toHaveBeenCalledWith(
mockResponse,
mockCreateSignInFlowProofCookiesResult,
mockCreateAuthFlowProofCookiesSetOptionsResult,
);
expect(isSSLOrigin).toHaveBeenCalledWith(mockOrigin);
},
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../../../src/auth/utils';

Expand All @@ -21,20 +20,14 @@ const mockCreateSignOutFlowProofCookies = jest.mocked(
createSignOutFlowProofCookies,
);
const mockResolveRedirectSignOutUrl = jest.mocked(resolveRedirectSignOutUrl);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignOutRequest', () => {
beforeAll(() => {
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
mockAppendSetCookieHeaders.mockClear();
mockCreateAuthFlowProofCookiesSetOptions.mockClear();
mockCreateLogoutEndpoint.mockClear();
mockCreateSignOutFlowProofCookies.mockClear();
mockResolveRedirectSignOutUrl.mockClear();
mockIsSSLOrigin.mockClear();
});

it('returns a 302 response with the correct headers and cookies', async () => {
Expand Down Expand Up @@ -98,12 +91,9 @@ describe('handleSignOutRequest', () => {
expect.any(URLSearchParams),
);
expect(mockCreateSignOutFlowProofCookies).toHaveBeenCalled();
expect(mockIsSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
secure: true,
},
mockOrigin,
);
expect(mockAppendSetCookieHeaders).toHaveBeenCalledWith(
expect.any(Headers),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../../../src/auth/utils';
import { createMockNextApiResponse } from '../testUtils';
Expand All @@ -24,7 +23,6 @@ const mockCreateSignOutFlowProofCookies = jest.mocked(
createSignOutFlowProofCookies,
);
const mockResolveRedirectSignOutUrl = jest.mocked(resolveRedirectSignOutUrl);
const mockIsSSLOrigin = jest.mocked(isSSLOrigin);

describe('handleSignOutRequest', () => {
const {
Expand All @@ -36,10 +34,6 @@ describe('handleSignOutRequest', () => {
mockResponse,
} = createMockNextApiResponse();

beforeAll(() => {
mockIsSSLOrigin.mockReturnValue(true);
});

afterEach(() => {
mockAppendSetCookieHeadersToNextApiResponse.mockClear();
mockCreateAuthFlowProofCookiesSetOptions.mockClear();
Expand Down Expand Up @@ -121,12 +115,9 @@ describe('handleSignOutRequest', () => {
expect.any(URLSearchParams),
);
expect(mockCreateSignOutFlowProofCookies).toHaveBeenCalled();
expect(mockIsSSLOrigin).toHaveBeenCalledWith(mockOrigin);
expect(mockCreateAuthFlowProofCookiesSetOptions).toHaveBeenCalledWith(
mockSetCookieOptions,
{
secure: true,
},
mockOrigin,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ describe('createAuthFlowProofCookiesSetOptions', () => {
sameSite: 'strict',
};

const options = createAuthFlowProofCookiesSetOptions(setCookieOptions);
const options = createAuthFlowProofCookiesSetOptions(
setCookieOptions,
'https://example.com',
);

expect(options).toEqual({
domain: setCookieOptions?.domain,
Expand All @@ -61,9 +64,10 @@ describe('createAuthFlowProofCookiesSetOptions', () => {
sameSite: 'strict',
};

const options = createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: false,
});
const options = createAuthFlowProofCookiesSetOptions(
setCookieOptions,
'http://example.com',
);

expect(options).toEqual({
domain: setCookieOptions?.domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ describe('createTokenCookiesSetOptions', () => {
expires: new Date('2024-09-17'),
};

const result = createTokenCookiesSetOptions(mockSetCookieOptions);
const result = createTokenCookiesSetOptions(
mockSetCookieOptions,
'https://example.com',
);

expect(result).toEqual({
domain: mockSetCookieOptions.domain,
Expand All @@ -97,7 +100,7 @@ describe('createTokenCookiesSetOptions', () => {

it('returns an object with the default expiry and sameSite properties', () => {
const dateNowSpy = jest.spyOn(Date, 'now').mockReturnValue(0);
const result = createTokenCookiesSetOptions({});
const result = createTokenCookiesSetOptions({}, 'https://example.com');

expect(result).toEqual({
domain: undefined,
Expand All @@ -118,9 +121,10 @@ describe('createTokenCookiesSetOptions', () => {
expires: new Date('2024-09-17'),
};

const result = createTokenCookiesSetOptions(mockSetCookieOptions, {
secure: false,
});
const result = createTokenCookiesSetOptions(
mockSetCookieOptions,
'http://example.com',
);

expect(result).toEqual({
domain: mockSetCookieOptions.domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const handleSignInCallbackRequest: HandleSignInCallbackRequest = async ({
tokensPayload,
userPoolClientId,
}),
createTokenCookiesSetOptions(setCookieOptions),
createTokenCookiesSetOptions(setCookieOptions, origin),
);
appendSetCookieHeaders(
headers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const handleSignInCallbackRequestForPagesRouter: HandleSignInCallbackRequ
tokensPayload,
userPoolClientId,
}),
createTokenCookiesSetOptions(setCookieOptions),
createTokenCookiesSetOptions(setCookieOptions, origin),
);
appendSetCookieHeadersToNextApiResponse(
response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isSSLOrigin,
} from '../utils';

import { HandleSignInSignUpRequest } from './types';
Expand Down Expand Up @@ -44,9 +43,7 @@ export const handleSignInSignUpRequest: HandleSignInSignUpRequest = ({
appendSetCookieHeaders(
headers,
createSignInFlowProofCookies({ state, pkce: codeVerifier.value }),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isSSLOrigin(origin),
}),
createAuthFlowProofCookiesSetOptions(setCookieOptions, origin),
);

return new Response(null, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
createSignInFlowProofCookies,
createSignUpEndpoint,
createUrlSearchParamsForSignInSignUp,
isSSLOrigin,
} from '../utils';

import { HandleSignInSignUpRequestForPagesRouter } from './types';
Expand Down Expand Up @@ -38,9 +37,7 @@ export const handleSignInSignUpRequestForPagesRouter: HandleSignInSignUpRequestF
appendSetCookieHeadersToNextApiResponse(
response,
createSignInFlowProofCookies({ state, pkce: codeVerifier.value }),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isSSLOrigin(origin),
}),
createAuthFlowProofCookiesSetOptions(setCookieOptions, origin),
);

const redirectUrl =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../utils';

Expand All @@ -31,9 +30,7 @@ export const handleSignOutRequest: HandleSignOutRequest = ({
appendSetCookieHeaders(
headers,
createSignOutFlowProofCookies(),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isSSLOrigin(origin),
}),
createAuthFlowProofCookiesSetOptions(setCookieOptions, origin),
);

return new Response(null, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
createAuthFlowProofCookiesSetOptions,
createLogoutEndpoint,
createSignOutFlowProofCookies,
isSSLOrigin,
resolveRedirectSignOutUrl,
} from '../utils';

Expand All @@ -22,9 +21,7 @@ export const handleSignOutRequestForPagesRouter: HandleSignOutRequestForPagesRou
appendSetCookieHeadersToNextApiResponse(
response,
createSignOutFlowProofCookies(),
createAuthFlowProofCookiesSetOptions(setCookieOptions, {
secure: isSSLOrigin(origin),
}),
createAuthFlowProofCookiesSetOptions(setCookieOptions, origin),
);

response.redirect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
STATE_COOKIE_NAME,
} from '../constant';

import { isSSLOrigin } from './origin';

export const createSignInFlowProofCookies = ({
state,
pkce,
Expand All @@ -37,12 +39,12 @@ export const createSignOutFlowProofCookies = () => [

export const createAuthFlowProofCookiesSetOptions = (
setCookieOptions: CookieStorage.SetCookieOptions,
overrides?: Pick<CookieStorage.SetCookieOptions, 'secure'>,
origin: string,
) => ({
domain: setCookieOptions?.domain,
path: '/',
httpOnly: true,
secure: overrides?.secure ?? true,
secure: isSSLOrigin(origin),
sameSite: 'lax' as const,
maxAge: AUTH_FLOW_PROOF_MAX_AGE,
});
Expand Down
Loading

0 comments on commit 19f5fc8

Please sign in to comment.