@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.
- package/bin/zea-auth-init.mjs +53 -0
- package/dist/components/index.d.mts +150 -0
- package/dist/components/index.d.ts +150 -0
- package/dist/components/index.js +957 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +947 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/hooks/index.d.mts +2 -0
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/index.js +621 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +618 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/index-BnHWPrKX.d.mts +69 -0
- package/dist/index-BnHWPrKX.d.ts +69 -0
- package/dist/index-C5rsqdqK.d.ts +453 -0
- package/dist/index-DtXFjTm2.d.mts +453 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1041 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1024 -0
- package/dist/index.mjs.map +1 -0
- package/dist/styles/base.css +219 -0
- package/package.json +72 -0
|
@@ -0,0 +1,618 @@
|
|
|
1
|
+
import { useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
4
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
5
|
+
}) : x)(function(x) {
|
|
6
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
7
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// src/client/auth/OAuth2.ts
|
|
11
|
+
var OAuth2 = class {
|
|
12
|
+
constructor(config) {
|
|
13
|
+
this.config = config;
|
|
14
|
+
}
|
|
15
|
+
config;
|
|
16
|
+
/**
|
|
17
|
+
* Generate OAuth2 authorization URL for user login
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const authUrl = thalamus.auth.getAuthorizationUrl({
|
|
22
|
+
* scope: ['openid', 'profile', 'email'],
|
|
23
|
+
* state: 'random-state-string'
|
|
24
|
+
* })
|
|
25
|
+
* // Redirect user to authUrl
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
getAuthorizationUrl(options) {
|
|
29
|
+
const {
|
|
30
|
+
scope = this.config.defaultScopes || ["openid", "profile", "email"],
|
|
31
|
+
state = this.generateState(),
|
|
32
|
+
responseType = "code",
|
|
33
|
+
codeChallenge,
|
|
34
|
+
codeChallengeMethod
|
|
35
|
+
} = options || {};
|
|
36
|
+
const params = new URLSearchParams({
|
|
37
|
+
response_type: responseType,
|
|
38
|
+
client_id: this.config.clientId,
|
|
39
|
+
redirect_uri: this.config.redirectUri,
|
|
40
|
+
scope: Array.isArray(scope) ? scope.join(" ") : scope,
|
|
41
|
+
state
|
|
42
|
+
});
|
|
43
|
+
if (codeChallenge) {
|
|
44
|
+
params.set("code_challenge", codeChallenge);
|
|
45
|
+
params.set("code_challenge_method", codeChallengeMethod || "S256");
|
|
46
|
+
}
|
|
47
|
+
return `${this.config.baseUrl}/oauth/authorize?${params.toString()}`;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Exchange authorization code for access token
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const tokens = await thalamus.auth.exchangeCode('authorization_code_here')
|
|
55
|
+
* console.log(tokens.access_token)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
async exchangeCode(codeOrOptions) {
|
|
59
|
+
const code = typeof codeOrOptions === "string" ? codeOrOptions : codeOrOptions.code;
|
|
60
|
+
const codeVerifier = typeof codeOrOptions === "string" ? void 0 : codeOrOptions.codeVerifier;
|
|
61
|
+
const body = {
|
|
62
|
+
grant_type: "authorization_code",
|
|
63
|
+
code,
|
|
64
|
+
client_id: this.config.clientId,
|
|
65
|
+
client_secret: this.config.clientSecret,
|
|
66
|
+
redirect_uri: this.config.redirectUri
|
|
67
|
+
};
|
|
68
|
+
if (codeVerifier) {
|
|
69
|
+
body.code_verifier = codeVerifier;
|
|
70
|
+
}
|
|
71
|
+
return this.requestToken(body);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get access token using client credentials (M2M)
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* const tokens = await thalamus.auth.getClientCredentialsToken({
|
|
79
|
+
* scope: ['api:read', 'api:write']
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
async getClientCredentialsToken(options) {
|
|
84
|
+
const { scope = this.config.defaultScopes || [] } = options || {};
|
|
85
|
+
const body = {
|
|
86
|
+
grant_type: "client_credentials",
|
|
87
|
+
client_id: this.config.clientId,
|
|
88
|
+
client_secret: this.config.clientSecret
|
|
89
|
+
};
|
|
90
|
+
if (scope.length > 0) {
|
|
91
|
+
body.scope = Array.isArray(scope) ? scope.join(" ") : scope;
|
|
92
|
+
}
|
|
93
|
+
return this.requestToken(body);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Refresh access token using refresh token
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const newTokens = await thalamus.auth.refreshToken({
|
|
101
|
+
* refreshToken: 'rt_...'
|
|
102
|
+
* })
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
async refreshToken(options) {
|
|
106
|
+
const body = {
|
|
107
|
+
grant_type: "refresh_token",
|
|
108
|
+
refresh_token: options.refreshToken,
|
|
109
|
+
client_id: this.config.clientId,
|
|
110
|
+
client_secret: this.config.clientSecret
|
|
111
|
+
};
|
|
112
|
+
return this.requestToken(body);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Revoke a token (access or refresh token)
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* await thalamus.auth.revokeToken('at_...')
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
async revokeToken(token, tokenTypeHint) {
|
|
123
|
+
const body = {
|
|
124
|
+
token
|
|
125
|
+
};
|
|
126
|
+
if (tokenTypeHint) {
|
|
127
|
+
body.token_type_hint = tokenTypeHint;
|
|
128
|
+
}
|
|
129
|
+
const response = await fetch(`${this.config.baseUrl}/oauth/revoke`, {
|
|
130
|
+
method: "POST",
|
|
131
|
+
headers: {
|
|
132
|
+
"Content-Type": "application/json"
|
|
133
|
+
},
|
|
134
|
+
body: JSON.stringify(body)
|
|
135
|
+
});
|
|
136
|
+
if (!response.ok) {
|
|
137
|
+
throw await this.handleError(response);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Generate random state for CSRF protection
|
|
142
|
+
*/
|
|
143
|
+
generateState(length = 32) {
|
|
144
|
+
const array = new Uint8Array(32);
|
|
145
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
146
|
+
crypto.getRandomValues(array);
|
|
147
|
+
} else {
|
|
148
|
+
const cryptoModule = __require("crypto");
|
|
149
|
+
cryptoModule.randomFillSync(array);
|
|
150
|
+
}
|
|
151
|
+
return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Make token request to /oauth/token
|
|
155
|
+
*/
|
|
156
|
+
async requestToken(body) {
|
|
157
|
+
const response = await fetch(`${this.config.baseUrl}/oauth/token`, {
|
|
158
|
+
method: "POST",
|
|
159
|
+
headers: {
|
|
160
|
+
"Content-Type": "application/json"
|
|
161
|
+
},
|
|
162
|
+
body: JSON.stringify(body)
|
|
163
|
+
});
|
|
164
|
+
if (!response.ok) {
|
|
165
|
+
throw await this.handleError(response);
|
|
166
|
+
}
|
|
167
|
+
return response.json();
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Handle API errors
|
|
171
|
+
*/
|
|
172
|
+
async handleError(response) {
|
|
173
|
+
let errorData = {};
|
|
174
|
+
try {
|
|
175
|
+
errorData = await response.json();
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
const error = new Error(
|
|
179
|
+
errorData.error_description || errorData.message || `HTTP ${response.status}`
|
|
180
|
+
);
|
|
181
|
+
error.statusCode = response.status;
|
|
182
|
+
error.error = errorData.error;
|
|
183
|
+
error.error_description = errorData.error_description;
|
|
184
|
+
return error;
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// src/client/tokens/TokenManager.ts
|
|
189
|
+
var TokenManager = class {
|
|
190
|
+
constructor(config) {
|
|
191
|
+
this.config = config;
|
|
192
|
+
}
|
|
193
|
+
config;
|
|
194
|
+
/**
|
|
195
|
+
* Introspect a token to check if it's valid and get metadata
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* const tokenInfo = await thalamus.tokens.introspect('at_...')
|
|
200
|
+
* if (tokenInfo.active) {
|
|
201
|
+
* console.log(tokenInfo.user_id)
|
|
202
|
+
* console.log(tokenInfo.scope)
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
async introspect(token) {
|
|
207
|
+
const response = await fetch(`${this.config.baseUrl}/oauth/introspect`, {
|
|
208
|
+
method: "POST",
|
|
209
|
+
headers: {
|
|
210
|
+
"Content-Type": "application/json"
|
|
211
|
+
},
|
|
212
|
+
body: JSON.stringify({ token })
|
|
213
|
+
});
|
|
214
|
+
if (!response.ok) {
|
|
215
|
+
throw await this.handleError(response);
|
|
216
|
+
}
|
|
217
|
+
return response.json();
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Get user information from OpenID Connect userinfo endpoint
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* const user = await thalamus.tokens.getUserInfo('at_...')
|
|
225
|
+
* console.log(user.email)
|
|
226
|
+
* console.log(user.name)
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
async getUserInfo(accessToken) {
|
|
230
|
+
const response = await fetch(`${this.config.baseUrl}/oauth/userinfo`, {
|
|
231
|
+
method: "GET",
|
|
232
|
+
headers: {
|
|
233
|
+
Authorization: `Bearer ${accessToken}`
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
throw await this.handleError(response);
|
|
238
|
+
}
|
|
239
|
+
return response.json();
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Validate token and return true if active, false otherwise
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* const isValid = await thalamus.tokens.validate('at_...')
|
|
247
|
+
* if (isValid) {
|
|
248
|
+
* // Token is valid
|
|
249
|
+
* }
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
async validate(token) {
|
|
253
|
+
try {
|
|
254
|
+
const result = await this.introspect(token);
|
|
255
|
+
return result.active === true;
|
|
256
|
+
} catch {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Handle API errors
|
|
262
|
+
*/
|
|
263
|
+
async handleError(response) {
|
|
264
|
+
let errorData = {};
|
|
265
|
+
try {
|
|
266
|
+
errorData = await response.json();
|
|
267
|
+
} catch {
|
|
268
|
+
}
|
|
269
|
+
const error = new Error(
|
|
270
|
+
errorData.error_description || errorData.message || `HTTP ${response.status}`
|
|
271
|
+
);
|
|
272
|
+
error.statusCode = response.status;
|
|
273
|
+
error.error = errorData.error;
|
|
274
|
+
error.error_description = errorData.error_description;
|
|
275
|
+
return error;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
279
|
+
// src/client/admin/AdminAPI.ts
|
|
280
|
+
var AdminAPI = class {
|
|
281
|
+
constructor(config) {
|
|
282
|
+
this.config = config;
|
|
283
|
+
}
|
|
284
|
+
config;
|
|
285
|
+
get baseUrl() {
|
|
286
|
+
return this.config.baseUrl;
|
|
287
|
+
}
|
|
288
|
+
// ── Users ────────────────────────────────────────────────────────────────
|
|
289
|
+
/** List all users */
|
|
290
|
+
async listUsers() {
|
|
291
|
+
const res = await this.request(`${this.baseUrl}/api/users`);
|
|
292
|
+
const json = await res.json();
|
|
293
|
+
return json.data ?? json;
|
|
294
|
+
}
|
|
295
|
+
/** List all agents (users with is_agent === true) */
|
|
296
|
+
async listAgents() {
|
|
297
|
+
const users = await this.listUsers();
|
|
298
|
+
return users.filter((u) => u.is_agent === true);
|
|
299
|
+
}
|
|
300
|
+
/** Get a single user */
|
|
301
|
+
async getUser(id) {
|
|
302
|
+
const res = await this.request(`${this.baseUrl}/api/users/${id}`);
|
|
303
|
+
const json = await res.json();
|
|
304
|
+
return json.data ?? json;
|
|
305
|
+
}
|
|
306
|
+
/** Add a member to an organization */
|
|
307
|
+
async addOrgMember(orgId, userId) {
|
|
308
|
+
const res = await this.request(`${this.baseUrl}/api/organizations/${orgId}/members`, {
|
|
309
|
+
method: "POST",
|
|
310
|
+
body: JSON.stringify({ user_id: userId })
|
|
311
|
+
});
|
|
312
|
+
return res.json();
|
|
313
|
+
}
|
|
314
|
+
/** Update a user (only name and agent_config are writable) */
|
|
315
|
+
async updateUser(id, data) {
|
|
316
|
+
const res = await this.request(`${this.baseUrl}/api/users/${id}`, {
|
|
317
|
+
method: "PATCH",
|
|
318
|
+
body: JSON.stringify({ user: data })
|
|
319
|
+
});
|
|
320
|
+
const json = await res.json();
|
|
321
|
+
return json.data ?? json;
|
|
322
|
+
}
|
|
323
|
+
/** Create a user */
|
|
324
|
+
async createUser(data) {
|
|
325
|
+
const res = await this.request(`${this.baseUrl}/api/users`, {
|
|
326
|
+
method: "POST",
|
|
327
|
+
body: JSON.stringify({ user: data })
|
|
328
|
+
});
|
|
329
|
+
const json = await res.json();
|
|
330
|
+
return json.data ?? json;
|
|
331
|
+
}
|
|
332
|
+
// ── Organizations ─────────────────────────────────────────────────────────
|
|
333
|
+
/** Get an organization */
|
|
334
|
+
async getOrganization(id) {
|
|
335
|
+
const res = await this.request(`${this.baseUrl}/api/organizations/${id}`);
|
|
336
|
+
const json = await res.json();
|
|
337
|
+
return json.data ?? json;
|
|
338
|
+
}
|
|
339
|
+
/** List all organizations */
|
|
340
|
+
async listOrganizations() {
|
|
341
|
+
const res = await this.request(`${this.baseUrl}/api/organizations`);
|
|
342
|
+
const json = await res.json();
|
|
343
|
+
return json.data ?? json;
|
|
344
|
+
}
|
|
345
|
+
// ── Domain Roles ──────────────────────────────────────────────────────────
|
|
346
|
+
/** List domain roles (optionally filtered) */
|
|
347
|
+
async listDomainRoles(filters) {
|
|
348
|
+
const params = new URLSearchParams();
|
|
349
|
+
if (filters?.user_id) params.set("user_id", filters.user_id);
|
|
350
|
+
if (filters?.organization_id) params.set("organization_id", filters.organization_id);
|
|
351
|
+
if (filters?.domain) params.set("domain", filters.domain);
|
|
352
|
+
const qs = params.toString();
|
|
353
|
+
const url = `${this.baseUrl}/api/domains/roles${qs ? `?${qs}` : ""}`;
|
|
354
|
+
const res = await this.request(url);
|
|
355
|
+
const json = await res.json();
|
|
356
|
+
return json.data ?? json;
|
|
357
|
+
}
|
|
358
|
+
/** Grant a domain role to a user */
|
|
359
|
+
async grantDomainRole(params) {
|
|
360
|
+
const res = await this.request(`${this.baseUrl}/api/domains/roles/grant`, {
|
|
361
|
+
method: "POST",
|
|
362
|
+
body: JSON.stringify(params)
|
|
363
|
+
});
|
|
364
|
+
return res.json();
|
|
365
|
+
}
|
|
366
|
+
/** Revoke a domain role from a user */
|
|
367
|
+
async revokeDomainRole(params) {
|
|
368
|
+
const res = await this.request(`${this.baseUrl}/api/domains/roles/revoke`, {
|
|
369
|
+
method: "DELETE",
|
|
370
|
+
body: JSON.stringify(params)
|
|
371
|
+
});
|
|
372
|
+
return res.json();
|
|
373
|
+
}
|
|
374
|
+
// ── Roles (RBAC) ──────────────────────────────────────────────────────────
|
|
375
|
+
/** List all roles */
|
|
376
|
+
async listRoles() {
|
|
377
|
+
const res = await this.request(`${this.baseUrl}/api/roles`);
|
|
378
|
+
const json = await res.json();
|
|
379
|
+
return json.data ?? json;
|
|
380
|
+
}
|
|
381
|
+
/** Create a role */
|
|
382
|
+
async createRole(params) {
|
|
383
|
+
const res = await this.request(`${this.baseUrl}/api/roles`, {
|
|
384
|
+
method: "POST",
|
|
385
|
+
body: JSON.stringify(params)
|
|
386
|
+
});
|
|
387
|
+
const json = await res.json();
|
|
388
|
+
return json.data ?? json;
|
|
389
|
+
}
|
|
390
|
+
/** Delete a role */
|
|
391
|
+
async deleteRole(id) {
|
|
392
|
+
await this.request(`${this.baseUrl}/api/roles/${id}`, { method: "DELETE" });
|
|
393
|
+
}
|
|
394
|
+
// ── User Roles ────────────────────────────────────────────────────────────
|
|
395
|
+
/** Get user's effective scopes */
|
|
396
|
+
async getEffectiveScopes(userId) {
|
|
397
|
+
const res = await this.request(`${this.baseUrl}/api/users/${userId}/effective-scopes`);
|
|
398
|
+
const json = await res.json();
|
|
399
|
+
return json.data ?? json;
|
|
400
|
+
}
|
|
401
|
+
// ── Internal ──────────────────────────────────────────────────────────────
|
|
402
|
+
getAccessToken() {
|
|
403
|
+
if (typeof globalThis !== "undefined" && "localStorage" in globalThis) {
|
|
404
|
+
const storageKey = this.config.storageKey || "thalamus_auth";
|
|
405
|
+
const saved = globalThis.localStorage.getItem(storageKey);
|
|
406
|
+
if (saved) {
|
|
407
|
+
try {
|
|
408
|
+
return JSON.parse(saved).accessToken;
|
|
409
|
+
} catch {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
return null;
|
|
415
|
+
}
|
|
416
|
+
async request(url, options = {}) {
|
|
417
|
+
const token = this.getAccessToken();
|
|
418
|
+
const res = await fetch(url, {
|
|
419
|
+
...options,
|
|
420
|
+
headers: {
|
|
421
|
+
"Content-Type": "application/json",
|
|
422
|
+
...token ? { Authorization: `Bearer ${token}` } : {},
|
|
423
|
+
...options.headers
|
|
424
|
+
}
|
|
425
|
+
});
|
|
426
|
+
if (!res.ok) {
|
|
427
|
+
throw await this.toError(res);
|
|
428
|
+
}
|
|
429
|
+
return res;
|
|
430
|
+
}
|
|
431
|
+
async toError(response) {
|
|
432
|
+
let body = {};
|
|
433
|
+
try {
|
|
434
|
+
body = await response.json();
|
|
435
|
+
} catch {
|
|
436
|
+
}
|
|
437
|
+
const error = new Error(
|
|
438
|
+
body.error_description || body.error || `HTTP ${response.status}`
|
|
439
|
+
);
|
|
440
|
+
error.statusCode = response.status;
|
|
441
|
+
error.error = body.error;
|
|
442
|
+
error.error_description = body.error_description;
|
|
443
|
+
return error;
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
|
|
447
|
+
// src/client/ThalamusClient.ts
|
|
448
|
+
var ThalamusClient = class {
|
|
449
|
+
/** OAuth2 authentication methods */
|
|
450
|
+
auth;
|
|
451
|
+
/** Token management and introspection */
|
|
452
|
+
tokens;
|
|
453
|
+
/** Admin API — users, orgs, roles, domain management */
|
|
454
|
+
admin;
|
|
455
|
+
config;
|
|
456
|
+
/**
|
|
457
|
+
* Create a new Thalamus client
|
|
458
|
+
*
|
|
459
|
+
* @param config - Client configuration
|
|
460
|
+
*/
|
|
461
|
+
constructor(config) {
|
|
462
|
+
if (!config.clientId) {
|
|
463
|
+
throw new Error("clientId is required");
|
|
464
|
+
}
|
|
465
|
+
if (!config.redirectUri) {
|
|
466
|
+
throw new Error("redirectUri is required");
|
|
467
|
+
}
|
|
468
|
+
if (!config.baseUrl) {
|
|
469
|
+
throw new Error("baseUrl is required");
|
|
470
|
+
}
|
|
471
|
+
config.baseUrl = config.baseUrl.replace(/\/$/, "");
|
|
472
|
+
this.config = config;
|
|
473
|
+
this.auth = new OAuth2(config);
|
|
474
|
+
this.tokens = new TokenManager(config);
|
|
475
|
+
this.admin = new AdminAPI(config);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Get the current configuration
|
|
479
|
+
*/
|
|
480
|
+
getConfig() {
|
|
481
|
+
return Object.freeze({ ...this.config });
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// src/hooks/useThalamus.ts
|
|
486
|
+
function useThalamus(options) {
|
|
487
|
+
const storageKey = options.storageKey || "thalamus_auth";
|
|
488
|
+
const clientRef = useRef(new ThalamusClient(options));
|
|
489
|
+
const client = clientRef.current;
|
|
490
|
+
const [token, setToken] = useState(null);
|
|
491
|
+
const [user, setUser] = useState(null);
|
|
492
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
493
|
+
const [error, setError] = useState(null);
|
|
494
|
+
useEffect(() => {
|
|
495
|
+
try {
|
|
496
|
+
const saved = localStorage.getItem(storageKey);
|
|
497
|
+
if (saved) {
|
|
498
|
+
const { accessToken, refreshToken: rt } = JSON.parse(saved);
|
|
499
|
+
setToken(accessToken);
|
|
500
|
+
client.tokens.getUserInfo(accessToken).then((u) => setUser(u)).catch(() => {
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
} catch {
|
|
504
|
+
} finally {
|
|
505
|
+
setIsLoading(false);
|
|
506
|
+
}
|
|
507
|
+
}, [storageKey]);
|
|
508
|
+
useEffect(() => {
|
|
509
|
+
const params = new URLSearchParams(window.location.search);
|
|
510
|
+
const code = params.get("code");
|
|
511
|
+
const state = params.get("state");
|
|
512
|
+
if (!code || !state) return;
|
|
513
|
+
window.history.replaceState({}, "", window.location.pathname);
|
|
514
|
+
const savedState = sessionStorage.getItem(`${storageKey}_state`);
|
|
515
|
+
if (state !== savedState) {
|
|
516
|
+
setError("State mismatch \u2014 possible CSRF attack");
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
const verifier = sessionStorage.getItem(`${storageKey}_verifier`);
|
|
520
|
+
if (!verifier) {
|
|
521
|
+
setError("Missing code verifier");
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
client.auth.exchangeCode({ code, codeVerifier: verifier }).then((data) => {
|
|
525
|
+
persistAuth(data);
|
|
526
|
+
setToken(data.access_token);
|
|
527
|
+
client.tokens.getUserInfo(data.access_token).then((u) => setUser(u)).catch(() => {
|
|
528
|
+
});
|
|
529
|
+
}).catch((err) => setError(err.message));
|
|
530
|
+
}, [storageKey]);
|
|
531
|
+
const persistAuth = (data) => {
|
|
532
|
+
localStorage.setItem(storageKey, JSON.stringify({ accessToken: data.access_token, refreshToken: data.refresh_token || null, expiresAt: Date.now() + data.expires_in * 1e3 }));
|
|
533
|
+
};
|
|
534
|
+
const login = useCallback(async (opts) => {
|
|
535
|
+
setError(null);
|
|
536
|
+
const verifier = client.auth.generateState();
|
|
537
|
+
const challenge = await pkceChallenge(verifier);
|
|
538
|
+
sessionStorage.setItem(`${storageKey}_verifier`, verifier);
|
|
539
|
+
const url = client.auth.getAuthorizationUrl({ scope: opts?.scope, codeChallenge: challenge, codeChallengeMethod: "S256" });
|
|
540
|
+
sessionStorage.setItem(`${storageKey}_state`, new URL(url).searchParams.get("state") || "");
|
|
541
|
+
window.location.href = url;
|
|
542
|
+
}, [storageKey]);
|
|
543
|
+
const logout = useCallback(() => {
|
|
544
|
+
localStorage.removeItem(storageKey);
|
|
545
|
+
setToken(null);
|
|
546
|
+
setUser(null);
|
|
547
|
+
}, [storageKey]);
|
|
548
|
+
const refreshToken = useCallback(async () => {
|
|
549
|
+
try {
|
|
550
|
+
const saved = localStorage.getItem(storageKey);
|
|
551
|
+
if (!saved) return;
|
|
552
|
+
const { refreshToken: rt } = JSON.parse(saved);
|
|
553
|
+
if (!rt) return;
|
|
554
|
+
const data = await client.auth.refreshToken({ refreshToken: rt });
|
|
555
|
+
persistAuth(data);
|
|
556
|
+
setToken(data.access_token);
|
|
557
|
+
} catch {
|
|
558
|
+
}
|
|
559
|
+
}, [storageKey]);
|
|
560
|
+
return {
|
|
561
|
+
login,
|
|
562
|
+
logout,
|
|
563
|
+
token,
|
|
564
|
+
user,
|
|
565
|
+
isAuthenticated: !!token,
|
|
566
|
+
isLoading,
|
|
567
|
+
refreshToken,
|
|
568
|
+
client,
|
|
569
|
+
error
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
async function pkceChallenge(verifier) {
|
|
573
|
+
const encoder = new TextEncoder();
|
|
574
|
+
const hash = await crypto.subtle.digest("SHA-256", encoder.encode(verifier));
|
|
575
|
+
return btoa(String.fromCharCode(...new Uint8Array(hash))).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
576
|
+
}
|
|
577
|
+
function useAdmin(options) {
|
|
578
|
+
const { baseUrl } = options;
|
|
579
|
+
const clientRef = useRef(new ThalamusClient({ clientId: "admin", redirectUri: typeof window !== "undefined" ? window.location.origin : "http://localhost", baseUrl }));
|
|
580
|
+
const [users, setUsers] = useState([]);
|
|
581
|
+
const [loading, setLoading] = useState(false);
|
|
582
|
+
const [error, setError] = useState(null);
|
|
583
|
+
const refresh = async () => {
|
|
584
|
+
setLoading(true);
|
|
585
|
+
setError(null);
|
|
586
|
+
try {
|
|
587
|
+
const u = await clientRef.current.admin.listUsers();
|
|
588
|
+
setUsers(u);
|
|
589
|
+
} catch (e) {
|
|
590
|
+
setError(e.message);
|
|
591
|
+
}
|
|
592
|
+
setLoading(false);
|
|
593
|
+
};
|
|
594
|
+
useEffect(() => {
|
|
595
|
+
if (options.autoFetch !== false) refresh();
|
|
596
|
+
}, [baseUrl]);
|
|
597
|
+
const agents = users.filter((u) => u.is_agent);
|
|
598
|
+
const organizations = [];
|
|
599
|
+
const roles = [];
|
|
600
|
+
const createUser = async (data) => {
|
|
601
|
+
try {
|
|
602
|
+
const u = await clientRef.current.admin.createUser(data);
|
|
603
|
+
setUsers((prev) => [...prev, u]);
|
|
604
|
+
return u;
|
|
605
|
+
} catch (e) {
|
|
606
|
+
setError(e.message);
|
|
607
|
+
return null;
|
|
608
|
+
}
|
|
609
|
+
};
|
|
610
|
+
const listDomainRoles = async (filters) => {
|
|
611
|
+
return clientRef.current.admin.listDomainRoles(filters);
|
|
612
|
+
};
|
|
613
|
+
return { users, agents, organizations, roles, loading, error, refresh, createUser, listDomainRoles };
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
export { useAdmin, useThalamus };
|
|
617
|
+
//# sourceMappingURL=index.mjs.map
|
|
618
|
+
//# sourceMappingURL=index.mjs.map
|