axiam-sdk 1.1.3 → 1.2.5

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.
Files changed (50) hide show
  1. package/README.md +73 -3
  2. package/dist/amqp/index.js.map +1 -1
  3. package/dist/amqp/index.mjs.map +1 -1
  4. package/dist/browser/index.d.cts +1 -1
  5. package/dist/browser/index.d.ts +1 -1
  6. package/dist/{client-BGFn1VQh.d.cts → client-Bp7riC4k.d.cts} +147 -3
  7. package/dist/{client-DrQZZ1js.d.ts → client-CaurIxje.d.ts} +147 -3
  8. package/dist/{cookieJar-BTmpjXZe.d.ts → cookieJar-DC4GLKrJ.d.ts} +2 -2
  9. package/dist/{cookieJar-BqYOnNN-.d.cts → cookieJar-oKazfbN1.d.cts} +2 -2
  10. package/dist/{errors-DZ3k8a8i.d.cts → errors-CWlh-wcm.d.cts} +7 -1
  11. package/dist/{errors-DZ3k8a8i.d.ts → errors-CWlh-wcm.d.ts} +7 -1
  12. package/dist/{errors-dlGXAsbh.d.ts → errors-DDo3oxcH.d.ts} +1 -1
  13. package/dist/{errors-RvGlTyUj.d.cts → errors-DHauOsqx.d.cts} +1 -1
  14. package/dist/grpc/index.d.cts +4 -4
  15. package/dist/grpc/index.d.ts +4 -4
  16. package/dist/grpc/index.js +26 -5
  17. package/dist/grpc/index.js.map +1 -1
  18. package/dist/grpc/index.mjs +26 -5
  19. package/dist/grpc/index.mjs.map +1 -1
  20. package/dist/index.d.cts +4 -4
  21. package/dist/index.d.ts +4 -4
  22. package/dist/index.js +26 -5
  23. package/dist/index.js.map +1 -1
  24. package/dist/index.mjs +26 -5
  25. package/dist/index.mjs.map +1 -1
  26. package/dist/middleware/index.d.cts +5 -5
  27. package/dist/middleware/index.d.ts +5 -5
  28. package/dist/middleware/index.js +164 -12
  29. package/dist/middleware/index.js.map +1 -1
  30. package/dist/middleware/index.mjs +164 -12
  31. package/dist/middleware/index.mjs.map +1 -1
  32. package/dist/nestjs/index.js.map +1 -1
  33. package/dist/nestjs/index.mjs.map +1 -1
  34. package/dist/node/index.d.cts +15 -4
  35. package/dist/node/index.d.ts +15 -4
  36. package/dist/node/index.js +186 -14
  37. package/dist/node/index.js.map +1 -1
  38. package/dist/node/index.mjs +185 -15
  39. package/dist/node/index.mjs.map +1 -1
  40. package/dist/{oidcState-C3VLn3eD.d.ts → oidcState-CZhj0MBP.d.ts} +123 -5
  41. package/dist/{oidcState-BaJ-SM_R.d.cts → oidcState-EnEJmoF5.d.cts} +123 -5
  42. package/dist/rest/index.d.cts +6 -6
  43. package/dist/rest/index.d.ts +6 -6
  44. package/dist/rest/index.js +26 -5
  45. package/dist/rest/index.js.map +1 -1
  46. package/dist/rest/index.mjs +26 -5
  47. package/dist/rest/index.mjs.map +1 -1
  48. package/dist/{session-B03NsqVs.d.ts → session-Beo11VT_.d.ts} +9 -0
  49. package/dist/{session-plDr4lFu.d.cts → session-CeIUXgpE.d.cts} +9 -0
  50. package/package.json +1 -1
@@ -35,11 +35,18 @@ var OAuthProtocolError = class _OAuthProtocolError extends AuthError {
35
35
  * OPTIONAL, and §2 (contract 1.53, §28.12.3) dispatches on `error` alone.
36
36
  */
37
37
  errorDescription;
38
- constructor(error, errorDescription = "") {
38
+ /**
39
+ * The server's `Retry-After` in milliseconds, when this error is a `429` that carried one in
40
+ * delta-seconds form (contract 1.65 §37.3, C165-17). Absent otherwise; an HTTP-date is not read.
41
+ * Additive: honour it before retrying a rate-limited native sign-in or CIBA call.
42
+ */
43
+ retryAfterMs;
44
+ constructor(error, errorDescription = "", retryAfterMs2) {
39
45
  super(errorDescription ? `${error}: ${errorDescription}` : error);
40
46
  this.name = "OAuthProtocolError";
41
47
  this.error = error;
42
48
  this.errorDescription = errorDescription;
49
+ if (retryAfterMs2 !== void 0) this.retryAfterMs = retryAfterMs2;
43
50
  Object.setPrototypeOf(this, _OAuthProtocolError.prototype);
44
51
  }
45
52
  };
@@ -100,7 +107,7 @@ function isOAuth2ErrorBody(body) {
100
107
  const record = body;
101
108
  return typeof record.error === "string" && typeof record.error_description === "string";
102
109
  }
103
- function oauth2ErrorFromBody(body) {
110
+ function oauth2ErrorFromBody(body, retryAfterMs2) {
104
111
  if (body === null || typeof body !== "object" || Array.isArray(body)) {
105
112
  return void 0;
106
113
  }
@@ -109,7 +116,7 @@ function oauth2ErrorFromBody(body) {
109
116
  return void 0;
110
117
  }
111
118
  const description = typeof record.error_description === "string" ? record.error_description : "";
112
- return new OAuthProtocolError(record.error, description);
119
+ return new OAuthProtocolError(record.error, description, retryAfterMs2);
113
120
  }
114
121
  var SAFE_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
115
122
  "content-type",
@@ -1553,9 +1560,9 @@ function pollRateLimit(err) {
1553
1560
  if (axiosStatus(err) !== 429) return void 0;
1554
1561
  const body = axiosBody(err);
1555
1562
  const description = body !== null && typeof body === "object" && typeof body.error_description === "string" ? body.error_description : "";
1556
- const limited = new OAuthProtocolError("rate_limit_exceeded", description);
1557
1563
  const response = err.response;
1558
1564
  const retryAfter = response ? retryAfterMs(response) : void 0;
1565
+ const limited = new OAuthProtocolError("rate_limit_exceeded", description, retryAfter);
1559
1566
  if (retryAfter !== void 0) POLL_RETRY_AFTER_MS.set(limited, retryAfter);
1560
1567
  return limited;
1561
1568
  }
@@ -1575,6 +1582,24 @@ var RESERVED_AUTHORIZE_PARAMS = /* @__PURE__ */ new Set([
1575
1582
  "code_challenge",
1576
1583
  "code_challenge_method"
1577
1584
  ]);
1585
+ var IDP_HINT_PARAM = "axiam_idp_hint";
1586
+ var IDP_HINT_MAX_BYTES = 256;
1587
+ var PROVIDER_ID_TOKEN_GRANT_TYPE = "urn:axiam:params:oauth:grant-type:provider-id-token";
1588
+ function checkIdpHint(operation, hint) {
1589
+ if (hint === "") {
1590
+ throw new ValidationError(operation, 400, `${operation}: idpHint must not be empty (CONTRACT.md \xA712.1)`, [
1591
+ { field: "idpHint", message: "must not be empty" }
1592
+ ]);
1593
+ }
1594
+ if (Buffer.byteLength(hint, "utf8") > IDP_HINT_MAX_BYTES) {
1595
+ throw new ValidationError(
1596
+ operation,
1597
+ 400,
1598
+ `${operation}: idpHint must be at most ${IDP_HINT_MAX_BYTES} bytes (CONTRACT.md \xA712.1)`,
1599
+ [{ field: "idpHint", message: `must be at most ${IDP_HINT_MAX_BYTES} bytes` }]
1600
+ );
1601
+ }
1602
+ }
1578
1603
  var UUID_RE2 = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
1579
1604
  var DiscoveryCache = class {
1580
1605
  #documents = /* @__PURE__ */ new Map();
@@ -1637,11 +1662,25 @@ function bodyMessage(body, fallback) {
1637
1662
  }
1638
1663
  return fallback;
1639
1664
  }
1665
+ function beginUrlIdpHint(url) {
1666
+ try {
1667
+ const value = new URL(url).searchParams.get(IDP_HINT_PARAM);
1668
+ return value === null || value === "" ? void 0 : value;
1669
+ } catch {
1670
+ return void 0;
1671
+ }
1672
+ }
1673
+ function redirectRefused(operation, status) {
1674
+ return new NetworkError(`${operation}: the server answered HTTP ${status}; a redirect is never followed (CONTRACT.md \xA712.1 rule 3)`);
1675
+ }
1640
1676
  function mapOidcError(err, url, fallbackMessage) {
1641
1677
  if (err instanceof AxiamError) {
1642
1678
  return err;
1643
1679
  }
1644
1680
  const status = axiosStatus(err);
1681
+ if (status !== void 0 && status >= 300 && status < 400) {
1682
+ return redirectRefused(fallbackMessage, status);
1683
+ }
1645
1684
  if (status !== void 0) {
1646
1685
  const body = axiosBody(err);
1647
1686
  return mapHttpStatusToError(status, bodyMessage(body, fallbackMessage), { url, body, cause: err });
@@ -1811,7 +1850,8 @@ function jsonBody(data) {
1811
1850
  }
1812
1851
  }
1813
1852
  function cibaError(operation, response) {
1814
- return oauth2ErrorFromBody(jsonBody(response.data)) ?? mapHttpStatusToError(response.status, `${operation}: HTTP ${response.status}`);
1853
+ if (response.status >= 300 && response.status < 400) return redirectRefused(operation, response.status);
1854
+ return oauth2ErrorFromBody(jsonBody(response.data), response.status === 429 ? retryAfterMs(response) : void 0) ?? mapHttpStatusToError(response.status, `${operation}: HTTP ${response.status}`);
1815
1855
  }
1816
1856
  function authorizationValues(headers) {
1817
1857
  const out = [];
@@ -1880,15 +1920,19 @@ var OidcClient = class {
1880
1920
  * fresh PKCE verifier/challenge pair using **S256 only** — `plain` is not
1881
1921
  * implemented anywhere in this SDK. The URL is built from the discovery
1882
1922
  * document's `authorization_endpoint` with exactly the eight parameters
1883
- * §12.1 rule 5 mandates, plus any `extraParams` the caller adds.
1923
+ * §12.1 rule 5 mandates, plus any `extraParams` the caller adds and, when
1924
+ * `idpHint` is set, the one `axiam_idp_hint` (contract 1.65).
1884
1925
  *
1885
1926
  * Nothing is stored: persist the returned `state`, `nonce` and
1886
1927
  * `codeVerifier` yourself (§12.3 rule 1).
1887
1928
  *
1888
1929
  * @throws Error when `extraParams` tries to override one of the eight
1889
1930
  * SDK-owned parameters — a programming error, caught at call time.
1931
+ * @throws ValidationError when `idpHint` is empty or over 256 UTF-8 bytes;
1932
+ * no URL is built.
1890
1933
  */
1891
1934
  oidcBegin(params) {
1935
+ if (params.idpHint !== void 0) checkIdpHint("oidcBegin", params.idpHint);
1892
1936
  const state = randomUrlSafeToken();
1893
1937
  const nonce = randomUrlSafeToken();
1894
1938
  const codeVerifier = generateCodeVerifier();
@@ -1902,6 +1946,7 @@ var OidcClient = class {
1902
1946
  `oidcBegin: extraParams may not override the SDK-owned authorization parameter "${key}" (CONTRACT.md \xA712.1 rule 5).`
1903
1947
  );
1904
1948
  }
1949
+ if (key === IDP_HINT_PARAM && params.idpHint !== void 0) continue;
1905
1950
  query.set(key, value);
1906
1951
  }
1907
1952
  query.set("response_type", "code");
@@ -1912,6 +1957,7 @@ var OidcClient = class {
1912
1957
  query.set("nonce", nonce);
1913
1958
  query.set("code_challenge", codeChallenge);
1914
1959
  query.set("code_challenge_method", CODE_CHALLENGE_METHOD_S256);
1960
+ if (params.idpHint !== void 0) query.set(IDP_HINT_PARAM, params.idpHint);
1915
1961
  target.search = query.toString().replace(/\+/g, "%20");
1916
1962
  return { url: target.toString(), state, nonce, codeVerifier };
1917
1963
  }
@@ -2008,6 +2054,94 @@ var OidcClient = class {
2008
2054
  return tokenSet;
2009
2055
  }
2010
2056
  // -------------------------------------------------------------------------
2057
+ // §37 loginWithProviderIdToken
2058
+ // -------------------------------------------------------------------------
2059
+ /**
2060
+ * `POST /oauth2/token` with
2061
+ * `grant_type=urn:axiam:params:oauth:grant-type:provider-id-token`
2062
+ * (CONTRACT.md §37, contract 1.65) — sign a native-app user in with the ID
2063
+ * token a Google, Apple or Facebook Limited Login SDK produced.
2064
+ *
2065
+ * @param provider - The provider's UUID or its `provider_slug`, sent verbatim:
2066
+ * never trimmed, case-folded, resolved or looked up. A provider *kind* name
2067
+ * (`google`) is not accepted here unless the operator also chose it as the
2068
+ * slug (§37.2 rule 2).
2069
+ * @param idToken - The provider's ID token (a credential; wrapped or bare).
2070
+ * @param nonce - The **raw** nonce, byte for byte: the provider's SDK was
2071
+ * given `SHA-256(raw)` as hexadecimal (`nativeNonce()` produces the
2072
+ * pair). Never hashed, replaced or generated here (§37.2 rule 3).
2073
+ * @param options - `scopes`, `resource`, `tenantId`, `configuration`.
2074
+ *
2075
+ * @remarks
2076
+ * **Never retried** — not on a status, a `429` or a transport failure: the
2077
+ * provider's token is single-use across the deployment (§37.2 rule 10). An
2078
+ * `invalid_grant` is the server's one generic answer to every refusal of the
2079
+ * token and is neither retried nor refined; the recovery is a **fresh**
2080
+ * provider token with a **fresh** nonce. A transport failure after the
2081
+ * request left is indeterminate and surfaces as `NetworkError`.
2082
+ *
2083
+ * No `openid` or other scope is added: omit `scopes` and the response carries
2084
+ * no `id_token`. This is not token exchange (§15) and never falls back to it
2085
+ * or to a browser flow. A public client sends `client_id` alone; a client
2086
+ * configured with a `clientSecret` sends it in the body.
2087
+ *
2088
+ * The result is the ordinary {@link OidcTokenSet}; an `id_token`, when
2089
+ * present, is validated under §12.4 with the raw `nonce` as the expected
2090
+ * nonce. As with `deviceLogin`, the token set is **returned and not
2091
+ * adopted** as this client's credential (§37.2 rule 9).
2092
+ *
2093
+ * Neither the ID token nor the raw nonce is logged, put in an error, or kept
2094
+ * after the call returns; hold the raw nonce only for the length of the
2095
+ * attempt.
2096
+ *
2097
+ * @throws ValidationError (local, no request) for an empty `provider`,
2098
+ * `idToken` or `nonce`.
2099
+ * @throws AuthError (local, no request) for a slug-only client (§12.3 rule 4).
2100
+ * @throws OAuthProtocolError for a body carrying `error`, at any status.
2101
+ * @throws NetworkError for a transport failure or a body-less `5xx`.
2102
+ *
2103
+ * @example
2104
+ * ```ts
2105
+ * const { raw, hashed } = nativeNonce();
2106
+ * const idToken = await providerSdk.signIn({ nonce: hashed });
2107
+ * const tokens = await oidc.loginWithProviderIdToken('corp-google', idToken, raw, {
2108
+ * scopes: ['openid', 'profile'],
2109
+ * });
2110
+ * ```
2111
+ */
2112
+ async loginWithProviderIdToken(provider, idToken, nonce, options = {}) {
2113
+ const operation = "loginWithProviderIdToken";
2114
+ const refuse2 = (field) => new ValidationError(operation, 400, `${operation}: ${field} must not be empty (CONTRACT.md \xA737.2 rule 4)`, [
2115
+ { field, message: "must not be empty" }
2116
+ ]);
2117
+ const idTokenValue = typeof idToken === "string" ? idToken : idToken.expose();
2118
+ const nonceValue = typeof nonce === "string" ? nonce : nonce.expose();
2119
+ if (provider === "") throw refuse2("provider");
2120
+ if (idTokenValue === "") throw refuse2("idToken");
2121
+ if (nonceValue === "") throw refuse2("nonce");
2122
+ this.#resolveTenantId(options.tenantId);
2123
+ const configuration = options.configuration ?? await this.oidcDiscover();
2124
+ const form = new URLSearchParams();
2125
+ form.set("grant_type", PROVIDER_ID_TOKEN_GRANT_TYPE);
2126
+ form.set("client_id", this.#options.clientId);
2127
+ form.set("provider", provider);
2128
+ form.set("id_token", idTokenValue);
2129
+ form.set("nonce", nonceValue);
2130
+ if (options.scopes !== void 0 && options.scopes.length > 0) {
2131
+ form.set("scope", options.scopes.join(" "));
2132
+ }
2133
+ if (options.resource !== void 0) {
2134
+ form.set("resource", options.resource);
2135
+ }
2136
+ this.#appendClientSecret(form);
2137
+ const url = this.#endpointUrl(this.#requiredEndpoint(configuration, "token_endpoint"), options.tenantId);
2138
+ const response = await this.#postCibaForm(url, form, operation);
2139
+ if (response.status < 200 || response.status >= 300) {
2140
+ throw cibaError(operation, response);
2141
+ }
2142
+ return this.#toTokenSet(response.data, configuration, nonceValue);
2143
+ }
2144
+ // -------------------------------------------------------------------------
2011
2145
  // 6. introspect
2012
2146
  // -------------------------------------------------------------------------
2013
2147
  /**
@@ -2440,6 +2574,7 @@ var OidcClient = class {
2440
2574
  * ```
2441
2575
  */
2442
2576
  async oidcPar(params) {
2577
+ if (params.idpHint !== void 0) checkIdpHint("oidcPar", params.idpHint);
2443
2578
  const configuration = params.configuration ?? await this.oidcDiscover();
2444
2579
  const endpoint = this.#optionalEndpoint(configuration, "pushed_authorization_request_endpoint");
2445
2580
  if (!endpoint) {
@@ -2460,6 +2595,11 @@ var OidcClient = class {
2460
2595
  if (params.dpopJkt !== void 0) {
2461
2596
  form.set("dpop_jkt", params.dpopJkt);
2462
2597
  }
2598
+ const pushedHint = params.idpHint ?? beginUrlIdpHint(request.url);
2599
+ if (pushedHint !== void 0) {
2600
+ checkIdpHint("oidcPar", pushedHint);
2601
+ form.set(IDP_HINT_PARAM, pushedHint);
2602
+ }
2463
2603
  this.#appendClientSecret(form);
2464
2604
  const url = this.#endpointUrl(endpoint, params.tenantId);
2465
2605
  const { data } = await this.#postForm(
@@ -2806,7 +2946,8 @@ var OidcClient = class {
2806
2946
  let data;
2807
2947
  try {
2808
2948
  const config = {
2809
- headers: { "Content-Type": "application/x-www-form-urlencoded" }
2949
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
2950
+ maxRedirects: 0
2810
2951
  };
2811
2952
  const response = await this.#session.axios.post(
2812
2953
  url,
@@ -3137,7 +3278,13 @@ var OidcClient = class {
3137
3278
  if (response.status >= 500) {
3138
3279
  throw transient(retryableNetworkError(`${operation}: HTTP ${response.status}`, response));
3139
3280
  }
3140
- const protocol = oauth2ErrorFromBody(jsonBody(response.data));
3281
+ if (response.status >= 300 && response.status < 400) {
3282
+ return { error: redirectRefused(operation, response.status) };
3283
+ }
3284
+ const protocol = oauth2ErrorFromBody(
3285
+ jsonBody(response.data),
3286
+ response.status === 429 ? retryAfterMs(response) : void 0
3287
+ );
3141
3288
  if (protocol) return { error: protocol };
3142
3289
  if (statusIsRetryable(response.status)) {
3143
3290
  throw transient(retryableNetworkError(`${operation}: HTTP ${response.status}`, response));
@@ -3295,7 +3442,8 @@ var OidcClient = class {
3295
3442
  try {
3296
3443
  return await this.#session.axios.post(url, form.toString(), {
3297
3444
  headers: { "Content-Type": "application/x-www-form-urlencoded" },
3298
- validateStatus: () => true
3445
+ validateStatus: () => true,
3446
+ maxRedirects: 0
3299
3447
  });
3300
3448
  } catch (err) {
3301
3449
  const code = err && typeof err === "object" && typeof err.code === "string" ? ` (${err.code})` : "";
@@ -3315,7 +3463,8 @@ var OidcClient = class {
3315
3463
  async () => {
3316
3464
  try {
3317
3465
  const response = await this.#session.axios.post(url, form.toString(), {
3318
- headers: { "Content-Type": "application/x-www-form-urlencoded" }
3466
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
3467
+ maxRedirects: 0
3319
3468
  });
3320
3469
  return { wire: response.data };
3321
3470
  } catch (err) {
@@ -3523,13 +3672,16 @@ var OidcClient = class {
3523
3672
  /** POST a form-encoded body (§12.1 note 1) through the session transport. */
3524
3673
  async #postForm(url, form, fallbackMessage) {
3525
3674
  const config = {
3526
- headers: { "Content-Type": "application/x-www-form-urlencoded" }
3675
+ headers: { "Content-Type": "application/x-www-form-urlencoded" },
3676
+ // §12.1 rule 3 (C165-14): never follow a redirect from an `/oauth2/*` POST.
3677
+ maxRedirects: 0
3527
3678
  };
3528
3679
  try {
3529
3680
  return await this.#session.axios.post(url, form.toString(), config);
3530
3681
  } catch (err) {
3531
3682
  if (axiosStatus(err) === 429) {
3532
- const protocol = oauth2ErrorFromBody(axiosBody(err));
3683
+ const raw = err.response;
3684
+ const protocol = oauth2ErrorFromBody(axiosBody(err), raw ? retryAfterMs(raw) : void 0);
3533
3685
  if (protocol) throw protocol;
3534
3686
  }
3535
3687
  throw mapOidcError(err, url, fallbackMessage);