@zea.cl/auth 0.1.6

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.
@@ -0,0 +1,947 @@
1
+ import { useState, useEffect, useRef, useCallback } from 'react';
2
+ import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
3
+
4
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
5
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
6
+ }) : x)(function(x) {
7
+ if (typeof require !== "undefined") return require.apply(this, arguments);
8
+ throw Error('Dynamic require of "' + x + '" is not supported');
9
+ });
10
+
11
+ // src/client/auth/OAuth2.ts
12
+ var OAuth2 = class {
13
+ constructor(config) {
14
+ this.config = config;
15
+ }
16
+ config;
17
+ /**
18
+ * Generate OAuth2 authorization URL for user login
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const authUrl = thalamus.auth.getAuthorizationUrl({
23
+ * scope: ['openid', 'profile', 'email'],
24
+ * state: 'random-state-string'
25
+ * })
26
+ * // Redirect user to authUrl
27
+ * ```
28
+ */
29
+ getAuthorizationUrl(options) {
30
+ const {
31
+ scope = this.config.defaultScopes || ["openid", "profile", "email"],
32
+ state = this.generateState(),
33
+ responseType = "code",
34
+ codeChallenge,
35
+ codeChallengeMethod
36
+ } = options || {};
37
+ const params = new URLSearchParams({
38
+ response_type: responseType,
39
+ client_id: this.config.clientId,
40
+ redirect_uri: this.config.redirectUri,
41
+ scope: Array.isArray(scope) ? scope.join(" ") : scope,
42
+ state
43
+ });
44
+ if (codeChallenge) {
45
+ params.set("code_challenge", codeChallenge);
46
+ params.set("code_challenge_method", codeChallengeMethod || "S256");
47
+ }
48
+ return `${this.config.baseUrl}/oauth/authorize?${params.toString()}`;
49
+ }
50
+ /**
51
+ * Exchange authorization code for access token
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * const tokens = await thalamus.auth.exchangeCode('authorization_code_here')
56
+ * console.log(tokens.access_token)
57
+ * ```
58
+ */
59
+ async exchangeCode(codeOrOptions) {
60
+ const code = typeof codeOrOptions === "string" ? codeOrOptions : codeOrOptions.code;
61
+ const codeVerifier = typeof codeOrOptions === "string" ? void 0 : codeOrOptions.codeVerifier;
62
+ const body = {
63
+ grant_type: "authorization_code",
64
+ code,
65
+ client_id: this.config.clientId,
66
+ client_secret: this.config.clientSecret,
67
+ redirect_uri: this.config.redirectUri
68
+ };
69
+ if (codeVerifier) {
70
+ body.code_verifier = codeVerifier;
71
+ }
72
+ return this.requestToken(body);
73
+ }
74
+ /**
75
+ * Get access token using client credentials (M2M)
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * const tokens = await thalamus.auth.getClientCredentialsToken({
80
+ * scope: ['api:read', 'api:write']
81
+ * })
82
+ * ```
83
+ */
84
+ async getClientCredentialsToken(options) {
85
+ const { scope = this.config.defaultScopes || [] } = options || {};
86
+ const body = {
87
+ grant_type: "client_credentials",
88
+ client_id: this.config.clientId,
89
+ client_secret: this.config.clientSecret
90
+ };
91
+ if (scope.length > 0) {
92
+ body.scope = Array.isArray(scope) ? scope.join(" ") : scope;
93
+ }
94
+ return this.requestToken(body);
95
+ }
96
+ /**
97
+ * Refresh access token using refresh token
98
+ *
99
+ * @example
100
+ * ```ts
101
+ * const newTokens = await thalamus.auth.refreshToken({
102
+ * refreshToken: 'rt_...'
103
+ * })
104
+ * ```
105
+ */
106
+ async refreshToken(options) {
107
+ const body = {
108
+ grant_type: "refresh_token",
109
+ refresh_token: options.refreshToken,
110
+ client_id: this.config.clientId,
111
+ client_secret: this.config.clientSecret
112
+ };
113
+ return this.requestToken(body);
114
+ }
115
+ /**
116
+ * Revoke a token (access or refresh token)
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * await thalamus.auth.revokeToken('at_...')
121
+ * ```
122
+ */
123
+ async revokeToken(token, tokenTypeHint) {
124
+ const body = {
125
+ token
126
+ };
127
+ if (tokenTypeHint) {
128
+ body.token_type_hint = tokenTypeHint;
129
+ }
130
+ const response = await fetch(`${this.config.baseUrl}/oauth/revoke`, {
131
+ method: "POST",
132
+ headers: {
133
+ "Content-Type": "application/json"
134
+ },
135
+ body: JSON.stringify(body)
136
+ });
137
+ if (!response.ok) {
138
+ throw await this.handleError(response);
139
+ }
140
+ }
141
+ /**
142
+ * Generate random state for CSRF protection
143
+ */
144
+ generateState(length = 32) {
145
+ const array = new Uint8Array(32);
146
+ if (typeof crypto !== "undefined" && crypto.getRandomValues) {
147
+ crypto.getRandomValues(array);
148
+ } else {
149
+ const cryptoModule = __require("crypto");
150
+ cryptoModule.randomFillSync(array);
151
+ }
152
+ return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
153
+ }
154
+ /**
155
+ * Make token request to /oauth/token
156
+ */
157
+ async requestToken(body) {
158
+ const response = await fetch(`${this.config.baseUrl}/oauth/token`, {
159
+ method: "POST",
160
+ headers: {
161
+ "Content-Type": "application/json"
162
+ },
163
+ body: JSON.stringify(body)
164
+ });
165
+ if (!response.ok) {
166
+ throw await this.handleError(response);
167
+ }
168
+ return response.json();
169
+ }
170
+ /**
171
+ * Handle API errors
172
+ */
173
+ async handleError(response) {
174
+ let errorData = {};
175
+ try {
176
+ errorData = await response.json();
177
+ } catch {
178
+ }
179
+ const error = new Error(
180
+ errorData.error_description || errorData.message || `HTTP ${response.status}`
181
+ );
182
+ error.statusCode = response.status;
183
+ error.error = errorData.error;
184
+ error.error_description = errorData.error_description;
185
+ return error;
186
+ }
187
+ };
188
+
189
+ // src/client/tokens/TokenManager.ts
190
+ var TokenManager = class {
191
+ constructor(config) {
192
+ this.config = config;
193
+ }
194
+ config;
195
+ /**
196
+ * Introspect a token to check if it's valid and get metadata
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const tokenInfo = await thalamus.tokens.introspect('at_...')
201
+ * if (tokenInfo.active) {
202
+ * console.log(tokenInfo.user_id)
203
+ * console.log(tokenInfo.scope)
204
+ * }
205
+ * ```
206
+ */
207
+ async introspect(token) {
208
+ const response = await fetch(`${this.config.baseUrl}/oauth/introspect`, {
209
+ method: "POST",
210
+ headers: {
211
+ "Content-Type": "application/json"
212
+ },
213
+ body: JSON.stringify({ token })
214
+ });
215
+ if (!response.ok) {
216
+ throw await this.handleError(response);
217
+ }
218
+ return response.json();
219
+ }
220
+ /**
221
+ * Get user information from OpenID Connect userinfo endpoint
222
+ *
223
+ * @example
224
+ * ```ts
225
+ * const user = await thalamus.tokens.getUserInfo('at_...')
226
+ * console.log(user.email)
227
+ * console.log(user.name)
228
+ * ```
229
+ */
230
+ async getUserInfo(accessToken) {
231
+ const response = await fetch(`${this.config.baseUrl}/oauth/userinfo`, {
232
+ method: "GET",
233
+ headers: {
234
+ Authorization: `Bearer ${accessToken}`
235
+ }
236
+ });
237
+ if (!response.ok) {
238
+ throw await this.handleError(response);
239
+ }
240
+ return response.json();
241
+ }
242
+ /**
243
+ * Validate token and return true if active, false otherwise
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const isValid = await thalamus.tokens.validate('at_...')
248
+ * if (isValid) {
249
+ * // Token is valid
250
+ * }
251
+ * ```
252
+ */
253
+ async validate(token) {
254
+ try {
255
+ const result = await this.introspect(token);
256
+ return result.active === true;
257
+ } catch {
258
+ return false;
259
+ }
260
+ }
261
+ /**
262
+ * Handle API errors
263
+ */
264
+ async handleError(response) {
265
+ let errorData = {};
266
+ try {
267
+ errorData = await response.json();
268
+ } catch {
269
+ }
270
+ const error = new Error(
271
+ errorData.error_description || errorData.message || `HTTP ${response.status}`
272
+ );
273
+ error.statusCode = response.status;
274
+ error.error = errorData.error;
275
+ error.error_description = errorData.error_description;
276
+ return error;
277
+ }
278
+ };
279
+
280
+ // src/client/admin/AdminAPI.ts
281
+ var AdminAPI = class {
282
+ constructor(config) {
283
+ this.config = config;
284
+ }
285
+ config;
286
+ get baseUrl() {
287
+ return this.config.baseUrl;
288
+ }
289
+ // ── Users ────────────────────────────────────────────────────────────────
290
+ /** List all users */
291
+ async listUsers() {
292
+ const res = await this.request(`${this.baseUrl}/api/users`);
293
+ const json = await res.json();
294
+ return json.data ?? json;
295
+ }
296
+ /** List all agents (users with is_agent === true) */
297
+ async listAgents() {
298
+ const users = await this.listUsers();
299
+ return users.filter((u) => u.is_agent === true);
300
+ }
301
+ /** Get a single user */
302
+ async getUser(id) {
303
+ const res = await this.request(`${this.baseUrl}/api/users/${id}`);
304
+ const json = await res.json();
305
+ return json.data ?? json;
306
+ }
307
+ /** Add a member to an organization */
308
+ async addOrgMember(orgId, userId) {
309
+ const res = await this.request(`${this.baseUrl}/api/organizations/${orgId}/members`, {
310
+ method: "POST",
311
+ body: JSON.stringify({ user_id: userId })
312
+ });
313
+ return res.json();
314
+ }
315
+ /** Update a user (only name and agent_config are writable) */
316
+ async updateUser(id, data) {
317
+ const res = await this.request(`${this.baseUrl}/api/users/${id}`, {
318
+ method: "PATCH",
319
+ body: JSON.stringify({ user: data })
320
+ });
321
+ const json = await res.json();
322
+ return json.data ?? json;
323
+ }
324
+ /** Create a user */
325
+ async createUser(data) {
326
+ const res = await this.request(`${this.baseUrl}/api/users`, {
327
+ method: "POST",
328
+ body: JSON.stringify({ user: data })
329
+ });
330
+ const json = await res.json();
331
+ return json.data ?? json;
332
+ }
333
+ // ── Organizations ─────────────────────────────────────────────────────────
334
+ /** Get an organization */
335
+ async getOrganization(id) {
336
+ const res = await this.request(`${this.baseUrl}/api/organizations/${id}`);
337
+ const json = await res.json();
338
+ return json.data ?? json;
339
+ }
340
+ /** List all organizations */
341
+ async listOrganizations() {
342
+ const res = await this.request(`${this.baseUrl}/api/organizations`);
343
+ const json = await res.json();
344
+ return json.data ?? json;
345
+ }
346
+ // ── Domain Roles ──────────────────────────────────────────────────────────
347
+ /** List domain roles (optionally filtered) */
348
+ async listDomainRoles(filters) {
349
+ const params = new URLSearchParams();
350
+ if (filters?.user_id) params.set("user_id", filters.user_id);
351
+ if (filters?.organization_id) params.set("organization_id", filters.organization_id);
352
+ if (filters?.domain) params.set("domain", filters.domain);
353
+ const qs = params.toString();
354
+ const url = `${this.baseUrl}/api/domains/roles${qs ? `?${qs}` : ""}`;
355
+ const res = await this.request(url);
356
+ const json = await res.json();
357
+ return json.data ?? json;
358
+ }
359
+ /** Grant a domain role to a user */
360
+ async grantDomainRole(params) {
361
+ const res = await this.request(`${this.baseUrl}/api/domains/roles/grant`, {
362
+ method: "POST",
363
+ body: JSON.stringify(params)
364
+ });
365
+ return res.json();
366
+ }
367
+ /** Revoke a domain role from a user */
368
+ async revokeDomainRole(params) {
369
+ const res = await this.request(`${this.baseUrl}/api/domains/roles/revoke`, {
370
+ method: "DELETE",
371
+ body: JSON.stringify(params)
372
+ });
373
+ return res.json();
374
+ }
375
+ // ── Roles (RBAC) ──────────────────────────────────────────────────────────
376
+ /** List all roles */
377
+ async listRoles() {
378
+ const res = await this.request(`${this.baseUrl}/api/roles`);
379
+ const json = await res.json();
380
+ return json.data ?? json;
381
+ }
382
+ /** Create a role */
383
+ async createRole(params) {
384
+ const res = await this.request(`${this.baseUrl}/api/roles`, {
385
+ method: "POST",
386
+ body: JSON.stringify(params)
387
+ });
388
+ const json = await res.json();
389
+ return json.data ?? json;
390
+ }
391
+ /** Delete a role */
392
+ async deleteRole(id) {
393
+ await this.request(`${this.baseUrl}/api/roles/${id}`, { method: "DELETE" });
394
+ }
395
+ // ── User Roles ────────────────────────────────────────────────────────────
396
+ /** Get user's effective scopes */
397
+ async getEffectiveScopes(userId) {
398
+ const res = await this.request(`${this.baseUrl}/api/users/${userId}/effective-scopes`);
399
+ const json = await res.json();
400
+ return json.data ?? json;
401
+ }
402
+ // ── Internal ──────────────────────────────────────────────────────────────
403
+ getAccessToken() {
404
+ if (typeof globalThis !== "undefined" && "localStorage" in globalThis) {
405
+ const storageKey = this.config.storageKey || "thalamus_auth";
406
+ const saved = globalThis.localStorage.getItem(storageKey);
407
+ if (saved) {
408
+ try {
409
+ return JSON.parse(saved).accessToken;
410
+ } catch {
411
+ return null;
412
+ }
413
+ }
414
+ }
415
+ return null;
416
+ }
417
+ async request(url, options = {}) {
418
+ const token = this.getAccessToken();
419
+ const res = await fetch(url, {
420
+ ...options,
421
+ headers: {
422
+ "Content-Type": "application/json",
423
+ ...token ? { Authorization: `Bearer ${token}` } : {},
424
+ ...options.headers
425
+ }
426
+ });
427
+ if (!res.ok) {
428
+ throw await this.toError(res);
429
+ }
430
+ return res;
431
+ }
432
+ async toError(response) {
433
+ let body = {};
434
+ try {
435
+ body = await response.json();
436
+ } catch {
437
+ }
438
+ const error = new Error(
439
+ body.error_description || body.error || `HTTP ${response.status}`
440
+ );
441
+ error.statusCode = response.status;
442
+ error.error = body.error;
443
+ error.error_description = body.error_description;
444
+ return error;
445
+ }
446
+ };
447
+
448
+ // src/client/ThalamusClient.ts
449
+ var ThalamusClient = class {
450
+ /** OAuth2 authentication methods */
451
+ auth;
452
+ /** Token management and introspection */
453
+ tokens;
454
+ /** Admin API — users, orgs, roles, domain management */
455
+ admin;
456
+ config;
457
+ /**
458
+ * Create a new Thalamus client
459
+ *
460
+ * @param config - Client configuration
461
+ */
462
+ constructor(config) {
463
+ if (!config.clientId) {
464
+ throw new Error("clientId is required");
465
+ }
466
+ if (!config.redirectUri) {
467
+ throw new Error("redirectUri is required");
468
+ }
469
+ if (!config.baseUrl) {
470
+ throw new Error("baseUrl is required");
471
+ }
472
+ config.baseUrl = config.baseUrl.replace(/\/$/, "");
473
+ this.config = config;
474
+ this.auth = new OAuth2(config);
475
+ this.tokens = new TokenManager(config);
476
+ this.admin = new AdminAPI(config);
477
+ }
478
+ /**
479
+ * Get the current configuration
480
+ */
481
+ getConfig() {
482
+ return Object.freeze({ ...this.config });
483
+ }
484
+ };
485
+
486
+ // src/hooks/useThalamus.ts
487
+ function useThalamus(options) {
488
+ const storageKey = options.storageKey || "thalamus_auth";
489
+ const clientRef = useRef(new ThalamusClient(options));
490
+ const client = clientRef.current;
491
+ const [token, setToken] = useState(null);
492
+ const [user, setUser] = useState(null);
493
+ const [isLoading, setIsLoading] = useState(true);
494
+ const [error, setError] = useState(null);
495
+ useEffect(() => {
496
+ try {
497
+ const saved = localStorage.getItem(storageKey);
498
+ if (saved) {
499
+ const { accessToken, refreshToken: rt } = JSON.parse(saved);
500
+ setToken(accessToken);
501
+ client.tokens.getUserInfo(accessToken).then((u) => setUser(u)).catch(() => {
502
+ });
503
+ }
504
+ } catch {
505
+ } finally {
506
+ setIsLoading(false);
507
+ }
508
+ }, [storageKey]);
509
+ useEffect(() => {
510
+ const params = new URLSearchParams(window.location.search);
511
+ const code = params.get("code");
512
+ const state = params.get("state");
513
+ if (!code || !state) return;
514
+ window.history.replaceState({}, "", window.location.pathname);
515
+ const savedState = sessionStorage.getItem(`${storageKey}_state`);
516
+ if (state !== savedState) {
517
+ setError("State mismatch \u2014 possible CSRF attack");
518
+ return;
519
+ }
520
+ const verifier = sessionStorage.getItem(`${storageKey}_verifier`);
521
+ if (!verifier) {
522
+ setError("Missing code verifier");
523
+ return;
524
+ }
525
+ client.auth.exchangeCode({ code, codeVerifier: verifier }).then((data) => {
526
+ persistAuth(data);
527
+ setToken(data.access_token);
528
+ client.tokens.getUserInfo(data.access_token).then((u) => setUser(u)).catch(() => {
529
+ });
530
+ }).catch((err) => setError(err.message));
531
+ }, [storageKey]);
532
+ const persistAuth = (data) => {
533
+ localStorage.setItem(storageKey, JSON.stringify({ accessToken: data.access_token, refreshToken: data.refresh_token || null, expiresAt: Date.now() + data.expires_in * 1e3 }));
534
+ };
535
+ const login = useCallback(async (opts) => {
536
+ setError(null);
537
+ const verifier = client.auth.generateState();
538
+ const challenge = await pkceChallenge(verifier);
539
+ sessionStorage.setItem(`${storageKey}_verifier`, verifier);
540
+ const url = client.auth.getAuthorizationUrl({ scope: opts?.scope, codeChallenge: challenge, codeChallengeMethod: "S256" });
541
+ sessionStorage.setItem(`${storageKey}_state`, new URL(url).searchParams.get("state") || "");
542
+ window.location.href = url;
543
+ }, [storageKey]);
544
+ const logout = useCallback(() => {
545
+ localStorage.removeItem(storageKey);
546
+ setToken(null);
547
+ setUser(null);
548
+ }, [storageKey]);
549
+ const refreshToken = useCallback(async () => {
550
+ try {
551
+ const saved = localStorage.getItem(storageKey);
552
+ if (!saved) return;
553
+ const { refreshToken: rt } = JSON.parse(saved);
554
+ if (!rt) return;
555
+ const data = await client.auth.refreshToken({ refreshToken: rt });
556
+ persistAuth(data);
557
+ setToken(data.access_token);
558
+ } catch {
559
+ }
560
+ }, [storageKey]);
561
+ return {
562
+ login,
563
+ logout,
564
+ token,
565
+ user,
566
+ isAuthenticated: !!token,
567
+ isLoading,
568
+ refreshToken,
569
+ client,
570
+ error
571
+ };
572
+ }
573
+ async function pkceChallenge(verifier) {
574
+ const encoder = new TextEncoder();
575
+ const hash = await crypto.subtle.digest("SHA-256", encoder.encode(verifier));
576
+ return btoa(String.fromCharCode(...new Uint8Array(hash))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
577
+ }
578
+ function LoginButton({
579
+ config,
580
+ storageKey = "thalamus_auth",
581
+ label = "Login with ZEA",
582
+ scopes,
583
+ className,
584
+ style
585
+ }) {
586
+ const { login, isAuthenticated, isLoading, error } = useThalamus({ ...config, storageKey });
587
+ if (isLoading) return /* @__PURE__ */ jsx("span", { style: { color: "#8b949e", fontSize: 14 }, children: "Loading..." });
588
+ if (isAuthenticated) return null;
589
+ return /* @__PURE__ */ jsxs("div", { style: { display: "flex", flexDirection: "column", alignItems: "center", gap: 8 }, children: [
590
+ /* @__PURE__ */ jsx(
591
+ "button",
592
+ {
593
+ onClick: () => login({ scope: scopes }),
594
+ className,
595
+ style: {
596
+ padding: "12px 32px",
597
+ borderRadius: 8,
598
+ border: "none",
599
+ cursor: "pointer",
600
+ background: "#238636",
601
+ color: "#fff",
602
+ fontSize: 16,
603
+ fontWeight: 600,
604
+ fontFamily: "system-ui, sans-serif",
605
+ ...style
606
+ },
607
+ children: label
608
+ }
609
+ ),
610
+ error && /* @__PURE__ */ jsx("span", { style: { color: "#ff7b72", fontSize: 12 }, children: error })
611
+ ] });
612
+ }
613
+ function RegisterButton({ config, orgName, appOrigin, label = "Create Account", className, style }) {
614
+ const handleClick = () => {
615
+ const authParams = new URLSearchParams({
616
+ client_id: config.clientId,
617
+ redirect_uri: config.redirectUri,
618
+ response_type: "code",
619
+ scope: (config.defaultScopes || ["openid", "profile", "email"]).join(" "),
620
+ state: crypto.randomUUID()
621
+ });
622
+ if (orgName) authParams.set("org_name", orgName);
623
+ if (appOrigin) authParams.set("app_origin", appOrigin);
624
+ const returnTo = `/oauth/authorize?${authParams.toString()}`;
625
+ window.location.href = `${config.baseUrl}/register?return_to=${encodeURIComponent(returnTo)}`;
626
+ };
627
+ return /* @__PURE__ */ jsx("button", { onClick: handleClick, className, style: { padding: "12px 32px", borderRadius: 8, border: "1px solid #30363d", cursor: "pointer", background: "transparent", color: "#e6edf3", fontSize: 16, fontWeight: 600, fontFamily: "system-ui, sans-serif", ...style }, children: label });
628
+ }
629
+ function UserMenu({ config, storageKey = "thalamus_auth", renderUser, className }) {
630
+ const { user, logout, isAuthenticated, isLoading} = useThalamus({ ...config, storageKey });
631
+ if (isLoading || !isAuthenticated || !user) return null;
632
+ const initials = user.name ? user.name.split(" ").map((n) => n[0]).join("").toUpperCase().slice(0, 2) : (user.email || user.sub).slice(0, 2).toUpperCase();
633
+ return /* @__PURE__ */ jsxs("div", { className, style: { display: "flex", alignItems: "center", gap: 10, fontFamily: "system-ui, sans-serif" }, children: [
634
+ renderUser ? renderUser(user) : /* @__PURE__ */ jsxs(Fragment, { children: [
635
+ /* @__PURE__ */ jsx("div", { style: {
636
+ width: 28,
637
+ height: 28,
638
+ borderRadius: "50%",
639
+ background: "#238636",
640
+ color: "#fff",
641
+ display: "flex",
642
+ alignItems: "center",
643
+ justifyContent: "center",
644
+ fontSize: 11,
645
+ fontWeight: 700,
646
+ flexShrink: 0
647
+ }, children: user.picture ? /* @__PURE__ */ jsx("img", { src: user.picture, alt: "", style: { width: 28, height: 28, borderRadius: "50%" } }) : initials }),
648
+ /* @__PURE__ */ jsx("div", { style: { minWidth: 0 }, children: /* @__PURE__ */ jsx("div", { style: { fontSize: 12, fontWeight: 600, color: "#e6edf3", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, children: user.name || user.email || user.sub }) })
649
+ ] }),
650
+ /* @__PURE__ */ jsx(
651
+ "button",
652
+ {
653
+ onClick: logout,
654
+ style: {
655
+ background: "none",
656
+ border: "1px solid #30363d",
657
+ color: "#8b949e",
658
+ padding: "4px 10px",
659
+ borderRadius: 6,
660
+ cursor: "pointer",
661
+ fontSize: 11,
662
+ fontFamily: "inherit"
663
+ },
664
+ children: "Logout"
665
+ }
666
+ )
667
+ ] });
668
+ }
669
+ function APIKeyManager({ baseUrl, authStorageKey = "thalamus_auth", label = "+ Generate API Key", className }) {
670
+ const [keys, setKeys] = useState([]);
671
+ const [newKey, setNewKey] = useState(null);
672
+ const [loading, setLoading] = useState(false);
673
+ const [error, setError] = useState("");
674
+ const token = typeof window !== "undefined" ? (() => {
675
+ try {
676
+ const s = localStorage.getItem(authStorageKey);
677
+ return s ? JSON.parse(s).accessToken : null;
678
+ } catch {
679
+ return null;
680
+ }
681
+ })() : null;
682
+ useEffect(() => {
683
+ if (!token) return;
684
+ const controller = new AbortController();
685
+ fetch(`${baseUrl}/api/personal-access-tokens`, {
686
+ headers: { Authorization: `Bearer ${token}` },
687
+ signal: controller.signal
688
+ }).then((res) => res.json()).then((data) => {
689
+ if (data.data) {
690
+ setKeys(data.data.map((k) => ({
691
+ api_key: k.api_key || k.id,
692
+ prefix: k.token_prefix || k.prefix || "zpat_",
693
+ created: k.inserted_at || k.created_at || k.created || (/* @__PURE__ */ new Date()).toISOString()
694
+ })));
695
+ }
696
+ }).catch((e) => {
697
+ if (e.name !== "AbortError") console.error("Failed to load API keys:", e);
698
+ });
699
+ return () => controller.abort();
700
+ }, [baseUrl, token]);
701
+ const createKey = async () => {
702
+ setLoading(true);
703
+ setError("");
704
+ try {
705
+ const res = await fetch(`${baseUrl}/api/personal-access-tokens`, {
706
+ method: "POST",
707
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
708
+ body: JSON.stringify({ name: `key-${Date.now()}`, scopes: ["api:read", "api:write"] })
709
+ });
710
+ const data = await res.json();
711
+ if (data.api_key) {
712
+ setNewKey(data.api_key);
713
+ setKeys((prev) => [...prev, { api_key: data.api_key, prefix: data.prefix, created: (/* @__PURE__ */ new Date()).toISOString() }]);
714
+ } else setError(data.error || "Failed to create key");
715
+ } catch (e) {
716
+ setError(e.message);
717
+ }
718
+ setLoading(false);
719
+ };
720
+ const copyKey = (key) => {
721
+ navigator.clipboard?.writeText(key);
722
+ };
723
+ return /* @__PURE__ */ jsxs("div", { className, style: { fontFamily: "system-ui, sans-serif" }, children: [
724
+ error && /* @__PURE__ */ jsx("div", { style: { padding: "8px 12px", background: "#fff0f0", borderRadius: 6, color: "#c00", marginBottom: 12, fontSize: 12 }, children: error }),
725
+ /* @__PURE__ */ jsx("button", { onClick: createKey, disabled: loading, style: {
726
+ padding: "8px 20px",
727
+ borderRadius: 6,
728
+ border: "none",
729
+ cursor: "pointer",
730
+ background: "#238636",
731
+ color: "#fff",
732
+ fontSize: 13,
733
+ fontWeight: 600,
734
+ opacity: loading ? 0.7 : 1,
735
+ fontFamily: "inherit"
736
+ }, children: loading ? "Generating..." : label }),
737
+ newKey && /* @__PURE__ */ jsxs("div", { style: { marginTop: 16, padding: "12px 16px", background: "#f0fff0", borderRadius: 8, border: "1px solid #2ea44f" }, children: [
738
+ /* @__PURE__ */ jsx("div", { style: { fontSize: 12, fontWeight: 600, color: "#2ea44f", marginBottom: 6 }, children: "\u2705 Key created \u2014 copy it now" }),
739
+ /* @__PURE__ */ jsxs("div", { style: { display: "flex", gap: 8, alignItems: "center" }, children: [
740
+ /* @__PURE__ */ jsx("code", { style: { flex: 1, padding: "8px 12px", background: "#fff", borderRadius: 4, fontSize: 11, fontFamily: "monospace", wordBreak: "break-all", userSelect: "all" }, children: newKey }),
741
+ /* @__PURE__ */ jsx("button", { onClick: () => copyKey(newKey), style: { padding: "6px 12px", borderRadius: 4, border: "1px solid #2ea44f", background: "#dafbe1", color: "#1a7f37", cursor: "pointer", fontSize: 11, fontFamily: "inherit" }, children: "Copy" })
742
+ ] })
743
+ ] }),
744
+ keys.length > 0 && /* @__PURE__ */ jsxs("div", { style: { marginTop: 20 }, children: [
745
+ /* @__PURE__ */ jsxs("div", { style: { fontSize: 12, fontWeight: 600, marginBottom: 8 }, children: [
746
+ "Existing keys (",
747
+ keys.length,
748
+ ")"
749
+ ] }),
750
+ keys.map((k, i) => /* @__PURE__ */ jsxs("div", { style: { padding: "6px 0", borderBottom: "1px solid #eee", fontSize: 11, fontFamily: "monospace", color: "#656d76" }, children: [
751
+ k.prefix,
752
+ "...",
753
+ k.api_key?.slice(-8),
754
+ " \xB7 ",
755
+ new Date(k.created).toLocaleDateString()
756
+ ] }, i))
757
+ ] })
758
+ ] });
759
+ }
760
+ function OrgSwitcher({ config, onSwitch, className, style }) {
761
+ const [orgs, setOrgs] = useState([]);
762
+ const [selected, setSelected] = useState(() => {
763
+ try {
764
+ const authStr = typeof window !== "undefined" ? localStorage.getItem("thalamus_auth") : null;
765
+ if (authStr) {
766
+ const auth = JSON.parse(authStr);
767
+ return auth.user?.organization_id || "";
768
+ }
769
+ } catch (e) {
770
+ console.error(e);
771
+ }
772
+ return "";
773
+ });
774
+ const [loading, setLoading] = useState(true);
775
+ useEffect(() => {
776
+ const client = new ThalamusClient(config);
777
+ client.admin.listOrganizations().then((o) => {
778
+ setOrgs(o);
779
+ setLoading(false);
780
+ if (o.length > 0) {
781
+ let currentSelected = "";
782
+ try {
783
+ const authStr = typeof window !== "undefined" ? localStorage.getItem("thalamus_auth") : null;
784
+ if (authStr) {
785
+ const auth = JSON.parse(authStr);
786
+ currentSelected = auth.user?.organization_id || "";
787
+ }
788
+ } catch (e) {
789
+ }
790
+ if (!currentSelected) {
791
+ const defaultOrgId = "ea7b11ea-852c-44e5-aee1-a761ec76eaea";
792
+ const target = o.find((org) => org.id === defaultOrgId) ? defaultOrgId : o[0].id;
793
+ setSelected(target);
794
+ onSwitch?.(target);
795
+ }
796
+ }
797
+ }).catch(() => setLoading(false));
798
+ }, [config.baseUrl]);
799
+ if (loading) return null;
800
+ if (orgs.length === 0) return null;
801
+ return /* @__PURE__ */ jsxs(
802
+ "select",
803
+ {
804
+ className: `th-select ${className || ""}`,
805
+ value: selected,
806
+ onChange: (e) => {
807
+ setSelected(e.target.value);
808
+ onSwitch?.(e.target.value);
809
+ },
810
+ style,
811
+ children: [
812
+ /* @__PURE__ */ jsx("option", { value: "", children: "Select org..." }),
813
+ orgs.map((org) => /* @__PURE__ */ jsx("option", { value: org.id, children: org.name }, org.id))
814
+ ]
815
+ }
816
+ );
817
+ }
818
+ function OrgManager({ config, className }) {
819
+ const [orgs, setOrgs] = useState([]);
820
+ const [loading, setLoading] = useState(true);
821
+ const [error, setError] = useState("");
822
+ useEffect(() => {
823
+ const client = new ThalamusClient(config);
824
+ client.admin.listOrganizations().then((o) => {
825
+ setOrgs(o);
826
+ setLoading(false);
827
+ }).catch((e) => {
828
+ setError(e.message);
829
+ setLoading(false);
830
+ });
831
+ }, [config.baseUrl, config.clientId, config.redirectUri]);
832
+ if (loading) return /* @__PURE__ */ jsx("p", { className: "th-loading", children: "Loading..." });
833
+ if (error) return /* @__PURE__ */ jsx("div", { className: "th-alert", children: error });
834
+ if (orgs.length === 0) return /* @__PURE__ */ jsx("p", { className: "th-empty", children: "No organizations." });
835
+ return /* @__PURE__ */ jsx("div", { className: `th-list ${className || ""}`, children: orgs.map((org) => /* @__PURE__ */ jsxs("div", { className: "th-card", children: [
836
+ /* @__PURE__ */ jsx("div", { className: "th-card-title", children: org.name }),
837
+ /* @__PURE__ */ jsxs("div", { className: "th-card-desc", children: [
838
+ "Domains: ",
839
+ (org.domains || []).join(", ") || "none"
840
+ ] })
841
+ ] }, org.id)) });
842
+ }
843
+ function UserCreateForm({ config, onCreated, className }) {
844
+ const [email, setEmail] = useState("");
845
+ const [password, setPassword] = useState("");
846
+ const [confirmPassword, setConfirmPassword] = useState("");
847
+ const [name, setName] = useState("");
848
+ const [isAgent, setIsAgent] = useState(false);
849
+ const [loading, setLoading] = useState(false);
850
+ const [error, setError] = useState("");
851
+ const [showPassword, setShowPassword] = useState(false);
852
+ const generatePassword = () => {
853
+ const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
854
+ let pass = "";
855
+ for (let i = 0; i < 16; i++) {
856
+ pass += chars.charAt(Math.floor(Math.random() * chars.length));
857
+ }
858
+ setPassword(pass);
859
+ setConfirmPassword(pass);
860
+ setShowPassword(true);
861
+ };
862
+ const handleSubmit = async (e) => {
863
+ e.preventDefault();
864
+ if (password !== confirmPassword) {
865
+ setError("Passwords do not match");
866
+ return;
867
+ }
868
+ setLoading(true);
869
+ setError("");
870
+ try {
871
+ const client = new ThalamusClient(config);
872
+ const user = await client.admin.createUser({ email, password, name, is_agent: isAgent });
873
+ setEmail("");
874
+ setPassword("");
875
+ setConfirmPassword("");
876
+ setName("");
877
+ setIsAgent(false);
878
+ setShowPassword(false);
879
+ onCreated?.(user);
880
+ } catch (err) {
881
+ setError(err.message);
882
+ }
883
+ setLoading(false);
884
+ };
885
+ return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className: `th-form ${className || ""}`, children: [
886
+ /* @__PURE__ */ jsxs("div", { className: "th-form-grid", children: [
887
+ /* @__PURE__ */ jsx("input", { required: true, placeholder: "Email", type: "email", value: email, onChange: (e) => setEmail(e.target.value), className: "th-input" }),
888
+ /* @__PURE__ */ jsx("input", { placeholder: "Name (optional)", value: name, onChange: (e) => setName(e.target.value), className: "th-input" }),
889
+ /* @__PURE__ */ jsxs("div", { style: { position: "relative", display: "flex", gap: "8px" }, children: [
890
+ /* @__PURE__ */ jsx("input", { required: true, placeholder: "Password", type: showPassword ? "text" : "password", value: password, onChange: (e) => setPassword(e.target.value), className: "th-input", minLength: 8, style: { flex: 1, minWidth: 0 } }),
891
+ /* @__PURE__ */ jsx("button", { type: "button", onClick: generatePassword, className: "th-btn th-btn--ghost", title: "Generate Password", style: { padding: "0 12px" }, children: /* @__PURE__ */ jsx("svg", { xmlns: "http://www.w3.org/2000/svg", width: "16", height: "16", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: /* @__PURE__ */ jsx("path", { d: "M21 2l-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.778 7.778 5.5 5.5 0 0 1 7.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4" }) }) })
892
+ ] }),
893
+ /* @__PURE__ */ jsx("input", { required: true, placeholder: "Confirm Password", type: showPassword ? "text" : "password", value: confirmPassword, onChange: (e) => setConfirmPassword(e.target.value), className: "th-input", minLength: 8 }),
894
+ /* @__PURE__ */ jsxs("select", { value: isAgent ? "agent" : "user", onChange: (e) => setIsAgent(e.target.value === "agent"), className: "th-select", style: { gridColumn: "1 / -1" }, children: [
895
+ /* @__PURE__ */ jsx("option", { value: "user", children: "Human User" }),
896
+ /* @__PURE__ */ jsx("option", { value: "agent", children: "AI Agent" })
897
+ ] })
898
+ ] }),
899
+ error && /* @__PURE__ */ jsx("div", { className: "th-alert", children: error }),
900
+ /* @__PURE__ */ jsx("div", { style: { marginTop: "8px" }, children: /* @__PURE__ */ jsx("button", { type: "submit", disabled: loading, className: "th-btn th-btn--primary", children: loading ? "Creating..." : "Create User" }) })
901
+ ] });
902
+ }
903
+ function UserTable({ users, loading, error, className }) {
904
+ if (loading) return /* @__PURE__ */ jsx("p", { className: "th-loading", children: "Loading..." });
905
+ if (error) return /* @__PURE__ */ jsx("div", { className: "th-alert", children: error });
906
+ if (users.length === 0) return /* @__PURE__ */ jsx("p", { className: "th-empty", children: "No users found." });
907
+ return /* @__PURE__ */ jsx("div", { className: `th-table-container ${className || ""}`, children: /* @__PURE__ */ jsxs("table", { className: "th-table", children: [
908
+ /* @__PURE__ */ jsx("thead", { children: /* @__PURE__ */ jsxs("tr", { children: [
909
+ /* @__PURE__ */ jsx("th", { children: "Name" }),
910
+ /* @__PURE__ */ jsx("th", { children: "Email" }),
911
+ /* @__PURE__ */ jsx("th", { children: "Organization" }),
912
+ /* @__PURE__ */ jsx("th", { children: "User Type" }),
913
+ /* @__PURE__ */ jsx("th", { children: "Status" })
914
+ ] }) }),
915
+ /* @__PURE__ */ jsx("tbody", { children: users.map((u) => /* @__PURE__ */ jsxs("tr", { children: [
916
+ /* @__PURE__ */ jsx("td", { children: u.name || "\u2014" }),
917
+ /* @__PURE__ */ jsx("td", { className: "th-text-accent", children: u.email }),
918
+ /* @__PURE__ */ jsx("td", { style: { color: "var(--th-text-muted)" }, children: u.organization_id ? u.organization_id.split("-")[0] + "..." : "\u2014" }),
919
+ /* @__PURE__ */ jsx("td", { style: { display: "flex", alignItems: "center", gap: 6, padding: "10px 16px" }, children: u.is_agent ? /* @__PURE__ */ jsxs(Fragment, { children: [
920
+ /* @__PURE__ */ jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
921
+ /* @__PURE__ */ jsx("rect", { width: "18", height: "14", x: "3", y: "7", rx: "2", ry: "2" }),
922
+ /* @__PURE__ */ jsx("path", { d: "M12 7V3" }),
923
+ /* @__PURE__ */ jsx("path", { d: "M9 3h6" }),
924
+ /* @__PURE__ */ jsx("path", { d: "M12 11h.01" }),
925
+ /* @__PURE__ */ jsx("path", { d: "M15 15h.01" }),
926
+ /* @__PURE__ */ jsx("path", { d: "M9 15h.01" })
927
+ ] }),
928
+ " Agent"
929
+ ] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
930
+ /* @__PURE__ */ jsxs("svg", { xmlns: "http://www.w3.org/2000/svg", width: "14", height: "14", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: "2", strokeLinecap: "round", strokeLinejoin: "round", children: [
931
+ /* @__PURE__ */ jsx("path", { d: "M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2" }),
932
+ /* @__PURE__ */ jsx("circle", { cx: "12", cy: "7", r: "4" })
933
+ ] }),
934
+ " User"
935
+ ] }) }),
936
+ /* @__PURE__ */ jsx("td", { children: /* @__PURE__ */ jsx(StatusBadge, { status: u.status }) })
937
+ ] }, u.id)) })
938
+ ] }) });
939
+ }
940
+ function StatusBadge({ status }) {
941
+ const badgeClass = status ? `th-badge--${status}` : "";
942
+ return /* @__PURE__ */ jsx("span", { className: `th-badge ${badgeClass}`, children: status || "unknown" });
943
+ }
944
+
945
+ export { APIKeyManager, LoginButton, OrgManager, OrgSwitcher, RegisterButton, StatusBadge, UserCreateForm, UserMenu, UserTable };
946
+ //# sourceMappingURL=index.mjs.map
947
+ //# sourceMappingURL=index.mjs.map