@smartcrab/react 0.3.2 → 0.4.0-next-20261016193833
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/headless.d.ts +7 -0
- package/dist/headless.js +9 -3
- package/dist/hooks.d.ts +9 -1
- package/dist/hooks.js +33 -15
- package/dist/index.d.ts +1 -1
- package/dist/sign-in-flow.d.ts +33 -10
- package/dist/sign-in-flow.js +142 -54
- package/package.json +2 -2
- package/src/headless.tsx +26 -2
- package/src/hooks.ts +47 -15
- package/src/index.ts +3 -0
- package/src/sign-in-flow.adaptive.test.ts +374 -0
- package/src/sign-in-flow.ts +251 -78
package/src/sign-in-flow.ts
CHANGED
|
@@ -17,33 +17,63 @@ import { err, ok } from "@smartcrab/browser";
|
|
|
17
17
|
* without a renderer (react-dom is intentionally not a dependency here).
|
|
18
18
|
*
|
|
19
19
|
* Flow: idle → starting → awaiting_method → (email_challenge_sent |
|
|
20
|
-
* ceremony) → completing → signed_in.
|
|
20
|
+
* ceremony | challenge_required) → completing → signed_in. Other failures land
|
|
21
|
+
* in `failed`; challenge proof retry resumes the retained operation/transaction.
|
|
21
22
|
* `reset()` returns to `idle`. Social sign-in leaves the page via top-level
|
|
22
|
-
* redirect (design.md §22.3),
|
|
23
|
-
* redirect back, the app calls `client.handleHostedAuthorizationCallback`.
|
|
23
|
+
* redirect (design.md §22.3), then resumes through the callback handler.
|
|
24
24
|
*/
|
|
25
25
|
|
|
26
|
+
type AwaitingMethodState = {
|
|
27
|
+
readonly step: "awaiting_method";
|
|
28
|
+
readonly transactionId: string;
|
|
29
|
+
readonly methods: BrowserTransaction["availableMethods"];
|
|
30
|
+
readonly expiresAt: string;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type EmailChallengeSentState = {
|
|
34
|
+
readonly step: "email_challenge_sent";
|
|
35
|
+
readonly transactionId: string;
|
|
36
|
+
readonly challengeId: string;
|
|
37
|
+
readonly email: string;
|
|
38
|
+
readonly challengeExpiresAt: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type CeremonyState = {
|
|
42
|
+
readonly step: "ceremony";
|
|
43
|
+
readonly transactionId: string;
|
|
44
|
+
readonly ceremony: "passkey_registration" | "passkey_authentication";
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type SignInFlowChallengeResumeState =
|
|
48
|
+
| AwaitingMethodState
|
|
49
|
+
| EmailChallengeSentState
|
|
50
|
+
| CeremonyState;
|
|
51
|
+
|
|
52
|
+
export interface SignInFlowAdaptiveChallengeState {
|
|
53
|
+
readonly step: "challenge_required";
|
|
54
|
+
readonly challengeId: string;
|
|
55
|
+
readonly siteKey: string;
|
|
56
|
+
readonly action: Extract<
|
|
57
|
+
BrowserAuthError,
|
|
58
|
+
{ readonly type: "adaptive_challenge_required" }
|
|
59
|
+
>["action"];
|
|
60
|
+
readonly transactionId: string;
|
|
61
|
+
readonly requestId?: string;
|
|
62
|
+
readonly previousState: SignInFlowChallengeResumeState;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export type SignInFlowAdaptiveChallengeResult =
|
|
66
|
+
| { readonly challengeId: string }
|
|
67
|
+
| AuthenticatedSession
|
|
68
|
+
| { readonly redirected: true };
|
|
69
|
+
|
|
26
70
|
export type SignInFlowState =
|
|
27
71
|
| { readonly step: "idle" }
|
|
28
72
|
| { readonly step: "starting" }
|
|
29
|
-
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
readonly expiresAt: string;
|
|
34
|
-
}
|
|
35
|
-
| {
|
|
36
|
-
readonly step: "email_challenge_sent";
|
|
37
|
-
readonly transactionId: string;
|
|
38
|
-
readonly challengeId: string;
|
|
39
|
-
readonly email: string;
|
|
40
|
-
readonly challengeExpiresAt: string;
|
|
41
|
-
}
|
|
42
|
-
| {
|
|
43
|
-
readonly step: "ceremony";
|
|
44
|
-
readonly transactionId: string;
|
|
45
|
-
readonly ceremony: "passkey_registration" | "passkey_authentication";
|
|
46
|
-
}
|
|
73
|
+
| AwaitingMethodState
|
|
74
|
+
| EmailChallengeSentState
|
|
75
|
+
| CeremonyState
|
|
76
|
+
| SignInFlowAdaptiveChallengeState
|
|
47
77
|
| { readonly step: "completing"; readonly transactionId: string }
|
|
48
78
|
| { readonly step: "signed_in"; readonly session: AuthenticatedSession }
|
|
49
79
|
| { readonly step: "failed"; readonly error: BrowserAuthError };
|
|
@@ -109,6 +139,10 @@ export interface SignInFlow {
|
|
|
109
139
|
signInWithSocial(
|
|
110
140
|
provider: SocialProvider,
|
|
111
141
|
): Promise<Outcome<{ redirected: true }, SignInFlowError>>;
|
|
142
|
+
/** Replays the retained factor-initiation request with its Turnstile proof. */
|
|
143
|
+
retryAdaptiveChallenge(
|
|
144
|
+
challengeToken: string,
|
|
145
|
+
): Promise<Outcome<SignInFlowAdaptiveChallengeResult, SignInFlowError>>;
|
|
112
146
|
/** Best-effort server-side cancel, then back to `idle`. */
|
|
113
147
|
cancel(): Promise<Outcome<{ cancelled: true }, SignInFlowError>>;
|
|
114
148
|
/** Local-only reset (no server call). */
|
|
@@ -117,9 +151,25 @@ export interface SignInFlow {
|
|
|
117
151
|
|
|
118
152
|
const invalidState = (expected: string, actual: string): Outcome<never, SignInFlowError> =>
|
|
119
153
|
err({ type: "invalid_flow_state", expected, actual });
|
|
154
|
+
type AdaptiveChallengeRetry = (
|
|
155
|
+
token: string,
|
|
156
|
+
attemptId: number,
|
|
157
|
+
) => Promise<Outcome<SignInFlowAdaptiveChallengeResult, SignInFlowError>>;
|
|
158
|
+
|
|
159
|
+
interface PendingAdaptiveChallenge {
|
|
160
|
+
readonly pendingAction: Exclude<PendingAction, null>;
|
|
161
|
+
readonly retry: AdaptiveChallengeRetry;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
type AdaptiveChallengeAction = Extract<
|
|
165
|
+
BrowserAuthError,
|
|
166
|
+
{ readonly type: "adaptive_challenge_required" }
|
|
167
|
+
>["action"];
|
|
120
168
|
|
|
121
169
|
export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
122
170
|
let snapshot: SignInFlowSnapshot = { state: { step: "idle" }, pendingAction: null };
|
|
171
|
+
let pendingAdaptiveChallenge: PendingAdaptiveChallenge | null = null;
|
|
172
|
+
let challengeAttemptId = 0;
|
|
123
173
|
const listeners = new Set<SignInFlowListener>();
|
|
124
174
|
|
|
125
175
|
const transition = (state: SignInFlowState, pendingAction: PendingAction = null): void => {
|
|
@@ -130,9 +180,38 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
130
180
|
};
|
|
131
181
|
|
|
132
182
|
const fail = (error: BrowserAuthError): void => {
|
|
183
|
+
pendingAdaptiveChallenge = null;
|
|
184
|
+
challengeAttemptId += 1;
|
|
133
185
|
transition({ step: "failed", error });
|
|
134
186
|
};
|
|
135
187
|
|
|
188
|
+
const handleOperationFailure = (
|
|
189
|
+
error: BrowserAuthError,
|
|
190
|
+
action: AdaptiveChallengeAction,
|
|
191
|
+
resumeState: SignInFlowChallengeResumeState,
|
|
192
|
+
pendingAction: Exclude<PendingAction, null>,
|
|
193
|
+
retry: AdaptiveChallengeRetry,
|
|
194
|
+
): void => {
|
|
195
|
+
if (
|
|
196
|
+
error.type !== "adaptive_challenge_required" ||
|
|
197
|
+
error.action !== action ||
|
|
198
|
+
error.transactionId !== resumeState.transactionId
|
|
199
|
+
) {
|
|
200
|
+
fail(error);
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
pendingAdaptiveChallenge = { pendingAction, retry };
|
|
204
|
+
transition({
|
|
205
|
+
step: "challenge_required",
|
|
206
|
+
challengeId: error.challengeId,
|
|
207
|
+
siteKey: error.siteKey,
|
|
208
|
+
action: error.action,
|
|
209
|
+
transactionId: error.transactionId,
|
|
210
|
+
...(error.requestId !== undefined ? { requestId: error.requestId } : {}),
|
|
211
|
+
previousState: resumeState,
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
|
|
136
215
|
/** complete → exchange, shared by every verify path (design.md §16.3). */
|
|
137
216
|
const finalize = async (
|
|
138
217
|
transactionId: string,
|
|
@@ -178,46 +257,76 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
178
257
|
const sendEmailCode = async (
|
|
179
258
|
email: string,
|
|
180
259
|
): Promise<Outcome<{ challengeId: string }, SignInFlowError>> => {
|
|
181
|
-
const
|
|
182
|
-
if (
|
|
183
|
-
return invalidState("awaiting_method",
|
|
260
|
+
const flowState = snapshot.state;
|
|
261
|
+
if (flowState.step !== "awaiting_method") {
|
|
262
|
+
return invalidState("awaiting_method", flowState.step);
|
|
184
263
|
}
|
|
185
|
-
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
264
|
+
const state: AwaitingMethodState = flowState;
|
|
265
|
+
const retry: AdaptiveChallengeRetry = (token, attemptId) => run(token, attemptId);
|
|
266
|
+
async function run(
|
|
267
|
+
challengeToken?: string,
|
|
268
|
+
attemptId?: number,
|
|
269
|
+
): Promise<Outcome<{ challengeId: string }, SignInFlowError>> {
|
|
270
|
+
const outcome = await client.startEmailOtp(
|
|
271
|
+
state.transactionId,
|
|
272
|
+
challengeToken === undefined ? { email } : { email, challenge_token: challengeToken },
|
|
273
|
+
);
|
|
274
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
275
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
276
|
+
}
|
|
277
|
+
if (!outcome.ok) {
|
|
278
|
+
handleOperationFailure(outcome.error, "email_start", state, "send_email", retry);
|
|
279
|
+
return outcome;
|
|
280
|
+
}
|
|
281
|
+
pendingAdaptiveChallenge = null;
|
|
282
|
+
transition({
|
|
283
|
+
step: "email_challenge_sent",
|
|
284
|
+
transactionId: state.transactionId,
|
|
285
|
+
challengeId: outcome.value.challenge_id,
|
|
286
|
+
email,
|
|
287
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
288
|
+
});
|
|
289
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
190
290
|
}
|
|
191
|
-
transition(
|
|
192
|
-
|
|
193
|
-
transactionId: state.transactionId,
|
|
194
|
-
challengeId: outcome.value.challenge_id,
|
|
195
|
-
email,
|
|
196
|
-
challengeExpiresAt: outcome.value.expires_at,
|
|
197
|
-
});
|
|
198
|
-
return ok({ challengeId: outcome.value.challenge_id });
|
|
291
|
+
transition(state, "send_email");
|
|
292
|
+
return run(undefined, challengeAttemptId);
|
|
199
293
|
};
|
|
200
294
|
|
|
201
295
|
const resendEmailCode = async (): Promise<Outcome<{ challengeId: string }, SignInFlowError>> => {
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
204
|
-
return invalidState("email_challenge_sent",
|
|
296
|
+
const flowState = snapshot.state;
|
|
297
|
+
if (flowState.step !== "email_challenge_sent") {
|
|
298
|
+
return invalidState("email_challenge_sent", flowState.step);
|
|
205
299
|
}
|
|
206
|
-
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
300
|
+
const state: EmailChallengeSentState = flowState;
|
|
301
|
+
const retry: AdaptiveChallengeRetry = (token, attemptId) => run(token, attemptId);
|
|
302
|
+
async function run(
|
|
303
|
+
challengeToken?: string,
|
|
304
|
+
attemptId?: number,
|
|
305
|
+
): Promise<Outcome<{ challengeId: string }, SignInFlowError>> {
|
|
306
|
+
const outcome = await client.resendEmailOtp(
|
|
307
|
+
state.transactionId,
|
|
308
|
+
challengeToken === undefined
|
|
309
|
+
? { challenge_id: state.challengeId }
|
|
310
|
+
: { challenge_id: state.challengeId, challenge_token: challengeToken },
|
|
311
|
+
);
|
|
312
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
313
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
314
|
+
}
|
|
315
|
+
if (!outcome.ok) {
|
|
316
|
+
handleOperationFailure(outcome.error, "email_resend", state, "resend_email", retry);
|
|
317
|
+
return outcome;
|
|
318
|
+
}
|
|
319
|
+
pendingAdaptiveChallenge = null;
|
|
320
|
+
// design.md §18.2: resend invalidates the prior code — adopt the new challenge.
|
|
321
|
+
transition({
|
|
322
|
+
...state,
|
|
323
|
+
challengeId: outcome.value.challenge_id,
|
|
324
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
325
|
+
});
|
|
326
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
213
327
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
...state,
|
|
217
|
-
challengeId: outcome.value.challenge_id,
|
|
218
|
-
challengeExpiresAt: outcome.value.expires_at,
|
|
219
|
-
});
|
|
220
|
-
return ok({ challengeId: outcome.value.challenge_id });
|
|
328
|
+
transition(state, "resend_email");
|
|
329
|
+
return run(undefined, challengeAttemptId);
|
|
221
330
|
};
|
|
222
331
|
|
|
223
332
|
const verifyEmailCode = async (
|
|
@@ -240,20 +349,45 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
240
349
|
};
|
|
241
350
|
|
|
242
351
|
const signInWithPasskey = async (): Promise<Outcome<AuthenticatedSession, SignInFlowError>> => {
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
return invalidState("awaiting_method",
|
|
352
|
+
const flowState = snapshot.state;
|
|
353
|
+
if (flowState.step !== "awaiting_method") {
|
|
354
|
+
return invalidState("awaiting_method", flowState.step);
|
|
246
355
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
"
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
356
|
+
const state: AwaitingMethodState = flowState;
|
|
357
|
+
const resumeState: CeremonyState = {
|
|
358
|
+
step: "ceremony",
|
|
359
|
+
transactionId: state.transactionId,
|
|
360
|
+
ceremony: "passkey_authentication",
|
|
361
|
+
};
|
|
362
|
+
const retry: AdaptiveChallengeRetry = (token, attemptId) => run(token, attemptId);
|
|
363
|
+
async function run(
|
|
364
|
+
challengeToken?: string,
|
|
365
|
+
attemptId?: number,
|
|
366
|
+
): Promise<Outcome<AuthenticatedSession, SignInFlowError>> {
|
|
367
|
+
const outcome =
|
|
368
|
+
challengeToken === undefined
|
|
369
|
+
? await client.startPasskeyAuthentication(state.transactionId)
|
|
370
|
+
: await client.startPasskeyAuthentication(state.transactionId, {
|
|
371
|
+
challenge_token: challengeToken,
|
|
372
|
+
});
|
|
373
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
374
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
375
|
+
}
|
|
376
|
+
if (!outcome.ok) {
|
|
377
|
+
handleOperationFailure(
|
|
378
|
+
outcome.error,
|
|
379
|
+
"passkey_auth_options",
|
|
380
|
+
resumeState,
|
|
381
|
+
"passkey",
|
|
382
|
+
retry,
|
|
383
|
+
);
|
|
384
|
+
return outcome;
|
|
385
|
+
}
|
|
386
|
+
pendingAdaptiveChallenge = null;
|
|
387
|
+
return finalize(state.transactionId);
|
|
255
388
|
}
|
|
256
|
-
|
|
389
|
+
transition(resumeState, "passkey");
|
|
390
|
+
return run(undefined, challengeAttemptId);
|
|
257
391
|
};
|
|
258
392
|
|
|
259
393
|
const registerPasskey = async (input?: {
|
|
@@ -278,21 +412,54 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
278
412
|
const signInWithSocial = async (
|
|
279
413
|
provider: SocialProvider,
|
|
280
414
|
): Promise<Outcome<{ redirected: true }, SignInFlowError>> => {
|
|
281
|
-
const
|
|
282
|
-
if (
|
|
283
|
-
return invalidState("awaiting_method",
|
|
415
|
+
const flowState = snapshot.state;
|
|
416
|
+
if (flowState.step !== "awaiting_method") {
|
|
417
|
+
return invalidState("awaiting_method", flowState.step);
|
|
418
|
+
}
|
|
419
|
+
const state: AwaitingMethodState = flowState;
|
|
420
|
+
const retry: AdaptiveChallengeRetry = (token, attemptId) => run(token, attemptId);
|
|
421
|
+
async function run(
|
|
422
|
+
challengeToken?: string,
|
|
423
|
+
attemptId?: number,
|
|
424
|
+
): Promise<Outcome<{ redirected: true }, SignInFlowError>> {
|
|
425
|
+
const outcome =
|
|
426
|
+
challengeToken === undefined
|
|
427
|
+
? await client.startSocialSignIn(state.transactionId, provider)
|
|
428
|
+
: await client.startSocialSignIn(state.transactionId, provider, {
|
|
429
|
+
challenge_token: challengeToken,
|
|
430
|
+
});
|
|
431
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
432
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
433
|
+
}
|
|
434
|
+
if (!outcome.ok) {
|
|
435
|
+
handleOperationFailure(outcome.error, "social_start", state, "social", retry);
|
|
436
|
+
return outcome;
|
|
437
|
+
}
|
|
438
|
+
pendingAdaptiveChallenge = null;
|
|
439
|
+
// Success = the browser is navigating away (top-level redirect). The flow
|
|
440
|
+
// intentionally stays at method selection until the page reloads.
|
|
441
|
+
transition(state);
|
|
442
|
+
return ok({ redirected: true });
|
|
284
443
|
}
|
|
285
444
|
transition(state, "social");
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
445
|
+
return run(undefined, challengeAttemptId);
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const retryAdaptiveChallenge = (
|
|
449
|
+
challengeToken: string,
|
|
450
|
+
): Promise<Outcome<SignInFlowAdaptiveChallengeResult, SignInFlowError>> => {
|
|
451
|
+
const state = snapshot.state;
|
|
452
|
+
const pending = pendingAdaptiveChallenge;
|
|
453
|
+
if (
|
|
454
|
+
state.step !== "challenge_required" ||
|
|
455
|
+
pending === null ||
|
|
456
|
+
snapshot.pendingAction !== null
|
|
457
|
+
) {
|
|
458
|
+
return Promise.resolve(invalidState("challenge_required", state.step));
|
|
290
459
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
transition(state);
|
|
295
|
-
return ok({ redirected: true });
|
|
460
|
+
const attemptId = ++challengeAttemptId;
|
|
461
|
+
transition(state, pending.pendingAction);
|
|
462
|
+
return pending.retry(challengeToken, attemptId);
|
|
296
463
|
};
|
|
297
464
|
|
|
298
465
|
const cancel = async (): Promise<Outcome<{ cancelled: true }, SignInFlowError>> => {
|
|
@@ -301,9 +468,12 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
301
468
|
state.step === "awaiting_method" ||
|
|
302
469
|
state.step === "email_challenge_sent" ||
|
|
303
470
|
state.step === "ceremony" ||
|
|
471
|
+
state.step === "challenge_required" ||
|
|
304
472
|
state.step === "completing"
|
|
305
473
|
? state.transactionId
|
|
306
474
|
: null;
|
|
475
|
+
pendingAdaptiveChallenge = null;
|
|
476
|
+
challengeAttemptId += 1;
|
|
307
477
|
if (transactionId === null) {
|
|
308
478
|
transition({ step: "idle" });
|
|
309
479
|
return ok({ cancelled: true });
|
|
@@ -319,6 +489,8 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
319
489
|
};
|
|
320
490
|
|
|
321
491
|
const reset = (): void => {
|
|
492
|
+
pendingAdaptiveChallenge = null;
|
|
493
|
+
challengeAttemptId += 1;
|
|
322
494
|
transition({ step: "idle" });
|
|
323
495
|
};
|
|
324
496
|
|
|
@@ -335,6 +507,7 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
335
507
|
signInWithPasskey,
|
|
336
508
|
registerPasskey,
|
|
337
509
|
signInWithSocial,
|
|
510
|
+
retryAdaptiveChallenge,
|
|
338
511
|
cancel,
|
|
339
512
|
reset,
|
|
340
513
|
};
|