@passflow/core 0.23.0 → 0.25.0

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/README.md CHANGED
@@ -34,6 +34,7 @@
34
34
  - [Passkey Authentication](#passkey-authentication)
35
35
  - [Password Reset](#password-reset)
36
36
  - [Email verification](#email-verification)
37
+ - [Two-factor sign-in and first-time enrolment](#two-factor-sign-in-and-first-time-enrolment)
37
38
  - [Token Management](#token-management)
38
39
  - [Tenant Management](#tenant-management)
39
40
  - [Invitation Management](#invitation-management)
@@ -364,6 +365,36 @@ const completePasswordless = async (challengeId, otp) => {
364
365
  };
365
366
  ```
366
367
 
368
+ **PKCE apps: approving a magic link from any device.** On an app with PKCE on, a magic link's answer
369
+ holds a `match_code`. The tab that started the sign-in shows that code and finishes the sign-in. The
370
+ device that opens the link only approves or denies, by typing the code, and gets no tokens. A second
371
+ tab in the same browser approves like any other device.
372
+
373
+ ```typescript
374
+ // Tab A: the tab that called passwordlessSignIn
375
+ const { challenge_id } = await passflow.passwordlessSignIn({
376
+ email: "user@example.com",
377
+ challenge_type: "magic_link",
378
+ redirect_url: "https://yourapp.com/auth/callback",
379
+ });
380
+
381
+ const pending = passflow.getPendingPasswordlessSignIn(challenge_id);
382
+ if (pending) {
383
+ showCode(pending.matchCode);
384
+ const result = await passflow.waitForPasswordlessSignIn(challenge_id);
385
+ // result.status: "signed-in" | "second-factor-required" | "denied" | "expired" | "completed"
386
+ }
387
+
388
+ // Device B: the link carries challenge_id, otp and flow=approval
389
+ const info = await passflow.getPasswordlessApproval(challengeId); // app, browser, OS, location, attempts_left
390
+ await passflow.decidePasswordlessApproval(challengeId, otp, true, typedMatchCode); // or false to deny
391
+ ```
392
+
393
+ If `getPendingPasswordlessSignIn` answers `undefined`, there is no approval step: show today's "check
394
+ your email" screen. The record that finishes the sign-in lives in this tab only. If it is lost or
395
+ replaced (a new start, Resend, sign-out), the wait rejects and the user starts again. Results, errors
396
+ and the device B screen: [docs/pkce-sign-in.md](docs/pkce-sign-in.md).
397
+
367
398
  ### Federated Authentication
368
399
 
369
400
  ```javascript
@@ -382,6 +413,17 @@ passflow.federatedAuthWithRedirect(
382
413
  );
383
414
  ```
384
415
 
416
+ **PKCE apps.** `federatedAuthWithRedirect` still returns `void`, but it now navigates asynchronously:
417
+ a missing `appId` still throws synchronously, and a later failure emits `Error` with operation
418
+ `federated.redirect`. The landing (`?code=&user_state=`) is taken by the constructor under
419
+ `parseQueryParams`, or by `handleTokensRedirect()`, which returns `undefined` for it. The exchange runs
420
+ in the background, so `SignIn` with `source: "federated-landing"`, `TwoFactorRequired` or that `Error` is
421
+ the signal. A `?code=` this tab did not ask for leaves the URL alone and emits no `SignIn`.
422
+
423
+ A host that builds its instance during render, as React StrictMode may discard it, sets
424
+ `deferFederatedLanding: true` and calls `completeFederatedLanding()` once the instance is kept. Details:
425
+ [docs/pkce-sign-in.md](docs/pkce-sign-in.md).
426
+
385
427
  ### Passkey Authentication
386
428
 
387
429
  ```javascript
@@ -528,6 +570,40 @@ new `challenge_id`.
528
570
  an optional third argument. Without it, the SDK reads `token` from the URL query, then from the
529
571
  query of a hash route (`#/reset?token=...`).
530
572
 
573
+ ### Two-factor sign-in and first-time enrolment
574
+
575
+ A `required` app answers every sign-in with a `tfa_token`, also for a user who has no factor yet.
576
+ Ask the server what this sign-in can do next before asking for a code:
577
+
578
+ ```typescript
579
+ switch (await passflow.getTwoFactorEnrollment()) {
580
+ case "verify": // the user has a factor
581
+ await passflow.verifyTwoFactor(code);
582
+ break;
583
+ case "email_link": // no factor, validated email: the user enrols from the email, then signs in again
584
+ await passflow.sendTwoFactorSetupLink();
585
+ break;
586
+ case "direct": { // no factor, no validated email: enrol on this sign-in
587
+ const setup = await passflow.beginTwoFactorEnrollment(); // setup.secret, setup.qr_code (base64 PNG)
588
+ const { recovery_codes } = await passflow.confirmTwoFactorEnrollment(code); // show these once
589
+ await passflow.verifyTwoFactor(code); // the same code finishes the sign-in
590
+ break;
591
+ }
592
+ case "unavailable": // no way to enrol here: tell the user to contact the administrator
593
+ break;
594
+ }
595
+ ```
596
+
597
+ - **The server decides.** Setup calls in the wrong state answer 403 `error.2fa.enrollment.not_allowed`.
598
+ - **The sign-in is over** on a 401 `error.tfa.token.invalid` or `error.otp.already.used`: sign in again.
599
+ - **Rate limits are per user.** `sendTwoFactorSetupLink`, `beginTwoFactorEnrollment`,
600
+ `requestChallenge` and `switchToAlternative` share 3 requests in 5 minutes, then a 10-minute
601
+ lockout. `confirmTwoFactorEnrollment` and `verifyTwoFactor` share 5 in 15 minutes. These 429s
602
+ reject with the raw axios error (`error.response.status`).
603
+ - **The emailed link.** Call `validateTwoFactorSetupMagicLink(token)` on its page. While that session
604
+ is active, `getTwoFactorStatus`, `beginTwoFactorSetup` and `confirmTwoFactorSetup` run on it instead
605
+ of the stored session, and a correct confirm ends it. A used link reads as error code `ALREADY_USED`.
606
+
531
607
  ## Token Management
532
608
 
533
609
  ```javascript
@@ -700,7 +776,8 @@ const subscriber = {
700
776
  // Handle different event types
701
777
  switch (eventType) {
702
778
  case "signin":
703
- // Handle sign in
779
+ // Handle sign in. source.source is "federated-landing" only when this tab
780
+ // took a federated redirect's landing and exchanged its code.
704
781
  break;
705
782
  case "signout":
706
783
  // Handle sign out
@@ -779,6 +856,7 @@ Configuration options:
779
856
  - `scopes`: Token scopes to request (default: ['id', 'offline', 'tenant', 'email', 'oidc', 'openid', 'access:tenant:all'])
780
857
  - `createTenantForNewUser`: Whether to create a tenant for new users (default: false)
781
858
  - `parseQueryParams`: Whether to parse tokens from URL query parameters (default: false)
859
+ - `deferFederatedLanding`: Leave a federated `?code=` landing to `completeFederatedLanding()` (default: false)
782
860
  - `keyStoragePrefix`: Prefix for localStorage keys
783
861
 
784
862
  #### Methods
@@ -800,6 +878,11 @@ Configuration options:
800
878
  | `sendPasswordResetEmail(payload)` | Send password reset email |
801
879
  | `resetPassword(newPassword, scopes?, resetToken?)` | Reset password |
802
880
  | `verifyEmailCode({ challenge_id, otp })` | Finish an email verification step |
881
+ | `waitForPasswordlessSignIn(challengeId, { signal? })` | Wait for approval on another device, then finish here |
882
+ | `getPendingPasswordlessSignIn(challengeId)` | The match code to show while waiting |
883
+ | `getPasswordlessApproval(challengeId)` | What the approving device shows |
884
+ | `decidePasswordlessApproval(challengeId, otp, approve, matchCode?)` | Approve or deny a waiting sign-in |
885
+ | `completeFederatedLanding()` | Take a federated landing, with `deferFederatedLanding` |
803
886
 
804
887
  **Token Methods**
805
888
 
@@ -870,6 +953,7 @@ type PassflowConfig = {
870
953
  scopes?: string[];
871
954
  createTenantForNewUser?: boolean;
872
955
  parseQueryParams?: boolean;
956
+ deferFederatedLanding?: boolean; // see docs/pkce-sign-in.md
873
957
  keyStoragePrefix?: string;
874
958
  refreshCoordination?: "web-locks" | "none" | RefreshCoordinator; // see docs/token-refresh.md
875
959
  refreshStrikes?: number;