Skip to content

Commit e0fd02a

Browse files
committed
chore(backend): Generate suffixed/un-suffixed cookies in handshake
1 parent 27a1ea4 commit e0fd02a

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

packages/backend/src/tokens/cookie.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const getCookieName = (cookieDirective: string): string => {
2+
return cookieDirective.split(';')[0]?.split('=')[0];
3+
};
4+
5+
const getSuffixedName = (name: string, suffix: string): string => {
6+
if (name.endsWith(suffix)) {
7+
return name;
8+
}
9+
return `${name}_${suffix}`;
10+
};
11+
12+
export const suffixCookie = (suffix: string, cookieDirective: string): string => {
13+
const name = getCookieName(cookieDirective);
14+
const suffixedName = getSuffixedName(name, suffix);
15+
16+
return cookieDirective.replace(name + '=', suffixedName + '=');
17+
};
18+
19+
export const unSuffixCookie = (suffix: string, cookieDirective: string): string => {
20+
const name = getCookieName(cookieDirective).replace('_' + suffix, '');
21+
const suffixedName = getSuffixedName(suffix, cookieDirective);
22+
return cookieDirective.replace(suffixedName + '=', name + '=');
23+
};

packages/backend/src/tokens/request.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import type { TokenCarrier } from '../errors';
33
import { TokenVerificationError, TokenVerificationErrorReason } from '../errors';
44
import { decodeJwt } from '../jwt/verifyJwt';
55
import { assertValidSecretKey } from '../util/optionsAssertions';
6-
import { isDevelopmentFromSecretKey } from '../util/shared';
6+
import { getCookieSuffix, isDevelopmentFromSecretKey } from '../util/shared';
77
import type { AuthenticateContext } from './authenticateContext';
88
import { createAuthenticateContext } from './authenticateContext';
99
import type { RequestState } from './authStatus';
1010
import { AuthErrorReason, handshake, signedIn, signedOut } from './authStatus';
1111
import { createClerkRequest } from './clerkRequest';
12+
import { suffixCookie, unSuffixCookie } from './cookie';
1213
import { verifyHandshakeToken } from './handshake';
1314
import type { AuthenticateRequestOptions } from './types';
1415
import { verifyToken } from './verify';
@@ -105,12 +106,16 @@ export async function authenticateRequest(
105106

106107
const handshakePayload = await verifyHandshakeToken(authenticateContext.handshakeToken!, authenticateContext);
107108
const cookiesToSet = handshakePayload.handshake;
109+
const cookieSuffix = getCookieSuffix(authenticateContext.publishableKey);
108110

109111
let sessionToken = '';
110112
cookiesToSet.forEach((x: string) => {
111-
headers.append('Set-Cookie', x);
112-
if (x.startsWith(`${constants.Cookies.Session}=`)) {
113-
sessionToken = x.split(';')[0].substring(10);
113+
const suffixedCookie = suffixCookie(cookieSuffix, x);
114+
headers.append('Set-Cookie', suffixedCookie);
115+
const unSuffixedCookie = unSuffixCookie(cookieSuffix, x);
116+
headers.append('Set-Cookie', unSuffixedCookie);
117+
if (unSuffixedCookie.startsWith(`${constants.Cookies.Session}=`)) {
118+
sessionToken = unSuffixedCookie.split(';')[0].substring(10);
114119
}
115120
});
116121

packages/backend/src/util/shared.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export { addClerkPrefix, getScriptUrl, getClerkJsMajorVersionOrTag } from '@clerk/shared/url';
22
export { callWithRetry } from '@clerk/shared/callWithRetry';
3-
export { isDevelopmentFromSecretKey, isProductionFromSecretKey, parsePublishableKey } from '@clerk/shared/keys';
3+
export {
4+
isDevelopmentFromSecretKey,
5+
isProductionFromSecretKey,
6+
parsePublishableKey,
7+
getCookieSuffix,
8+
} from '@clerk/shared/keys';
49
export { deprecated, deprecatedProperty } from '@clerk/shared/deprecated';
510

611
import { buildErrorThrower } from '@clerk/shared/error';

0 commit comments

Comments
 (0)