@smartcrab/react 0.3.2 → 0.4.0-next-20261017141739
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/sessions-controller.js +1 -4
- package/dist/sign-in-flow.d.ts +33 -10
- package/dist/sign-in-flow.js +138 -54
- package/dist/user-controller.js +1 -4
- 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/sessions-controller.ts +1 -5
- package/src/sign-in-flow.adaptive.test.ts +374 -0
- package/src/sign-in-flow.ts +241 -78
- package/src/user-controller.ts +1 -5
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,74 @@ 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
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
264
|
+
const state: AwaitingMethodState = flowState;
|
|
265
|
+
async function run(
|
|
266
|
+
challengeToken?: string,
|
|
267
|
+
attemptId?: number,
|
|
268
|
+
): Promise<Outcome<{ challengeId: string }, SignInFlowError>> {
|
|
269
|
+
const outcome = await client.startEmailOtp(
|
|
270
|
+
state.transactionId,
|
|
271
|
+
challengeToken === undefined ? { email } : { email, challenge_token: challengeToken },
|
|
272
|
+
);
|
|
273
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
274
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
275
|
+
}
|
|
276
|
+
if (!outcome.ok) {
|
|
277
|
+
handleOperationFailure(outcome.error, "email_start", state, "send_email", run);
|
|
278
|
+
return outcome;
|
|
279
|
+
}
|
|
280
|
+
pendingAdaptiveChallenge = null;
|
|
281
|
+
transition({
|
|
282
|
+
step: "email_challenge_sent",
|
|
283
|
+
transactionId: state.transactionId,
|
|
284
|
+
challengeId: outcome.value.challenge_id,
|
|
285
|
+
email,
|
|
286
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
287
|
+
});
|
|
288
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
190
289
|
}
|
|
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 });
|
|
290
|
+
transition(state, "send_email");
|
|
291
|
+
return run(undefined, challengeAttemptId);
|
|
199
292
|
};
|
|
200
293
|
|
|
201
294
|
const resendEmailCode = async (): Promise<Outcome<{ challengeId: string }, SignInFlowError>> => {
|
|
202
|
-
const
|
|
203
|
-
if (
|
|
204
|
-
return invalidState("email_challenge_sent",
|
|
295
|
+
const flowState = snapshot.state;
|
|
296
|
+
if (flowState.step !== "email_challenge_sent") {
|
|
297
|
+
return invalidState("email_challenge_sent", flowState.step);
|
|
205
298
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
299
|
+
const state: EmailChallengeSentState = flowState;
|
|
300
|
+
async function run(
|
|
301
|
+
challengeToken?: string,
|
|
302
|
+
attemptId?: number,
|
|
303
|
+
): Promise<Outcome<{ challengeId: string }, SignInFlowError>> {
|
|
304
|
+
const outcome = await client.resendEmailOtp(
|
|
305
|
+
state.transactionId,
|
|
306
|
+
challengeToken === undefined
|
|
307
|
+
? { challenge_id: state.challengeId }
|
|
308
|
+
: { challenge_id: state.challengeId, challenge_token: challengeToken },
|
|
309
|
+
);
|
|
310
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
311
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
312
|
+
}
|
|
313
|
+
if (!outcome.ok) {
|
|
314
|
+
handleOperationFailure(outcome.error, "email_resend", state, "resend_email", run);
|
|
315
|
+
return outcome;
|
|
316
|
+
}
|
|
317
|
+
pendingAdaptiveChallenge = null;
|
|
318
|
+
// design.md §18.2: resend invalidates the prior code — adopt the new challenge.
|
|
319
|
+
transition({
|
|
320
|
+
...state,
|
|
321
|
+
challengeId: outcome.value.challenge_id,
|
|
322
|
+
challengeExpiresAt: outcome.value.expires_at,
|
|
323
|
+
});
|
|
324
|
+
return ok({ challengeId: outcome.value.challenge_id });
|
|
213
325
|
}
|
|
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 });
|
|
326
|
+
transition(state, "resend_email");
|
|
327
|
+
return run(undefined, challengeAttemptId);
|
|
221
328
|
};
|
|
222
329
|
|
|
223
330
|
const verifyEmailCode = async (
|
|
@@ -240,20 +347,38 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
240
347
|
};
|
|
241
348
|
|
|
242
349
|
const signInWithPasskey = async (): Promise<Outcome<AuthenticatedSession, SignInFlowError>> => {
|
|
243
|
-
const
|
|
244
|
-
if (
|
|
245
|
-
return invalidState("awaiting_method",
|
|
350
|
+
const flowState = snapshot.state;
|
|
351
|
+
if (flowState.step !== "awaiting_method") {
|
|
352
|
+
return invalidState("awaiting_method", flowState.step);
|
|
246
353
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
"
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
354
|
+
const state: AwaitingMethodState = flowState;
|
|
355
|
+
const resumeState: CeremonyState = {
|
|
356
|
+
step: "ceremony",
|
|
357
|
+
transactionId: state.transactionId,
|
|
358
|
+
ceremony: "passkey_authentication",
|
|
359
|
+
};
|
|
360
|
+
async function run(
|
|
361
|
+
challengeToken?: string,
|
|
362
|
+
attemptId?: number,
|
|
363
|
+
): Promise<Outcome<AuthenticatedSession, SignInFlowError>> {
|
|
364
|
+
const outcome =
|
|
365
|
+
challengeToken === undefined
|
|
366
|
+
? await client.startPasskeyAuthentication(state.transactionId)
|
|
367
|
+
: await client.startPasskeyAuthentication(state.transactionId, {
|
|
368
|
+
challenge_token: challengeToken,
|
|
369
|
+
});
|
|
370
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
371
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
372
|
+
}
|
|
373
|
+
if (!outcome.ok) {
|
|
374
|
+
handleOperationFailure(outcome.error, "passkey_auth_options", resumeState, "passkey", run);
|
|
375
|
+
return outcome;
|
|
376
|
+
}
|
|
377
|
+
pendingAdaptiveChallenge = null;
|
|
378
|
+
return finalize(state.transactionId);
|
|
255
379
|
}
|
|
256
|
-
|
|
380
|
+
transition(resumeState, "passkey");
|
|
381
|
+
return run(undefined, challengeAttemptId);
|
|
257
382
|
};
|
|
258
383
|
|
|
259
384
|
const registerPasskey = async (input?: {
|
|
@@ -278,21 +403,53 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
278
403
|
const signInWithSocial = async (
|
|
279
404
|
provider: SocialProvider,
|
|
280
405
|
): Promise<Outcome<{ redirected: true }, SignInFlowError>> => {
|
|
281
|
-
const
|
|
282
|
-
if (
|
|
283
|
-
return invalidState("awaiting_method",
|
|
406
|
+
const flowState = snapshot.state;
|
|
407
|
+
if (flowState.step !== "awaiting_method") {
|
|
408
|
+
return invalidState("awaiting_method", flowState.step);
|
|
409
|
+
}
|
|
410
|
+
const state: AwaitingMethodState = flowState;
|
|
411
|
+
async function run(
|
|
412
|
+
challengeToken?: string,
|
|
413
|
+
attemptId?: number,
|
|
414
|
+
): Promise<Outcome<{ redirected: true }, SignInFlowError>> {
|
|
415
|
+
const outcome =
|
|
416
|
+
challengeToken === undefined
|
|
417
|
+
? await client.startSocialSignIn(state.transactionId, provider)
|
|
418
|
+
: await client.startSocialSignIn(state.transactionId, provider, {
|
|
419
|
+
challenge_token: challengeToken,
|
|
420
|
+
});
|
|
421
|
+
if (attemptId !== undefined && attemptId !== challengeAttemptId) {
|
|
422
|
+
return invalidState("challenge_required", snapshot.state.step);
|
|
423
|
+
}
|
|
424
|
+
if (!outcome.ok) {
|
|
425
|
+
handleOperationFailure(outcome.error, "social_start", state, "social", run);
|
|
426
|
+
return outcome;
|
|
427
|
+
}
|
|
428
|
+
pendingAdaptiveChallenge = null;
|
|
429
|
+
// Success = the browser is navigating away (top-level redirect). The flow
|
|
430
|
+
// intentionally stays at method selection until the page reloads.
|
|
431
|
+
transition(state);
|
|
432
|
+
return ok({ redirected: true });
|
|
284
433
|
}
|
|
285
434
|
transition(state, "social");
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
435
|
+
return run(undefined, challengeAttemptId);
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const retryAdaptiveChallenge = (
|
|
439
|
+
challengeToken: string,
|
|
440
|
+
): Promise<Outcome<SignInFlowAdaptiveChallengeResult, SignInFlowError>> => {
|
|
441
|
+
const state = snapshot.state;
|
|
442
|
+
const pending = pendingAdaptiveChallenge;
|
|
443
|
+
if (
|
|
444
|
+
state.step !== "challenge_required" ||
|
|
445
|
+
pending === null ||
|
|
446
|
+
snapshot.pendingAction !== null
|
|
447
|
+
) {
|
|
448
|
+
return Promise.resolve(invalidState("challenge_required", state.step));
|
|
290
449
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
transition(state);
|
|
295
|
-
return ok({ redirected: true });
|
|
450
|
+
const attemptId = ++challengeAttemptId;
|
|
451
|
+
transition(state, pending.pendingAction);
|
|
452
|
+
return pending.retry(challengeToken, attemptId);
|
|
296
453
|
};
|
|
297
454
|
|
|
298
455
|
const cancel = async (): Promise<Outcome<{ cancelled: true }, SignInFlowError>> => {
|
|
@@ -301,9 +458,12 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
301
458
|
state.step === "awaiting_method" ||
|
|
302
459
|
state.step === "email_challenge_sent" ||
|
|
303
460
|
state.step === "ceremony" ||
|
|
461
|
+
state.step === "challenge_required" ||
|
|
304
462
|
state.step === "completing"
|
|
305
463
|
? state.transactionId
|
|
306
464
|
: null;
|
|
465
|
+
pendingAdaptiveChallenge = null;
|
|
466
|
+
challengeAttemptId += 1;
|
|
307
467
|
if (transactionId === null) {
|
|
308
468
|
transition({ step: "idle" });
|
|
309
469
|
return ok({ cancelled: true });
|
|
@@ -319,6 +479,8 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
319
479
|
};
|
|
320
480
|
|
|
321
481
|
const reset = (): void => {
|
|
482
|
+
pendingAdaptiveChallenge = null;
|
|
483
|
+
challengeAttemptId += 1;
|
|
322
484
|
transition({ step: "idle" });
|
|
323
485
|
};
|
|
324
486
|
|
|
@@ -335,6 +497,7 @@ export const createSignInFlow = (client: SignInFlowClient): SignInFlow => {
|
|
|
335
497
|
signInWithPasskey,
|
|
336
498
|
registerPasskey,
|
|
337
499
|
signInWithSocial,
|
|
500
|
+
retryAdaptiveChallenge,
|
|
338
501
|
cancel,
|
|
339
502
|
reset,
|
|
340
503
|
};
|
package/src/user-controller.ts
CHANGED
|
@@ -89,10 +89,6 @@ export const createUserProfileController = (client: UserProfileClient): UserProf
|
|
|
89
89
|
return ok(outcome.value);
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
const clear = (): void => {
|
|
93
|
-
transition(IDLE);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
92
|
return {
|
|
97
93
|
getSnapshot: () => snapshot,
|
|
98
94
|
subscribe: (listener) => {
|
|
@@ -101,6 +97,6 @@ export const createUserProfileController = (client: UserProfileClient): UserProf
|
|
|
101
97
|
},
|
|
102
98
|
load,
|
|
103
99
|
update,
|
|
104
|
-
clear,
|
|
100
|
+
clear: () => transition(IDLE),
|
|
105
101
|
};
|
|
106
102
|
};
|