@neetru/sdk 3.5.0 → 3.6.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/dist/index.d.cts CHANGED
@@ -70,6 +70,6 @@ declare function checkCoreVersionCompat(coreVersion: string): void;
70
70
  */
71
71
 
72
72
  /** Versão do SDK — exportada explicitamente pra detecção em runtime. */
73
- declare const VERSION: "3.5.0";
73
+ declare const VERSION: "3.6.0";
74
74
 
75
75
  export { VERSION, checkCoreVersionCompat };
package/dist/index.d.ts CHANGED
@@ -70,6 +70,6 @@ declare function checkCoreVersionCompat(coreVersion: string): void;
70
70
  */
71
71
 
72
72
  /** Versão do SDK — exportada explicitamente pra detecção em runtime. */
73
- declare const VERSION: "3.5.0";
73
+ declare const VERSION: "3.6.0";
74
74
 
75
75
  export { VERSION, checkCoreVersionCompat };
package/dist/index.mjs CHANGED
@@ -81,7 +81,7 @@ function compareCalVer(a, b) {
81
81
  }
82
82
  function readInjectedSdkVersion() {
83
83
  try {
84
- return "3.5.0";
84
+ return "3.6.0";
85
85
  } catch {
86
86
  return void 0;
87
87
  }
@@ -7400,6 +7400,94 @@ var MockAi = class {
7400
7400
  }
7401
7401
  };
7402
7402
 
7403
+ // src/auth-pkce-shared.ts
7404
+ init_errors();
7405
+ function getWebCrypto() {
7406
+ const c = globalThis.crypto;
7407
+ if (!c || typeof c.getRandomValues !== "function" || !c.subtle) {
7408
+ throw new NeetruError(
7409
+ "runtime_unsupported",
7410
+ "auth.signIn requer WebCrypto (crypto.subtle) \u2014 indispon\xEDvel neste runtime. Use HTTPS (ou http://localhost) num browser moderno, ou instale um polyfill WebCrypto (ex: react-native-quick-crypto) em React Native."
7411
+ );
7412
+ }
7413
+ return c;
7414
+ }
7415
+ function base64UrlFromBytes(bytes) {
7416
+ let str = "";
7417
+ for (const b of bytes) str += String.fromCharCode(b);
7418
+ const b64 = typeof btoa === "function" ? btoa(str) : Buffer.from(str, "binary").toString("base64");
7419
+ return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
7420
+ }
7421
+ function randomUrlSafe(crypto2, byteLen) {
7422
+ const bytes = new Uint8Array(byteLen);
7423
+ crypto2.getRandomValues(bytes);
7424
+ return base64UrlFromBytes(bytes);
7425
+ }
7426
+ async function pkceChallengeS256(crypto2, verifier) {
7427
+ const data = new TextEncoder().encode(verifier);
7428
+ const digest = await crypto2.subtle.digest("SHA-256", data);
7429
+ return base64UrlFromBytes(new Uint8Array(digest));
7430
+ }
7431
+ function resolveIdpOrigin(baseUrl) {
7432
+ const overrideAuthUrl = typeof globalThis.NEETRU_AUTH_URL === "string" ? globalThis.NEETRU_AUTH_URL : null;
7433
+ return overrideAuthUrl ?? baseUrl.replace(/^https?:\/\/api\./, "https://auth.");
7434
+ }
7435
+ function resolveOidcClientId(config) {
7436
+ let clientId = config.oidcClientId;
7437
+ if (!clientId && config.apiKey) {
7438
+ clientId = parseApiKey(config.apiKey).keyId;
7439
+ }
7440
+ if (!clientId) {
7441
+ throw new NeetruError(
7442
+ "invalid_config",
7443
+ "auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
7444
+ );
7445
+ }
7446
+ return clientId;
7447
+ }
7448
+ function buildAuthorizeUrl(params) {
7449
+ const url = new URL("/api/v1/oauth/authorize", params.idpOrigin);
7450
+ url.searchParams.set("response_type", "code");
7451
+ url.searchParams.set("client_id", params.clientId);
7452
+ url.searchParams.set("redirect_uri", params.redirectUri);
7453
+ url.searchParams.set("scope", params.scope);
7454
+ url.searchParams.set("state", params.state);
7455
+ url.searchParams.set("nonce", params.nonce);
7456
+ url.searchParams.set("code_challenge", params.codeChallenge);
7457
+ url.searchParams.set("code_challenge_method", "S256");
7458
+ return url;
7459
+ }
7460
+ function decodeJwtPayload(token) {
7461
+ const parts = token.split(".");
7462
+ if (parts.length !== 3) return null;
7463
+ try {
7464
+ const b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
7465
+ const pad = "=".repeat((4 - b64.length % 4) % 4);
7466
+ const decoded = typeof atob === "function" ? atob(b64 + pad) : Buffer.from(b64 + pad, "base64").toString("utf-8");
7467
+ return JSON.parse(decoded);
7468
+ } catch {
7469
+ return null;
7470
+ }
7471
+ }
7472
+ function jwtToNeetruUser(token) {
7473
+ const payload = decodeJwtPayload(token);
7474
+ if (!payload || typeof payload.sub !== "string") return null;
7475
+ if (typeof payload.exp === "number" && payload.exp * 1e3 < Date.now()) {
7476
+ return null;
7477
+ }
7478
+ return {
7479
+ uid: payload.sub,
7480
+ email: typeof payload.email === "string" ? payload.email : "",
7481
+ emailVerified: typeof payload.email_verified === "boolean" ? payload.email_verified : void 0,
7482
+ displayName: typeof payload.name === "string" ? payload.name : void 0,
7483
+ photoURL: typeof payload.picture === "string" ? payload.picture : void 0,
7484
+ isStaff: payload.is_staff === true,
7485
+ isCustomer: payload.is_customer === true,
7486
+ tenantId: typeof payload.tenant_id === "string" ? payload.tenant_id : void 0,
7487
+ ...payload
7488
+ };
7489
+ }
7490
+
7403
7491
  // src/auth.ts
7404
7492
  function readEnvApiKey() {
7405
7493
  try {
@@ -7454,36 +7542,6 @@ function _jwtPayloadToUser(payload) {
7454
7542
  };
7455
7543
  }
7456
7544
  var OIDC_TX_KEY = "neetru_oauth_tx";
7457
- function resolveIdpOrigin(baseUrl) {
7458
- const overrideAuthUrl = typeof globalThis.NEETRU_AUTH_URL === "string" ? globalThis.NEETRU_AUTH_URL : null;
7459
- return overrideAuthUrl ?? baseUrl.replace(/^https?:\/\/api\./, "https://auth.");
7460
- }
7461
- function getWebCrypto() {
7462
- const c = globalThis.crypto;
7463
- if (!c || typeof c.getRandomValues !== "function" || !c.subtle) {
7464
- throw new NeetruError(
7465
- "runtime_unsupported",
7466
- "auth.signIn requer WebCrypto (crypto.subtle) \u2014 indispon\xEDvel neste runtime. Use HTTPS (ou http://localhost) num browser moderno."
7467
- );
7468
- }
7469
- return c;
7470
- }
7471
- function base64UrlFromBytes(bytes) {
7472
- let str = "";
7473
- for (const b of bytes) str += String.fromCharCode(b);
7474
- const b64 = typeof btoa === "function" ? btoa(str) : Buffer.from(str, "binary").toString("base64");
7475
- return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
7476
- }
7477
- function randomUrlSafe(crypto2, byteLen) {
7478
- const bytes = new Uint8Array(byteLen);
7479
- crypto2.getRandomValues(bytes);
7480
- return base64UrlFromBytes(bytes);
7481
- }
7482
- async function pkceChallengeS256(crypto2, verifier) {
7483
- const data = new TextEncoder().encode(verifier);
7484
- const digest = await crypto2.subtle.digest("SHA-256", data);
7485
- return base64UrlFromBytes(new Uint8Array(digest));
7486
- }
7487
7545
  function cleanCallbackUrl(parsed) {
7488
7546
  try {
7489
7547
  const history = globalThis.history;
@@ -7517,36 +7575,6 @@ function createOidcAuthNamespace(config) {
7517
7575
  return null;
7518
7576
  }
7519
7577
  }
7520
- function decodeJwtPayload(token) {
7521
- const parts = token.split(".");
7522
- if (parts.length !== 3) return null;
7523
- try {
7524
- const b64 = parts[1].replace(/-/g, "+").replace(/_/g, "/");
7525
- const pad = "=".repeat((4 - b64.length % 4) % 4);
7526
- const decoded = typeof atob === "function" ? atob(b64 + pad) : Buffer.from(b64 + pad, "base64").toString("utf-8");
7527
- return JSON.parse(decoded);
7528
- } catch {
7529
- return null;
7530
- }
7531
- }
7532
- function tokenToUser(token) {
7533
- const payload = decodeJwtPayload(token);
7534
- if (!payload || typeof payload.sub !== "string") return null;
7535
- if (typeof payload.exp === "number" && payload.exp * 1e3 < Date.now()) {
7536
- return null;
7537
- }
7538
- return {
7539
- uid: payload.sub,
7540
- email: typeof payload.email === "string" ? payload.email : "",
7541
- emailVerified: typeof payload.email_verified === "boolean" ? payload.email_verified : void 0,
7542
- displayName: typeof payload.name === "string" ? payload.name : void 0,
7543
- photoURL: typeof payload.picture === "string" ? payload.picture : void 0,
7544
- isStaff: payload.is_staff === true,
7545
- isCustomer: payload.is_customer === true,
7546
- tenantId: typeof payload.tenant_id === "string" ? payload.tenant_id : void 0,
7547
- ...payload
7548
- };
7549
- }
7550
7578
  function loadCachedUser() {
7551
7579
  if (cachedUser) {
7552
7580
  const exp = cachedUser.exp;
@@ -7567,7 +7595,7 @@ function createOidcAuthNamespace(config) {
7567
7595
  attemptSilentRefresh();
7568
7596
  return null;
7569
7597
  }
7570
- const user = tokenToUser(token);
7598
+ const user = jwtToNeetruUser(token);
7571
7599
  if (!user) {
7572
7600
  storage.removeItem(STORAGE_KEY);
7573
7601
  attemptSilentRefresh();
@@ -7638,7 +7666,7 @@ function createOidcAuthNamespace(config) {
7638
7666
  if (!res.ok || typeof payload?.error === "string") return false;
7639
7667
  const idToken = payload?.id_token;
7640
7668
  if (typeof idToken !== "string" || idToken.length === 0) return false;
7641
- const user = tokenToUser(idToken);
7669
+ const user = jwtToNeetruUser(idToken);
7642
7670
  if (!user) return false;
7643
7671
  const storage = getStorage();
7644
7672
  storage?.setItem(STORAGE_KEY, idToken);
@@ -7684,17 +7712,7 @@ function createOidcAuthNamespace(config) {
7684
7712
  }, msUntilExpiry + 1);
7685
7713
  }
7686
7714
  function resolveClientId() {
7687
- let clientId = config.oidcClientId;
7688
- if (!clientId && config.apiKey) {
7689
- clientId = parseApiKey(config.apiKey).keyId;
7690
- }
7691
- if (!clientId) {
7692
- throw new NeetruError(
7693
- "invalid_config",
7694
- "auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
7695
- );
7696
- }
7697
- return clientId;
7715
+ return resolveOidcClientId(config);
7698
7716
  }
7699
7717
  async function handleRedirectCallback(currentUrl) {
7700
7718
  const href = currentUrl ?? (typeof globalThis.location !== "undefined" ? globalThis.location.href : void 0);
@@ -7812,7 +7830,7 @@ function createOidcAuthNamespace(config) {
7812
7830
  "nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
7813
7831
  );
7814
7832
  }
7815
- const user = tokenToUser(idToken);
7833
+ const user = jwtToNeetruUser(idToken);
7816
7834
  if (!user) {
7817
7835
  throw new NeetruError(
7818
7836
  "token_exchange_failed",
@@ -7857,15 +7875,15 @@ function createOidcAuthNamespace(config) {
7857
7875
  };
7858
7876
  session.setItem(OIDC_TX_KEY, JSON.stringify(tx));
7859
7877
  const idpOrigin = resolveIdpOrigin(config.baseUrl);
7860
- const url = new URL("/api/v1/oauth/authorize", idpOrigin);
7861
- url.searchParams.set("response_type", "code");
7862
- url.searchParams.set("client_id", clientId);
7863
- url.searchParams.set("redirect_uri", redirectUri);
7864
- url.searchParams.set("scope", scope);
7865
- url.searchParams.set("state", state);
7866
- url.searchParams.set("nonce", nonce);
7867
- url.searchParams.set("code_challenge", codeChallenge);
7868
- url.searchParams.set("code_challenge_method", "S256");
7878
+ const url = buildAuthorizeUrl({
7879
+ idpOrigin,
7880
+ clientId,
7881
+ redirectUri,
7882
+ scope,
7883
+ state,
7884
+ nonce,
7885
+ codeChallenge
7886
+ });
7869
7887
  globalThis.location.assign(url.toString());
7870
7888
  return;
7871
7889
  }
@@ -7880,7 +7898,7 @@ function createOidcAuthNamespace(config) {
7880
7898
  const storage = getStorage();
7881
7899
  const token = storage?.getItem(STORAGE_KEY) ?? null;
7882
7900
  if (!token) return null;
7883
- return tokenToUser(token) ? token : null;
7901
+ return jwtToNeetruUser(token) ? token : null;
7884
7902
  },
7885
7903
  async signOut() {
7886
7904
  const storage = getStorage();
@@ -8043,7 +8061,7 @@ function getServerNeetruClient(config = {}) {
8043
8061
  init_errors();
8044
8062
  init_db_errors();
8045
8063
  init_http();
8046
- var VERSION = "3.5.0";
8064
+ var VERSION = "3.6.0";
8047
8065
 
8048
8066
  export { DEFAULT_BASE_URL, DEV_FIXTURE_USER, MockAi, MockAuth, MockCheckout, MockDb, MockEntitlements, MockNotifications, MockSupport, MockUsage, MockWebhooks, NEETRU_SENTINEL_KEY, NeetruDbError, NeetruError, VERSION, checkCoreVersionCompat, createAiNamespace, createCheckoutNamespace, createNeetruClient, createNeetruDb, createNotificationsNamespace, createWebhooksNamespace, getServerNeetruClient, increment, isFieldSentinel, isIncrementSentinel, isServerTimestampSentinel, serverTimestamp, verifyWebhookRequest, verifyWebhookSignature };
8049
8067
  //# sourceMappingURL=index.mjs.map