@burdenoff/microfe-security 2026.530.1 → 2026.601.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.
|
@@ -1,103 +1,111 @@
|
|
|
1
1
|
import { createContext as e, use as t, useMemo as n } from "react";
|
|
2
2
|
import { PermissionProvider as r } from "@burdenoff/fe-libs/shared/providers/shell";
|
|
3
3
|
import { MemoryRouter as i } from "react-router-dom";
|
|
4
|
-
import {
|
|
5
|
-
import { ApolloClient as
|
|
6
|
-
import { onError as
|
|
7
|
-
import { ApolloProvider as
|
|
8
|
-
import { isAuthNetworkError as
|
|
9
|
-
import { jsx as
|
|
4
|
+
import { MutationCache as a, QueryClient as o, QueryClientProvider as s } from "@tanstack/react-query";
|
|
5
|
+
import { ApolloClient as c, HttpLink as l, InMemoryCache as u, from as d } from "@apollo/client";
|
|
6
|
+
import { onError as f } from "@apollo/client/link/error";
|
|
7
|
+
import { ApolloProvider as p } from "@apollo/client/react";
|
|
8
|
+
import { isAuthNetworkError as m, redirectToLogin as h } from "@burdenoff/fe-libs/shared/utils";
|
|
9
|
+
import { jsx as g } from "react/jsx-runtime";
|
|
10
10
|
//#region src/security/providers/SecurityProvider.tsx
|
|
11
|
-
function
|
|
11
|
+
function _(e = []) {
|
|
12
12
|
return e.some((e) => {
|
|
13
13
|
let t = typeof e.extensions?.code == "string" ? e.extensions.code : "", n = typeof e.extensions?.errorCode == "string" ? e.extensions.errorCode : "", r = (e.message ?? "").toLowerCase();
|
|
14
14
|
return t === "UNAUTHENTICATED" || n === "TOKEN_EXPIRED" || r.includes("token_expired") || r.includes("token expired") || r.includes("session expired") || r.includes("unauthorized") || r.includes("unauthenticated") || r.includes("authentication required") || r.includes("workspace context missing");
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
-
var
|
|
18
|
-
let e = t(
|
|
17
|
+
var v = e(null), y = () => {
|
|
18
|
+
let e = t(v);
|
|
19
19
|
if (!e) throw Error("useSecurity must be used within SecurityProvider");
|
|
20
20
|
return e;
|
|
21
|
-
},
|
|
22
|
-
let
|
|
21
|
+
}, b = ({ children: e, basePath: t, navigate: y, onAuthError: b, getWorkspaceToken: x, currentUser: S, workspaceId: C, apiGatewayUrl: w, authToken: T, workspaceToken: E }) => {
|
|
22
|
+
let D = {
|
|
23
23
|
basePath: t,
|
|
24
|
-
navigate:
|
|
25
|
-
onAuthError:
|
|
26
|
-
getWorkspaceToken:
|
|
27
|
-
currentUser:
|
|
28
|
-
workspaceId:
|
|
29
|
-
apiGatewayUrl:
|
|
30
|
-
authToken:
|
|
31
|
-
workspaceToken:
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
24
|
+
navigate: y,
|
|
25
|
+
onAuthError: b,
|
|
26
|
+
getWorkspaceToken: x,
|
|
27
|
+
currentUser: S,
|
|
28
|
+
workspaceId: C,
|
|
29
|
+
apiGatewayUrl: w,
|
|
30
|
+
authToken: T,
|
|
31
|
+
workspaceToken: E
|
|
32
|
+
}, O = n(() => {
|
|
33
|
+
let e = new o({
|
|
34
|
+
mutationCache: new a({ onSuccess: () => {
|
|
35
|
+
e.invalidateQueries();
|
|
36
|
+
} }),
|
|
37
|
+
defaultOptions: { queries: {
|
|
38
|
+
retry: 1,
|
|
39
|
+
refetchOnWindowFocus: !1,
|
|
40
|
+
staleTime: 0
|
|
41
|
+
} }
|
|
42
|
+
});
|
|
43
|
+
return e;
|
|
44
|
+
}, []), k = n(() => {
|
|
37
45
|
let e = {};
|
|
38
|
-
|
|
39
|
-
let t = new
|
|
40
|
-
uri:
|
|
46
|
+
T && (e.Authorization = `Bearer ${T}`), E && (e["X-Workspace-Authorization"] = `Bearer ${E}`);
|
|
47
|
+
let t = new l({
|
|
48
|
+
uri: w ? `${w}/workspaces/graphql` : "/workspaces/graphql",
|
|
41
49
|
headers: Object.keys(e).length > 0 ? e : void 0
|
|
42
50
|
});
|
|
43
|
-
return new
|
|
44
|
-
link:
|
|
51
|
+
return new c({
|
|
52
|
+
link: d([f(({ error: e }) => {
|
|
45
53
|
if (e) {
|
|
46
|
-
if (
|
|
47
|
-
if (
|
|
48
|
-
|
|
54
|
+
if (_("errors" in e && Array.isArray(e.errors) ? e.errors : [])) {
|
|
55
|
+
if (T && !E && C) {
|
|
56
|
+
x?.();
|
|
49
57
|
return;
|
|
50
58
|
}
|
|
51
|
-
if (
|
|
52
|
-
Promise.resolve(
|
|
59
|
+
if (b) {
|
|
60
|
+
Promise.resolve(b());
|
|
53
61
|
return;
|
|
54
62
|
}
|
|
55
|
-
|
|
63
|
+
h();
|
|
56
64
|
return;
|
|
57
65
|
}
|
|
58
|
-
if (e instanceof Error &&
|
|
59
|
-
if (
|
|
60
|
-
|
|
66
|
+
if (e instanceof Error && m(e)) {
|
|
67
|
+
if (T && !E && C) {
|
|
68
|
+
x?.();
|
|
61
69
|
return;
|
|
62
70
|
}
|
|
63
|
-
if (
|
|
64
|
-
Promise.resolve(
|
|
71
|
+
if (b) {
|
|
72
|
+
Promise.resolve(b());
|
|
65
73
|
return;
|
|
66
74
|
}
|
|
67
|
-
|
|
75
|
+
h();
|
|
68
76
|
}
|
|
69
77
|
}
|
|
70
78
|
}), t]),
|
|
71
|
-
cache: new
|
|
79
|
+
cache: new u(),
|
|
72
80
|
defaultOptions: {
|
|
73
81
|
watchQuery: { fetchPolicy: "network-only" },
|
|
74
82
|
query: { fetchPolicy: "network-only" }
|
|
75
83
|
}
|
|
76
84
|
});
|
|
77
85
|
}, [
|
|
78
|
-
C,
|
|
79
86
|
w,
|
|
80
87
|
T,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
b
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
88
|
+
E,
|
|
89
|
+
C,
|
|
90
|
+
b,
|
|
91
|
+
x
|
|
92
|
+
]), A = !!t, j = /* @__PURE__ */ g(s, {
|
|
93
|
+
client: O,
|
|
94
|
+
children: /* @__PURE__ */ g(v.Provider, {
|
|
95
|
+
value: D,
|
|
96
|
+
children: /* @__PURE__ */ g(r, {
|
|
97
|
+
scopeId: C ?? null,
|
|
90
98
|
gateway: "workspace",
|
|
91
99
|
children: e
|
|
92
100
|
})
|
|
93
101
|
})
|
|
94
102
|
});
|
|
95
|
-
return
|
|
96
|
-
client:
|
|
97
|
-
children: /* @__PURE__ */
|
|
103
|
+
return A ? j : /* @__PURE__ */ g(p, {
|
|
104
|
+
client: k,
|
|
105
|
+
children: /* @__PURE__ */ g(i, { children: j })
|
|
98
106
|
});
|
|
99
107
|
};
|
|
100
108
|
//#endregion
|
|
101
|
-
export {
|
|
109
|
+
export { b as SecurityProvider, y as useSecurity };
|
|
102
110
|
|
|
103
111
|
//# sourceMappingURL=SecurityProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SecurityProvider.js","names":[],"sources":["../../../src/security/providers/SecurityProvider.tsx"],"sourcesContent":["import type { FC, PropsWithChildren } from 'react';\nimport { createContext, use, useMemo } from 'react';\nimport { MemoryRouter } from 'react-router-dom';\nimport { QueryClient, QueryClientProvider } from '@tanstack/react-query';\nimport { ApolloClient, InMemoryCache, HttpLink, from } from '@apollo/client';\nimport { onError } from '@apollo/client/link/error';\nimport { ApolloProvider } from '@apollo/client/react';\nimport { isAuthNetworkError, redirectToLogin } from '@burdenoff/fe-libs/shared/utils';\nimport { PermissionProvider } from '@burdenoff/fe-libs/shared/providers/shell';\nimport type { SecurityRootProps } from '../types';\n\nfunction hasSessionAuthErrors(\n errors: ReadonlyArray<{ message?: string; extensions?: Record<string, unknown> }> = []\n): boolean {\n return errors.some((error) => {\n const code = typeof error.extensions?.code === 'string' ? error.extensions.code : '';\n const errorCode =\n typeof error.extensions?.errorCode === 'string' ? error.extensions.errorCode : '';\n const message = (error.message ?? '').toLowerCase();\n\n return (\n code === 'UNAUTHENTICATED' ||\n errorCode === 'TOKEN_EXPIRED' ||\n message.includes('token_expired') ||\n message.includes('token expired') ||\n message.includes('session expired') ||\n message.includes('unauthorized') ||\n message.includes('unauthenticated') ||\n message.includes('authentication required') ||\n message.includes('workspace context missing')\n );\n });\n}\n\n/**\n * Context value for Security microfrontend\n */\ntype SecurityContextValue = SecurityRootProps;\n\nconst SecurityContext = createContext<SecurityContextValue | null>(null);\n\n/**\n * Hook to access Security context\n */\nexport const useSecurity = () => {\n const context = use(SecurityContext);\n if (!context) {\n throw new Error('useSecurity must be used within SecurityProvider');\n }\n return context;\n};\n\n/**\n * Provider component for Security microfrontend\n * Sets up routing, context, Apollo Client, and React Query client\n */\nexport const SecurityProvider: FC<PropsWithChildren<SecurityRootProps>> = ({\n children,\n basePath,\n navigate: shellNavigate,\n onAuthError,\n getWorkspaceToken,\n currentUser,\n workspaceId,\n apiGatewayUrl,\n authToken,\n workspaceToken,\n}) => {\n const contextValue: SecurityContextValue = {\n basePath,\n navigate: shellNavigate,\n onAuthError,\n getWorkspaceToken,\n currentUser,\n workspaceId,\n apiGatewayUrl,\n authToken,\n workspaceToken,\n };\n\n // Create QueryClient instance (memoized to avoid recreation on re-renders)\n const queryClient = useMemo(\n ()
|
|
1
|
+
{"version":3,"file":"SecurityProvider.js","names":[],"sources":["../../../src/security/providers/SecurityProvider.tsx"],"sourcesContent":["import type { FC, PropsWithChildren } from 'react';\nimport { createContext, use, useMemo } from 'react';\nimport { MemoryRouter } from 'react-router-dom';\nimport { MutationCache, QueryClient, QueryClientProvider } from '@tanstack/react-query';\nimport { ApolloClient, InMemoryCache, HttpLink, from } from '@apollo/client';\nimport { onError } from '@apollo/client/link/error';\nimport { ApolloProvider } from '@apollo/client/react';\nimport { isAuthNetworkError, redirectToLogin } from '@burdenoff/fe-libs/shared/utils';\nimport { PermissionProvider } from '@burdenoff/fe-libs/shared/providers/shell';\nimport type { SecurityRootProps } from '../types';\n\nfunction hasSessionAuthErrors(\n errors: ReadonlyArray<{ message?: string; extensions?: Record<string, unknown> }> = []\n): boolean {\n return errors.some((error) => {\n const code = typeof error.extensions?.code === 'string' ? error.extensions.code : '';\n const errorCode =\n typeof error.extensions?.errorCode === 'string' ? error.extensions.errorCode : '';\n const message = (error.message ?? '').toLowerCase();\n\n return (\n code === 'UNAUTHENTICATED' ||\n errorCode === 'TOKEN_EXPIRED' ||\n message.includes('token_expired') ||\n message.includes('token expired') ||\n message.includes('session expired') ||\n message.includes('unauthorized') ||\n message.includes('unauthenticated') ||\n message.includes('authentication required') ||\n message.includes('workspace context missing')\n );\n });\n}\n\n/**\n * Context value for Security microfrontend\n */\ntype SecurityContextValue = SecurityRootProps;\n\nconst SecurityContext = createContext<SecurityContextValue | null>(null);\n\n/**\n * Hook to access Security context\n */\nexport const useSecurity = () => {\n const context = use(SecurityContext);\n if (!context) {\n throw new Error('useSecurity must be used within SecurityProvider');\n }\n return context;\n};\n\n/**\n * Provider component for Security microfrontend\n * Sets up routing, context, Apollo Client, and React Query client\n */\nexport const SecurityProvider: FC<PropsWithChildren<SecurityRootProps>> = ({\n children,\n basePath,\n navigate: shellNavigate,\n onAuthError,\n getWorkspaceToken,\n currentUser,\n workspaceId,\n apiGatewayUrl,\n authToken,\n workspaceToken,\n}) => {\n const contextValue: SecurityContextValue = {\n basePath,\n navigate: shellNavigate,\n onAuthError,\n getWorkspaceToken,\n currentUser,\n workspaceId,\n apiGatewayUrl,\n authToken,\n workspaceToken,\n };\n\n // Create QueryClient instance (memoized to avoid recreation on re-renders)\n const queryClient = useMemo(\n () => {\n const client = new QueryClient({\n mutationCache: new MutationCache({\n onSuccess: () => {\n void client.invalidateQueries();\n },\n }),\n defaultOptions: {\n queries: {\n retry: 1,\n refetchOnWindowFocus: false,\n staleTime: 0, // always refetch\n },\n },\n });\n return client;\n },\n []\n );\n\n // Create ApolloClient instance (memoized, recreated when tokens change)\n // The workspace gateway requires dual-token auth:\n // Authorization: Bearer <authToken> (platform identity)\n // X-Workspace-Authorization: Bearer <workspaceToken> (workspace access)\n // NOTE: shellNavigate is deliberately NOT a dep of this memo — see the\n // dep array at the bottom of the function for the rationale. Do not\n // reference shellNavigate inside this memo body without also adding it\n // to the dep array (which would defeat the purpose of the omission).\n const apolloClient = useMemo(() => {\n const headers: Record<string, string> = {};\n if (authToken) {\n headers['Authorization'] = `Bearer ${authToken}`;\n }\n if (workspaceToken) {\n headers['X-Workspace-Authorization'] = `Bearer ${workspaceToken}`;\n }\n\n const graphqlUri = apiGatewayUrl\n ? `${apiGatewayUrl}/workspaces/graphql`\n : '/workspaces/graphql';\n\n const httpLink = new HttpLink({\n uri: graphqlUri,\n headers: Object.keys(headers).length > 0 ? headers : undefined,\n });\n\n const errorLink = onError(({ error }) => {\n if (!error) return;\n\n const graphQLErrors = 'errors' in error && Array.isArray(error.errors) ? error.errors : [];\n\n if (hasSessionAuthErrors(graphQLErrors)) {\n if (authToken && !workspaceToken && workspaceId) {\n void getWorkspaceToken?.();\n return;\n }\n\n if (onAuthError) {\n void Promise.resolve(onAuthError());\n return;\n }\n\n redirectToLogin();\n return;\n }\n\n if (error instanceof Error && isAuthNetworkError(error)) {\n if (authToken && !workspaceToken && workspaceId) {\n void getWorkspaceToken?.();\n return;\n }\n\n if (onAuthError) {\n void Promise.resolve(onAuthError());\n return;\n }\n\n redirectToLogin();\n }\n });\n\n return new ApolloClient({\n link: from([errorLink, httpLink]),\n cache: new InMemoryCache(),\n defaultOptions: {\n watchQuery: {\n fetchPolicy: 'network-only',\n },\n query: {\n fetchPolicy: 'network-only',\n },\n },\n });\n }, [\n apiGatewayUrl,\n authToken,\n workspaceToken,\n workspaceId,\n onAuthError,\n getWorkspaceToken,\n // shellNavigate is intentionally NOT in this dep array — it is not\n // referenced inside the memo body, and shell apps typically pass a\n // new function reference on every render which would tear the\n // entire Apollo client + cache down once per parent render.\n ]);\n\n const isIntegrated = !!basePath;\n\n // Wrap content with QueryClientProvider and SecurityContext.Provider.\n // PermissionProvider requires the shell's AuthProvider — pages inside\n // security routes call usePermissions() and would throw without it.\n // Security calls workspace gateway, so scope to workspaceId.\n const content = (\n <QueryClientProvider client={queryClient}>\n <SecurityContext.Provider value={contextValue}>\n <PermissionProvider scopeId={workspaceId ?? null} gateway=\"workspace\">\n {children}\n </PermissionProvider>\n </SecurityContext.Provider>\n </QueryClientProvider>\n );\n\n // When integrated into a shell (basePath provided), the shell already has a router and ApolloProvider\n // so we don't create another one. When standalone (no basePath), use MemoryRouter and ApolloProvider.\n return isIntegrated ? (\n content\n ) : (\n <ApolloProvider client={apolloClient}>\n <MemoryRouter>{content}</MemoryRouter>\n </ApolloProvider>\n );\n};\n"],"mappings":";;;;;;;;;;AAWA,SAAS,EACP,IAAoF,EAAE,EAC7E;AACT,QAAO,EAAO,MAAM,MAAU;EAC5B,IAAM,IAAO,OAAO,EAAM,YAAY,QAAS,WAAW,EAAM,WAAW,OAAO,IAC5E,IACJ,OAAO,EAAM,YAAY,aAAc,WAAW,EAAM,WAAW,YAAY,IAC3E,KAAW,EAAM,WAAW,IAAI,aAAa;AAEnD,SACE,MAAS,qBACT,MAAc,mBACd,EAAQ,SAAS,gBAAgB,IACjC,EAAQ,SAAS,gBAAgB,IACjC,EAAQ,SAAS,kBAAkB,IACnC,EAAQ,SAAS,eAAe,IAChC,EAAQ,SAAS,kBAAkB,IACnC,EAAQ,SAAS,0BAA0B,IAC3C,EAAQ,SAAS,4BAA4B;GAE/C;;AAQJ,IAAM,IAAkB,EAA2C,KAAK,EAK3D,UAAoB;CAC/B,IAAM,IAAU,EAAI,EAAgB;AACpC,KAAI,CAAC,EACH,OAAU,MAAM,mDAAmD;AAErE,QAAO;GAOI,KAA8D,EACzE,aACA,aACA,UAAU,GACV,gBACA,sBACA,gBACA,gBACA,kBACA,cACA,wBACI;CACJ,IAAM,IAAqC;EACzC;EACA,UAAU;EACV;EACA;EACA;EACA;EACA;EACA;EACA;EACD,EAGK,IAAc,QACZ;EACJ,IAAM,IAAS,IAAI,EAAY;GAC7B,eAAe,IAAI,EAAc,EAC/B,iBAAiB;AACV,MAAO,mBAAmB;MAElC,CAAC;GACF,gBAAgB,E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