@neetru/sdk 3.6.0 → 3.8.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/CHANGELOG.md +104 -0
- package/README.md +66 -1
- package/dist/ai.cjs +298 -391
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +298 -391
- package/dist/auth.cjs +3717 -5005
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +3714 -5002
- package/dist/build-guard.cjs +101 -119
- package/dist/build-guard.mjs +91 -109
- package/dist/catalog.cjs +220 -225
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +220 -225
- package/dist/checkout.cjs +307 -323
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +306 -322
- package/dist/db-react.cjs +59 -87
- package/dist/db-react.d.cts +1 -1
- package/dist/db-react.d.ts +1 -1
- package/dist/db-react.mjs +56 -84
- package/dist/db.cjs +2128 -3014
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +2141 -3027
- package/dist/entitlements.cjs +19 -20
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +19 -20
- package/dist/errors.cjs +16 -12
- package/dist/errors.d.cts +59 -3
- package/dist/errors.d.ts +59 -3
- package/dist/errors.mjs +15 -12
- package/dist/firestore-compat.cjs +241 -297
- package/dist/firestore-compat.d.cts +1 -1
- package/dist/firestore-compat.d.ts +1 -1
- package/dist/firestore-compat.mjs +215 -271
- package/dist/index.cjs +3942 -5134
- package/dist/index.d.cts +181 -5
- package/dist/index.d.ts +181 -5
- package/dist/index.mjs +4040 -5239
- package/dist/mobile.cjs +459 -344
- package/dist/mobile.d.cts +59 -4
- package/dist/mobile.d.ts +59 -4
- package/dist/mobile.mjs +459 -344
- package/dist/mocks.cjs +319 -447
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +313 -441
- package/dist/notifications.cjs +296 -317
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +295 -316
- package/dist/react.cjs +557 -707
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +547 -697
- package/dist/support.cjs +402 -328
- package/dist/support.d.cts +29 -3
- package/dist/support.d.ts +29 -3
- package/dist/support.mjs +401 -328
- package/dist/telemetry.cjs +267 -304
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +267 -304
- package/dist/{types-Du_iioJ7.d.cts → types-D-9bjlM0.d.cts} +74 -1
- package/dist/{types-BPATkXYu.d.ts → types-bVztFQTI.d.ts} +74 -1
- package/dist/usage.cjs +233 -230
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +233 -230
- package/dist/webhooks.cjs +328 -362
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +325 -359
- package/package.json +5 -3
- package/dist/ai.cjs.map +0 -1
- package/dist/ai.mjs.map +0 -1
- package/dist/auth.cjs.map +0 -1
- package/dist/auth.mjs.map +0 -1
- package/dist/build-guard.cjs.map +0 -1
- package/dist/build-guard.mjs.map +0 -1
- package/dist/catalog.cjs.map +0 -1
- package/dist/catalog.mjs.map +0 -1
- package/dist/checkout.cjs.map +0 -1
- package/dist/checkout.mjs.map +0 -1
- package/dist/db-react.cjs.map +0 -1
- package/dist/db-react.mjs.map +0 -1
- package/dist/db.cjs.map +0 -1
- package/dist/db.mjs.map +0 -1
- package/dist/entitlements.cjs.map +0 -1
- package/dist/entitlements.mjs.map +0 -1
- package/dist/errors.cjs.map +0 -1
- package/dist/errors.mjs.map +0 -1
- package/dist/firestore-compat.cjs.map +0 -1
- package/dist/firestore-compat.mjs.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/mobile.cjs.map +0 -1
- package/dist/mobile.mjs.map +0 -1
- package/dist/mocks.cjs.map +0 -1
- package/dist/mocks.mjs.map +0 -1
- package/dist/notifications.cjs.map +0 -1
- package/dist/notifications.mjs.map +0 -1
- package/dist/react.cjs.map +0 -1
- package/dist/react.mjs.map +0 -1
- package/dist/support.cjs.map +0 -1
- package/dist/support.mjs.map +0 -1
- package/dist/telemetry.cjs.map +0 -1
- package/dist/telemetry.mjs.map +0 -1
- package/dist/usage.cjs.map +0 -1
- package/dist/usage.mjs.map +0 -1
- package/dist/webhooks.cjs.map +0 -1
- package/dist/webhooks.mjs.map +0 -1
package/dist/mobile.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/errors.ts
|
|
2
|
-
var
|
|
2
|
+
var n = class t extends Error {
|
|
3
3
|
code;
|
|
4
4
|
status;
|
|
5
5
|
requestId;
|
|
@@ -12,502 +12,617 @@ var NeetruError = class _NeetruError extends Error {
|
|
|
12
12
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
13
13
|
*/
|
|
14
14
|
reason;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
15
|
+
/**
|
|
16
|
+
* 3.8.0: `x-correlation-id` da requisição que falhou. O SDK gera um UUID por
|
|
17
|
+
* chamada (ou usa o que o chamador passou) e manda no header; o Core grava o
|
|
18
|
+
* mesmo id nos logs/auditoria — cite-o ao abrir um ticket de suporte.
|
|
19
|
+
* `undefined` em erros que acontecem antes de qualquer requisição.
|
|
20
|
+
*/
|
|
21
|
+
correlationId;
|
|
22
|
+
constructor(e, o, s, i, p, m) {
|
|
23
|
+
super(o), this.name = "NeetruError", this.code = e, this.status = s, this.requestId = i, this.reason = p, this.correlationId = m, Object.setPrototypeOf(this, t.prototype);
|
|
24
|
+
}
|
|
25
|
+
}, v = class t extends n {
|
|
26
|
+
constructor(e, o, s, i, p) {
|
|
27
|
+
super("auth_required", e, s, i, o, p), this.name = "NeetruAuthRequiredError", Object.setPrototypeOf(this, t.prototype);
|
|
23
28
|
}
|
|
24
29
|
};
|
|
25
30
|
|
|
26
31
|
// src/types.ts
|
|
27
|
-
var
|
|
32
|
+
var H = "https://api.neetru.com";
|
|
28
33
|
|
|
29
34
|
// src/base-url-allowlist.ts
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
var SAFE_DEV_HOST_RE = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
34
|
-
function isSafeDevHost(hostname) {
|
|
35
|
-
return SAFE_DEV_HOST_RE.test(hostname);
|
|
35
|
+
var B = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), le = ".neetru.com", ue = "neetru.com", de = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
36
|
+
function j(t) {
|
|
37
|
+
return de.test(t);
|
|
36
38
|
}
|
|
37
|
-
function
|
|
38
|
-
return
|
|
39
|
+
function pe(t) {
|
|
40
|
+
return t.toLowerCase().replace(/\.+$/, "");
|
|
39
41
|
}
|
|
40
|
-
function
|
|
41
|
-
if (typeof
|
|
42
|
-
throw new
|
|
43
|
-
|
|
44
|
-
let parsed;
|
|
42
|
+
function F(t, e = []) {
|
|
43
|
+
if (typeof t != "string" || t.length === 0)
|
|
44
|
+
throw new n("invalid_config", "baseUrl is required");
|
|
45
|
+
let o;
|
|
45
46
|
try {
|
|
46
|
-
|
|
47
|
+
o = new URL(t);
|
|
47
48
|
} catch {
|
|
48
|
-
throw new
|
|
49
|
-
}
|
|
50
|
-
const protocol = parsed.protocol;
|
|
51
|
-
const hostname = normalizeHostname(parsed.hostname);
|
|
52
|
-
if (isInsecureBypassActive()) {
|
|
53
|
-
if (typeof console !== "undefined") {
|
|
54
|
-
console.warn(
|
|
55
|
-
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${baseUrl} aceito sem valida\xE7\xE3o.`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
return baseUrl.replace(/\/+$/, "");
|
|
49
|
+
throw new n("invalid_config", `baseUrl is not a valid URL: ${t}`);
|
|
59
50
|
}
|
|
60
|
-
|
|
61
|
-
|
|
51
|
+
let s = o.protocol, i = pe(o.hostname);
|
|
52
|
+
if (fe())
|
|
53
|
+
return typeof console < "u" && console.warn(
|
|
54
|
+
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${t} aceito sem valida\xE7\xE3o.`
|
|
55
|
+
), t.replace(/\/+$/, "");
|
|
56
|
+
if (s !== "https:" && s !== "http:")
|
|
57
|
+
throw new n(
|
|
62
58
|
"invalid_config",
|
|
63
|
-
`baseUrl protocol must be http: or https:, got ${
|
|
59
|
+
`baseUrl protocol must be http: or https:, got ${s}`
|
|
64
60
|
);
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!isLoopback) {
|
|
69
|
-
throw new NeetruError(
|
|
61
|
+
if (s === "http:") {
|
|
62
|
+
if (!(B.has(i) || j(i)))
|
|
63
|
+
throw new n(
|
|
70
64
|
"invalid_config",
|
|
71
|
-
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${
|
|
65
|
+
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${i}. Use https: or set NEETRU_ALLOW_INSECURE_BASEURL=1 to bypass.`
|
|
72
66
|
);
|
|
73
|
-
|
|
74
|
-
return baseUrl.replace(/\/+$/, "");
|
|
67
|
+
return t.replace(/\/+$/, "");
|
|
75
68
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (!isNeetruDomain && !isAllowedHost && !isLoopbackHttps) {
|
|
83
|
-
throw new NeetruError(
|
|
69
|
+
let p = i === ue || i.endsWith(le), m = e.some((h) => {
|
|
70
|
+
let w = h.toLowerCase();
|
|
71
|
+
return i === w || i.endsWith(`.${w}`);
|
|
72
|
+
}), I = B.has(i) || j(i);
|
|
73
|
+
if (!p && !m && !I)
|
|
74
|
+
throw new n(
|
|
84
75
|
"invalid_config",
|
|
85
|
-
`baseUrl ${
|
|
76
|
+
`baseUrl ${i} is not in the allowlist. Allowed: *.neetru.com, localhost, or configure { allowedHosts: ['...'] }. Bypass: NEETRU_ALLOW_INSECURE_BASEURL=1.`
|
|
86
77
|
);
|
|
87
|
-
|
|
88
|
-
return baseUrl.replace(/\/+$/, "");
|
|
78
|
+
return t.replace(/\/+$/, "");
|
|
89
79
|
}
|
|
90
|
-
function
|
|
80
|
+
function fe() {
|
|
91
81
|
try {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (envVal === "1" || envVal === "true") return true;
|
|
82
|
+
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
83
|
+
if (e === "1" || e === "true") return !0;
|
|
95
84
|
} catch {
|
|
96
85
|
}
|
|
97
86
|
try {
|
|
98
|
-
|
|
99
|
-
if (
|
|
87
|
+
let t = globalThis.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
88
|
+
if (t === "1" || t === "true" || t === !0) return !0;
|
|
100
89
|
} catch {
|
|
101
90
|
}
|
|
102
91
|
return false;
|
|
103
92
|
}
|
|
104
93
|
|
|
105
94
|
// src/api-key.ts
|
|
106
|
-
var
|
|
107
|
-
function
|
|
108
|
-
if (typeof
|
|
109
|
-
throw new
|
|
95
|
+
var ge = /^nrt_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/;
|
|
96
|
+
function z(t) {
|
|
97
|
+
if (typeof t != "string" || t.length === 0)
|
|
98
|
+
throw new n(
|
|
110
99
|
"invalid_config",
|
|
111
100
|
"apiKey is required (formato nrt_<keyId>_<secret>)"
|
|
112
101
|
);
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
throw new NeetruError(
|
|
102
|
+
let e = ge.exec(t);
|
|
103
|
+
if (!e)
|
|
104
|
+
throw new n(
|
|
117
105
|
"invalid_config",
|
|
118
106
|
"apiKey malformado - esperado formato nrt_<keyId>_<secret>"
|
|
119
107
|
);
|
|
120
|
-
}
|
|
121
|
-
return { keyId: match[1], secret: match[2] };
|
|
108
|
+
return { keyId: e[1], secret: e[2] };
|
|
122
109
|
}
|
|
123
110
|
|
|
124
111
|
// src/auth-pkce-shared.ts
|
|
125
|
-
function
|
|
126
|
-
|
|
127
|
-
if (!
|
|
128
|
-
throw new
|
|
112
|
+
function V() {
|
|
113
|
+
let t = globalThis.crypto;
|
|
114
|
+
if (!t || typeof t.getRandomValues != "function" || !t.subtle)
|
|
115
|
+
throw new n(
|
|
129
116
|
"runtime_unsupported",
|
|
130
117
|
"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."
|
|
131
118
|
);
|
|
132
|
-
|
|
133
|
-
return c;
|
|
119
|
+
return t;
|
|
134
120
|
}
|
|
135
|
-
function
|
|
136
|
-
let
|
|
137
|
-
for (
|
|
138
|
-
|
|
139
|
-
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
121
|
+
function W(t) {
|
|
122
|
+
let e = "";
|
|
123
|
+
for (let s of t) e += String.fromCharCode(s);
|
|
124
|
+
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
140
125
|
}
|
|
141
|
-
function
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return base64UrlFromBytes(bytes);
|
|
126
|
+
function x(t, e) {
|
|
127
|
+
let o = new Uint8Array(e);
|
|
128
|
+
return t.getRandomValues(o), W(o);
|
|
145
129
|
}
|
|
146
|
-
async function
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return base64UrlFromBytes(new Uint8Array(digest));
|
|
130
|
+
async function J(t, e) {
|
|
131
|
+
let o = new TextEncoder().encode(e), s = await t.subtle.digest("SHA-256", o);
|
|
132
|
+
return W(new Uint8Array(s));
|
|
150
133
|
}
|
|
151
|
-
function
|
|
152
|
-
|
|
153
|
-
return overrideAuthUrl ?? baseUrl.replace(/^https?:\/\/api\./, "https://auth.");
|
|
134
|
+
function S(t) {
|
|
135
|
+
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? t.replace(/^https?:\/\/api\./, "https://auth.");
|
|
154
136
|
}
|
|
155
|
-
function
|
|
156
|
-
let
|
|
157
|
-
if (!
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
if (!clientId) {
|
|
161
|
-
throw new NeetruError(
|
|
137
|
+
function L(t) {
|
|
138
|
+
let e = t.oidcClientId;
|
|
139
|
+
if (!e && t.apiKey && (e = z(t.apiKey).keyId), !e)
|
|
140
|
+
throw new n(
|
|
162
141
|
"invalid_config",
|
|
163
142
|
"auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
|
|
164
143
|
);
|
|
165
|
-
|
|
166
|
-
return clientId;
|
|
144
|
+
return e;
|
|
167
145
|
}
|
|
168
|
-
function
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
url.searchParams.set("client_id", params.clientId);
|
|
172
|
-
url.searchParams.set("redirect_uri", params.redirectUri);
|
|
173
|
-
url.searchParams.set("scope", params.scope);
|
|
174
|
-
url.searchParams.set("state", params.state);
|
|
175
|
-
url.searchParams.set("nonce", params.nonce);
|
|
176
|
-
url.searchParams.set("code_challenge", params.codeChallenge);
|
|
177
|
-
url.searchParams.set("code_challenge_method", "S256");
|
|
178
|
-
return url;
|
|
146
|
+
function G(t) {
|
|
147
|
+
let e = new URL("/api/v1/oauth/authorize", t.idpOrigin);
|
|
148
|
+
return e.searchParams.set("response_type", "code"), e.searchParams.set("client_id", t.clientId), e.searchParams.set("redirect_uri", t.redirectUri), e.searchParams.set("scope", t.scope), e.searchParams.set("state", t.state), e.searchParams.set("nonce", t.nonce), e.searchParams.set("code_challenge", t.codeChallenge), e.searchParams.set("code_challenge_method", "S256"), e;
|
|
179
149
|
}
|
|
180
|
-
var
|
|
181
|
-
function
|
|
182
|
-
|
|
183
|
-
if (
|
|
150
|
+
var X = "/api/v1/oauth/token";
|
|
151
|
+
function O(t) {
|
|
152
|
+
let e = t.split(".");
|
|
153
|
+
if (e.length !== 3) return null;
|
|
184
154
|
try {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
const decoded = typeof atob === "function" ? atob(b64 + pad) : Buffer.from(b64 + pad, "base64").toString("utf-8");
|
|
188
|
-
return JSON.parse(decoded);
|
|
155
|
+
let o = e[1].replace(/-/g, "+").replace(/_/g, "/"), s = "=".repeat((4 - o.length % 4) % 4), i = typeof atob == "function" ? atob(o + s) : Buffer.from(o + s, "base64").toString("utf-8");
|
|
156
|
+
return JSON.parse(i);
|
|
189
157
|
} catch {
|
|
190
158
|
return null;
|
|
191
159
|
}
|
|
192
160
|
}
|
|
193
|
-
function
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
isStaff: payload.is_staff === true,
|
|
206
|
-
isCustomer: payload.is_customer === true,
|
|
207
|
-
tenantId: typeof payload.tenant_id === "string" ? payload.tenant_id : void 0,
|
|
208
|
-
...payload
|
|
161
|
+
function T(t) {
|
|
162
|
+
let e = O(t);
|
|
163
|
+
return !e || typeof e.sub != "string" || typeof e.exp == "number" && e.exp * 1e3 < Date.now() ? null : {
|
|
164
|
+
uid: e.sub,
|
|
165
|
+
email: typeof e.email == "string" ? e.email : "",
|
|
166
|
+
emailVerified: typeof e.email_verified == "boolean" ? e.email_verified : void 0,
|
|
167
|
+
displayName: typeof e.name == "string" ? e.name : void 0,
|
|
168
|
+
photoURL: typeof e.picture == "string" ? e.picture : void 0,
|
|
169
|
+
isStaff: e.is_staff === true,
|
|
170
|
+
isCustomer: e.is_customer === true,
|
|
171
|
+
tenantId: typeof e.tenant_id == "string" ? e.tenant_id : void 0,
|
|
172
|
+
...e
|
|
209
173
|
};
|
|
210
174
|
}
|
|
175
|
+
var me = "/api/v1/oauth/revoke";
|
|
176
|
+
async function Y(t, e, o, s) {
|
|
177
|
+
let i = `${e}${me}`, p = true;
|
|
178
|
+
for (let m of s) {
|
|
179
|
+
if (!m.token) continue;
|
|
180
|
+
let I = new URLSearchParams({
|
|
181
|
+
token: m.token,
|
|
182
|
+
token_type_hint: m.hint,
|
|
183
|
+
client_id: o
|
|
184
|
+
}).toString(), h = false;
|
|
185
|
+
for (let w = 0; w < 2 && !h; w++)
|
|
186
|
+
try {
|
|
187
|
+
if (h = (await t(i, {
|
|
188
|
+
method: "POST",
|
|
189
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
190
|
+
body: I
|
|
191
|
+
})).ok, !h) break;
|
|
192
|
+
} catch {
|
|
193
|
+
w === 0 && await new Promise((_) => setTimeout(_, 500));
|
|
194
|
+
}
|
|
195
|
+
h || (p = false);
|
|
196
|
+
}
|
|
197
|
+
return p;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/server-only-key.ts
|
|
201
|
+
function Z(t) {
|
|
202
|
+
if (!(t == null || t === ""))
|
|
203
|
+
throw new n(
|
|
204
|
+
"server_only_key",
|
|
205
|
+
"[Neetru SDK] Recusado: @neetru/sdk/mobile n\xE3o aceita apiKey. A credencial nrt_ do produto \xE9 S\xD3 DE SERVIDOR \u2014 dentro do app ela \xE9 extra\xEDvel do pacote (.apk/.ipa). Remova a apiKey (e qualquer EXPO_PUBLIC_*_KEY com ela) do app: fa\xE7a login com oidcClientId (client OIDC p\xFAblico do app) e chame a API do seu produto com o access_token do usu\xE1rio; o backend do produto usa o SDK com a chave.",
|
|
206
|
+
void 0,
|
|
207
|
+
void 0,
|
|
208
|
+
"react-native"
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// src/correlation.ts
|
|
213
|
+
var Q = "x-correlation-id";
|
|
214
|
+
var he = /^[A-Za-z0-9._:\-]+$/;
|
|
215
|
+
function ee(t) {
|
|
216
|
+
return typeof t == "string" && t.length > 0 && t.length <= 128 && he.test(t);
|
|
217
|
+
}
|
|
218
|
+
function ye() {
|
|
219
|
+
let t = globalThis.crypto;
|
|
220
|
+
try {
|
|
221
|
+
if (t && typeof t.randomUUID == "function") return t.randomUUID();
|
|
222
|
+
} catch {
|
|
223
|
+
}
|
|
224
|
+
let e = new Uint8Array(16), o = false;
|
|
225
|
+
try {
|
|
226
|
+
t && typeof t.getRandomValues == "function" && (t.getRandomValues(e), o = !0);
|
|
227
|
+
} catch {
|
|
228
|
+
o = false;
|
|
229
|
+
}
|
|
230
|
+
if (!o)
|
|
231
|
+
for (let i = 0; i < e.length; i++) e[i] = Math.floor(Math.random() * 256);
|
|
232
|
+
e[6] = e[6] & 15 | 64, e[8] = e[8] & 63 | 128;
|
|
233
|
+
let s = Array.from(e, (i) => i.toString(16).padStart(2, "0")).join("");
|
|
234
|
+
return `${s.slice(0, 8)}-${s.slice(8, 12)}-${s.slice(12, 16)}-${s.slice(16, 20)}-${s.slice(20)}`;
|
|
235
|
+
}
|
|
236
|
+
function K(t, e) {
|
|
237
|
+
let o, s = {};
|
|
238
|
+
for (let [p, m] of Object.entries(t)) {
|
|
239
|
+
if (p.toLowerCase() === Q) {
|
|
240
|
+
ee(m) && (o = m);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
s[p] = m;
|
|
244
|
+
}
|
|
245
|
+
let i = o ?? (ee(e) ? e : void 0) ?? ye();
|
|
246
|
+
return s[Q] = i, { headers: s, correlationId: i };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/http.ts
|
|
250
|
+
function te(t) {
|
|
251
|
+
return t === 401 ? "unauthorized" : t === 403 ? "forbidden" : t === 404 ? "not_found" : t === 409 ? "conflict" : t === 410 ? "gone" : t === 422 || t === 400 ? "validation_failed" : t === 429 ? "rate_limited" : t >= 500 ? "server_error" : "unknown";
|
|
252
|
+
}
|
|
211
253
|
|
|
212
254
|
// src/mobile.ts
|
|
213
|
-
var
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
255
|
+
var k = "neetru_oauth_tx", U = "neetru_id_token", E = "neetru_refresh_token", C = "neetru_access_token", be = 3e4, we = "/api/v1/oauth/userinfo";
|
|
256
|
+
function _e(t) {
|
|
257
|
+
if (t === void 0 || t === "") return null;
|
|
258
|
+
let e;
|
|
259
|
+
try {
|
|
260
|
+
e = new URL(t);
|
|
261
|
+
} catch {
|
|
262
|
+
throw new n("invalid_config", `productApiUrl inv\xE1lida: ${t}`);
|
|
263
|
+
}
|
|
264
|
+
let o = e.hostname === "localhost" || e.hostname === "127.0.0.1";
|
|
265
|
+
if (e.protocol !== "https:" && !(e.protocol === "http:" && o))
|
|
266
|
+
throw new n(
|
|
267
|
+
"invalid_config",
|
|
268
|
+
"productApiUrl precisa ser https:// (http s\xF3 para localhost) \u2014 o access_token n\xE3o pode trafegar sem TLS."
|
|
269
|
+
);
|
|
270
|
+
return e;
|
|
271
|
+
}
|
|
272
|
+
function ke(t) {
|
|
273
|
+
if (Z(t.apiKey), !t.storage)
|
|
274
|
+
throw new n(
|
|
219
275
|
"invalid_config",
|
|
220
276
|
"createMobileAuthNamespace requer `storage` (MobileAuthStorage) \u2014 ver JSDoc de src/mobile.ts requisito 4."
|
|
221
277
|
);
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
throw new NeetruError(
|
|
278
|
+
if (typeof t.openAuthUrl != "function")
|
|
279
|
+
throw new n(
|
|
225
280
|
"invalid_config",
|
|
226
281
|
"createMobileAuthNamespace requer `openAuthUrl` (browser externo, NUNCA WebView embutida) \u2014 ver JSDoc de src/mobile.ts requisito 3."
|
|
227
282
|
);
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
throw new NeetruError(
|
|
283
|
+
let e = t.fetch ?? globalThis.fetch;
|
|
284
|
+
if (typeof e != "function")
|
|
285
|
+
throw new n(
|
|
232
286
|
"invalid_config",
|
|
233
287
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createMobileAuthNamespace."
|
|
234
288
|
);
|
|
235
|
-
|
|
236
|
-
|
|
289
|
+
if (typeof t.oidcClientId != "string" || t.oidcClientId.length === 0)
|
|
290
|
+
throw new n(
|
|
291
|
+
"invalid_config",
|
|
292
|
+
"createMobileAuthNamespace requer `oidcClientId` \u2014 o client OIDC P\xDABLICO do app registrado no Core (a apiKey n\xE3o \xE9 aceita no app; ver requisito 6 no JSDoc de src/mobile.ts)."
|
|
293
|
+
);
|
|
294
|
+
let o = F(t.baseUrl ?? H, t.allowedHosts ?? []);
|
|
237
295
|
return {
|
|
238
|
-
fetchImpl:
|
|
239
|
-
baseUrl,
|
|
240
|
-
oidcClientId:
|
|
241
|
-
|
|
242
|
-
storage:
|
|
243
|
-
openAuthUrl:
|
|
296
|
+
fetchImpl: e.bind(globalThis),
|
|
297
|
+
baseUrl: o,
|
|
298
|
+
oidcClientId: t.oidcClientId,
|
|
299
|
+
productApiUrl: _e(t.productApiUrl),
|
|
300
|
+
storage: t.storage,
|
|
301
|
+
openAuthUrl: t.openAuthUrl
|
|
244
302
|
};
|
|
245
303
|
}
|
|
246
|
-
|
|
247
|
-
let
|
|
304
|
+
function ve(t) {
|
|
305
|
+
let o = O(t)?.exp;
|
|
306
|
+
return typeof o == "number" ? o * 1e3 : null;
|
|
307
|
+
}
|
|
308
|
+
async function re(t, e, o) {
|
|
309
|
+
let s;
|
|
248
310
|
try {
|
|
249
|
-
|
|
311
|
+
s = await t(e, {
|
|
250
312
|
method: "POST",
|
|
251
313
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
252
|
-
body:
|
|
314
|
+
body: o.toString()
|
|
253
315
|
});
|
|
254
|
-
} catch (
|
|
255
|
-
throw new
|
|
316
|
+
} catch (p) {
|
|
317
|
+
throw new n(
|
|
256
318
|
"network_error",
|
|
257
|
-
`POST ${
|
|
319
|
+
`POST ${e} falhou (network): ${p?.message ?? String(p)}`
|
|
258
320
|
);
|
|
259
321
|
}
|
|
260
|
-
let
|
|
322
|
+
let i = null;
|
|
261
323
|
try {
|
|
262
|
-
|
|
324
|
+
i = await s.json();
|
|
263
325
|
} catch {
|
|
264
|
-
|
|
326
|
+
i = null;
|
|
265
327
|
}
|
|
266
|
-
if (!
|
|
267
|
-
|
|
268
|
-
throw new
|
|
328
|
+
if (!s.ok || typeof i?.error == "string") {
|
|
329
|
+
let p = typeof i?.error_description == "string" && i.error_description || typeof i?.error == "string" && i.error || `HTTP ${s.status}`;
|
|
330
|
+
throw new n(
|
|
269
331
|
"token_exchange_failed",
|
|
270
|
-
`Troca de tokens falhou no /token: ${
|
|
271
|
-
|
|
332
|
+
`Troca de tokens falhou no /token: ${p}`,
|
|
333
|
+
s.status
|
|
272
334
|
);
|
|
273
335
|
}
|
|
274
|
-
return
|
|
336
|
+
return i ?? {};
|
|
275
337
|
}
|
|
276
|
-
function
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return `${resolveIdpOrigin(resolved.baseUrl)}${OAUTH_TOKEN_PATH}`;
|
|
338
|
+
function je(t) {
|
|
339
|
+
let e = ke(t), o = /* @__PURE__ */ new Set();
|
|
340
|
+
function s() {
|
|
341
|
+
return `${S(e.baseUrl)}${X}`;
|
|
281
342
|
}
|
|
282
|
-
async function
|
|
283
|
-
|
|
284
|
-
for (
|
|
343
|
+
async function i() {
|
|
344
|
+
let r = await h();
|
|
345
|
+
for (let a of o)
|
|
285
346
|
try {
|
|
286
|
-
|
|
347
|
+
a(r);
|
|
287
348
|
} catch {
|
|
288
349
|
}
|
|
289
|
-
}
|
|
290
350
|
}
|
|
291
|
-
async function
|
|
292
|
-
if (!
|
|
293
|
-
throw new
|
|
351
|
+
async function p(r) {
|
|
352
|
+
if (!r?.redirectUri)
|
|
353
|
+
throw new n(
|
|
294
354
|
"invalid_config",
|
|
295
355
|
"mobile auth.signIn requer `redirectUri` (Universal Link/App Link https://... registrado no Core) \u2014 n\xE3o h\xE1 location.origin impl\xEDcito em React Native."
|
|
296
356
|
);
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
const codeVerifier = randomUrlSafe(crypto, 32);
|
|
304
|
-
const codeChallenge = await pkceChallengeS256(crypto, codeVerifier);
|
|
305
|
-
const tx = {
|
|
306
|
-
state,
|
|
307
|
-
nonce,
|
|
308
|
-
codeVerifier,
|
|
309
|
-
redirectUri: options.redirectUri,
|
|
310
|
-
postLoginRedirect: options.postLoginRedirect
|
|
357
|
+
let a = V(), c = r.scope ?? "openid profile email", l = L(e), d = x(a, 16), u = x(a, 16), f = x(a, 32), g = await J(a, f), y = {
|
|
358
|
+
state: d,
|
|
359
|
+
nonce: u,
|
|
360
|
+
codeVerifier: f,
|
|
361
|
+
redirectUri: r.redirectUri,
|
|
362
|
+
postLoginRedirect: r.postLoginRedirect
|
|
311
363
|
};
|
|
312
|
-
await
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
codeChallenge
|
|
364
|
+
await e.storage.setItem(k, JSON.stringify(y));
|
|
365
|
+
let b = S(e.baseUrl), R = G({
|
|
366
|
+
idpOrigin: b,
|
|
367
|
+
clientId: l,
|
|
368
|
+
redirectUri: r.redirectUri,
|
|
369
|
+
scope: c,
|
|
370
|
+
state: d,
|
|
371
|
+
nonce: u,
|
|
372
|
+
codeChallenge: g
|
|
322
373
|
});
|
|
323
|
-
await
|
|
374
|
+
await e.openAuthUrl(R.toString());
|
|
324
375
|
}
|
|
325
|
-
async function
|
|
326
|
-
if (!
|
|
327
|
-
throw new
|
|
376
|
+
async function m(r) {
|
|
377
|
+
if (!r || typeof r != "string")
|
|
378
|
+
throw new n(
|
|
328
379
|
"invalid_config",
|
|
329
380
|
"mobile auth.completeSignIn requer `callbackUrl` (a URL completa do deep link de retorno) \u2014 mobile n\xE3o tem location.href impl\xEDcito."
|
|
330
381
|
);
|
|
331
|
-
|
|
332
|
-
let parsed;
|
|
382
|
+
let a;
|
|
333
383
|
try {
|
|
334
|
-
|
|
384
|
+
a = new URL(r);
|
|
335
385
|
} catch {
|
|
336
|
-
throw new
|
|
386
|
+
throw new n("invalid_config", `callbackUrl n\xE3o \xE9 uma URL v\xE1lida: ${r}`);
|
|
337
387
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
await resolved.storage.removeItem(OIDC_TX_KEY);
|
|
343
|
-
throw new NeetruError(idpError, `OIDC authorize falhou: ${desc}`);
|
|
388
|
+
let c = a.searchParams, l = c.get("error");
|
|
389
|
+
if (l) {
|
|
390
|
+
let ce = c.get("error_description") ?? l;
|
|
391
|
+
throw await e.storage.removeItem(k), new n(l, `OIDC authorize falhou: ${ce}`);
|
|
344
392
|
}
|
|
345
|
-
|
|
346
|
-
if (!
|
|
347
|
-
await
|
|
348
|
-
throw new NeetruError(
|
|
393
|
+
let d = c.get("code");
|
|
394
|
+
if (!d)
|
|
395
|
+
throw await e.storage.removeItem(k), new n(
|
|
349
396
|
"invalid_config",
|
|
350
397
|
"callbackUrl n\xE3o cont\xE9m `?code=` nem `?error=` \u2014 n\xE3o \xE9 um callback OIDC v\xE1lido."
|
|
351
398
|
);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const txRaw = await resolved.storage.getItem(OIDC_TX_KEY);
|
|
355
|
-
let tx = null;
|
|
356
|
-
if (txRaw) {
|
|
399
|
+
let u = c.get("state"), f = await e.storage.getItem(k), g = null;
|
|
400
|
+
if (f)
|
|
357
401
|
try {
|
|
358
|
-
|
|
402
|
+
g = JSON.parse(f);
|
|
359
403
|
} catch {
|
|
360
|
-
|
|
404
|
+
g = null;
|
|
361
405
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
throw new NeetruError(
|
|
406
|
+
if (!g)
|
|
407
|
+
throw new n(
|
|
365
408
|
"oidc_state_mismatch",
|
|
366
409
|
"Callback OIDC sem transa\xE7\xE3o pendente \u2014 `signIn` n\xE3o foi iniciado (ou j\xE1 consumido)."
|
|
367
410
|
);
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
await resolved.storage.removeItem(OIDC_TX_KEY);
|
|
371
|
-
throw new NeetruError(
|
|
411
|
+
if (!u || u !== g.state)
|
|
412
|
+
throw await e.storage.removeItem(k), new n(
|
|
372
413
|
"oidc_state_mismatch",
|
|
373
414
|
"state do callback OIDC n\xE3o confere com o da transa\xE7\xE3o (poss\xEDvel CSRF)."
|
|
374
415
|
);
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
const clientId = resolveOidcClientId(resolved);
|
|
378
|
-
const form = new URLSearchParams({
|
|
416
|
+
await e.storage.removeItem(k);
|
|
417
|
+
let y = L(e), b = new URLSearchParams({
|
|
379
418
|
grant_type: "authorization_code",
|
|
380
|
-
code,
|
|
381
|
-
redirect_uri:
|
|
382
|
-
client_id:
|
|
383
|
-
code_verifier:
|
|
384
|
-
});
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
if (typeof idToken !== "string" || idToken.length === 0) {
|
|
388
|
-
throw new NeetruError(
|
|
419
|
+
code: d,
|
|
420
|
+
redirect_uri: g.redirectUri,
|
|
421
|
+
client_id: y,
|
|
422
|
+
code_verifier: g.codeVerifier
|
|
423
|
+
}), R = await re(e.fetchImpl, s(), b), A = R.id_token;
|
|
424
|
+
if (typeof A != "string" || A.length === 0)
|
|
425
|
+
throw new n(
|
|
389
426
|
"token_exchange_failed",
|
|
390
427
|
"Resposta do /token sem `id_token` \u2014 o login n\xE3o pode ser completado."
|
|
391
428
|
);
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
throw new NeetruError(
|
|
429
|
+
let $ = O(A);
|
|
430
|
+
if (!$)
|
|
431
|
+
throw new n(
|
|
396
432
|
"token_exchange_failed",
|
|
397
433
|
"id_token retornado pelo /token \xE9 malformado (payload indecodific\xE1vel)."
|
|
398
434
|
);
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
throw new NeetruError(
|
|
435
|
+
if ($.nonce !== g.nonce)
|
|
436
|
+
throw new n(
|
|
402
437
|
"oidc_nonce_mismatch",
|
|
403
438
|
"nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
|
|
404
439
|
);
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
throw new NeetruError(
|
|
440
|
+
let q = T(A);
|
|
441
|
+
if (!q)
|
|
442
|
+
throw new n(
|
|
409
443
|
"token_exchange_failed",
|
|
410
444
|
"id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
|
|
411
445
|
);
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
return { user, postLoginRedirect: tx.postLoginRedirect };
|
|
446
|
+
await e.storage.setItem(U, A);
|
|
447
|
+
let M = R.refresh_token;
|
|
448
|
+
return typeof M == "string" && M.length > 0 && await e.storage.setItem(E, M), await w(R.access_token), await i(), { user: q, postLoginRedirect: g.postLoginRedirect };
|
|
449
|
+
}
|
|
450
|
+
async function I() {
|
|
451
|
+
let r = await e.storage.getItem(U);
|
|
452
|
+
return r && T(r) ? r : null;
|
|
420
453
|
}
|
|
421
|
-
async function
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
return jwtToNeetruUser(token) ? token : null;
|
|
454
|
+
async function h() {
|
|
455
|
+
let r = await e.storage.getItem(U);
|
|
456
|
+
return r ? T(r) : null;
|
|
425
457
|
}
|
|
426
|
-
async function
|
|
427
|
-
|
|
428
|
-
if (!token) return null;
|
|
429
|
-
return jwtToNeetruUser(token);
|
|
458
|
+
async function w(r) {
|
|
459
|
+
typeof r == "string" && r.length > 0 ? await e.storage.setItem(C, r) : await e.storage.removeItem(C);
|
|
430
460
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
461
|
+
let _ = null;
|
|
462
|
+
function N() {
|
|
463
|
+
return _ || (_ = ne().finally(() => {
|
|
464
|
+
_ = null;
|
|
465
|
+
}), _);
|
|
466
|
+
}
|
|
467
|
+
async function ne() {
|
|
468
|
+
let r = await e.storage.getItem(E);
|
|
469
|
+
if (!r) return false;
|
|
470
|
+
let a;
|
|
435
471
|
try {
|
|
436
|
-
|
|
472
|
+
a = L(e);
|
|
437
473
|
} catch {
|
|
438
474
|
return false;
|
|
439
475
|
}
|
|
440
|
-
|
|
476
|
+
let c = new URLSearchParams({
|
|
441
477
|
grant_type: "refresh_token",
|
|
442
|
-
refresh_token:
|
|
443
|
-
client_id:
|
|
444
|
-
});
|
|
445
|
-
let payload;
|
|
478
|
+
refresh_token: r,
|
|
479
|
+
client_id: a
|
|
480
|
+
}), l;
|
|
446
481
|
try {
|
|
447
|
-
|
|
482
|
+
l = await re(e.fetchImpl, s(), c);
|
|
448
483
|
} catch {
|
|
449
484
|
return false;
|
|
450
485
|
}
|
|
451
|
-
|
|
452
|
-
if (typeof
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
await
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
486
|
+
let d = l.id_token;
|
|
487
|
+
if (typeof d != "string" || d.length === 0 || !T(d)) return false;
|
|
488
|
+
await e.storage.setItem(U, d);
|
|
489
|
+
let f = l.refresh_token;
|
|
490
|
+
return typeof f == "string" && f.length > 0 && await e.storage.setItem(E, f), await w(l.access_token), await i(), true;
|
|
491
|
+
}
|
|
492
|
+
async function P() {
|
|
493
|
+
let r = await e.storage.getItem(C);
|
|
494
|
+
if (!r) return null;
|
|
495
|
+
let a = ve(r);
|
|
496
|
+
return a === null || a - be <= Date.now() ? null : r;
|
|
497
|
+
}
|
|
498
|
+
async function D() {
|
|
499
|
+
let r = await P();
|
|
500
|
+
return r || (await N() ? P() : null);
|
|
501
|
+
}
|
|
502
|
+
async function oe() {
|
|
503
|
+
let r = `${S(e.baseUrl)}${we}`, a = async (f) => {
|
|
504
|
+
let { headers: g, correlationId: y } = K({
|
|
505
|
+
accept: "application/json",
|
|
506
|
+
authorization: `Bearer ${f}`
|
|
507
|
+
});
|
|
508
|
+
try {
|
|
509
|
+
return { res: await e.fetchImpl(r, { method: "GET", headers: g }), correlationId: y };
|
|
510
|
+
} catch (b) {
|
|
511
|
+
throw new n(
|
|
512
|
+
"network_error",
|
|
513
|
+
`GET ${r} falhou (network): ${b?.message ?? String(b)}`,
|
|
514
|
+
void 0,
|
|
515
|
+
void 0,
|
|
516
|
+
void 0,
|
|
517
|
+
y
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}, c = await D();
|
|
521
|
+
if (!c)
|
|
522
|
+
throw new v(
|
|
523
|
+
"Sem sess\xE3o: fa\xE7a login (auth.signIn) antes de consultar o userinfo.",
|
|
524
|
+
"no_signed_in_user"
|
|
525
|
+
);
|
|
526
|
+
let { res: l, correlationId: d } = await a(c);
|
|
527
|
+
if (l.status === 401 && await N() && (c = await P(), c && ({ res: l, correlationId: d } = await a(c))), l.status === 401)
|
|
528
|
+
throw new v(
|
|
529
|
+
"O Core recusou a sess\xE3o (token revogado/expirado ou usu\xE1rio inativo). Fa\xE7a login novamente.",
|
|
530
|
+
"invalid_access_token",
|
|
531
|
+
401,
|
|
532
|
+
void 0,
|
|
533
|
+
d
|
|
534
|
+
);
|
|
535
|
+
let u;
|
|
536
|
+
try {
|
|
537
|
+
u = await l.json();
|
|
538
|
+
} catch {
|
|
539
|
+
u = void 0;
|
|
540
|
+
}
|
|
541
|
+
if (!l.ok) {
|
|
542
|
+
let f = u && typeof u == "object" && typeof u.error_description == "string" ? u.error_description : `HTTP ${l.status}`;
|
|
543
|
+
throw new n(te(l.status), `userinfo falhou: ${f}`, l.status, void 0, void 0, d);
|
|
459
544
|
}
|
|
460
|
-
|
|
461
|
-
|
|
545
|
+
if (!u || typeof u != "object" || typeof u.sub != "string")
|
|
546
|
+
throw new n("invalid_response", "userinfo sem `sub`.", l.status, void 0, void 0, d);
|
|
547
|
+
return u;
|
|
462
548
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
549
|
+
function ie(r) {
|
|
550
|
+
let a = e.productApiUrl;
|
|
551
|
+
if (!a)
|
|
552
|
+
throw new n(
|
|
553
|
+
"invalid_config",
|
|
554
|
+
"auth.authFetch requer `productApiUrl` (URL da API do seu produto) no createMobileAuthNamespace."
|
|
555
|
+
);
|
|
556
|
+
let c;
|
|
557
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(r))
|
|
470
558
|
try {
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
body: JSON.stringify({ token: storedToken })
|
|
475
|
-
});
|
|
476
|
-
} catch (err) {
|
|
477
|
-
if (typeof console !== "undefined") {
|
|
478
|
-
console.warn(
|
|
479
|
-
"[neetru-sdk] mobile signOut: server-side revoke falhou. Refresh token pode estar stale.",
|
|
480
|
-
err
|
|
481
|
-
);
|
|
482
|
-
}
|
|
559
|
+
c = new URL(r);
|
|
560
|
+
} catch {
|
|
561
|
+
throw new n("invalid_config", `URL inv\xE1lida: ${r}`);
|
|
483
562
|
}
|
|
563
|
+
else {
|
|
564
|
+
let l = a.pathname.replace(/\/+$/, ""), d = r.startsWith("/") ? r : `/${r}`;
|
|
565
|
+
c = new URL(`${a.origin}${l}${d}`);
|
|
484
566
|
}
|
|
485
|
-
|
|
567
|
+
if (c.origin !== a.origin)
|
|
568
|
+
throw new n(
|
|
569
|
+
"invalid_config",
|
|
570
|
+
`auth.authFetch s\xF3 envia o access_token para a API do produto (${a.origin}); recusado: ${c.origin}.`
|
|
571
|
+
);
|
|
572
|
+
return c.toString();
|
|
573
|
+
}
|
|
574
|
+
async function se(r, a = {}) {
|
|
575
|
+
let c = ie(r), l = {};
|
|
576
|
+
new Headers(a.headers ?? {}).forEach((y, b) => {
|
|
577
|
+
l[b] = y;
|
|
578
|
+
});
|
|
579
|
+
let d = (y) => {
|
|
580
|
+
let { headers: b } = K({ ...l, authorization: `Bearer ${y}` });
|
|
581
|
+
return e.fetchImpl(c, { ...a, headers: b });
|
|
582
|
+
}, u = await D();
|
|
583
|
+
if (!u)
|
|
584
|
+
throw new v(
|
|
585
|
+
"Sem sess\xE3o: fa\xE7a login (auth.signIn) antes de chamar a API do produto.",
|
|
586
|
+
"no_signed_in_user"
|
|
587
|
+
);
|
|
588
|
+
let f = await d(u), g = a.body === void 0 || a.body === null || typeof a.body == "string";
|
|
589
|
+
return f.status === 401 && g && await N() && (u = await P(), u && (f = await d(u))), f;
|
|
590
|
+
}
|
|
591
|
+
async function ae() {
|
|
592
|
+
let r = await e.storage.getItem(E), a = await e.storage.getItem(C);
|
|
593
|
+
await e.storage.removeItem(U), await e.storage.removeItem(E), await e.storage.removeItem(C), await e.storage.removeItem(k);
|
|
594
|
+
let c = [];
|
|
595
|
+
r && c.push({ token: r, hint: "refresh_token" }), a && c.push({ token: a, hint: "access_token" }), c.length > 0 && !await Y(
|
|
596
|
+
e.fetchImpl,
|
|
597
|
+
S(e.baseUrl),
|
|
598
|
+
e.oidcClientId,
|
|
599
|
+
c
|
|
600
|
+
) && typeof console < "u" && console.warn(
|
|
601
|
+
"[neetru-sdk] mobile signOut: a revoga\xE7\xE3o no servidor n\xE3o foi confirmada. A sess\xE3o local foi encerrada, mas o refresh_token pode seguir v\xE1lido at\xE9 expirar."
|
|
602
|
+
), await i();
|
|
486
603
|
}
|
|
487
604
|
return {
|
|
488
|
-
signIn,
|
|
489
|
-
completeSignIn,
|
|
490
|
-
getIdToken,
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
605
|
+
signIn: p,
|
|
606
|
+
completeSignIn: m,
|
|
607
|
+
getIdToken: I,
|
|
608
|
+
getAccessToken: D,
|
|
609
|
+
getUserInfo: oe,
|
|
610
|
+
authFetch: se,
|
|
611
|
+
getUser: h,
|
|
612
|
+
onAuthStateChanged(r) {
|
|
613
|
+
return o.add(r), h().then((a) => {
|
|
614
|
+
if (o.has(r))
|
|
496
615
|
try {
|
|
497
|
-
|
|
616
|
+
r(a);
|
|
498
617
|
} catch {
|
|
499
618
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
return () => {
|
|
503
|
-
listeners.delete(listener);
|
|
619
|
+
}), () => {
|
|
620
|
+
o.delete(r);
|
|
504
621
|
};
|
|
505
622
|
},
|
|
506
|
-
refreshTokens,
|
|
507
|
-
signOut
|
|
623
|
+
refreshTokens: N,
|
|
624
|
+
signOut: ae
|
|
508
625
|
};
|
|
509
626
|
}
|
|
510
627
|
|
|
511
|
-
export { createMobileAuthNamespace };
|
|
512
|
-
//# sourceMappingURL=mobile.mjs.map
|
|
513
|
-
//# sourceMappingURL=mobile.mjs.map
|
|
628
|
+
export { je as createMobileAuthNamespace };
|