@scalio-cloud/ui 0.105.1 → 0.106.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.
package/README.md CHANGED
@@ -53,7 +53,7 @@ fails on an export without a type, a type without an export, and an `exports` pa
53
53
 
54
54
  `nginx/security-headers.conf` holds the security headers of every Scalio app (security review M6): `nosniff`,
55
55
  framing only by the own origin, no plugins, `Referrer-Policy`, no camera/microphone/location. From 0.104.0 also HSTS
56
- (one year, `includeSubDomains`) and a Content-Security-Policy (BL-1080): scripts only from the own origin (no inline
56
+ (one year, without `includeSubDomains`, from 0.106.0) and a Content-Security-Policy (BL-1080): scripts only from the own origin (no inline
57
57
  script, no `eval`), inline styles allowed, images also `data:`, `blob:` and HTTPS, fonts `data:`, connections to
58
58
  HTTPS/WSS. The file explains every exception. An app that needs more (another script host, `eval`) must say so
59
59
  before it upgrades; test the upgrade on dev with the browser console open (CSP errors show there).
@@ -2,7 +2,8 @@
2
2
  # app's Dockerfile to /etc/nginx/snippets/. Include it in the server block AND in every location that has its own
3
3
  # add_header: nginx drops the server-level add_header lines in such a location.
4
4
  #
5
- # HSTS: one year, subdomains included, no preload (preload cannot be undone quickly). Browsers ignore it over plain
5
+ # HSTS: one year, no includeSubDomains (self-hosted apps often run on the customer's apex domain, so all its
6
+ # subdomains would be forced to HTTPS for a year), no preload (preload cannot be undone quickly). Browsers ignore it over plain
6
7
  # HTTP, so an instance without TLS keeps working.
7
8
  #
8
9
  # Content-Security-Policy, what each part allows and why:
@@ -19,7 +20,7 @@
19
20
  # frame-ancestors 'self' framing only by the own origin (same as X-Frame-Options)
20
21
  # object-src 'none'; base-uri 'self' no plugins, no <base> from injected markup
21
22
  # No form-action: the Microsoft sign-in leaves by redirect, and Chrome checks form-action on redirects as well.
22
- add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
23
+ add_header Strict-Transport-Security "max-age=31536000" always;
23
24
  add_header X-Content-Type-Options "nosniff" always;
24
25
  add_header X-Frame-Options "SAMEORIGIN" always;
25
26
  add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; font-src 'self' data:; connect-src 'self' https: wss:; frame-src 'self' blob:; worker-src 'self' blob:; frame-ancestors 'self'; object-src 'none'; base-uri 'self'" always;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalio-cloud/ui",
3
- "version": "0.105.1",
3
+ "version": "0.106.0",
4
4
  "description": "Scalio Control Room design tokens, Tailwind preset, self-hosted fonts, the shared app header, sign-in layout, sidebar and page blocks",
5
5
  "license": "SEE LICENSE IN README.md",
6
6
  "private": false,
@@ -14,6 +14,10 @@ export type AuthState = {
14
14
  setup: (name: string, email: string, password: string, setupCode: string) => Promise<AuthResult>;
15
15
  logout: () => Promise<void>;
16
16
  updateUser: (user: any) => void;
17
+ /** The page signed in by itself: keeps the user and tells onSignedIn(true). */
18
+ loginWith: (user: any) => void;
19
+ /** Reloads /auth/me and answers the user (throws when the call fails). */
20
+ refresh: () => Promise<any>;
17
21
  };
18
22
 
19
23
  export declare const createAuth: (opts: {
@@ -26,6 +30,14 @@ export declare const createAuth: (opts: {
26
30
  passkeyCancelled?: (error: unknown) => boolean;
27
31
  /** localStorage key of an older token, removed on start. */
28
32
  legacyTokenKey?: string;
33
+ /** Awaited before /auth/me (e.g. redeem a one-time code). Errors are ignored. */
34
+ bootstrap?: (api: ApiClient) => void | Promise<void>;
35
+ /** Called when /auth/me refuses. Answer true after navigating away (hub redirect): the provider then stays loading. */
36
+ onNoSession?: (api: ApiClient) => boolean | void | Promise<boolean | void>;
37
+ /** Every change of the signed-in state. */
38
+ onSignedIn?: (signedIn: boolean) => void;
39
+ /** The window event "auth:unauthorized" while a user was signed in (e.g. show a toast). The user is signed out afterwards. */
40
+ onUnauthorized?: (previousUser: any) => void;
29
41
  }) => {
30
42
  AuthProvider: ScalioComponent<{ children?: React.ReactNode }>;
31
43
  /** Outside the AuthProvider the context is null at run time. */
package/react/session.js CHANGED
@@ -6,6 +6,14 @@
6
6
  // { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser }:
7
7
  // user is null while checking, false when signed out, else /api/auth/me. Every action answers { ok, error?, status? };
8
8
  // login with an authenticator app answers { ok: false, mfaToken } and the page asks for the code.
9
+ // It also has loginWith(user) (the page already signed in) and refresh() (reloads /auth/me, answers the user).
10
+ // Optional hooks (Compass: SSO hub, toast, request guard):
11
+ // bootstrap(api) awaited before /auth/me (e.g. redeem a one-time code); errors are ignored.
12
+ // onNoSession(api) called when /auth/me refuses; answer true when it navigated away (hub redirect),
13
+ // the provider then stays loading (ready false, user null).
14
+ // onSignedIn(bool) every change of the signed-in state (start, sign-in, loginWith, logout, unauthorized).
15
+ // onUnauthorized(prev) the window event "auth:unauthorized" (401 of the api client): signs out locally,
16
+ // calls the hook with the previous user (only when there was one).
9
17
  const React = require("react");
10
18
  const { setUserTimezone } = require("./profile");
11
19
 
@@ -17,6 +25,9 @@ function createAuth(opts) {
17
25
  function AuthProvider(props) {
18
26
  const [user, setUser] = React.useState(null); // null = checking
19
27
  const [ready, setReady] = React.useState(false);
28
+ const userRef = React.useRef(null);
29
+ userRef.current = user;
30
+ const notify = (v) => { if (opts.onSignedIn) opts.onSignedIn(v); };
20
31
 
21
32
  React.useEffect(() => {
22
33
  if (opts.legacyTokenKey) {
@@ -26,14 +37,42 @@ function createAuth(opts) {
26
37
  /* private mode */
27
38
  }
28
39
  }
29
- api
30
- .get("/auth/me")
31
- .then((r) => setUser(r.data))
32
- .catch(() => setUser(false))
33
- .finally(() => setReady(true));
40
+ (async () => {
41
+ if (opts.bootstrap) {
42
+ try { await opts.bootstrap(api); } catch (e) { /* e.g. expired code: continue signed out */ }
43
+ }
44
+ let me = null;
45
+ try { me = (await api.get("/auth/me")).data; } catch (e) { me = null; }
46
+ if (me) {
47
+ notify(true);
48
+ setUser(me);
49
+ } else {
50
+ let away = false;
51
+ if (opts.onNoSession) {
52
+ try { away = !!(await opts.onNoSession(api)); } catch (e) { away = false; }
53
+ }
54
+ if (away) return; // navigating away: keep loading
55
+ notify(false);
56
+ setUser(false);
57
+ }
58
+ setReady(true);
59
+ })();
60
+ }, []);
61
+
62
+ React.useEffect(() => {
63
+ if (!opts.onUnauthorized && !opts.onSignedIn) return undefined;
64
+ const handler = () => {
65
+ const prev = userRef.current;
66
+ if (opts.onUnauthorized && prev) opts.onUnauthorized(prev);
67
+ notify(false);
68
+ setUser(false);
69
+ };
70
+ window.addEventListener("auth:unauthorized", handler);
71
+ return () => window.removeEventListener("auth:unauthorized", handler);
34
72
  }, []);
35
73
 
36
74
  const signedIn = (data) => {
75
+ notify(true);
37
76
  setUser(data.user);
38
77
  // /auth/me adds the permissions of the role (own roles).
39
78
  api.get("/auth/me").then((r) => setUser(r.data)).catch(() => null);
@@ -74,6 +113,13 @@ function createAuth(opts) {
74
113
  attempt(async () => signedIn((await api.post("/auth/setup", { name, email, password, setup_code })).data));
75
114
  // Profile page: new name. After a password change the backend sets the new cookie itself.
76
115
  const updateUser = (u) => setUser(u);
116
+ // The page signed in by itself (answer of its own call): only keep the user.
117
+ const loginWith = (u) => { notify(true); setUser(u); };
118
+ const refresh = async () => {
119
+ const r = await api.get("/auth/me");
120
+ setUser(r.data);
121
+ return r.data;
122
+ };
77
123
  // Signs out at the server (the cookie is removed there), then locally even if the call fails.
78
124
  const logout = async () => {
79
125
  try {
@@ -81,13 +127,14 @@ function createAuth(opts) {
81
127
  } catch (e) {
82
128
  /* already signed out */
83
129
  }
130
+ notify(false);
84
131
  setUser(false);
85
132
  };
86
133
 
87
134
  // Times everywhere follow the profile time zone; set before the children render.
88
135
  setUserTimezone(user ? user.timezone : "");
89
136
 
90
- const value = { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser };
137
+ const value = { user, ready, login, loginMfa, loginPasskey, loginScalio, register, setup, logout, updateUser, loginWith, refresh };
91
138
  return React.createElement(AuthContext.Provider, { value }, props.children);
92
139
  }
93
140