@passflow/core 0.31.0 → 0.32.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
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## Changelog
4
4
 
5
+ ### Version 0.32.0
6
+
7
+ - `passflow.withAuth(fetch, { origins })`, `passflow.attachAuth(axios, { origins })` and
8
+ `passflow.createClient(config, { origins })` add `Authorization: Bearer <access token>` to a request
9
+ for the page's own origin or a listed origin when the request has no `Authorization` header,
10
+ refreshing an expired token first. Other origins get no token. Before, they only handled step-up,
11
+ so a request without the header now carries one. See
12
+ [Calling your API with the access token](#calling-your-api-with-the-access-token).
13
+ - `WithAuthOptions` is exported.
14
+
5
15
  ### Version 0.31.0
6
16
 
7
17
  Matches the SDK to the server's real two-factor, step-up and sign-out answers.
@@ -63,9 +73,9 @@ Matches the SDK to the server's real two-factor, step-up and sign-out answers.
63
73
 
64
74
  **Known limits:**
65
75
  - The `hostedUpgrade` memory assumes the app's elevation window (`auth_assurance.elevation_max_age`)
66
- is at least 10 seconds. Servers up to src/v0.49.1, the current release, accept any window, so the
67
- 10 s minimum does not apply on them. src/v0.50.0, the next server release (not yet released),
68
- refuses new windows under 10 seconds but keeps a shorter one already stored. With a window under
76
+ is at least 10 seconds. Servers before src/v0.50.0 accept any window, so the 10 s minimum does not
77
+ apply on them. src/v0.50.0 (not yet released) refuses new windows under 10 seconds but keeps a
78
+ shorter one already stored. With a window under
69
79
  10 seconds, a re-challenge for the same requirement within 10 seconds of the proof is refused with
70
80
  `UpgradeInsufficientError`, costing the user one extra sign-in. Keep every window at 10 seconds or
71
81
  more, and re-save any shorter one.
@@ -830,9 +840,9 @@ switch (enrollment) {
830
840
  requirement's `max_age`, if shorter): after that the same requirement prompts again. A sign-in or
831
841
  sign-out clears it. Keep the app's elevation window (`auth_assurance.elevation_max_age`) at 10
832
842
  seconds or more: a shorter window can lapse inside those 10 seconds, and the re-challenge is then
833
- refused, costing one extra sign-in. Servers up to src/v0.49.1 accept any window. src/v0.50.0, the
834
- next server release (not yet released), refuses new windows under 10 seconds; after upgrading,
835
- re-save any shorter one already stored.
843
+ refused, costing one extra sign-in. Servers before src/v0.50.0 accept any window. src/v0.50.0 (not
844
+ yet released) refuses new windows under 10 seconds; after upgrading, re-save any shorter one
845
+ already stored.
836
846
  - **The server decides.** Setup calls in the wrong state answer 403 `error.2fa.enrollment.not_allowed`.
837
847
  - **The sign-in is over** on a 401 `error.tfa.token.invalid` or `error.otp.already.used`: sign in again.
838
848
  - **Rate limits are per user.** `sendTwoFactorSetupLink`, `beginTwoFactorEnrollment`,
@@ -910,6 +920,44 @@ with nothing saved. Verification needs the Web Crypto API (https or localhost).
910
920
  fragment) follows the same Simple-only rule and is verified the same way; on a PKCE app it requires a
911
921
  code.
912
922
 
923
+ ### Calling your API with the access token
924
+
925
+ `passflow.withAuth(fetch, options?)` returns a `fetch` that adds `Authorization: Bearer <access
926
+ token>` for you, and handles a step-up challenge on the answer. `passflow.attachAuth(axios, options?)`
927
+ and `passflow.createClient(axiosConfig?, options?)` do the same for axios, with a request interceptor
928
+ next to the step-up one; `attachAuth`'s disposer removes both.
929
+
930
+ ```javascript
931
+ const api = passflow.withAuth(fetch, { origins: ["https://api.example.com"] });
932
+ const res = await api("https://api.example.com/orders"); // carries the token
933
+ await api("/api/me"); // the page's own origin: carries the token
934
+ await api("https://cdn.example.net/data.json"); // any other origin: no token
935
+
936
+ const client = passflow.createClient({ baseURL: "https://api.example.com" }, { origins: ["https://api.example.com"] });
937
+ await client.get("/orders"); // carries the token
938
+ ```
939
+
940
+ - The token goes only to the page's own origin (`location.origin`) and to the origins in
941
+ `options.origins`. Origins are compared exactly, scheme, host and port: `https://api.example.com`
942
+ does not cover `http://api.example.com`, `https://api.example.com:8443` or
943
+ `https://api.example.com.evil.com`. A relative URL resolves against the page. On axios the check
944
+ uses the URL axios sends: a relative URL is joined to `baseURL`, an absolute one is judged by its own
945
+ origin, and with `allowAbsoluteUrls: false` an absolute one is joined to `baseURL` too. Outside a
946
+ browser page (SSR, React Native) only the listed origins get it. List http(s) origins only: an
947
+ `origins` entry that is not a URL, or has no origin to match (`capacitor://localhost`, `file:`, a
948
+ custom app scheme), throws.
949
+ - A request that already has an `Authorization` header goes out as you set it, including an axios
950
+ client default.
951
+ - An expired access token is refreshed first, as `getTokens(true)` does. With no signed-in user, or
952
+ when the refresh fails (the expired token is never sent), no header is added.
953
+ - On an axios instance you already own, a request interceptor you added before `attachAuth` runs
954
+ after the token one (axios runs request interceptors last-added first). If it rewrites `baseURL`
955
+ or `url`, the request goes where it points, with the token decided for the URL before the rewrite.
956
+ Add such interceptors after `attachAuth`, or set the header in them yourself.
957
+ - Requires axios 1.x. On 0.27.x the interceptor cannot see a client default
958
+ `defaults.headers.common.Authorization`, so the token replaces it.
959
+ - A cookie or BFF session holds no access token in the page, so nothing is added there.
960
+
913
961
  ### Token refresh across tabs
914
962
 
915
963
  Every refresh in a `Passflow` instance (`refreshToken()`, `getTokens(true)`,