@neetru/sdk 3.6.0 → 3.7.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 +49 -0
- package/dist/ai.cjs +254 -391
- package/dist/ai.d.cts +1 -1
- package/dist/ai.d.ts +1 -1
- package/dist/ai.mjs +254 -391
- package/dist/auth.cjs +3547 -4998
- package/dist/auth.d.cts +1 -1
- package/dist/auth.d.ts +1 -1
- package/dist/auth.mjs +3544 -4995
- package/dist/build-guard.cjs +101 -119
- package/dist/build-guard.mjs +91 -109
- package/dist/catalog.cjs +152 -224
- package/dist/catalog.d.cts +1 -1
- package/dist/catalog.d.ts +1 -1
- package/dist/catalog.mjs +152 -224
- package/dist/checkout.cjs +237 -320
- package/dist/checkout.d.cts +1 -1
- package/dist/checkout.d.ts +1 -1
- package/dist/checkout.mjs +236 -319
- 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 +2055 -3013
- package/dist/db.d.cts +1 -1
- package/dist/db.d.ts +1 -1
- package/dist/db.mjs +2068 -3026
- package/dist/entitlements.cjs +12 -20
- package/dist/entitlements.d.cts +1 -1
- package/dist/entitlements.d.ts +1 -1
- package/dist/entitlements.mjs +12 -20
- package/dist/errors.cjs +9 -12
- package/dist/errors.d.cts +37 -2
- package/dist/errors.d.ts +37 -2
- package/dist/errors.mjs +8 -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 +3605 -5131
- package/dist/index.d.cts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.mjs +3710 -5236
- package/dist/mobile.cjs +238 -342
- package/dist/mobile.d.cts +1 -1
- package/dist/mobile.d.ts +1 -1
- package/dist/mobile.mjs +238 -342
- package/dist/mocks.cjs +312 -447
- package/dist/mocks.d.cts +1 -1
- package/dist/mocks.d.ts +1 -1
- package/dist/mocks.mjs +306 -441
- package/dist/notifications.cjs +226 -314
- package/dist/notifications.d.cts +1 -1
- package/dist/notifications.d.ts +1 -1
- package/dist/notifications.mjs +225 -313
- package/dist/react.cjs +522 -693
- package/dist/react.d.cts +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.mjs +512 -683
- package/dist/support.cjs +329 -325
- package/dist/support.d.cts +25 -3
- package/dist/support.d.ts +25 -3
- package/dist/support.mjs +328 -325
- package/dist/telemetry.cjs +197 -301
- package/dist/telemetry.d.cts +1 -1
- package/dist/telemetry.d.ts +1 -1
- package/dist/telemetry.mjs +197 -301
- package/dist/{types-Du_iioJ7.d.cts → types-Bx4kTLmk.d.cts} +26 -0
- package/dist/{types-BPATkXYu.d.ts → types-Dx0IrOSF.d.ts} +26 -0
- package/dist/usage.cjs +163 -227
- package/dist/usage.d.cts +1 -1
- package/dist/usage.d.ts +1 -1
- package/dist/usage.mjs +163 -227
- package/dist/webhooks.cjs +258 -359
- package/dist/webhooks.d.cts +1 -1
- package/dist/webhooks.d.ts +1 -1
- package/dist/webhooks.mjs +255 -356
- package/package.json +4 -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.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// src/errors.ts
|
|
4
|
-
var
|
|
4
|
+
var r = class t extends Error {
|
|
5
5
|
code;
|
|
6
6
|
status;
|
|
7
7
|
requestId;
|
|
@@ -14,502 +14,398 @@ var NeetruError = class _NeetruError extends Error {
|
|
|
14
14
|
* para um `NeetruDbErrorCode` mais específico (recuperável vs não).
|
|
15
15
|
*/
|
|
16
16
|
reason;
|
|
17
|
-
constructor(
|
|
18
|
-
super(
|
|
19
|
-
this.name = "NeetruError";
|
|
20
|
-
this.code = code;
|
|
21
|
-
this.status = status;
|
|
22
|
-
this.requestId = requestId;
|
|
23
|
-
this.reason = reason;
|
|
24
|
-
Object.setPrototypeOf(this, _NeetruError.prototype);
|
|
17
|
+
constructor(e, i, s, o, l) {
|
|
18
|
+
super(i), this.name = "NeetruError", this.code = e, this.status = s, this.requestId = o, this.reason = l, Object.setPrototypeOf(this, t.prototype);
|
|
25
19
|
}
|
|
26
20
|
};
|
|
27
21
|
|
|
28
22
|
// src/types.ts
|
|
29
|
-
var
|
|
23
|
+
var L = "https://api.neetru.com";
|
|
30
24
|
|
|
31
25
|
// src/base-url-allowlist.ts
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
var SAFE_DEV_HOST_RE = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
36
|
-
function isSafeDevHost(hostname) {
|
|
37
|
-
return SAFE_DEV_HOST_RE.test(hostname);
|
|
26
|
+
var O = /* @__PURE__ */ new Set(["localhost", "127.0.0.1", "[::1]", "::1"]), F = ".neetru.com", J = "neetru.com", Y = /^[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?\.(test|localhost)$/;
|
|
27
|
+
function D(t) {
|
|
28
|
+
return Y.test(t);
|
|
38
29
|
}
|
|
39
|
-
function
|
|
40
|
-
return
|
|
30
|
+
function X(t) {
|
|
31
|
+
return t.toLowerCase().replace(/\.+$/, "");
|
|
41
32
|
}
|
|
42
|
-
function
|
|
43
|
-
if (typeof
|
|
44
|
-
throw new
|
|
45
|
-
|
|
46
|
-
let parsed;
|
|
33
|
+
function M(t, e = []) {
|
|
34
|
+
if (typeof t != "string" || t.length === 0)
|
|
35
|
+
throw new r("invalid_config", "baseUrl is required");
|
|
36
|
+
let i;
|
|
47
37
|
try {
|
|
48
|
-
|
|
38
|
+
i = new URL(t);
|
|
49
39
|
} catch {
|
|
50
|
-
throw new
|
|
40
|
+
throw new r("invalid_config", `baseUrl is not a valid URL: ${t}`);
|
|
51
41
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
return baseUrl.replace(/\/+$/, "");
|
|
61
|
-
}
|
|
62
|
-
if (protocol !== "https:" && protocol !== "http:") {
|
|
63
|
-
throw new NeetruError(
|
|
42
|
+
let s = i.protocol, o = X(i.hostname);
|
|
43
|
+
if (Z())
|
|
44
|
+
return typeof console < "u" && console.warn(
|
|
45
|
+
`[neetru-sdk] NEETRU_ALLOW_INSECURE_BASEURL=1 ativo. baseUrl=${t} aceito sem valida\xE7\xE3o.`
|
|
46
|
+
), t.replace(/\/+$/, "");
|
|
47
|
+
if (s !== "https:" && s !== "http:")
|
|
48
|
+
throw new r(
|
|
64
49
|
"invalid_config",
|
|
65
|
-
`baseUrl protocol must be http: or https:, got ${
|
|
50
|
+
`baseUrl protocol must be http: or https:, got ${s}`
|
|
66
51
|
);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
if (!isLoopback) {
|
|
71
|
-
throw new NeetruError(
|
|
52
|
+
if (s === "http:") {
|
|
53
|
+
if (!(O.has(o) || D(o)))
|
|
54
|
+
throw new r(
|
|
72
55
|
"invalid_config",
|
|
73
|
-
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${
|
|
56
|
+
`baseUrl with http: only allowed for localhost/127.0.0.1/*.test/*.localhost. Got: ${o}. Use https: or set NEETRU_ALLOW_INSECURE_BASEURL=1 to bypass.`
|
|
74
57
|
);
|
|
75
|
-
|
|
76
|
-
return baseUrl.replace(/\/+$/, "");
|
|
58
|
+
return t.replace(/\/+$/, "");
|
|
77
59
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (!isNeetruDomain && !isAllowedHost && !isLoopbackHttps) {
|
|
85
|
-
throw new NeetruError(
|
|
60
|
+
let l = o === J || o.endsWith(F), A = e.some((m) => {
|
|
61
|
+
let k = m.toLowerCase();
|
|
62
|
+
return o === k || o.endsWith(`.${k}`);
|
|
63
|
+
}), R = O.has(o) || D(o);
|
|
64
|
+
if (!l && !A && !R)
|
|
65
|
+
throw new r(
|
|
86
66
|
"invalid_config",
|
|
87
|
-
`baseUrl ${
|
|
67
|
+
`baseUrl ${o} is not in the allowlist. Allowed: *.neetru.com, localhost, or configure { allowedHosts: ['...'] }. Bypass: NEETRU_ALLOW_INSECURE_BASEURL=1.`
|
|
88
68
|
);
|
|
89
|
-
|
|
90
|
-
return baseUrl.replace(/\/+$/, "");
|
|
69
|
+
return t.replace(/\/+$/, "");
|
|
91
70
|
}
|
|
92
|
-
function
|
|
71
|
+
function Z() {
|
|
93
72
|
try {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (envVal === "1" || envVal === "true") return true;
|
|
73
|
+
let e = globalThis.process?.env?.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
74
|
+
if (e === "1" || e === "true") return !0;
|
|
97
75
|
} catch {
|
|
98
76
|
}
|
|
99
77
|
try {
|
|
100
|
-
|
|
101
|
-
if (
|
|
78
|
+
let t = globalThis.NEETRU_ALLOW_INSECURE_BASEURL;
|
|
79
|
+
if (t === "1" || t === "true" || t === !0) return !0;
|
|
102
80
|
} catch {
|
|
103
81
|
}
|
|
104
82
|
return false;
|
|
105
83
|
}
|
|
106
84
|
|
|
107
85
|
// src/api-key.ts
|
|
108
|
-
var
|
|
109
|
-
function
|
|
110
|
-
if (typeof
|
|
111
|
-
throw new
|
|
86
|
+
var G = /^nrt_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/;
|
|
87
|
+
function K(t) {
|
|
88
|
+
if (typeof t != "string" || t.length === 0)
|
|
89
|
+
throw new r(
|
|
112
90
|
"invalid_config",
|
|
113
91
|
"apiKey is required (formato nrt_<keyId>_<secret>)"
|
|
114
92
|
);
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
throw new NeetruError(
|
|
93
|
+
let e = G.exec(t);
|
|
94
|
+
if (!e)
|
|
95
|
+
throw new r(
|
|
119
96
|
"invalid_config",
|
|
120
97
|
"apiKey malformado - esperado formato nrt_<keyId>_<secret>"
|
|
121
98
|
);
|
|
122
|
-
}
|
|
123
|
-
return { keyId: match[1], secret: match[2] };
|
|
99
|
+
return { keyId: e[1], secret: e[2] };
|
|
124
100
|
}
|
|
125
101
|
|
|
126
102
|
// src/auth-pkce-shared.ts
|
|
127
|
-
function
|
|
128
|
-
|
|
129
|
-
if (!
|
|
130
|
-
throw new
|
|
103
|
+
function H() {
|
|
104
|
+
let t = globalThis.crypto;
|
|
105
|
+
if (!t || typeof t.getRandomValues != "function" || !t.subtle)
|
|
106
|
+
throw new r(
|
|
131
107
|
"runtime_unsupported",
|
|
132
108
|
"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."
|
|
133
109
|
);
|
|
134
|
-
|
|
135
|
-
return c;
|
|
110
|
+
return t;
|
|
136
111
|
}
|
|
137
|
-
function
|
|
138
|
-
let
|
|
139
|
-
for (
|
|
140
|
-
|
|
141
|
-
return b64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
112
|
+
function $(t) {
|
|
113
|
+
let e = "";
|
|
114
|
+
for (let s of t) e += String.fromCharCode(s);
|
|
115
|
+
return (typeof btoa == "function" ? btoa(e) : Buffer.from(e, "binary").toString("base64")).replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
|
|
142
116
|
}
|
|
143
|
-
function
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return base64UrlFromBytes(bytes);
|
|
117
|
+
function v(t, e) {
|
|
118
|
+
let i = new Uint8Array(e);
|
|
119
|
+
return t.getRandomValues(i), $(i);
|
|
147
120
|
}
|
|
148
|
-
async function
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
return base64UrlFromBytes(new Uint8Array(digest));
|
|
121
|
+
async function B(t, e) {
|
|
122
|
+
let i = new TextEncoder().encode(e), s = await t.subtle.digest("SHA-256", i);
|
|
123
|
+
return $(new Uint8Array(s));
|
|
152
124
|
}
|
|
153
|
-
function
|
|
154
|
-
|
|
155
|
-
return overrideAuthUrl ?? baseUrl.replace(/^https?:\/\/api\./, "https://auth.");
|
|
125
|
+
function U(t) {
|
|
126
|
+
return (typeof globalThis.NEETRU_AUTH_URL == "string" ? globalThis.NEETRU_AUTH_URL : null) ?? t.replace(/^https?:\/\/api\./, "https://auth.");
|
|
156
127
|
}
|
|
157
|
-
function
|
|
158
|
-
let
|
|
159
|
-
if (!
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
if (!clientId) {
|
|
163
|
-
throw new NeetruError(
|
|
128
|
+
function I(t) {
|
|
129
|
+
let e = t.oidcClientId;
|
|
130
|
+
if (!e && t.apiKey && (e = K(t.apiKey).keyId), !e)
|
|
131
|
+
throw new r(
|
|
164
132
|
"invalid_config",
|
|
165
133
|
"auth OIDC requer `oidcClientId` (ou `apiKey`) configurado para montar o client_id."
|
|
166
134
|
);
|
|
167
|
-
|
|
168
|
-
return clientId;
|
|
135
|
+
return e;
|
|
169
136
|
}
|
|
170
|
-
function
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
url.searchParams.set("client_id", params.clientId);
|
|
174
|
-
url.searchParams.set("redirect_uri", params.redirectUri);
|
|
175
|
-
url.searchParams.set("scope", params.scope);
|
|
176
|
-
url.searchParams.set("state", params.state);
|
|
177
|
-
url.searchParams.set("nonce", params.nonce);
|
|
178
|
-
url.searchParams.set("code_challenge", params.codeChallenge);
|
|
179
|
-
url.searchParams.set("code_challenge_method", "S256");
|
|
180
|
-
return url;
|
|
137
|
+
function q(t) {
|
|
138
|
+
let e = new URL("/api/v1/oauth/authorize", t.idpOrigin);
|
|
139
|
+
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;
|
|
181
140
|
}
|
|
182
|
-
var
|
|
183
|
-
function
|
|
184
|
-
|
|
185
|
-
if (
|
|
141
|
+
var W = "/api/v1/oauth/token";
|
|
142
|
+
function N(t) {
|
|
143
|
+
let e = t.split(".");
|
|
144
|
+
if (e.length !== 3) return null;
|
|
186
145
|
try {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const decoded = typeof atob === "function" ? atob(b64 + pad) : Buffer.from(b64 + pad, "base64").toString("utf-8");
|
|
190
|
-
return JSON.parse(decoded);
|
|
146
|
+
let i = e[1].replace(/-/g, "+").replace(/_/g, "/"), s = "=".repeat((4 - i.length % 4) % 4), o = typeof atob == "function" ? atob(i + s) : Buffer.from(i + s, "base64").toString("utf-8");
|
|
147
|
+
return JSON.parse(o);
|
|
191
148
|
} catch {
|
|
192
149
|
return null;
|
|
193
150
|
}
|
|
194
151
|
}
|
|
195
|
-
function
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
isStaff: payload.is_staff === true,
|
|
208
|
-
isCustomer: payload.is_customer === true,
|
|
209
|
-
tenantId: typeof payload.tenant_id === "string" ? payload.tenant_id : void 0,
|
|
210
|
-
...payload
|
|
152
|
+
function _(t) {
|
|
153
|
+
let e = N(t);
|
|
154
|
+
return !e || typeof e.sub != "string" || typeof e.exp == "number" && e.exp * 1e3 < Date.now() ? null : {
|
|
155
|
+
uid: e.sub,
|
|
156
|
+
email: typeof e.email == "string" ? e.email : "",
|
|
157
|
+
emailVerified: typeof e.email_verified == "boolean" ? e.email_verified : void 0,
|
|
158
|
+
displayName: typeof e.name == "string" ? e.name : void 0,
|
|
159
|
+
photoURL: typeof e.picture == "string" ? e.picture : void 0,
|
|
160
|
+
isStaff: e.is_staff === true,
|
|
161
|
+
isCustomer: e.is_customer === true,
|
|
162
|
+
tenantId: typeof e.tenant_id == "string" ? e.tenant_id : void 0,
|
|
163
|
+
...e
|
|
211
164
|
};
|
|
212
165
|
}
|
|
213
166
|
|
|
214
167
|
// src/mobile.ts
|
|
215
|
-
var
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (!config.storage) {
|
|
220
|
-
throw new NeetruError(
|
|
168
|
+
var h = "neetru_oauth_tx", y = "neetru_id_token", S = "neetru_refresh_token";
|
|
169
|
+
function Q(t) {
|
|
170
|
+
if (!t.storage)
|
|
171
|
+
throw new r(
|
|
221
172
|
"invalid_config",
|
|
222
173
|
"createMobileAuthNamespace requer `storage` (MobileAuthStorage) \u2014 ver JSDoc de src/mobile.ts requisito 4."
|
|
223
174
|
);
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
throw new NeetruError(
|
|
175
|
+
if (typeof t.openAuthUrl != "function")
|
|
176
|
+
throw new r(
|
|
227
177
|
"invalid_config",
|
|
228
178
|
"createMobileAuthNamespace requer `openAuthUrl` (browser externo, NUNCA WebView embutida) \u2014 ver JSDoc de src/mobile.ts requisito 3."
|
|
229
179
|
);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
throw new NeetruError(
|
|
180
|
+
let e = t.fetch ?? globalThis.fetch;
|
|
181
|
+
if (typeof e != "function")
|
|
182
|
+
throw new r(
|
|
234
183
|
"invalid_config",
|
|
235
184
|
"fetch is not available in this runtime. Pass `fetch` explicitly to createMobileAuthNamespace."
|
|
236
185
|
);
|
|
237
|
-
|
|
238
|
-
const baseUrl = validateBaseUrl(config.baseUrl ?? DEFAULT_BASE_URL, config.allowedHosts ?? []);
|
|
186
|
+
let i = M(t.baseUrl ?? L, t.allowedHosts ?? []);
|
|
239
187
|
return {
|
|
240
|
-
fetchImpl:
|
|
241
|
-
baseUrl,
|
|
242
|
-
oidcClientId:
|
|
243
|
-
apiKey:
|
|
244
|
-
storage:
|
|
245
|
-
openAuthUrl:
|
|
188
|
+
fetchImpl: e.bind(globalThis),
|
|
189
|
+
baseUrl: i,
|
|
190
|
+
oidcClientId: t.oidcClientId,
|
|
191
|
+
apiKey: t.apiKey,
|
|
192
|
+
storage: t.storage,
|
|
193
|
+
openAuthUrl: t.openAuthUrl
|
|
246
194
|
};
|
|
247
195
|
}
|
|
248
|
-
async function
|
|
249
|
-
let
|
|
196
|
+
async function z(t, e, i) {
|
|
197
|
+
let s;
|
|
250
198
|
try {
|
|
251
|
-
|
|
199
|
+
s = await t(e, {
|
|
252
200
|
method: "POST",
|
|
253
201
|
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
254
|
-
body:
|
|
202
|
+
body: i.toString()
|
|
255
203
|
});
|
|
256
|
-
} catch (
|
|
257
|
-
throw new
|
|
204
|
+
} catch (l) {
|
|
205
|
+
throw new r(
|
|
258
206
|
"network_error",
|
|
259
|
-
`POST ${
|
|
207
|
+
`POST ${e} falhou (network): ${l?.message ?? String(l)}`
|
|
260
208
|
);
|
|
261
209
|
}
|
|
262
|
-
let
|
|
210
|
+
let o = null;
|
|
263
211
|
try {
|
|
264
|
-
|
|
212
|
+
o = await s.json();
|
|
265
213
|
} catch {
|
|
266
|
-
|
|
214
|
+
o = null;
|
|
267
215
|
}
|
|
268
|
-
if (!
|
|
269
|
-
|
|
270
|
-
throw new
|
|
216
|
+
if (!s.ok || typeof o?.error == "string") {
|
|
217
|
+
let l = typeof o?.error_description == "string" && o.error_description || typeof o?.error == "string" && o.error || `HTTP ${s.status}`;
|
|
218
|
+
throw new r(
|
|
271
219
|
"token_exchange_failed",
|
|
272
|
-
`Troca de tokens falhou no /token: ${
|
|
273
|
-
|
|
220
|
+
`Troca de tokens falhou no /token: ${l}`,
|
|
221
|
+
s.status
|
|
274
222
|
);
|
|
275
223
|
}
|
|
276
|
-
return
|
|
224
|
+
return o ?? {};
|
|
277
225
|
}
|
|
278
|
-
function
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return `${resolveIdpOrigin(resolved.baseUrl)}${OAUTH_TOKEN_PATH}`;
|
|
226
|
+
function ge(t) {
|
|
227
|
+
let e = Q(t), i = /* @__PURE__ */ new Set();
|
|
228
|
+
function s() {
|
|
229
|
+
return `${U(e.baseUrl)}${W}`;
|
|
283
230
|
}
|
|
284
|
-
async function
|
|
285
|
-
|
|
286
|
-
for (
|
|
231
|
+
async function o() {
|
|
232
|
+
let n = await m();
|
|
233
|
+
for (let a of i)
|
|
287
234
|
try {
|
|
288
|
-
|
|
235
|
+
a(n);
|
|
289
236
|
} catch {
|
|
290
237
|
}
|
|
291
|
-
}
|
|
292
238
|
}
|
|
293
|
-
async function
|
|
294
|
-
if (!
|
|
295
|
-
throw new
|
|
239
|
+
async function l(n) {
|
|
240
|
+
if (!n?.redirectUri)
|
|
241
|
+
throw new r(
|
|
296
242
|
"invalid_config",
|
|
297
243
|
"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."
|
|
298
244
|
);
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const codeVerifier = randomUrlSafe(crypto, 32);
|
|
306
|
-
const codeChallenge = await pkceChallengeS256(crypto, codeVerifier);
|
|
307
|
-
const tx = {
|
|
308
|
-
state,
|
|
309
|
-
nonce,
|
|
310
|
-
codeVerifier,
|
|
311
|
-
redirectUri: options.redirectUri,
|
|
312
|
-
postLoginRedirect: options.postLoginRedirect
|
|
245
|
+
let a = H(), u = n.scope ?? "openid profile email", c = I(e), d = v(a, 16), f = v(a, 16), g = v(a, 32), p = await B(a, g), T = {
|
|
246
|
+
state: d,
|
|
247
|
+
nonce: f,
|
|
248
|
+
codeVerifier: g,
|
|
249
|
+
redirectUri: n.redirectUri,
|
|
250
|
+
postLoginRedirect: n.postLoginRedirect
|
|
313
251
|
};
|
|
314
|
-
await
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
codeChallenge
|
|
252
|
+
await e.storage.setItem(h, JSON.stringify(T));
|
|
253
|
+
let C = U(e.baseUrl), w = q({
|
|
254
|
+
idpOrigin: C,
|
|
255
|
+
clientId: c,
|
|
256
|
+
redirectUri: n.redirectUri,
|
|
257
|
+
scope: u,
|
|
258
|
+
state: d,
|
|
259
|
+
nonce: f,
|
|
260
|
+
codeChallenge: p
|
|
324
261
|
});
|
|
325
|
-
await
|
|
262
|
+
await e.openAuthUrl(w.toString());
|
|
326
263
|
}
|
|
327
|
-
async function
|
|
328
|
-
if (!
|
|
329
|
-
throw new
|
|
264
|
+
async function A(n) {
|
|
265
|
+
if (!n || typeof n != "string")
|
|
266
|
+
throw new r(
|
|
330
267
|
"invalid_config",
|
|
331
268
|
"mobile auth.completeSignIn requer `callbackUrl` (a URL completa do deep link de retorno) \u2014 mobile n\xE3o tem location.href impl\xEDcito."
|
|
332
269
|
);
|
|
333
|
-
|
|
334
|
-
let parsed;
|
|
270
|
+
let a;
|
|
335
271
|
try {
|
|
336
|
-
|
|
272
|
+
a = new URL(n);
|
|
337
273
|
} catch {
|
|
338
|
-
throw new
|
|
274
|
+
throw new r("invalid_config", `callbackUrl n\xE3o \xE9 uma URL v\xE1lida: ${n}`);
|
|
339
275
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
await resolved.storage.removeItem(OIDC_TX_KEY);
|
|
345
|
-
throw new NeetruError(idpError, `OIDC authorize falhou: ${desc}`);
|
|
276
|
+
let u = a.searchParams, c = u.get("error");
|
|
277
|
+
if (c) {
|
|
278
|
+
let j = u.get("error_description") ?? c;
|
|
279
|
+
throw await e.storage.removeItem(h), new r(c, `OIDC authorize falhou: ${j}`);
|
|
346
280
|
}
|
|
347
|
-
|
|
348
|
-
if (!
|
|
349
|
-
await
|
|
350
|
-
throw new NeetruError(
|
|
281
|
+
let d = u.get("code");
|
|
282
|
+
if (!d)
|
|
283
|
+
throw await e.storage.removeItem(h), new r(
|
|
351
284
|
"invalid_config",
|
|
352
285
|
"callbackUrl n\xE3o cont\xE9m `?code=` nem `?error=` \u2014 n\xE3o \xE9 um callback OIDC v\xE1lido."
|
|
353
286
|
);
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
const txRaw = await resolved.storage.getItem(OIDC_TX_KEY);
|
|
357
|
-
let tx = null;
|
|
358
|
-
if (txRaw) {
|
|
287
|
+
let f = u.get("state"), g = await e.storage.getItem(h), p = null;
|
|
288
|
+
if (g)
|
|
359
289
|
try {
|
|
360
|
-
|
|
290
|
+
p = JSON.parse(g);
|
|
361
291
|
} catch {
|
|
362
|
-
|
|
292
|
+
p = null;
|
|
363
293
|
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
throw new NeetruError(
|
|
294
|
+
if (!p)
|
|
295
|
+
throw new r(
|
|
367
296
|
"oidc_state_mismatch",
|
|
368
297
|
"Callback OIDC sem transa\xE7\xE3o pendente \u2014 `signIn` n\xE3o foi iniciado (ou j\xE1 consumido)."
|
|
369
298
|
);
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
await resolved.storage.removeItem(OIDC_TX_KEY);
|
|
373
|
-
throw new NeetruError(
|
|
299
|
+
if (!f || f !== p.state)
|
|
300
|
+
throw await e.storage.removeItem(h), new r(
|
|
374
301
|
"oidc_state_mismatch",
|
|
375
302
|
"state do callback OIDC n\xE3o confere com o da transa\xE7\xE3o (poss\xEDvel CSRF)."
|
|
376
303
|
);
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
const clientId = resolveOidcClientId(resolved);
|
|
380
|
-
const form = new URLSearchParams({
|
|
304
|
+
await e.storage.removeItem(h);
|
|
305
|
+
let T = I(e), C = new URLSearchParams({
|
|
381
306
|
grant_type: "authorization_code",
|
|
382
|
-
code,
|
|
383
|
-
redirect_uri:
|
|
384
|
-
client_id:
|
|
385
|
-
code_verifier:
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (typeof idToken !== "string" || idToken.length === 0) {
|
|
390
|
-
throw new NeetruError(
|
|
307
|
+
code: d,
|
|
308
|
+
redirect_uri: p.redirectUri,
|
|
309
|
+
client_id: T,
|
|
310
|
+
code_verifier: p.codeVerifier
|
|
311
|
+
}), w = await z(e.fetchImpl, s(), C), b = w.id_token;
|
|
312
|
+
if (typeof b != "string" || b.length === 0)
|
|
313
|
+
throw new r(
|
|
391
314
|
"token_exchange_failed",
|
|
392
315
|
"Resposta do /token sem `id_token` \u2014 o login n\xE3o pode ser completado."
|
|
393
316
|
);
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
throw new NeetruError(
|
|
317
|
+
let x = N(b);
|
|
318
|
+
if (!x)
|
|
319
|
+
throw new r(
|
|
398
320
|
"token_exchange_failed",
|
|
399
321
|
"id_token retornado pelo /token \xE9 malformado (payload indecodific\xE1vel)."
|
|
400
322
|
);
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
throw new NeetruError(
|
|
323
|
+
if (x.nonce !== p.nonce)
|
|
324
|
+
throw new r(
|
|
404
325
|
"oidc_nonce_mismatch",
|
|
405
326
|
"nonce do id_token n\xE3o confere com o da transa\xE7\xE3o OIDC (poss\xEDvel replay). Token descartado."
|
|
406
327
|
);
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
throw new NeetruError(
|
|
328
|
+
let E = _(b);
|
|
329
|
+
if (!E)
|
|
330
|
+
throw new r(
|
|
411
331
|
"token_exchange_failed",
|
|
412
332
|
"id_token retornado pelo /token n\xE3o tem `sub` ou j\xE1 est\xE1 expirado."
|
|
413
333
|
);
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
if (typeof refreshToken === "string" && refreshToken.length > 0) {
|
|
418
|
-
await resolved.storage.setItem(REFRESH_TOKEN_KEY, refreshToken);
|
|
419
|
-
}
|
|
420
|
-
await notify();
|
|
421
|
-
return { user, postLoginRedirect: tx.postLoginRedirect };
|
|
334
|
+
await e.storage.setItem(y, b);
|
|
335
|
+
let P = w.refresh_token;
|
|
336
|
+
return typeof P == "string" && P.length > 0 && await e.storage.setItem(S, P), await o(), { user: E, postLoginRedirect: p.postLoginRedirect };
|
|
422
337
|
}
|
|
423
|
-
async function
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
return jwtToNeetruUser(token) ? token : null;
|
|
338
|
+
async function R() {
|
|
339
|
+
let n = await e.storage.getItem(y);
|
|
340
|
+
return n && _(n) ? n : null;
|
|
427
341
|
}
|
|
428
|
-
async function
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
return jwtToNeetruUser(token);
|
|
342
|
+
async function m() {
|
|
343
|
+
let n = await e.storage.getItem(y);
|
|
344
|
+
return n ? _(n) : null;
|
|
432
345
|
}
|
|
433
|
-
async function
|
|
434
|
-
|
|
435
|
-
if (!
|
|
436
|
-
let
|
|
346
|
+
async function k() {
|
|
347
|
+
let n = await e.storage.getItem(S);
|
|
348
|
+
if (!n) return false;
|
|
349
|
+
let a;
|
|
437
350
|
try {
|
|
438
|
-
|
|
351
|
+
a = I(e);
|
|
439
352
|
} catch {
|
|
440
353
|
return false;
|
|
441
354
|
}
|
|
442
|
-
|
|
355
|
+
let u = new URLSearchParams({
|
|
443
356
|
grant_type: "refresh_token",
|
|
444
|
-
refresh_token:
|
|
445
|
-
client_id:
|
|
446
|
-
});
|
|
447
|
-
let payload;
|
|
357
|
+
refresh_token: n,
|
|
358
|
+
client_id: a
|
|
359
|
+
}), c;
|
|
448
360
|
try {
|
|
449
|
-
|
|
361
|
+
c = await z(e.fetchImpl, s(), u);
|
|
450
362
|
} catch {
|
|
451
363
|
return false;
|
|
452
364
|
}
|
|
453
|
-
|
|
454
|
-
if (typeof
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
await
|
|
458
|
-
const rotated = payload.refresh_token;
|
|
459
|
-
if (typeof rotated === "string" && rotated.length > 0) {
|
|
460
|
-
await resolved.storage.setItem(REFRESH_TOKEN_KEY, rotated);
|
|
461
|
-
}
|
|
462
|
-
await notify();
|
|
463
|
-
return true;
|
|
365
|
+
let d = c.id_token;
|
|
366
|
+
if (typeof d != "string" || d.length === 0 || !_(d)) return false;
|
|
367
|
+
await e.storage.setItem(y, d);
|
|
368
|
+
let g = c.refresh_token;
|
|
369
|
+
return typeof g == "string" && g.length > 0 && await e.storage.setItem(S, g), await o(), true;
|
|
464
370
|
}
|
|
465
|
-
async function
|
|
466
|
-
|
|
467
|
-
await
|
|
468
|
-
|
|
469
|
-
if (storedToken) {
|
|
470
|
-
const idpOrigin = resolveIdpOrigin(resolved.baseUrl);
|
|
471
|
-
const revokeUrl = `${idpOrigin}/api/v1/oauth/revoke`;
|
|
371
|
+
async function V() {
|
|
372
|
+
let n = await e.storage.getItem(y);
|
|
373
|
+
if (await e.storage.removeItem(y), await e.storage.removeItem(S), n) {
|
|
374
|
+
let u = `${U(e.baseUrl)}/api/v1/oauth/revoke`;
|
|
472
375
|
try {
|
|
473
|
-
await
|
|
376
|
+
await e.fetchImpl(u, {
|
|
474
377
|
method: "POST",
|
|
475
378
|
headers: { "content-type": "application/json" },
|
|
476
|
-
body: JSON.stringify({ token:
|
|
379
|
+
body: JSON.stringify({ token: n })
|
|
477
380
|
});
|
|
478
|
-
} catch (
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
);
|
|
484
|
-
}
|
|
381
|
+
} catch (c) {
|
|
382
|
+
typeof console < "u" && console.warn(
|
|
383
|
+
"[neetru-sdk] mobile signOut: server-side revoke falhou. Refresh token pode estar stale.",
|
|
384
|
+
c
|
|
385
|
+
);
|
|
485
386
|
}
|
|
486
387
|
}
|
|
487
|
-
await
|
|
388
|
+
await o();
|
|
488
389
|
}
|
|
489
390
|
return {
|
|
490
|
-
signIn,
|
|
491
|
-
completeSignIn,
|
|
492
|
-
getIdToken,
|
|
493
|
-
getUser,
|
|
494
|
-
onAuthStateChanged(
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
if (listeners.has(listener)) {
|
|
391
|
+
signIn: l,
|
|
392
|
+
completeSignIn: A,
|
|
393
|
+
getIdToken: R,
|
|
394
|
+
getUser: m,
|
|
395
|
+
onAuthStateChanged(n) {
|
|
396
|
+
return i.add(n), m().then((a) => {
|
|
397
|
+
if (i.has(n))
|
|
498
398
|
try {
|
|
499
|
-
|
|
399
|
+
n(a);
|
|
500
400
|
} catch {
|
|
501
401
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
return () => {
|
|
505
|
-
listeners.delete(listener);
|
|
402
|
+
}), () => {
|
|
403
|
+
i.delete(n);
|
|
506
404
|
};
|
|
507
405
|
},
|
|
508
|
-
refreshTokens,
|
|
509
|
-
signOut
|
|
406
|
+
refreshTokens: k,
|
|
407
|
+
signOut: V
|
|
510
408
|
};
|
|
511
409
|
}
|
|
512
410
|
|
|
513
|
-
exports.createMobileAuthNamespace =
|
|
514
|
-
//# sourceMappingURL=mobile.cjs.map
|
|
515
|
-
//# sourceMappingURL=mobile.cjs.map
|
|
411
|
+
exports.createMobileAuthNamespace = ge;
|