@passflow/core 0.31.0 → 0.32.1

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/index.d.ts CHANGED
@@ -288,6 +288,13 @@ declare class StorageManager {
288
288
  setAppId(appId: string | undefined): void;
289
289
  /** The stored value, or undefined when it is a JWT issued for another app than this one. */
290
290
  private ownToken;
291
+ /**
292
+ * The value under `key`, the prefixed form of `name`. Earlier versions stored it under the
293
+ * unprefixed `name`: such a value is moved to `key` on first read.
294
+ */
295
+ private readPrefixed;
296
+ /** Removes `key` and the unprefixed `name`, so an old value is not read again. */
297
+ private removePrefixed;
291
298
  private readToken;
292
299
  /** The keys a stored token lives in: the JSON tokens and the cookie-mode ID token. */
293
300
  private tokenValueKeys;
@@ -399,6 +406,8 @@ declare class StorageManager {
399
406
  }
400
407
  declare class DeviceService {
401
408
  private storageManager;
409
+ /** The id this service minted; used when the storage keeps nothing (server rendering). */
410
+ private mintedDeviceId;
402
411
  constructor(storageManager?: StorageManager);
403
412
  getDeviceId(): string;
404
413
  generateUniqueDeviceId(): string;
@@ -4853,6 +4862,109 @@ export declare class UserService {
4853
4862
  * the diagnostics.
4854
4863
  */
4855
4864
  export declare const requiredAcrLevel: (raw: string | undefined | null) => number | undefined;
4865
+ /** One captured header. Lower-cased name, value verbatim. */
4866
+ type SnapshotHeader = readonly [name: string, value: string];
4867
+ /**
4868
+ * The frozen description of one request.
4869
+ *
4870
+ * Immutable by construction: `Object.freeze` on the snapshot and on its header
4871
+ * list, and the body already captured by value where the shape is mutable. It
4872
+ * carries no live reference to anything the caller can still change.
4873
+ */
4874
+ type RequestSnapshot = {
4875
+ /** Exactly what the caller passed, for diagnostics only. NEVER for a decision. */
4876
+ readonly raw: string;
4877
+ /**
4878
+ * The absolute, platform-normalised URL the request will actually be sent to,
4879
+ * or `raw` when it could not be resolved at all (a malformed URL).
4880
+ *
4881
+ * This is the value every decision and the replay use.
4882
+ */
4883
+ readonly href: string;
4884
+ /**
4885
+ * The URL to actually DISPATCH a replay to.
4886
+ *
4887
+ * Identical to `href` wherever a real base existed — a browser, or any host
4888
+ * that resolved the URL absolutely — which is the case every decision cares
4889
+ * about. It falls back to `raw` only when resolution used the reserved
4890
+ * sentinel base, i.e. a relative URL on a host with no document: the
4891
+ * sentinel's origin is a reserved name that resolves to nothing, so sending
4892
+ * `href` there would send the request into a hole. The transport's own base
4893
+ * (axios' `baseURL`, the runtime's fetch base) then resolves the captured
4894
+ * string, exactly as it did for the original send.
4895
+ *
4896
+ * It is still a captured STRING, never the caller's object, so the TOCTOU
4897
+ * this module exists to close stays closed on that path too.
4898
+ */
4899
+ readonly dispatchUrl: string;
4900
+ /** The resolved origin, or `undefined` when the URL could not be resolved. */
4901
+ readonly origin?: string;
4902
+ /** The resolved pathname, or the raw path when the URL could not be resolved. */
4903
+ readonly pathname: string;
4904
+ /** False when the URL could not be parsed even against the base. */
4905
+ readonly resolved: boolean;
4906
+ /**
4907
+ * True when the resolved origin is the base's own — the page's origin on a
4908
+ * browser, the sentinel's off it, which means "genuinely relative".
4909
+ *
4910
+ * This is what a same-origin path-prefix pattern (`backend: '/api'`) tests,
4911
+ * and it is the one bit that makes `'/\\evil.example/x'` fail where `'/api/x'`
4912
+ * passes.
4913
+ */
4914
+ readonly relativeToBase: boolean;
4915
+ /** Upper-cased, with the same precedence the transport itself applies. */
4916
+ readonly method: string;
4917
+ /** The effective header set, captured once. Frozen. */
4918
+ readonly headers: readonly SnapshotHeader[];
4919
+ /** How the body can be re-sent, which is what decides replayability. */
4920
+ readonly bodyKind: BodyKind;
4921
+ /**
4922
+ * The already-captured body. `undefined` means "the caller passed no body
4923
+ * key", which is distinct from an explicit `null`.
4924
+ */
4925
+ readonly body?: unknown;
4926
+ };
4927
+ /** Our own `fetch` shape. Declared here rather than imported so no DOM lib is required. */
4928
+ type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
4929
+ /** How a body can be re-sent, which is the only thing that decides replayability. */
4930
+ type BodyKind =
4931
+ /** No body at all. Re-issue the same input. */
4932
+ 'none' |
4933
+ /** A value the caller still holds and `fetch` re-reads: string, Blob, buffer, FormData, params. */
4934
+ 'reusable' |
4935
+ /** A `Request` with a body, cloned before the first send. */
4936
+ 'request-clone' |
4937
+ /** Consumed by the first send and unrecoverable: a `ReadableStream`, or a failed clone. */
4938
+ 'one-shot';
4939
+ /** What the policy predicate is told about a request. */
4940
+ type UpgradeReplayInfo = {
4941
+ url: string;
4942
+ method: string;
4943
+ bodyKind: BodyKind;
4944
+ };
4945
+ /**
4946
+ * Replay policy.
4947
+ *
4948
+ * The object form is §13.5's: size and streaming limits. The three string and
4949
+ * function forms are §5.1's escape hatch, kept because `'idempotent-only'`
4950
+ * expresses a constraint the object form cannot — a backend whose challenge may
4951
+ * arrive after a side effect.
4952
+ */
4953
+ type ReplayPolicy = {
4954
+ /** Refuse to buffer a `Request` body larger than this many bytes, when the size is known. */
4955
+ maxBodyBytes?: number;
4956
+ /** A stream is never replayable; the field exists so the intent is written down. */
4957
+ allowStreamingBody?: false;
4958
+ } | 'always' | 'idempotent-only' | ((info: UpgradeReplayInfo) => boolean);
4959
+ /** What `withAuth`, `attachAuth` and `createClient` accept. */
4960
+ type WithAuthOptions = {
4961
+ /**
4962
+ * Origins besides the page's own that receive the access token, for example
4963
+ * `['https://api.example.com']`. Each entry is reduced to its origin; anything that is not a URL
4964
+ * throws when the wrapper or interceptor is built.
4965
+ */
4966
+ origins?: readonly string[];
4967
+ };
4856
4968
  /** What the UI is shown when an upgrade is required. */
4857
4969
  type UpgradePromptRequest = {
4858
4970
  /** What the server demanded — the MERGED requirement, when several challenges joined. */
@@ -5230,100 +5342,6 @@ export declare class UpgradeController {
5230
5342
  /** Runs one full proof for a single factor. */
5231
5343
  private proveAndCommit;
5232
5344
  }
5233
- /** One captured header. Lower-cased name, value verbatim. */
5234
- type SnapshotHeader = readonly [name: string, value: string];
5235
- /**
5236
- * The frozen description of one request.
5237
- *
5238
- * Immutable by construction: `Object.freeze` on the snapshot and on its header
5239
- * list, and the body already captured by value where the shape is mutable. It
5240
- * carries no live reference to anything the caller can still change.
5241
- */
5242
- type RequestSnapshot = {
5243
- /** Exactly what the caller passed, for diagnostics only. NEVER for a decision. */
5244
- readonly raw: string;
5245
- /**
5246
- * The absolute, platform-normalised URL the request will actually be sent to,
5247
- * or `raw` when it could not be resolved at all (a malformed URL).
5248
- *
5249
- * This is the value every decision and the replay use.
5250
- */
5251
- readonly href: string;
5252
- /**
5253
- * The URL to actually DISPATCH a replay to.
5254
- *
5255
- * Identical to `href` wherever a real base existed — a browser, or any host
5256
- * that resolved the URL absolutely — which is the case every decision cares
5257
- * about. It falls back to `raw` only when resolution used the reserved
5258
- * sentinel base, i.e. a relative URL on a host with no document: the
5259
- * sentinel's origin is a reserved name that resolves to nothing, so sending
5260
- * `href` there would send the request into a hole. The transport's own base
5261
- * (axios' `baseURL`, the runtime's fetch base) then resolves the captured
5262
- * string, exactly as it did for the original send.
5263
- *
5264
- * It is still a captured STRING, never the caller's object, so the TOCTOU
5265
- * this module exists to close stays closed on that path too.
5266
- */
5267
- readonly dispatchUrl: string;
5268
- /** The resolved origin, or `undefined` when the URL could not be resolved. */
5269
- readonly origin?: string;
5270
- /** The resolved pathname, or the raw path when the URL could not be resolved. */
5271
- readonly pathname: string;
5272
- /** False when the URL could not be parsed even against the base. */
5273
- readonly resolved: boolean;
5274
- /**
5275
- * True when the resolved origin is the base's own — the page's origin on a
5276
- * browser, the sentinel's off it, which means "genuinely relative".
5277
- *
5278
- * This is what a same-origin path-prefix pattern (`backend: '/api'`) tests,
5279
- * and it is the one bit that makes `'/\\evil.example/x'` fail where `'/api/x'`
5280
- * passes.
5281
- */
5282
- readonly relativeToBase: boolean;
5283
- /** Upper-cased, with the same precedence the transport itself applies. */
5284
- readonly method: string;
5285
- /** The effective header set, captured once. Frozen. */
5286
- readonly headers: readonly SnapshotHeader[];
5287
- /** How the body can be re-sent, which is what decides replayability. */
5288
- readonly bodyKind: BodyKind;
5289
- /**
5290
- * The already-captured body. `undefined` means "the caller passed no body
5291
- * key", which is distinct from an explicit `null`.
5292
- */
5293
- readonly body?: unknown;
5294
- };
5295
- /** Our own `fetch` shape. Declared here rather than imported so no DOM lib is required. */
5296
- type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
5297
- /** How a body can be re-sent, which is the only thing that decides replayability. */
5298
- type BodyKind =
5299
- /** No body at all. Re-issue the same input. */
5300
- 'none' |
5301
- /** A value the caller still holds and `fetch` re-reads: string, Blob, buffer, FormData, params. */
5302
- 'reusable' |
5303
- /** A `Request` with a body, cloned before the first send. */
5304
- 'request-clone' |
5305
- /** Consumed by the first send and unrecoverable: a `ReadableStream`, or a failed clone. */
5306
- 'one-shot';
5307
- /** What the policy predicate is told about a request. */
5308
- type UpgradeReplayInfo = {
5309
- url: string;
5310
- method: string;
5311
- bodyKind: BodyKind;
5312
- };
5313
- /**
5314
- * Replay policy.
5315
- *
5316
- * The object form is §13.5's: size and streaming limits. The three string and
5317
- * function forms are §5.1's escape hatch, kept because `'idempotent-only'`
5318
- * expresses a constraint the object form cannot — a backend whose challenge may
5319
- * arrive after a side effect.
5320
- */
5321
- type ReplayPolicy = {
5322
- /** Refuse to buffer a `Request` body larger than this many bytes, when the size is known. */
5323
- maxBodyBytes?: number;
5324
- /** A stream is never replayable; the field exists so the intent is written down. */
5325
- allowStreamingBody?: false;
5326
- } | 'always' | 'idempotent-only' | ((info: UpgradeReplayInfo) => boolean);
5327
5345
  /** "You were challenged and you declined" — as opposed to "you were not authorised". */
5328
5346
  export declare const isUpgradeCancelled: (response: object) => boolean;
5329
5347
  export declare const isUpgradeExhausted: (response: object) => boolean;
@@ -5345,6 +5363,11 @@ type ResponseInterceptorHost = {
5345
5363
  use(onFulfilled?: (value: any) => any, onRejected?: (error: any) => any): number;
5346
5364
  eject(id: number): void;
5347
5365
  };
5366
+ /** Where the access token is added. A client without one gets step-up handling only. */
5367
+ request?: {
5368
+ use(onFulfilled?: (value: any) => any, onRejected?: (error: any) => any): number;
5369
+ eject(id: number): void;
5370
+ };
5348
5371
  };
5349
5372
  };
5350
5373
  /**
@@ -5641,10 +5664,17 @@ type UpgradeFacade = {
5641
5664
  * prompt, because they share one controller.
5642
5665
  */
5643
5666
  elevate(challenge: UpgradeChallenge, waiter?: Waiter): Promise<UpgradeElevation>;
5644
- /** Wraps a fetch and returns the wrapper. Touches no global. */
5645
- withAuth(next?: FetchLike): FetchLike;
5667
+ /**
5668
+ * Wraps a fetch and returns the wrapper. Touches no global. Adds the access token to requests for
5669
+ * the page's own origin and `options.origins` that carry no `Authorization` header.
5670
+ */
5671
+ withAuth(next?: FetchLike, options?: WithAuthOptions): FetchLike;
5646
5672
  /** Attaches to an axios instance the developer owns, MUTATING it. Returns `detach()`. */
5647
- attachAuth(instance: ResponseInterceptorHost): () => void;
5673
+ /**
5674
+ * Attaches step-up handling to an axios instance and, under the same rule as `withAuth`, a
5675
+ * request interceptor that adds the access token. Returns a disposer that removes both.
5676
+ */
5677
+ attachAuth(instance: ResponseInterceptorHost, options?: WithAuthOptions): () => void;
5648
5678
  };
5649
5679
  /**
5650
5680
  * Orders the factors to offer for a challenge. Index 0 is the suggested
@@ -5750,7 +5780,12 @@ export declare class Passflow {
5750
5780
  */
5751
5781
  setAppId(appId: string): void;
5752
5782
  /**
5753
- * Wraps a `fetch`, returning a new function with step-up handling attached.
5783
+ * Wraps a `fetch`, returning a new function that adds the access token and handles step-up.
5784
+ *
5785
+ * A request for the page's own origin or one in `options.origins` (compared exactly: scheme, host
5786
+ * and port) that has no `Authorization` header gets `Bearer <access token>`, refreshed first when
5787
+ * it has expired. Any other origin, a request with its own header, and a request with no
5788
+ * signed-in user go out as given.
5754
5789
  *
5755
5790
  * Installs nothing and touches no global — this is the SSR- and
5756
5791
  * React-Native-safe surface. Two verbs exist because the semantics genuinely
@@ -5763,18 +5798,23 @@ export declare class Passflow {
5763
5798
  *
5764
5799
  * @example
5765
5800
  * ```typescript
5766
- * const authedFetch = passflow.withAuth(fetch);
5801
+ * const authedFetch = passflow.withAuth(fetch, { origins: ['https://api.example.com'] });
5767
5802
  * const res = await authedFetch('https://api.example.com/transfer', { method: 'POST' });
5768
5803
  * if (!res.ok && isUpgradeCancelled(res)) {
5769
5804
  * // the user declined the step-up — not an authorization failure
5770
5805
  * }
5771
5806
  * ```
5772
5807
  */
5773
- withAuth(next?: FetchLike): FetchLike;
5808
+ withAuth(next?: FetchLike, options?: WithAuthOptions): FetchLike;
5774
5809
  /**
5775
5810
  * Attaches step-up handling to an axios instance the caller already owns,
5776
5811
  * **mutating** it, and returns a `detach()` disposer.
5777
5812
  *
5813
+ * It also adds a request interceptor with `withAuth`'s rule: a request for the
5814
+ * page's own origin or one in `options.origins` (exact scheme, host and port,
5815
+ * `baseURL` joined as axios does) that has no `Authorization` header gets
5816
+ * `Bearer <access token>`. The disposer removes both interceptors.
5817
+ *
5778
5818
  * The parameter is typed structurally — duck-typed on
5779
5819
  * `interceptors.response.use` — so this package's axios version is not a
5780
5820
  * constraint on the caller's. It returns the disposer rather than the
@@ -5784,13 +5824,14 @@ export declare class Passflow {
5784
5824
  * @example
5785
5825
  * ```typescript
5786
5826
  * const api = axios.create({ baseURL: 'https://api.example.com' });
5787
- * const detach = passflow.attachAuth(api);
5827
+ * const detach = passflow.attachAuth(api, { origins: ['https://api.example.com'] });
5788
5828
  * ```
5789
5829
  */
5790
- attachAuth(instance: ResponseInterceptorHost): () => void;
5830
+ attachAuth(instance: ResponseInterceptorHost, options?: WithAuthOptions): () => void;
5791
5831
  /**
5792
- * Builds a ready axios instance with step-up handling already wired, for a
5793
- * caller with no HTTP client of their own.
5832
+ * Builds a ready axios instance with step-up handling and the access token
5833
+ * (as `attachAuth`, with `options.origins`) already wired, for a caller with no
5834
+ * HTTP client of their own.
5794
5835
  *
5795
5836
  * Pure passthrough of axios' own create options, so `baseURL`, `headers`,
5796
5837
  * `timeout` and `adapter` stay the caller's. A `baseURL` is NOT read as a
@@ -5799,10 +5840,10 @@ export declare class Passflow {
5799
5840
  *
5800
5841
  * @example
5801
5842
  * ```typescript
5802
- * const api = passflow.createClient({ baseURL: 'https://api.example.com' });
5843
+ * const api = passflow.createClient({ baseURL: 'https://api.example.com' }, { origins: ['https://api.example.com'] });
5803
5844
  * ```
5804
5845
  */
5805
- createClient(config?: CreateClientConfig): AxiosInstance;
5846
+ createClient(config?: CreateClientConfig, options?: WithAuthOptions): AxiosInstance;
5806
5847
  /**
5807
5848
  * The assurance bar the session currently holds, read from the `acr` claim of
5808
5849
  * the stored access token.
@@ -7193,4 +7234,4 @@ export declare function isValidPhoneNumber(phone: string): boolean;
7193
7234
  * @returns true if valid username format, false otherwise
7194
7235
  */
7195
7236
  export declare function isValidUsername(username: string): boolean;
7196
- export type { AssuranceBar, AuthCallbackResult, BodyKind, ChallengeChannel, CreateClientConfig, CurrentUrlInfo, ErrorPayload, EventOrigin, FetchLike, FetchMiddleware, GroupMembership, InvitationToken, M2MClientConfig, M2MErrorCode, M2MErrorResponse, M2MRateLimitInfo, M2MTokenCache, M2MTokenClaims, M2MTokenRequestInfo, M2MTokenRequestOptions, M2MTokenResponse, OidcErrorCode, OpenAuthUrlOptions, OperationErrorPayload, PassflowEmailVerificationOperation, PassflowEventPayload, PassflowOperation, PassflowRefreshErrorOptions, PassflowSubscriber, PasskeySupport, PlatformAdapter, PlatformStorage, RawUserMembership, RefreshChannel, RefreshClock, RefreshCoordination, RefreshCoordinator, RefreshErrorInfo, RefreshFailureKind, RefreshLockManager, RefreshLockOptions, RefreshTrigger, RegisterOptions, ReplayPolicy, RequestSnapshot, ResponseInterceptorHost, RetryStrategy, SessionExpiredReason, SnapshotHeader, Tenant, TenantMembership, Token, UpgradeAssertContext, UpgradeAssertion, UpgradeChallenge, UpgradeCommitInterruptedReason, UpgradeControllerOptions, UpgradeDiagnostic, UpgradeDiagnosticReason, UpgradeDiagnosticSink, UpgradeElevation, UpgradeEnableOptions, UpgradeEndpoint, UpgradeErrorKind, UpgradeFacade, UpgradeFactor, UpgradeFactorInput, UpgradeFactorType, UpgradeFlightState, UpgradeFlightSummary, UpgradeOptions, UpgradePrepared, UpgradePromptHandler, UpgradePromptRequest, UpgradePromptResult, UpgradeProof, UpgradeReplayInfo, UpgradeResult, UpgradeSessionPin, UpgradeTransport, UpgradeTransportErrorInit, UpgradeWaiterSummary, UserMembership, Waiter, WaiterEntry, WaiterOutcome, WaiterReport };
7237
+ export type { AssuranceBar, AuthCallbackResult, BodyKind, ChallengeChannel, CreateClientConfig, CurrentUrlInfo, ErrorPayload, EventOrigin, FetchLike, FetchMiddleware, GroupMembership, InvitationToken, M2MClientConfig, M2MErrorCode, M2MErrorResponse, M2MRateLimitInfo, M2MTokenCache, M2MTokenClaims, M2MTokenRequestInfo, M2MTokenRequestOptions, M2MTokenResponse, OidcErrorCode, OpenAuthUrlOptions, OperationErrorPayload, PassflowEmailVerificationOperation, PassflowEventPayload, PassflowOperation, PassflowRefreshErrorOptions, PassflowSubscriber, PasskeySupport, PlatformAdapter, PlatformStorage, RawUserMembership, RefreshChannel, RefreshClock, RefreshCoordination, RefreshCoordinator, RefreshErrorInfo, RefreshFailureKind, RefreshLockManager, RefreshLockOptions, RefreshTrigger, RegisterOptions, ReplayPolicy, RequestSnapshot, ResponseInterceptorHost, RetryStrategy, SessionExpiredReason, SnapshotHeader, Tenant, TenantMembership, Token, UpgradeAssertContext, UpgradeAssertion, UpgradeChallenge, UpgradeCommitInterruptedReason, UpgradeControllerOptions, UpgradeDiagnostic, UpgradeDiagnosticReason, UpgradeDiagnosticSink, UpgradeElevation, UpgradeEnableOptions, UpgradeEndpoint, UpgradeErrorKind, UpgradeFacade, UpgradeFactor, UpgradeFactorInput, UpgradeFactorType, UpgradeFlightState, UpgradeFlightSummary, UpgradeOptions, UpgradePrepared, UpgradePromptHandler, UpgradePromptRequest, UpgradePromptResult, UpgradeProof, UpgradeReplayInfo, UpgradeResult, UpgradeSessionPin, UpgradeTransport, UpgradeTransportErrorInit, UpgradeWaiterSummary, UserMembership, Waiter, WaiterEntry, WaiterOutcome, WaiterReport, WithAuthOptions };