@geekapps/auth-nextjs 0.12.0 → 0.14.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.
@@ -1,10 +1,803 @@
1
+ // src/connections.ts
2
+ import { createHash as createHash2, randomBytes as randomBytes2 } from "crypto";
3
+ import { buildModel, fetchRemoteCatalog, renderAugmentation, toRegistry } from "@geekapps/auth-cli/lib";
4
+ import { cookies as cookies2 } from "next/headers";
5
+ import { redirect as redirect2 } from "next/navigation";
6
+
1
7
  // src/server.ts
2
8
  import { createHash, randomBytes } from "crypto";
3
9
  import { cookies, headers } from "next/headers";
4
10
  import { redirect } from "next/navigation";
5
11
  import { NextResponse } from "next/server";
6
12
  import { createRemoteJWKSet, jwtVerify } from "jose";
13
+ var GeekappsSessionExpiredError = class extends Error {
14
+ code = "session_expired";
15
+ constructor(message = "A sess\xE3o expirou ou foi revogada. Entre novamente.") {
16
+ super(message);
17
+ this.name = "GeekappsSessionExpiredError";
18
+ }
19
+ };
7
20
  var GEEKAPPS_AUTH_SERVER_URL = "https://accounts-api.geekapps.com.br";
21
+ var MAX_ACCOUNTS = 8;
22
+ function normalizeBaseUrl(url) {
23
+ return url.replace(/\/$/, "");
24
+ }
25
+ function normalizeBasePath(path = "/api/auth") {
26
+ if (!path.startsWith("/")) return `/${path}`;
27
+ return path.replace(/\/$/, "");
28
+ }
29
+ function createUrl(baseUrl, path) {
30
+ if (/^https?:\/\//.test(path)) return path;
31
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
32
+ return `${normalizeBaseUrl(baseUrl)}${normalizedPath}`;
33
+ }
34
+ function env(name) {
35
+ const value = process.env[name];
36
+ return value && value.trim().length > 0 ? value : void 0;
37
+ }
38
+ function resolveIssuer(input) {
39
+ return normalizeBaseUrl(
40
+ input.issuer ?? env("GEEKAPPS_AUTH_SERVER_ISSUER") ?? env("GEEKAPPS_AUTH_SERVER_URL") ?? // Deprecated: kept for backwards compatibility during the auth-server rename.
41
+ env("GEEKAPPS_AUTH_ISSUER") ?? env("GEEKAPPS_AUTH_API_URL") ?? GEEKAPPS_AUTH_SERVER_URL
42
+ );
43
+ }
44
+ function resolveClientId(input) {
45
+ return input.clientId ?? env("GEEKAPPS_AUTH_CLIENT_ID") ?? "";
46
+ }
47
+ function resolveClientSecret(input) {
48
+ return input.clientSecret ?? env("GEEKAPPS_AUTH_CLIENT_SECRET");
49
+ }
50
+ function applyClientCredentials(url, config) {
51
+ if (!config.clientId || !config.clientSecret) return;
52
+ url.searchParams.set("client_id", config.clientId);
53
+ url.searchParams.set("client_secret", config.clientSecret);
54
+ }
55
+ function resolveConfiguredRedirectUri(input) {
56
+ return input.redirectUri ?? env("GEEKAPPS_AUTH_REDIRECT_URI");
57
+ }
58
+ function hashClientId(clientId) {
59
+ let h = 5381;
60
+ for (let i = 0; i < clientId.length; i++) {
61
+ h = (h << 5) + h ^ clientId.charCodeAt(i) & 255;
62
+ h = h >>> 0;
63
+ }
64
+ return h.toString(16).padStart(8, "0");
65
+ }
66
+ function deriveCookiePrefix(clientId) {
67
+ if (!clientId) return "gk";
68
+ return `gk_${hashClientId(clientId)}`;
69
+ }
70
+ function requestOrigin(request) {
71
+ const configured = env("GEEKAPPS_APP_URL");
72
+ if (configured) return configured.replace(/\/$/, "");
73
+ if (request.nextUrl?.origin) return request.nextUrl.origin;
74
+ throw new Error(
75
+ "Cannot determine app origin. Set GEEKAPPS_APP_URL or ensure request.nextUrl is available."
76
+ );
77
+ }
78
+ function resolveRedirectUri(config, request) {
79
+ return config.redirectUri ?? `${requestOrigin(request)}${config.basePath}/callback`;
80
+ }
81
+ function tokenFromResponse(data) {
82
+ return {
83
+ accessToken: data.accessToken ?? data.access_token ?? data.tokens?.accessToken ?? data.tokens?.access_token ?? null,
84
+ refreshToken: data.refreshToken ?? data.refresh_token ?? data.tokens?.refreshToken ?? data.tokens?.refresh_token ?? null,
85
+ idToken: data.idToken ?? data.id_token ?? data.tokens?.idToken ?? data.tokens?.id_token ?? null,
86
+ sessionId: data.sessionId ?? data.session_id ?? data.tokens?.sessionId ?? data.tokens?.session_id ?? null
87
+ };
88
+ }
89
+ function decodeJwtPayload(token) {
90
+ try {
91
+ const parts = token.split(".");
92
+ if (parts.length < 2) return null;
93
+ const payload = parts[1];
94
+ if (!payload) return null;
95
+ return JSON.parse(Buffer.from(payload, "base64url").toString("utf8"));
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+ function parseAccountFromJwt(tokens) {
101
+ if (!tokens.accessToken) return null;
102
+ const jwtToParse = tokens.idToken ?? tokens.accessToken;
103
+ const payload = decodeJwtPayload(jwtToParse);
104
+ if (!payload) return null;
105
+ const sub = typeof payload.sub === "string" ? payload.sub : null;
106
+ const id = sub ?? tokens.sessionId;
107
+ if (!id) return null;
108
+ return {
109
+ id,
110
+ accessToken: tokens.accessToken,
111
+ refreshToken: tokens.refreshToken,
112
+ idToken: tokens.idToken ?? null,
113
+ sessionId: tokens.sessionId,
114
+ user: {
115
+ sub: id,
116
+ name: typeof payload.name === "string" ? payload.name : null,
117
+ email: typeof payload.email === "string" ? payload.email : null,
118
+ picture: typeof payload.picture === "string" ? payload.picture : null,
119
+ phone: typeof payload.phone_number === "string" ? payload.phone_number : null
120
+ }
121
+ };
122
+ }
123
+ function getCookieNames(prefix) {
124
+ return {
125
+ active: `${prefix}_active`,
126
+ count: `${prefix}_count`,
127
+ activeOrganizationId: `${prefix}_ao`,
128
+ tokenPub: `${prefix}_tp`,
129
+ pkceVerifier: `${prefix}_pkce`,
130
+ slotMeta: (n) => `${prefix}_m${n}`,
131
+ slotAt: (n) => `${prefix}_at${n}`,
132
+ slotRt: (n) => `${prefix}_rt${n}`
133
+ };
134
+ }
135
+ function generateCodeVerifier() {
136
+ return randomBytes(64).toString("base64url");
137
+ }
138
+ function createCodeChallenge(codeVerifier) {
139
+ return createHash("sha256").update(codeVerifier).digest("base64url");
140
+ }
141
+ function encodeMeta(account) {
142
+ const m = { id: account.id };
143
+ if (account.sessionId) m.sid = account.sessionId;
144
+ if (account.user.name) m.n = account.user.name;
145
+ if (account.user.email) m.e = account.user.email;
146
+ if (account.user.picture) m.p = account.user.picture;
147
+ return Buffer.from(JSON.stringify(m), "utf8").toString("base64url");
148
+ }
149
+ function decodeMeta(value) {
150
+ try {
151
+ const m = JSON.parse(Buffer.from(value, "base64url").toString("utf8"));
152
+ return m.id ? m : null;
153
+ } catch {
154
+ return null;
155
+ }
156
+ }
157
+ function getCookieOptions(config) {
158
+ return {
159
+ httpOnly: true,
160
+ secure: config.secureCookies ?? process.env.NODE_ENV === "production",
161
+ sameSite: "lax",
162
+ path: "/"
163
+ };
164
+ }
165
+ async function readBody(request) {
166
+ const text = await request.text();
167
+ if (!text) return void 0;
168
+ try {
169
+ return JSON.parse(text);
170
+ } catch {
171
+ return text;
172
+ }
173
+ }
174
+ function appendSearchParams(url, request) {
175
+ const target = new URL(url);
176
+ request.nextUrl.searchParams.forEach((value, key) => {
177
+ target.searchParams.set(key, value);
178
+ });
179
+ return target.toString();
180
+ }
181
+ function routeFromRequest(request, basePath) {
182
+ const pathname = request.nextUrl.pathname;
183
+ if (!pathname.startsWith(basePath)) return pathname;
184
+ const route = pathname.slice(basePath.length);
185
+ return route || "/";
186
+ }
187
+ function isJsonResponse(response) {
188
+ return response.headers.get("content-type")?.includes("application/json");
189
+ }
190
+ async function responseFromApi(response) {
191
+ if (response.status === 204) return new NextResponse(null, { status: 204 });
192
+ const body = isJsonResponse(response) ? await response.json().catch(() => null) : await response.text();
193
+ return NextResponse.json(body, { status: response.status });
194
+ }
195
+ var _remoteJWKSCache = /* @__PURE__ */ new Map();
196
+ function getRemoteJWKS(issuer) {
197
+ if (!_remoteJWKSCache.has(issuer)) {
198
+ _remoteJWKSCache.set(issuer, createRemoteJWKSet(new URL(`${issuer}/.well-known/jwks.json`)));
199
+ }
200
+ return _remoteJWKSCache.get(issuer);
201
+ }
202
+ async function verifyToken(token, issuer) {
203
+ try {
204
+ const { payload } = await jwtVerify(token, getRemoteJWKS(issuer), {
205
+ algorithms: ["RS256"],
206
+ issuer
207
+ });
208
+ return payload;
209
+ } catch {
210
+ return null;
211
+ }
212
+ }
213
+ function configureGeekappsAuth(input = {}) {
214
+ const clientId = resolveClientId(input);
215
+ const config = {
216
+ ...input,
217
+ issuer: resolveIssuer(input),
218
+ clientId,
219
+ clientSecret: resolveClientSecret(input),
220
+ redirectUri: resolveConfiguredRedirectUri(input),
221
+ afterSignInRedirectTo: input.afterSignInRedirectTo ?? input.redirectTo ?? "/",
222
+ basePath: normalizeBasePath(input.basePath),
223
+ cookiePrefix: deriveCookiePrefix(clientId)
224
+ };
225
+ const cn = getCookieNames(config.cookiePrefix);
226
+ async function getCount() {
227
+ const cookieStore = await cookies();
228
+ const raw = cookieStore.get(cn.count)?.value;
229
+ const n = raw ? Number.parseInt(raw, 10) : 0;
230
+ return Number.isNaN(n) ? 0 : n;
231
+ }
232
+ async function getActiveIndex() {
233
+ const cookieStore = await cookies();
234
+ const raw = cookieStore.get(cn.active)?.value;
235
+ if (!raw) return 0;
236
+ const n = Number.parseInt(raw, 10);
237
+ return Number.isNaN(n) ? 0 : n;
238
+ }
239
+ function readSlot(cookieStore, n) {
240
+ const at = cookieStore.get(cn.slotAt(n))?.value;
241
+ if (!at) return null;
242
+ const meta = decodeMeta(cookieStore.get(cn.slotMeta(n))?.value ?? "");
243
+ if (!meta) return null;
244
+ return {
245
+ id: meta.id,
246
+ accessToken: at,
247
+ refreshToken: cookieStore.get(cn.slotRt(n))?.value ?? null,
248
+ idToken: null,
249
+ // not stored in cookies; claims are in meta
250
+ sessionId: meta.sid ?? null,
251
+ user: {
252
+ sub: meta.id,
253
+ name: meta.n ?? null,
254
+ email: meta.e ?? null,
255
+ picture: meta.p ?? null
256
+ }
257
+ };
258
+ }
259
+ function writeSlot(cookieStore, n, account) {
260
+ const opts = { ...getCookieOptions(config), maxAge: 60 * 60 * 24 * 30 };
261
+ cookieStore.set(cn.slotMeta(n), encodeMeta(account), opts);
262
+ cookieStore.set(cn.slotAt(n), account.accessToken, opts);
263
+ if (account.refreshToken) cookieStore.set(cn.slotRt(n), account.refreshToken, opts);
264
+ }
265
+ function clearSlot(cookieStore, n) {
266
+ const clear = { ...getCookieOptions(config), maxAge: 0 };
267
+ cookieStore.set(cn.slotMeta(n), "", clear);
268
+ cookieStore.set(cn.slotAt(n), "", clear);
269
+ cookieStore.set(cn.slotRt(n), "", clear);
270
+ }
271
+ async function getAccountsList() {
272
+ const [cookieStore, count] = await Promise.all([cookies(), getCount()]);
273
+ const accounts = [];
274
+ for (let i = 1; i <= count; i++) {
275
+ const slot = readSlot(cookieStore, i);
276
+ if (slot) accounts.push(slot);
277
+ }
278
+ return accounts;
279
+ }
280
+ async function getActiveAccount() {
281
+ const [cookieStore, activeIdx, count] = await Promise.all([cookies(), getActiveIndex(), getCount()]);
282
+ if (count === 0) return null;
283
+ if (activeIdx >= 1 && activeIdx <= count) {
284
+ const slot = readSlot(cookieStore, activeIdx);
285
+ if (slot) return slot;
286
+ }
287
+ return readSlot(cookieStore, 1);
288
+ }
289
+ async function getAccessToken() {
290
+ try {
291
+ const hdrs = await headers();
292
+ const auth = hdrs.get("authorization");
293
+ if (auth?.startsWith("Bearer ")) return auth.slice(7);
294
+ } catch {
295
+ }
296
+ const active = await getActiveAccount();
297
+ return active?.accessToken ?? null;
298
+ }
299
+ async function getRefreshToken() {
300
+ const active = await getActiveAccount();
301
+ return active?.refreshToken ?? null;
302
+ }
303
+ async function getSession() {
304
+ const active = await getActiveAccount();
305
+ if (!active) return null;
306
+ return {
307
+ accessToken: active.accessToken,
308
+ refreshToken: active.refreshToken,
309
+ idToken: active.idToken,
310
+ sessionId: active.sessionId
311
+ };
312
+ }
313
+ async function getUser() {
314
+ const active = await getActiveAccount();
315
+ return active?.user ?? null;
316
+ }
317
+ async function getUserById(userId, applicationId) {
318
+ const qs = new URLSearchParams({ ...applicationId ? { applicationId } : {} });
319
+ try {
320
+ return await apiRequest(`/users/${userId}?${qs}`);
321
+ } catch {
322
+ return null;
323
+ }
324
+ }
325
+ async function getOrganizations() {
326
+ const cookieStore = await cookies();
327
+ const currentOrgId = cookieStore.get(cn.activeOrganizationId)?.value ?? null;
328
+ try {
329
+ const data = await apiRequest("/organizations");
330
+ const organizations = Array.isArray(data) ? data : data.organizations;
331
+ return { organizations, currentOrgId };
332
+ } catch {
333
+ return { organizations: [], currentOrgId };
334
+ }
335
+ }
336
+ function clearPublicTokenCookie(cookieStore) {
337
+ if (cookieStore.get(cn.tokenPub)?.value) {
338
+ cookieStore.set(cn.tokenPub, "", { httpOnly: false, maxAge: 0, path: "/" });
339
+ }
340
+ }
341
+ async function saveAccountToList(tokens) {
342
+ const account = parseAccountFromJwt(tokens);
343
+ if (!account) return;
344
+ const cookieStore = await cookies();
345
+ const opts = { ...getCookieOptions(config), maxAge: 60 * 60 * 24 * 30 };
346
+ const count = await getCount();
347
+ let targetSlot = 0;
348
+ for (let i = 1; i <= count; i++) {
349
+ const existing = readSlot(cookieStore, i);
350
+ if (existing?.id === account.id) {
351
+ targetSlot = i;
352
+ break;
353
+ }
354
+ }
355
+ if (targetSlot === 0) {
356
+ if (count >= MAX_ACCOUNTS) return;
357
+ targetSlot = count + 1;
358
+ cookieStore.set(cn.count, String(targetSlot), opts);
359
+ }
360
+ writeSlot(cookieStore, targetSlot, account);
361
+ cookieStore.set(cn.active, String(targetSlot), opts);
362
+ clearPublicTokenCookie(cookieStore);
363
+ }
364
+ const setSessionCookies = saveAccountToList;
365
+ async function switchAccount(accountId) {
366
+ const [cookieStore, count] = await Promise.all([cookies(), getCount()]);
367
+ const opts = { ...getCookieOptions(config), maxAge: 60 * 60 * 24 * 30 };
368
+ for (let i = 1; i <= count; i++) {
369
+ const slot = readSlot(cookieStore, i);
370
+ if (slot && (slot.id === accountId || slot.sessionId === accountId)) {
371
+ cookieStore.set(cn.active, String(i), opts);
372
+ clearPublicTokenCookie(cookieStore);
373
+ return true;
374
+ }
375
+ }
376
+ return false;
377
+ }
378
+ async function removeActiveAccount() {
379
+ const [cookieStore, activeIdx, count] = await Promise.all([cookies(), getActiveIndex(), getCount()]);
380
+ const opts = { ...getCookieOptions(config), maxAge: 60 * 60 * 24 * 30 };
381
+ const clear = { ...getCookieOptions(config), maxAge: 0 };
382
+ if (count === 0) return;
383
+ const remaining = [];
384
+ for (let i = 1; i <= count; i++) {
385
+ if (i === activeIdx) continue;
386
+ const slot = readSlot(cookieStore, i);
387
+ if (slot) remaining.push(slot);
388
+ }
389
+ for (let i = 1; i <= count; i++) {
390
+ clearSlot(cookieStore, i);
391
+ }
392
+ if (remaining.length === 0) {
393
+ cookieStore.set(cn.count, "0", clear);
394
+ cookieStore.set(cn.active, "0", clear);
395
+ await clearPublicTokenCookie(cookieStore);
396
+ return;
397
+ }
398
+ for (let i = 0; i < remaining.length; i++) {
399
+ await writeSlot(cookieStore, i + 1, remaining[i]);
400
+ }
401
+ cookieStore.set(cn.count, String(remaining.length), opts);
402
+ cookieStore.set(cn.active, "1", opts);
403
+ clearPublicTokenCookie(cookieStore);
404
+ }
405
+ async function clearAllSessionCookies() {
406
+ const [cookieStore, count] = await Promise.all([cookies(), getCount()]);
407
+ const clear = { ...getCookieOptions(config), maxAge: 0 };
408
+ for (let i = 1; i <= count; i++) {
409
+ clearSlot(cookieStore, i);
410
+ }
411
+ cookieStore.set(cn.count, "0", clear);
412
+ cookieStore.set(cn.active, "0", clear);
413
+ clearPublicTokenCookie(cookieStore);
414
+ }
415
+ async function tryRefreshTokens() {
416
+ const refreshToken = await getRefreshToken();
417
+ if (!refreshToken || !config.clientId) return null;
418
+ const basicAuth = Buffer.from(`${config.clientId}:${config.clientSecret ?? ""}`).toString("base64");
419
+ const response = await fetch(createUrl(config.issuer, "/auth/token"), {
420
+ method: "POST",
421
+ headers: {
422
+ "content-type": "application/x-www-form-urlencoded",
423
+ authorization: `Basic ${basicAuth}`
424
+ },
425
+ body: new URLSearchParams({
426
+ grant_type: "refresh_token",
427
+ refresh_token: refreshToken
428
+ }),
429
+ cache: "no-store"
430
+ });
431
+ if (!response.ok) return null;
432
+ const data = await response.json().catch(() => null);
433
+ if (!data) return null;
434
+ const tokens = tokenFromResponse(data);
435
+ if (!tokens.accessToken) return null;
436
+ await saveAccountToList(tokens);
437
+ return tokens.accessToken;
438
+ }
439
+ async function apiRequest(path, init = {}) {
440
+ const accessToken = await getAccessToken();
441
+ const requestHeaders = new Headers(init.headers);
442
+ if (accessToken) requestHeaders.set("authorization", `Bearer ${accessToken}`);
443
+ if (init.body !== void 0 && !requestHeaders.has("content-type")) {
444
+ requestHeaders.set("content-type", "application/json");
445
+ }
446
+ const url = new URL(createUrl(config.issuer, path));
447
+ applyClientCredentials(url, config);
448
+ const response = await fetch(url, {
449
+ ...init,
450
+ headers: requestHeaders,
451
+ cache: "no-store"
452
+ });
453
+ if (response.status === 401) {
454
+ const newAccessToken = await tryRefreshTokens();
455
+ if (!newAccessToken) {
456
+ await clearAllSessionCookies().catch(() => {
457
+ });
458
+ throw new GeekappsSessionExpiredError();
459
+ }
460
+ if (newAccessToken) {
461
+ const retryHeaders = new Headers(init.headers);
462
+ retryHeaders.set("authorization", `Bearer ${newAccessToken}`);
463
+ if (init.body !== void 0 && !retryHeaders.has("content-type")) {
464
+ retryHeaders.set("content-type", "application/json");
465
+ }
466
+ const retryUrl = new URL(createUrl(config.issuer, path));
467
+ applyClientCredentials(retryUrl, config);
468
+ const retryResponse = await fetch(retryUrl, {
469
+ ...init,
470
+ headers: retryHeaders,
471
+ cache: "no-store"
472
+ });
473
+ if (!retryResponse.ok) {
474
+ const error = await retryResponse.json().catch(() => null);
475
+ throw new Error(error?.error_description ?? error?.error ?? error?.message ?? `Auth API error ${retryResponse.status}`);
476
+ }
477
+ if (retryResponse.status === 204) return null;
478
+ return await retryResponse.json();
479
+ }
480
+ }
481
+ if (!response.ok) {
482
+ const error = await response.json().catch(() => null);
483
+ throw new Error(error?.error_description ?? error?.error ?? error?.message ?? `Auth API error ${response.status}`);
484
+ }
485
+ if (response.status === 204) return null;
486
+ return await response.json();
487
+ }
488
+ function createApiClient() {
489
+ return {
490
+ get: (path) => apiRequest(path),
491
+ post: (path, body) => apiRequest(path, { method: "POST", body: body === void 0 ? void 0 : JSON.stringify(body) }),
492
+ patch: (path, body) => apiRequest(path, { method: "PATCH", body: body === void 0 ? void 0 : JSON.stringify(body) }),
493
+ put: (path, body) => apiRequest(path, { method: "PUT", body: body === void 0 ? void 0 : JSON.stringify(body) }),
494
+ delete: (path) => apiRequest(path, { method: "DELETE" })
495
+ };
496
+ }
497
+ async function requireUser(options = {}) {
498
+ const accessToken = await getAccessToken();
499
+ if (!accessToken) {
500
+ redirect(options.redirectTo ?? "/");
501
+ }
502
+ return accessToken;
503
+ }
504
+ function escapeHtml(str) {
505
+ return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
506
+ }
507
+ function sanitizeRedirectTo(value) {
508
+ const raw = value ?? "/";
509
+ if (raw.startsWith(config.basePath)) return "/";
510
+ if (raw.startsWith("/") && !raw.startsWith("//")) return raw;
511
+ return "/";
512
+ }
513
+ async function createAuthorizeRedirect(request, mode) {
514
+ const redirectTo = sanitizeRedirectTo(request.nextUrl.searchParams.get("redirectTo") ?? config.afterSignInRedirectTo);
515
+ const flowId = request.nextUrl.searchParams.get("flow_id");
516
+ const url = new URL(createUrl(config.issuer, mode === "google" ? "/auth/google" : "/authorize"));
517
+ if (!config.clientId) {
518
+ return NextResponse.json(
519
+ { error: "missing_client_id", message: "Missing GEEKAPPS_AUTH_CLIENT_ID or clientId." },
520
+ { status: 500 }
521
+ );
522
+ }
523
+ url.searchParams.set("client_id", config.clientId);
524
+ url.searchParams.set("redirect_uri", resolveRedirectUri(config, request));
525
+ url.searchParams.set("response_type", "code");
526
+ const scopes = config.scopes ?? ["openid", "profile", "email"];
527
+ if (!scopes.includes("offline_access")) scopes.push("offline_access");
528
+ url.searchParams.set("scope", scopes.join(" "));
529
+ url.searchParams.set("state", Buffer.from(JSON.stringify({ redirectTo, mode })).toString("base64url"));
530
+ if (mode === "register") url.searchParams.set("screen_hint", "signup");
531
+ if (mode === "add-account") {
532
+ url.searchParams.set("prompt", "login");
533
+ } else {
534
+ const prompt = request.nextUrl.searchParams.get("prompt");
535
+ const silentLogin = request.nextUrl.searchParams.get("silentLogin");
536
+ const resolvedPrompt = prompt ?? (silentLogin === "false" ? "login" : null);
537
+ if (resolvedPrompt) url.searchParams.set("prompt", resolvedPrompt);
538
+ }
539
+ if (flowId) url.searchParams.set("flow_id", flowId);
540
+ const codeVerifier = generateCodeVerifier();
541
+ url.searchParams.set("code_challenge", createCodeChallenge(codeVerifier));
542
+ url.searchParams.set("code_challenge_method", "S256");
543
+ const response = NextResponse.redirect(url);
544
+ const cn2 = getCookieNames(config.cookiePrefix);
545
+ response.cookies.set(cn2.pkceVerifier, codeVerifier, {
546
+ ...getCookieOptions(config),
547
+ maxAge: 60 * 10
548
+ });
549
+ return response;
550
+ }
551
+ async function exchangeCode(request) {
552
+ const code = request.nextUrl.searchParams.get("code");
553
+ const state = request.nextUrl.searchParams.get("state");
554
+ const origin = requestOrigin(request);
555
+ if (!code) {
556
+ const err = request.nextUrl.searchParams.get("error") ?? "missing_code";
557
+ const desc = request.nextUrl.searchParams.get("error_description") ?? request.nextUrl.searchParams.get("error_message") ?? "No authorization code received";
558
+ return NextResponse.redirect(new URL(`${config.basePath}/callback?error=${encodeURIComponent(err)}&error_description=${encodeURIComponent(desc)}`, origin));
559
+ }
560
+ if (!config.clientId) {
561
+ return NextResponse.redirect(new URL(`${config.basePath}/callback?error=missing_client_id&error_description=Auth+client+is+not+configured`, origin));
562
+ }
563
+ const basicAuth = Buffer.from(
564
+ `${config.clientId}:${config.clientSecret ?? ""}`
565
+ ).toString("base64");
566
+ const cn2 = getCookieNames(config.cookiePrefix);
567
+ const cookieStore = await cookies();
568
+ const codeVerifier = cookieStore.get(cn2.pkceVerifier)?.value;
569
+ cookieStore.set(cn2.pkceVerifier, "", { ...getCookieOptions(config), maxAge: 0 });
570
+ const tokenBody = new URLSearchParams({
571
+ grant_type: "authorization_code",
572
+ code,
573
+ redirect_uri: resolveRedirectUri(config, request)
574
+ });
575
+ if (codeVerifier) tokenBody.set("code_verifier", codeVerifier);
576
+ const response = await fetch(createUrl(config.issuer, "/auth/token"), {
577
+ method: "POST",
578
+ headers: {
579
+ "content-type": "application/x-www-form-urlencoded",
580
+ authorization: `Basic ${basicAuth}`
581
+ },
582
+ body: tokenBody,
583
+ cache: "no-store"
584
+ });
585
+ const data = await response.json().catch(() => ({}));
586
+ if (!response.ok) {
587
+ const error = encodeURIComponent(data.error ?? "token_exchange_failed");
588
+ const desc = encodeURIComponent(data.error_description ?? `Token exchange failed with status ${response.status}`);
589
+ return NextResponse.redirect(new URL(`${config.basePath}/callback?error=${error}&error_description=${desc}`, origin));
590
+ }
591
+ await saveAccountToList(tokenFromResponse(data));
592
+ let redirectTo = sanitizeRedirectTo(config.afterSignInRedirectTo);
593
+ if (state) {
594
+ try {
595
+ const parsed = JSON.parse(Buffer.from(state, "base64url").toString("utf8"));
596
+ redirectTo = sanitizeRedirectTo(parsed.redirectTo);
597
+ } catch {
598
+ }
599
+ }
600
+ return NextResponse.redirect(new URL(redirectTo, origin));
601
+ }
602
+ function mapLocalPathToApiPath(route) {
603
+ if (route === "/me") return "/auth/me";
604
+ if (route === "/accounts/switch") return "/auth/accounts/switch";
605
+ if (route === "/organizations/active") return "/auth/organizations/active";
606
+ if (route === "/permissions/check") return "/auth/permissions/check";
607
+ if (route === "/login/password") return "/auth/login/password";
608
+ if (route === "/register/password") return "/auth/register/password";
609
+ if (route === "/passwordless/link") return "/auth/passwordless/link";
610
+ return route;
611
+ }
612
+ async function handleAccountsLocal() {
613
+ const [accounts, activeIdx] = await Promise.all([getAccountsList(), getActiveIndex()]);
614
+ return NextResponse.json({
615
+ accounts: accounts.map((a, i) => ({
616
+ id: a.id,
617
+ sessionId: a.sessionId ?? a.id,
618
+ isActive: i + 1 === activeIdx,
619
+ user: {
620
+ id: a.user.sub,
621
+ name: a.user.name ?? null,
622
+ email: a.user.email ?? null,
623
+ imageUrl: a.user.picture ?? null,
624
+ phone: a.user.phone ?? null
625
+ }
626
+ }))
627
+ });
628
+ }
629
+ async function handleOrganizationsLocal() {
630
+ const cookieStore = await cookies();
631
+ const activeOrgId = cookieStore.get(cn.activeOrganizationId)?.value ?? null;
632
+ try {
633
+ const data = await apiRequest("/organizations");
634
+ const organizations = Array.isArray(data) ? data : data.organizations;
635
+ return NextResponse.json({ organizations, currentOrgId: activeOrgId });
636
+ } catch {
637
+ return NextResponse.json({ organizations: [], currentOrgId: activeOrgId });
638
+ }
639
+ }
640
+ async function handleSwitchAccountLocal(request) {
641
+ const body = await readBody(request);
642
+ const targetId = body && typeof body === "object" ? String(
643
+ body.accountId ?? body.sessionId ?? ""
644
+ ) : "";
645
+ if (!targetId) {
646
+ return NextResponse.json({ error: "accountId required" }, { status: 400 });
647
+ }
648
+ const switched = await switchAccount(targetId);
649
+ if (!switched) {
650
+ return NextResponse.json({ error: "Account not found" }, { status: 404 });
651
+ }
652
+ return NextResponse.json({ ok: true });
653
+ }
654
+ async function proxy(request, route) {
655
+ const accessToken = await getAccessToken();
656
+ const body = ["GET", "HEAD"].includes(request.method) ? void 0 : await readBody(request);
657
+ const url = new URL(appendSearchParams(createUrl(config.issuer, mapLocalPathToApiPath(route)), request));
658
+ applyClientCredentials(url, config);
659
+ const requestHeaders = new Headers();
660
+ if (accessToken) requestHeaders.set("authorization", `Bearer ${accessToken}`);
661
+ if (body !== void 0) requestHeaders.set("content-type", "application/json");
662
+ let response = await fetch(url, {
663
+ method: request.method,
664
+ headers: requestHeaders,
665
+ body: body === void 0 ? void 0 : typeof body === "string" ? body : JSON.stringify(body),
666
+ cache: "no-store"
667
+ });
668
+ if (response.status === 401) {
669
+ const refreshed = await tryRefreshTokens();
670
+ if (refreshed) {
671
+ requestHeaders.set("authorization", `Bearer ${refreshed}`);
672
+ response = await fetch(url, {
673
+ method: request.method,
674
+ headers: requestHeaders,
675
+ body: body === void 0 ? void 0 : typeof body === "string" ? body : JSON.stringify(body),
676
+ cache: "no-store"
677
+ });
678
+ } else {
679
+ await clearAllSessionCookies().catch(() => {
680
+ });
681
+ return NextResponse.json(
682
+ { error: "session_expired" },
683
+ { status: 401 }
684
+ );
685
+ }
686
+ }
687
+ const data = isJsonResponse(response) ? await response.json().catch(() => null) : null;
688
+ if (data) {
689
+ const tokens = tokenFromResponse(data);
690
+ if (tokens.accessToken || tokens.refreshToken || tokens.idToken || tokens.sessionId) {
691
+ await saveAccountToList(tokens);
692
+ }
693
+ return NextResponse.json(data, { status: response.status });
694
+ }
695
+ return responseFromApi(response);
696
+ }
697
+ async function handle(request) {
698
+ const route = routeFromRequest(request, config.basePath);
699
+ if (request.method === "GET" && route === "/sign-in") return createAuthorizeRedirect(request, "login");
700
+ if (request.method === "GET" && route === "/sign-up") return createAuthorizeRedirect(request, "register");
701
+ if (request.method === "GET" && route === "/add-account") return createAuthorizeRedirect(request, "add-account");
702
+ if (request.method === "GET" && route === "/login/google") return createAuthorizeRedirect(request, "google");
703
+ if (request.method === "GET" && route === "/callback") {
704
+ const error = request.nextUrl.searchParams.get("error");
705
+ if (error) {
706
+ const description = request.nextUrl.searchParams.get("error_description") ?? request.nextUrl.searchParams.get("error_message") ?? "An authentication error occurred.";
707
+ return new NextResponse(
708
+ `<!doctype html><html><head><meta charset="utf-8"><title>Authentication Error</title></head><body style="font-family:sans-serif;max-width:480px;margin:80px auto;padding:0 16px"><h2>Authentication Error</h2><p><strong>${escapeHtml(error)}</strong></p><p>${escapeHtml(description)}</p><a href="/">Go back</a></body></html>`,
709
+ { status: 400, headers: { "content-type": "text/html; charset=utf-8" } }
710
+ );
711
+ }
712
+ return exchangeCode(request);
713
+ }
714
+ if (request.method === "GET" && route === "/token") {
715
+ const accessToken = await getAccessToken();
716
+ if (!accessToken) return NextResponse.json({ error: "missing_token" }, { status: 401 });
717
+ return NextResponse.json({ accessToken });
718
+ }
719
+ if (request.method === "GET" && route === "/accounts") return handleAccountsLocal();
720
+ if (request.method === "POST" && route === "/accounts/switch") return handleSwitchAccountLocal(request);
721
+ if (request.method === "GET" && route === "/organizations") return handleOrganizationsLocal();
722
+ if (request.method === "POST" && route === "/organizations") {
723
+ const accessToken = await getAccessToken();
724
+ const body = await readBody(request);
725
+ if (body && accessToken && !body.applicationId) {
726
+ const claims = decodeJwtPayload(accessToken);
727
+ const applicationId = (typeof claims?.application_id === "string" ? claims.application_id : null) ?? (typeof claims?.client_id === "string" ? claims.client_id : null) ?? (typeof claims?.applicationId === "string" ? claims.applicationId : null);
728
+ if (applicationId) body.applicationId = applicationId;
729
+ }
730
+ const orgUrl = new URL(createUrl(config.issuer, "/organizations"));
731
+ applyClientCredentials(orgUrl, config);
732
+ const requestHeaders = new Headers();
733
+ if (accessToken) requestHeaders.set("authorization", `Bearer ${accessToken}`);
734
+ if (body !== void 0) requestHeaders.set("content-type", "application/json");
735
+ const response = await fetch(orgUrl, {
736
+ method: "POST",
737
+ headers: requestHeaders,
738
+ body: JSON.stringify(body),
739
+ cache: "no-store"
740
+ });
741
+ return responseFromApi(response);
742
+ }
743
+ if (request.method === "POST" && route === "/sign-out") {
744
+ await apiRequest("/auth/sign-out", { method: "POST" }).catch(() => null);
745
+ await removeActiveAccount();
746
+ return NextResponse.json({ ok: true });
747
+ }
748
+ if (request.method === "POST" && route === "/sign-out-all") {
749
+ await apiRequest("/auth/sign-out-all", { method: "POST" }).catch(() => null);
750
+ await clearAllSessionCookies();
751
+ return NextResponse.json({ ok: true });
752
+ }
753
+ if (request.method === "POST" && route === "/organizations/active") {
754
+ const body = await readBody(request);
755
+ const organizationId = body && typeof body === "object" && "organizationId" in body ? body.organizationId : null;
756
+ const response = NextResponse.json({ ok: true });
757
+ if (typeof organizationId === "string") {
758
+ const cookieOpts = {
759
+ httpOnly: true,
760
+ sameSite: "lax",
761
+ path: "/",
762
+ secure: process.env.NODE_ENV === "production",
763
+ maxAge: 60 * 60 * 24 * 7
764
+ };
765
+ response.cookies.set(cn.activeOrganizationId, organizationId, cookieOpts);
766
+ }
767
+ return response;
768
+ }
769
+ return proxy(request, route);
770
+ }
771
+ return {
772
+ handlers: {
773
+ GET: handle,
774
+ POST: handle,
775
+ PATCH: handle,
776
+ DELETE: handle
777
+ },
778
+ GetSession: getSession,
779
+ GetUser: getUser,
780
+ GetUserById: getUserById,
781
+ GetOrganizations: getOrganizations,
782
+ SaveSession: saveAccountToList,
783
+ GetAccessToken: getAccessToken,
784
+ GetRefreshToken: getRefreshToken,
785
+ IsAuthenticated: async () => await getAccessToken() !== null,
786
+ TryRefreshTokens: tryRefreshTokens,
787
+ RequireUser: requireUser,
788
+ CreateAuthApiClient: createApiClient,
789
+ cookiePrefix: config.cookiePrefix,
790
+ VerifyToken: (token) => verifyToken(token, config.issuer),
791
+ SignInUrl: async (redirectTo) => {
792
+ const configured = env("GEEKAPPS_APP_URL");
793
+ if (!configured) throw new Error("Cannot determine app origin. Set GEEKAPPS_APP_URL.");
794
+ const origin = configured.replace(/\/$/, "");
795
+ const url = new URL(`${origin}${config.basePath}/sign-in`);
796
+ if (redirectTo) url.searchParams.set("redirectTo", redirectTo);
797
+ return url.toString();
798
+ }
799
+ };
800
+ }
8
801
 
9
802
  // src/connections.ts
10
803
  var AppConnectionError = class extends Error {
@@ -19,16 +812,16 @@ var AppConnectionError = class extends Error {
19
812
  this.body = body;
20
813
  }
21
814
  };
22
- function env(name) {
815
+ function env2(name) {
23
816
  const value = process.env[name];
24
817
  return value && value.trim().length > 0 ? value : void 0;
25
818
  }
26
819
  function resolveConfig(config) {
27
- const issuer = (config.issuer ?? env("GEEKAPPS_AUTH_SERVER_ISSUER") ?? env("GEEKAPPS_AUTH_SERVER_URL") ?? GEEKAPPS_AUTH_SERVER_URL).replace(/\/$/, "");
820
+ const issuer = (config.issuer ?? env2("GEEKAPPS_AUTH_SERVER_ISSUER") ?? env2("GEEKAPPS_AUTH_SERVER_URL") ?? GEEKAPPS_AUTH_SERVER_URL).replace(/\/$/, "");
28
821
  return {
29
822
  issuer,
30
- clientId: config.clientId ?? env("GEEKAPPS_AUTH_CLIENT_ID") ?? "",
31
- clientSecret: config.clientSecret ?? env("GEEKAPPS_AUTH_CLIENT_SECRET") ?? ""
823
+ clientId: config.clientId ?? env2("GEEKAPPS_AUTH_CLIENT_ID") ?? "",
824
+ clientSecret: config.clientSecret ?? env2("GEEKAPPS_AUTH_CLIENT_SECRET") ?? ""
32
825
  };
33
826
  }
34
827
  async function callAppConnection(params) {
@@ -38,12 +831,22 @@ async function callAppConnection(params) {
38
831
  "App Connections: defina GEEKAPPS_AUTH_CLIENT_ID e GEEKAPPS_AUTH_CLIENT_SECRET \u2014 \xE9 com essas credenciais que o proxy identifica a app que est\xE1 chamando."
39
832
  );
40
833
  }
834
+ const userToken = params.userToken ?? await configureGeekappsAuth().GetAccessToken();
835
+ if (!userToken) {
836
+ throw new AppConnectionError(
837
+ "App Connections: n\xE3o h\xE1 usu\xE1rio logado \u2014 a conex\xE3o \xE9 do usu\xE1rio, a chamada precisa do access token dele.",
838
+ 401,
839
+ "user_token_required",
840
+ null
841
+ );
842
+ }
41
843
  const url = `${issuer}/connections/${encodeURIComponent(params.targetAppId)}/actions/${encodeURIComponent(params.actionKey)}`;
42
844
  const credentials = Buffer.from(`${clientId}:${clientSecret}`).toString("base64");
43
845
  const response = await fetch(url, {
44
846
  method: "POST",
45
847
  headers: {
46
848
  authorization: `Basic ${credentials}`,
849
+ "x-user-token": userToken,
47
850
  "content-type": "application/json"
48
851
  },
49
852
  body: JSON.stringify(params.payload === void 0 ? {} : { payload: params.payload }),
@@ -65,54 +868,193 @@ var _registry = /* @__PURE__ */ new Map();
65
868
  function registerConnections(entries) {
66
869
  for (const [name, entry] of Object.entries(entries)) _registry.set(name, entry);
67
870
  }
68
- function resolveEntry(appName, registry = _registry) {
69
- const entry = registry.get(appName);
70
- if (!entry) {
71
- throw new Error(
72
- `App Connections: app "${appName}" desconhecida. Rode "npx geekapps-connections" para gerar o cat\xE1logo e importe o arquivo gerado uma vez no boot da app.`
73
- );
871
+ var TYPES_MODULE = "@geekapps/auth-nextjs/connections";
872
+ var TYPES_FILE = "geekapps-connections.d.ts";
873
+ var REFRESH_MIN_INTERVAL_MS = 5e3;
874
+ var _remote = /* @__PURE__ */ new Map();
875
+ var _inflight = /* @__PURE__ */ new Map();
876
+ async function writeTypesInDev(model) {
877
+ if (process.env.NODE_ENV !== "development" || process.env.GEEKAPPS_CONNECTIONS_TYPES === "off") return;
878
+ try {
879
+ const { existsSync, readFileSync, writeFileSync } = await import("fs");
880
+ const { resolve } = await import("path");
881
+ const file = resolve(process.cwd(), TYPES_FILE);
882
+ const content = renderAugmentation(model, TYPES_MODULE);
883
+ if (existsSync(file) && readFileSync(file, "utf-8") === content) return;
884
+ writeFileSync(file, content, "utf-8");
885
+ } catch {
74
886
  }
75
- return entry;
76
887
  }
77
- function resolveActionKey(appName, actionName, registry = _registry) {
78
- const key = resolveEntry(appName, registry).actions[actionName];
79
- if (!key) {
888
+ async function loadRemoteRegistry(config, force = false) {
889
+ const { issuer, clientId, clientSecret } = resolveConfig(config);
890
+ if (!clientId || !clientSecret) {
80
891
  throw new Error(
81
- `App Connections: a\xE7\xE3o "${actionName}" n\xE3o existe em "${appName}", ou n\xE3o foi liberada na aprova\xE7\xE3o da conex\xE3o. Rode "npx geekapps-connections" para atualizar o cat\xE1logo.`
892
+ "App Connections: defina GEEKAPPS_AUTH_CLIENT_ID e GEEKAPPS_AUTH_CLIENT_SECRET \u2014 \xE9 com essas credenciais que o SDK descobre as apps conect\xE1veis."
82
893
  );
83
894
  }
84
- return key;
895
+ const cacheKey = `${issuer}|${clientId}`;
896
+ const ttl = process.env.NODE_ENV === "development" ? 3e4 : 5 * 6e4;
897
+ const cached = _remote.get(cacheKey);
898
+ const age = cached ? Date.now() - cached.at : Infinity;
899
+ if (cached && (force ? age < REFRESH_MIN_INTERVAL_MS : age < ttl)) return cached.registry;
900
+ const pending = _inflight.get(cacheKey);
901
+ if (pending) return pending;
902
+ const request = (async () => {
903
+ try {
904
+ const model = buildModel(await fetchRemoteCatalog({ issuer, clientId, clientSecret }));
905
+ const registry = new Map(Object.entries(toRegistry(model)));
906
+ _remote.set(cacheKey, { at: Date.now(), registry });
907
+ void writeTypesInDev(model);
908
+ return registry;
909
+ } catch (error) {
910
+ if (cached) return cached.registry;
911
+ throw new AppConnectionError(`App Connections: ${error.message}`, 502, "catalog_unavailable", null);
912
+ } finally {
913
+ _inflight.delete(cacheKey);
914
+ }
915
+ })();
916
+ _inflight.set(cacheKey, request);
917
+ return request;
918
+ }
919
+ async function resolveTarget(provider, appName, actionName) {
920
+ for (const force of [false, true]) {
921
+ const registry = await provider(force);
922
+ const entry = registry.get(appName);
923
+ const key = entry?.actions[actionName];
924
+ if (entry && key) return { appId: entry.appId, key };
925
+ }
926
+ throw new Error(
927
+ `App Connections: "${appName}.${actionName}" n\xE3o existe no cat\xE1logo. Confira se a app alvo cadastrou a a\xE7\xE3o (geekapps-actions sync).`
928
+ );
85
929
  }
86
- function createCatalog(config, registry = _registry) {
930
+ function createCatalog(config, provider) {
87
931
  return new Proxy(/* @__PURE__ */ Object.create(null), {
88
932
  get(_target, appName) {
89
933
  if (typeof appName !== "string") return void 0;
90
934
  return new Proxy(/* @__PURE__ */ Object.create(null), {
91
935
  get(_t, actionName) {
92
936
  if (typeof actionName !== "string") return void 0;
93
- return (payload) => callAppConnection({
94
- targetAppId: resolveEntry(appName, registry).appId,
95
- actionKey: resolveActionKey(appName, actionName, registry),
96
- payload,
97
- config
98
- });
937
+ return async (payload, options) => {
938
+ const target = await resolveTarget(provider, appName, actionName);
939
+ return callAppConnection({
940
+ targetAppId: target.appId,
941
+ actionKey: target.key,
942
+ payload,
943
+ userToken: options?.userToken,
944
+ config
945
+ });
946
+ };
99
947
  }
100
948
  });
101
949
  }
102
950
  });
103
951
  }
952
+ var _devRefresh = /* @__PURE__ */ new Set();
953
+ function startDevRefresh(config) {
954
+ if (process.env.NODE_ENV !== "development") return;
955
+ const { issuer, clientId, clientSecret } = resolveConfig(config);
956
+ if (!clientId || !clientSecret) return;
957
+ const key = `${issuer}|${clientId}`;
958
+ if (_devRefresh.has(key)) return;
959
+ _devRefresh.add(key);
960
+ const tick = () => void loadRemoteRegistry(config, true).catch(() => void 0);
961
+ tick();
962
+ setInterval(tick, 3e4).unref?.();
963
+ }
104
964
  function createAppConnections(config = {}) {
105
- return createCatalog(config);
965
+ startDevRefresh(config);
966
+ const provider = async (force) => {
967
+ const remote = await loadRemoteRegistry(config, force);
968
+ return new Map([..._registry, ...remote]);
969
+ };
970
+ return createCatalog(config, provider);
106
971
  }
107
972
  function defineAppConnections(catalog, config = {}) {
108
973
  const registry = new Map(Object.entries(catalog));
109
- return createCatalog(config, registry);
974
+ return createCatalog(config, async () => registry);
110
975
  }
111
976
  var appConnections = createAppConnections();
977
+ var CONNECT_COOKIES = {
978
+ verifier: "gk_conn_pkce",
979
+ state: "gk_conn_state",
980
+ returnTo: "gk_conn_return",
981
+ redirectUri: "gk_conn_redirect"
982
+ };
983
+ async function connectApp(params) {
984
+ const { issuer, clientId } = resolveConfig(params.config ?? {});
985
+ if (!clientId) {
986
+ throw new Error("App Connections: defina GEEKAPPS_AUTH_CLIENT_ID \u2014 \xE9 o client_id desta app, a que pede a conex\xE3o.");
987
+ }
988
+ if (params.actions.length === 0) {
989
+ throw new Error("App Connections: informe ao menos uma a\xE7\xE3o em `actions`.");
990
+ }
991
+ const codeVerifier = randomBytes2(64).toString("base64url");
992
+ const state = randomBytes2(24).toString("base64url");
993
+ const store = await cookies2();
994
+ const options = { httpOnly: true, secure: true, sameSite: "lax", path: "/", maxAge: 600 };
995
+ store.set(CONNECT_COOKIES.verifier, codeVerifier, options);
996
+ store.set(CONNECT_COOKIES.state, state, options);
997
+ store.set(CONNECT_COOKIES.returnTo, params.returnTo ?? "/", options);
998
+ store.set(CONNECT_COOKIES.redirectUri, params.redirectUri, options);
999
+ const url = new URL(`${issuer}/authorize`);
1000
+ url.searchParams.set("client_id", clientId);
1001
+ url.searchParams.set("redirect_uri", params.redirectUri);
1002
+ url.searchParams.set("response_type", "code");
1003
+ url.searchParams.set("scope", `openid ${params.actions.join(" ")}`);
1004
+ url.searchParams.set("state", state);
1005
+ url.searchParams.set("code_challenge", createHash2("sha256").update(codeVerifier).digest("base64url"));
1006
+ url.searchParams.set("code_challenge_method", "S256");
1007
+ url.searchParams.set("target_app_id", params.targetAppId);
1008
+ if (params.displayName) url.searchParams.set("connection_display_name", params.displayName);
1009
+ redirect2(url.toString());
1010
+ }
1011
+ async function completeAppConnection(params) {
1012
+ const { issuer, clientId, clientSecret } = resolveConfig(params.config ?? {});
1013
+ if (!clientId || !clientSecret) {
1014
+ throw new Error("App Connections: defina GEEKAPPS_AUTH_CLIENT_ID e GEEKAPPS_AUTH_CLIENT_SECRET.");
1015
+ }
1016
+ const store = await cookies2();
1017
+ const read = (name) => store.get(name)?.value;
1018
+ const expectedState = read(CONNECT_COOKIES.state);
1019
+ const codeVerifier = read(CONNECT_COOKIES.verifier);
1020
+ const redirectUri = read(CONNECT_COOKIES.redirectUri);
1021
+ const returnTo = read(CONNECT_COOKIES.returnTo) ?? "/";
1022
+ for (const name of Object.values(CONNECT_COOKIES)) store.set(name, "", { maxAge: 0, path: "/" });
1023
+ if (!expectedState || !codeVerifier || !redirectUri || params.state !== expectedState) {
1024
+ throw new AppConnectionError("App Connections: estado inv\xE1lido \u2014 inicie a conex\xE3o novamente.", 400, "invalid_state", null);
1025
+ }
1026
+ const response = await fetch(`${issuer}/oauth/token`, {
1027
+ method: "POST",
1028
+ headers: {
1029
+ authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString("base64")}`,
1030
+ "content-type": "application/x-www-form-urlencoded"
1031
+ },
1032
+ body: new URLSearchParams({
1033
+ grant_type: "authorization_code",
1034
+ code: params.code,
1035
+ redirect_uri: redirectUri,
1036
+ code_verifier: codeVerifier
1037
+ }),
1038
+ cache: "no-store"
1039
+ });
1040
+ if (!response.ok) {
1041
+ const body = await response.json().catch(() => null);
1042
+ const code = body && typeof body.error === "string" ? body.error : void 0;
1043
+ throw new AppConnectionError(
1044
+ `App Connections: n\xE3o foi poss\xEDvel concluir a conex\xE3o (${response.status}${code ? ` ${code}` : ""})`,
1045
+ response.status,
1046
+ code,
1047
+ body
1048
+ );
1049
+ }
1050
+ return { returnTo };
1051
+ }
112
1052
  export {
113
1053
  AppConnectionError,
114
1054
  appConnections,
115
1055
  callAppConnection,
1056
+ completeAppConnection,
1057
+ connectApp,
116
1058
  createAppConnections,
117
1059
  defineAppConnections,
118
1060
  registerConnections