@enterprisestandard/react 0.0.3-beta.2 → 0.0.3-beta.20251014.1
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/dist/index.d.ts +0 -1
- package/dist/index.js +54 -40
- package/dist/sso.d.ts +4 -4
- package/dist/vault.d.ts +3 -3
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -12,7 +12,6 @@ export type EnterpriseStandard = {
|
|
|
12
12
|
type ESConfig = {
|
|
13
13
|
ioniteUrl?: string;
|
|
14
14
|
defaultInstance?: boolean;
|
|
15
|
-
ssoUserUrl?: string;
|
|
16
15
|
};
|
|
17
16
|
export declare function enterpriseStandard(appId: string, appKey?: string, initConfig?: ESConfig): Promise<EnterpriseStandard>;
|
|
18
17
|
export type * from './enterprise-user';
|
package/dist/index.js
CHANGED
|
@@ -34,10 +34,10 @@ var jwksCache = new Map;
|
|
|
34
34
|
function sso(config) {
|
|
35
35
|
const configWithDefaults = {
|
|
36
36
|
...config,
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
cookies_secure: config.cookies_secure !== undefined ? config.cookies_secure : true,
|
|
38
|
+
cookies_same_site: config.cookies_same_site !== undefined ? config.cookies_same_site : "Strict",
|
|
39
|
+
cookies_prefix: config.cookies_prefix ?? `es.sso.${config.client_id}`,
|
|
40
|
+
cookies_path: config.cookies_path ?? "/"
|
|
41
41
|
};
|
|
42
42
|
async function getUser(request) {
|
|
43
43
|
if (!configWithDefaults) {
|
|
@@ -45,10 +45,10 @@ function sso(config) {
|
|
|
45
45
|
return;
|
|
46
46
|
}
|
|
47
47
|
try {
|
|
48
|
-
const
|
|
49
|
-
if (!
|
|
48
|
+
const { tokens } = await getTokenFromCookies(request);
|
|
49
|
+
if (!tokens)
|
|
50
50
|
return;
|
|
51
|
-
return await parseUser(
|
|
51
|
+
return await parseUser(tokens);
|
|
52
52
|
} catch (error) {
|
|
53
53
|
console.error("Error parsing user from cookies:", error);
|
|
54
54
|
return;
|
|
@@ -334,7 +334,7 @@ function sso(config) {
|
|
|
334
334
|
const refresh_token = getCookie("refresh", req);
|
|
335
335
|
const control = getCookie("control", req, true);
|
|
336
336
|
if (!access_token || !id_token || !refresh_token || !control) {
|
|
337
|
-
return;
|
|
337
|
+
return { tokens: undefined, refreshHeaders: [] };
|
|
338
338
|
}
|
|
339
339
|
let tokenResponse = {
|
|
340
340
|
access_token,
|
|
@@ -344,17 +344,20 @@ function sso(config) {
|
|
|
344
344
|
};
|
|
345
345
|
if (control.expires && refresh_token && Date.now() > new Date(control.expires).getTime()) {
|
|
346
346
|
tokenResponse = await refreshToken(refresh_token);
|
|
347
|
+
const user = await parseUser(tokenResponse);
|
|
348
|
+
const refreshHeaders = createJwtCookies(tokenResponse, user.sso.expires);
|
|
349
|
+
return { tokens: tokenResponse, refreshHeaders };
|
|
347
350
|
}
|
|
348
|
-
return tokenResponse;
|
|
351
|
+
return { tokens: tokenResponse, refreshHeaders: [] };
|
|
349
352
|
}
|
|
350
353
|
async function getJwt(request) {
|
|
351
|
-
const
|
|
352
|
-
if (!
|
|
354
|
+
const { tokens } = await getTokenFromCookies(request);
|
|
355
|
+
if (!tokens)
|
|
353
356
|
return;
|
|
354
|
-
return
|
|
357
|
+
return tokens.access_token;
|
|
355
358
|
}
|
|
356
359
|
function createCookie(name, value, expires) {
|
|
357
|
-
name = `${configWithDefaults.
|
|
360
|
+
name = `${configWithDefaults.cookies_prefix}.${name}`;
|
|
358
361
|
if (typeof value !== "string") {
|
|
359
362
|
value = btoa(JSON.stringify(value));
|
|
360
363
|
}
|
|
@@ -369,16 +372,16 @@ function sso(config) {
|
|
|
369
372
|
if (value.length > 4000) {
|
|
370
373
|
throw new Error(`Error setting cookie: ${name}. Cookie length is: ${value.length}`);
|
|
371
374
|
}
|
|
372
|
-
return `${name}=${value}; ${exp}; Path=${configWithDefaults.
|
|
375
|
+
return `${name}=${value}; ${exp}; Path=${configWithDefaults.cookies_path}; HttpOnly;${configWithDefaults.cookies_secure ? " Secure;" : ""} SameSite=${configWithDefaults.cookies_same_site};`;
|
|
373
376
|
}
|
|
374
377
|
function clearCookie(name) {
|
|
375
|
-
return `${configWithDefaults.
|
|
378
|
+
return `${configWithDefaults.cookies_prefix}.${name}=; Max-Age=0; Path=${configWithDefaults.cookies_path}; HttpOnly;${configWithDefaults.cookies_secure ? " Secure;" : ""} SameSite=${configWithDefaults.cookies_same_site};`;
|
|
376
379
|
}
|
|
377
380
|
function getCookie(name, req, parse = false) {
|
|
378
381
|
const header = req.headers.get("cookie");
|
|
379
382
|
if (!header)
|
|
380
383
|
return null;
|
|
381
|
-
const cookie = header.split(";").find((row) => row.trim().startsWith(`${configWithDefaults.
|
|
384
|
+
const cookie = header.split(";").find((row) => row.trim().startsWith(`${configWithDefaults.cookies_prefix}.${name}=`));
|
|
382
385
|
if (!cookie)
|
|
383
386
|
return null;
|
|
384
387
|
const val = cookie.split("=")[1].trim();
|
|
@@ -396,32 +399,39 @@ function sso(config) {
|
|
|
396
399
|
return callbackHandler(request);
|
|
397
400
|
}
|
|
398
401
|
if (userUrl === path) {
|
|
399
|
-
const
|
|
400
|
-
if (!
|
|
402
|
+
const { tokens, refreshHeaders } = await getTokenFromCookies(request);
|
|
403
|
+
if (!tokens) {
|
|
401
404
|
return new Response("User not logged in", { status: 401 });
|
|
402
405
|
}
|
|
406
|
+
const user = await parseUser(tokens);
|
|
403
407
|
return new Response(JSON.stringify(user), {
|
|
404
|
-
headers: [["Content-Type", "application/json"]]
|
|
408
|
+
headers: [["Content-Type", "application/json"], ...refreshHeaders]
|
|
405
409
|
});
|
|
406
410
|
}
|
|
407
411
|
if (tokenUrl === path) {
|
|
408
|
-
const
|
|
409
|
-
if (!
|
|
412
|
+
const { tokens, refreshHeaders } = await getTokenFromCookies(request);
|
|
413
|
+
if (!tokens) {
|
|
410
414
|
return new Response("User not logged in", { status: 401 });
|
|
411
415
|
}
|
|
412
416
|
return new Response(JSON.stringify({
|
|
413
|
-
token:
|
|
414
|
-
expires:
|
|
417
|
+
token: tokens.access_token,
|
|
418
|
+
expires: tokens.expires
|
|
415
419
|
}), {
|
|
416
|
-
headers: [["Content-Type", "application/json"]]
|
|
420
|
+
headers: [["Content-Type", "application/json"], ...refreshHeaders]
|
|
417
421
|
});
|
|
418
422
|
}
|
|
419
423
|
if (refreshUrl === path) {
|
|
420
|
-
const
|
|
421
|
-
if (!
|
|
424
|
+
const refresh_token = getCookie("refresh", request);
|
|
425
|
+
if (!refresh_token) {
|
|
422
426
|
return new Response("User not logged in", { status: 401 });
|
|
423
427
|
}
|
|
424
|
-
|
|
428
|
+
const newTokenResponse = await refreshToken(refresh_token);
|
|
429
|
+
const user = await parseUser(newTokenResponse);
|
|
430
|
+
const refreshHeaders = createJwtCookies(newTokenResponse, user.sso.expires);
|
|
431
|
+
return new Response("Refresh Complete", {
|
|
432
|
+
status: 200,
|
|
433
|
+
headers: refreshHeaders
|
|
434
|
+
});
|
|
425
435
|
}
|
|
426
436
|
if (loginUrl === "*" || loginUrl === path) {
|
|
427
437
|
return initiateLogin({
|
|
@@ -442,8 +452,8 @@ function sso(config) {
|
|
|
442
452
|
}
|
|
443
453
|
|
|
444
454
|
// src/vault.ts
|
|
445
|
-
function vault(url
|
|
446
|
-
async function getFullSecret(path) {
|
|
455
|
+
function vault(url) {
|
|
456
|
+
async function getFullSecret(path, token) {
|
|
447
457
|
const resp = await fetch(`${url}/${path}`, { headers: { "X-Vault-Token": token } });
|
|
448
458
|
if (resp.status !== 200) {
|
|
449
459
|
throw new Error(`Vault returned invalid status, ${resp.status}: '${resp.statusText}' from URL: ${url}`);
|
|
@@ -458,8 +468,8 @@ function vault(url, token) {
|
|
|
458
468
|
return {
|
|
459
469
|
url,
|
|
460
470
|
getFullSecret,
|
|
461
|
-
getSecret: async (path) => {
|
|
462
|
-
return (await getFullSecret(path)).data;
|
|
471
|
+
getSecret: async (path, token) => {
|
|
472
|
+
return (await getFullSecret(path, token)).data;
|
|
463
473
|
}
|
|
464
474
|
};
|
|
465
475
|
}
|
|
@@ -632,8 +642,8 @@ function SignedIn({ children }) {
|
|
|
632
642
|
// src/ui/signed-out.tsx
|
|
633
643
|
import { jsxDEV as jsxDEV3, Fragment as Fragment3 } from "react/jsx-dev-runtime";
|
|
634
644
|
function SignedOut({ children }) {
|
|
635
|
-
const { user } = useUser();
|
|
636
|
-
if (user)
|
|
645
|
+
const { user, isLoading } = useUser();
|
|
646
|
+
if (user || isLoading)
|
|
637
647
|
return null;
|
|
638
648
|
return /* @__PURE__ */ jsxDEV3(Fragment3, {
|
|
639
649
|
children
|
|
@@ -870,29 +880,33 @@ function useToken() {
|
|
|
870
880
|
async function enterpriseStandard(appId, appKey, initConfig) {
|
|
871
881
|
let vaultUrl;
|
|
872
882
|
let vaultToken;
|
|
873
|
-
let
|
|
883
|
+
let secrets;
|
|
874
884
|
const ioniteUrl = initConfig?.ioniteUrl ?? "https://ionite.com";
|
|
875
885
|
if (appId === "IONITE_PUBLIC_DEMO") {
|
|
876
886
|
vaultUrl = "https://vault-ionite.ionite.dev/v1/secret/data";
|
|
877
|
-
|
|
878
|
-
|
|
887
|
+
secrets = {
|
|
888
|
+
sso: {
|
|
889
|
+
path: "public/IONITE_PUBLIC_DEMO_SSO",
|
|
890
|
+
token: "hvs.CAESIDGntTzqry6HOySoqAGUMQyfkF4RDj3xas23zhsJC6-uGh4KHGh2cy5mSzZXNHFQRWlZMG9VVnpWSlA1Zk1YeUE"
|
|
891
|
+
}
|
|
892
|
+
};
|
|
879
893
|
} else if (appKey) {
|
|
880
894
|
if (!vaultUrl || !vaultToken) {
|
|
881
895
|
throw new Error("TODO something is wrong with the ionite config, handle this error");
|
|
882
896
|
}
|
|
883
|
-
|
|
897
|
+
secrets = {};
|
|
884
898
|
} else {
|
|
885
899
|
throw new Error("TODO tell them how to connect to ionite");
|
|
886
900
|
}
|
|
887
901
|
const defaultInstance2 = getDefaultInstance();
|
|
888
|
-
const vaultClient = await vault(vaultUrl
|
|
902
|
+
const vaultClient = await vault(vaultUrl);
|
|
889
903
|
const result = {
|
|
890
904
|
appId,
|
|
891
905
|
ioniteUrl,
|
|
892
906
|
defaultInstance: initConfig?.defaultInstance || initConfig?.defaultInstance !== false && !defaultInstance2,
|
|
893
907
|
vault: vaultClient,
|
|
894
|
-
sso:
|
|
895
|
-
iam:
|
|
908
|
+
sso: secrets.sso ? sso(await vaultClient.getSecret(secrets.sso.path, secrets.sso.token)) : undefined,
|
|
909
|
+
iam: secrets.iam ? await iam(await vaultClient.getSecret(secrets.iam.path, secrets.iam.token)) : undefined
|
|
896
910
|
};
|
|
897
911
|
if (result.defaultInstance) {
|
|
898
912
|
if (defaultInstance2) {
|
package/dist/sso.d.ts
CHANGED
|
@@ -10,10 +10,10 @@ export type SSOConfig = {
|
|
|
10
10
|
post_logout_redirect_uri?: string;
|
|
11
11
|
silent_redirect_uri?: string;
|
|
12
12
|
jwks_uri?: string;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
cookies_prefix?: string;
|
|
14
|
+
cookies_path?: string;
|
|
15
|
+
cookies_secure?: boolean;
|
|
16
|
+
cookies_same_site?: 'Strict' | 'Lax';
|
|
17
17
|
};
|
|
18
18
|
export type ESConfig = {
|
|
19
19
|
es?: EnterpriseStandard;
|
package/dist/vault.d.ts
CHANGED
|
@@ -10,8 +10,8 @@ type MetaData = {
|
|
|
10
10
|
};
|
|
11
11
|
export type Vault = {
|
|
12
12
|
url: string;
|
|
13
|
-
getFullSecret: <T>(path: string) => Promise<Secret<T>>;
|
|
14
|
-
getSecret: <T>(path: string) => Promise<T>;
|
|
13
|
+
getFullSecret: <T>(path: string, token: string) => Promise<Secret<T>>;
|
|
14
|
+
getSecret: <T>(path: string, token: string) => Promise<T>;
|
|
15
15
|
};
|
|
16
|
-
export declare function vault(url: string
|
|
16
|
+
export declare function vault(url: string): Vault;
|
|
17
17
|
export {};
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@enterprisestandard/react",
|
|
3
|
-
"version": "0.0.3-beta.
|
|
3
|
+
"version": "0.0.3-beta.20251014.1",
|
|
4
4
|
"description": "Enterprise Standard React Components",
|
|
5
5
|
"private": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build": "bun run build.ts"
|
|
9
|
-
"prepublishOnly": "bun run build"
|
|
8
|
+
"build": "bun run build.ts"
|
|
10
9
|
},
|
|
11
10
|
"types": "./dist/index.d.ts",
|
|
12
11
|
"exports": {
|
|
@@ -30,6 +29,7 @@
|
|
|
30
29
|
"access": "public"
|
|
31
30
|
},
|
|
32
31
|
"devDependencies": {
|
|
32
|
+
"@types/react": "^18.0.0",
|
|
33
33
|
"typescript": "^5.0.0"
|
|
34
34
|
},
|
|
35
35
|
"author": "enterprisestandard",
|