Skip to content

Commit 23046d5

Browse files
feat(backend): Try the new Client Handshake mechanism
1 parent 02976d4 commit 23046d5

File tree

2 files changed

+241
-44
lines changed

2 files changed

+241
-44
lines changed

packages/backend/src/tokens/authStatus.ts

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export enum AuthStatus {
99
SignedIn = 'signed-in',
1010
SignedOut = 'signed-out',
1111
Interstitial = 'interstitial',
12+
Handshake = 'handshake',
1213
Unknown = 'unknown',
1314
}
1415

@@ -28,6 +29,7 @@ export type SignedInState = {
2829
isInterstitial: false;
2930
isUnknown: false;
3031
toAuth: () => SignedInAuthObject;
32+
headers: Headers | null;
3133
};
3234

3335
export type SignedOutState = {
@@ -46,6 +48,7 @@ export type SignedOutState = {
4648
isInterstitial: false;
4749
isUnknown: false;
4850
toAuth: () => SignedOutAuthObject;
51+
headers: Headers | null;
4952
};
5053

5154
export type InterstitialState = Omit<SignedOutState, 'isInterstitial' | 'status' | 'toAuth'> & {
@@ -54,13 +57,29 @@ export type InterstitialState = Omit<SignedOutState, 'isInterstitial' | 'status'
5457
toAuth: () => null;
5558
};
5659

60+
export type HandshakeState = Omit<SignedOutState, 'headers' | 'status' | 'toAuth'> & {
61+
status: AuthStatus.Handshake;
62+
headers: Headers;
63+
isInterstitial: false;
64+
toAuth: () => null;
65+
};
66+
5767
export type UnknownState = Omit<InterstitialState, 'status' | 'isInterstitial' | 'isUnknown'> & {
5868
status: AuthStatus.Unknown;
5969
isInterstitial: false;
6070
isUnknown: true;
6171
};
6272

6373
export enum AuthErrorReason {
74+
SessionTokenMissing = 'session-token-missing',
75+
SessionTokenWithoutClientUAT = 'session-token-but-no-client-uat',
76+
ClientUATWithoutSessionToken = 'client-uat-but-no-session-token',
77+
SessionTokenOutdated = 'session-token-outdated',
78+
ClockSkew = 'clock-skew',
79+
UnexpectedError = 'unexpected-error',
80+
Unknown = 'unknown',
81+
82+
// Delete these old crap
6483
CookieAndUATMissing = 'cookie-and-uat-missing',
6584
CookieMissing = 'cookie-missing',
6685
CookieOutDated = 'cookie-outdated',
@@ -73,13 +92,11 @@ export enum AuthErrorReason {
7392
PrimaryRespondsToSyncing = 'primary-responds-to-syncing',
7493
StandardSignedIn = 'standard-signed-in',
7594
StandardSignedOut = 'standard-signed-out',
76-
UnexpectedError = 'unexpected-error',
77-
Unknown = 'unknown',
7895
}
7996

8097
export type AuthReason = AuthErrorReason | TokenVerificationErrorReason;
8198

82-
export type RequestState = SignedInState | SignedOutState | InterstitialState | UnknownState;
99+
export type RequestState = SignedInState | SignedOutState | InterstitialState | HandshakeState | UnknownState;
83100

84101
type LoadResourcesOptions = {
85102
loadSession?: boolean;
@@ -99,12 +116,16 @@ type RequestStateParams = {
99116
};
100117

101118
type AuthParams = {
102-
/* Client token cookie value */
103-
cookieToken?: string;
119+
/* Session token cookie value */
120+
sessionTokenInCookie?: string;
121+
/* Client token header value */
122+
sessionTokenInHeader?: string;
104123
/* Client uat cookie value */
105124
clientUat?: string;
106-
/* Client token header value */
107-
headerToken?: string;
125+
/* DevBrowser token value */
126+
devBrowserToken?: string;
127+
/* Handshake token value */
128+
handshakeToken?: string;
108129
};
109130

110131
export type AuthStatusOptionsType = LoadResourcesOptions &
@@ -115,6 +136,7 @@ export type AuthStatusOptionsType = LoadResourcesOptions &
115136
export async function signedIn<T extends AuthStatusOptionsType>(
116137
options: T,
117138
sessionClaims: JwtPayload,
139+
headers: Headers | null = null,
118140
): Promise<SignedInState> {
119141
const {
120142
publishableKey = '',
@@ -128,8 +150,8 @@ export async function signedIn<T extends AuthStatusOptionsType>(
128150
secretKey,
129151
apiUrl,
130152
apiVersion,
131-
cookieToken,
132-
headerToken,
153+
sessionTokenInCookie,
154+
sessionTokenInHeader,
133155
loadSession,
134156
loadUser,
135157
loadOrganization,
@@ -159,7 +181,7 @@ export async function signedIn<T extends AuthStatusOptionsType>(
159181
secretKey,
160182
apiUrl,
161183
apiVersion,
162-
token: cookieToken || headerToken || '',
184+
token: sessionTokenInCookie || sessionTokenInHeader || '',
163185
session,
164186
user,
165187
organization,
@@ -183,12 +205,14 @@ export async function signedIn<T extends AuthStatusOptionsType>(
183205
isInterstitial: false,
184206
isUnknown: false,
185207
toAuth: () => authObject,
208+
headers,
186209
};
187210
}
188211
export function signedOut<T extends AuthStatusOptionsType>(
189212
options: T,
190213
reason: AuthReason,
191214
message = '',
215+
headers: Headers | null = null,
192216
): SignedOutState {
193217
const {
194218
publishableKey = '',
@@ -216,6 +240,7 @@ export function signedOut<T extends AuthStatusOptionsType>(
216240
isSignedIn: false,
217241
isInterstitial: false,
218242
isUnknown: false,
243+
headers,
219244
toAuth: () => signedOutAuthObject({ ...options, status: AuthStatus.SignedOut, reason, message }),
220245
};
221246
}
@@ -252,6 +277,44 @@ export function interstitial<T extends AuthStatusOptionsType>(
252277
isInterstitial: true,
253278
isUnknown: false,
254279
toAuth: () => null,
280+
headers: new Headers(),
281+
};
282+
}
283+
284+
export function handshake<T extends AuthStatusOptionsType>(
285+
options: T,
286+
reason: AuthReason,
287+
message = '',
288+
headers: Headers,
289+
): HandshakeState {
290+
const {
291+
publishableKey = '',
292+
proxyUrl = '',
293+
isSatellite = false,
294+
domain = '',
295+
signInUrl = '',
296+
signUpUrl = '',
297+
afterSignInUrl = '',
298+
afterSignUpUrl = '',
299+
} = options;
300+
301+
return {
302+
status: AuthStatus.Handshake,
303+
reason,
304+
message,
305+
publishableKey,
306+
isSatellite,
307+
domain,
308+
proxyUrl,
309+
signInUrl,
310+
signUpUrl,
311+
afterSignInUrl,
312+
afterSignUpUrl,
313+
isSignedIn: false,
314+
isUnknown: false,
315+
headers,
316+
isInterstitial: false,
317+
toAuth: () => null,
255318
};
256319
}
257320

@@ -283,5 +346,6 @@ export function unknownState(options: AuthStatusOptionsType, reason: AuthReason,
283346
isInterstitial: false,
284347
isUnknown: true,
285348
toAuth: () => null,
349+
headers: new Headers(),
286350
};
287351
}

0 commit comments

Comments
 (0)