autosolutions-os 0.3.0 → 0.3.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/CHANGELOG.md +59 -0
- package/README.md +39 -0
- package/dist/federation.d.ts +143 -0
- package/dist/federation.js +408 -0
- package/dist/nav.d.ts +2 -0
- package/dist/nav.js +3 -1
- package/dist/session.d.ts +19 -0
- package/dist/session.js +10 -3
- package/package.json +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,64 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.1 - 2026-10-16
|
|
4
|
+
|
|
5
|
+
"Sign in with AutoSolutions" for the apps that keep their own Supabase
|
|
6
|
+
project (Key Accounts, Asset Portal, Candidate Hub, Ops Hub), and Label
|
|
7
|
+
Maker's fixed menu link on its autosolutions.app address. Nothing changes for
|
|
8
|
+
an app on the core: no entry it imports changed behaviour.
|
|
9
|
+
|
|
10
|
+
### `autosolutions-os/federation` (new entry)
|
|
11
|
+
|
|
12
|
+
For an app on its own project that adds the core as the custom OpenID Connect
|
|
13
|
+
provider `custom:autosolutions` (docs/CONTRACT.md, "Federated: own project,
|
|
14
|
+
one sign-in"; docs/RUNBOOK.md, "Add an app to Sign in with AutoSolutions").
|
|
15
|
+
It takes the app's own supabase-js client and imports neither supabase-js nor
|
|
16
|
+
React.
|
|
17
|
+
|
|
18
|
+
- `signInWithAutoSolutions(client, { redirectTo })` starts the flow through
|
|
19
|
+
the app's own project. With `automatic: true` the sign-in page can call it by
|
|
20
|
+
itself: it starts only on an autosolutions.app address, only when the core's
|
|
21
|
+
shared cookie shows a live session, at most once a minute, and never after a
|
|
22
|
+
sign-out here, a "no access" answer (`holdAutomaticSignIn()`) or an attempt
|
|
23
|
+
that did not come back, until the person clicks. `skipBrowserRedirect` is
|
|
24
|
+
passed through for a popup flow (Ops Hub).
|
|
25
|
+
- `followCoreSession(client, { onSignedOut })`, installed once at the app's
|
|
26
|
+
root on every address, marks the session that came back through
|
|
27
|
+
AutoSolutions (`as-sso`, the core user id, in the app's localStorage) and
|
|
28
|
+
keeps only that session in step with the core. On an autosolutions.app
|
|
29
|
+
address it reads the core's cookies on load, on focus, when the tab comes
|
|
30
|
+
back into view and once a minute: the sign-out note with no live core
|
|
31
|
+
session signs out here (local scope); another person at the core signs out
|
|
32
|
+
here; the same person, or no cookie and no note, does nothing. Google,
|
|
33
|
+
password, email-link, partner and candidate sessions are never touched.
|
|
34
|
+
- `signOutWithAutoSolutions(client, { returnTo })`: for an AutoSolutions
|
|
35
|
+
session, sign out here (local scope), hold the automatic start, then go
|
|
36
|
+
through the Hub's `/sign-out` back to `returnTo`, so the Hub and every app
|
|
37
|
+
follow. For any other session it does nothing and returns false.
|
|
38
|
+
- `signOutEverywhereUrl(returnTo)`: that Hub address,
|
|
39
|
+
`https://autosolutions.app/sign-out?return_to=...`.
|
|
40
|
+
- `coreIdentity(user)`: the person's `custom:autosolutions` identity in the
|
|
41
|
+
app's project (the core user id, the email the core vouched for and whether
|
|
42
|
+
it said it was verified), or null.
|
|
43
|
+
- `cameThroughAutoSolutions(user?)`, `holdAutomaticSignIn()`,
|
|
44
|
+
`AUTOSOLUTIONS_PROVIDER`, `SIGN_OUT_PATH`.
|
|
45
|
+
|
|
46
|
+
The cookie rules are session.ts's own: `followCoreSession` reads the shared
|
|
47
|
+
copy with `sharedSessionCookieName` and `joinChunks`, and the note with the
|
|
48
|
+
same parser and the same "a copy the sign-out ended" rule
|
|
49
|
+
(`parseSignedOutNote`, `signedOutCopy`, `signedOutCookieName`, now exported
|
|
50
|
+
from session.ts for it; nothing in session.ts behaves differently). The
|
|
51
|
+
functions type-check against supabase-js 2.106.2 (Ops Hub), 2.112.3 and
|
|
52
|
+
2.118.0 with no cast.
|
|
53
|
+
|
|
54
|
+
### Label Maker's fixed address
|
|
55
|
+
|
|
56
|
+
`OS_TOOL_LINKS["carton-label-generator"]` (and so `staticLinks()`) is
|
|
57
|
+
`https://label-maker.autosolutions.app`, where Label Maker is served and can
|
|
58
|
+
read the shared session, instead of `https://label-maker-app.lovable.app`. Only
|
|
59
|
+
frames off the core use the fixed address; frames on the core follow the
|
|
60
|
+
catalogue's `launch_url`, which is checked separately.
|
|
61
|
+
|
|
3
62
|
## 0.3.0 - 2026-10-16
|
|
4
63
|
|
|
5
64
|
The OS menu becomes Cliff's menu map, every app shows the Hub panel, and
|
package/README.md
CHANGED
|
@@ -51,6 +51,45 @@ publishSessionChanges(supabase, ref);
|
|
|
51
51
|
followSharedSession(supabase, ref);
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
## Sign in with AutoSolutions (an app on its own project)
|
|
55
|
+
|
|
56
|
+
An app that keeps its own Supabase project, its own people and its own roles
|
|
57
|
+
(Key Accounts, Asset Portal, Candidate Hub, Ops Hub) adds the core as the
|
|
58
|
+
custom OpenID Connect provider `custom:autosolutions` and uses
|
|
59
|
+
`autosolutions-os/federation` with its own client (docs/CONTRACT.md,
|
|
60
|
+
"Federated: own project, one sign-in"; docs/RUNBOOK.md has the setup, click by
|
|
61
|
+
click):
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import {
|
|
65
|
+
followCoreSession, holdAutomaticSignIn, signInWithAutoSolutions, signOutWithAutoSolutions,
|
|
66
|
+
} from "autosolutions-os/federation";
|
|
67
|
+
|
|
68
|
+
// Once, at the root, on every address.
|
|
69
|
+
followCoreSession(supabase, { onSignedOut: () => navigate({ to: "/auth" }) });
|
|
70
|
+
|
|
71
|
+
// The sign-in page: the button, and the start with no click once the app knows there is no session.
|
|
72
|
+
const redirectTo = `${window.location.origin}/auth`;
|
|
73
|
+
await signInWithAutoSolutions(supabase, { redirectTo }); // on a click
|
|
74
|
+
await signInWithAutoSolutions(supabase, { redirectTo, automatic: true }); // by itself; refuses when it should
|
|
75
|
+
|
|
76
|
+
// The app's own rules said no: show "No access to <app>", and do not start by itself again.
|
|
77
|
+
holdAutomaticSignIn();
|
|
78
|
+
|
|
79
|
+
// Sign out: through the Hub for an AutoSolutions session, the app's own way otherwise.
|
|
80
|
+
if (!(await signOutWithAutoSolutions(supabase, { returnTo: "/auth", user }))) await supabase.auth.signOut();
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The person keeps the same account in the app: the app's project links the
|
|
84
|
+
AutoSolutions identity to the user with the same verified email.
|
|
85
|
+
`coreIdentity(user)` reads it (the core user id and the email). On an
|
|
86
|
+
autosolutions.app address an AutoSolutions session follows the core: signed
|
|
87
|
+
out at the Hub, or someone else signed in there, and it signs out here at the
|
|
88
|
+
next load, focus, tab switch or minute. Other sessions (Google, passwords,
|
|
89
|
+
email links, partners, candidates) are never touched. On a lovable.app address
|
|
90
|
+
the core's cookie cannot be read, so the session lasts until it expires or the
|
|
91
|
+
person signs out in the app.
|
|
92
|
+
|
|
54
93
|
## The workspace frame
|
|
55
94
|
|
|
56
95
|
`OsWorkspaceFrame` is the frame every app on the core renders around its pages:
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/** The provider identifier every federated project registers. */
|
|
2
|
+
export declare const AUTOSOLUTIONS_PROVIDER = "custom:autosolutions";
|
|
3
|
+
/** The Hub's sign-out page. */
|
|
4
|
+
export declare const SIGN_OUT_PATH = "/sign-out";
|
|
5
|
+
type Identity = {
|
|
6
|
+
provider?: unknown;
|
|
7
|
+
id?: unknown;
|
|
8
|
+
identity_id?: unknown;
|
|
9
|
+
email?: unknown;
|
|
10
|
+
last_sign_in_at?: unknown;
|
|
11
|
+
identity_data?: {
|
|
12
|
+
[key: string]: unknown;
|
|
13
|
+
} | null;
|
|
14
|
+
};
|
|
15
|
+
type UserLike = {
|
|
16
|
+
id?: unknown;
|
|
17
|
+
identities?: Identity[] | null;
|
|
18
|
+
} | null | undefined;
|
|
19
|
+
type SessionLike = {
|
|
20
|
+
user?: UserLike;
|
|
21
|
+
} | null | undefined;
|
|
22
|
+
/** The parts of a supabase-js client this module uses (any supabase-js 2.x client fits). */
|
|
23
|
+
export type FederatedAuth = {
|
|
24
|
+
onAuthStateChange(callback: (event: string, session: SessionLike) => void): unknown;
|
|
25
|
+
signOut(options?: {
|
|
26
|
+
scope?: "global" | "local" | "others";
|
|
27
|
+
}): Promise<unknown>;
|
|
28
|
+
};
|
|
29
|
+
export type FederatedSignIn = {
|
|
30
|
+
signInWithOAuth(credentials: {
|
|
31
|
+
provider: any;
|
|
32
|
+
options?: {
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
};
|
|
35
|
+
}): Promise<{
|
|
36
|
+
data: unknown;
|
|
37
|
+
error: unknown;
|
|
38
|
+
}>;
|
|
39
|
+
};
|
|
40
|
+
/** The person's AutoSolutions identity in the app's project. */
|
|
41
|
+
export type CoreIdentity = {
|
|
42
|
+
/** The core user id: the same person in the Hub and every app on the core. */
|
|
43
|
+
sub: string;
|
|
44
|
+
/** The address the core vouched for when it was linked, or last signed in. */
|
|
45
|
+
email: string | null;
|
|
46
|
+
/** Whether the core said that address is verified; null when it did not say. */
|
|
47
|
+
emailVerified: boolean | null;
|
|
48
|
+
/** The identity's own row id in the app's project, when supabase-js reports it. */
|
|
49
|
+
identityId: string | null;
|
|
50
|
+
/** When the person last came in through AutoSolutions, as the app's project recorded it. */
|
|
51
|
+
lastSignInAt: string | null;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The `custom:autosolutions` identity of a user of the app's project, or null
|
|
55
|
+
* when the person has never come in through AutoSolutions.
|
|
56
|
+
*/
|
|
57
|
+
export declare function coreIdentity(user: UserLike): CoreIdentity | null;
|
|
58
|
+
export type SignInOptions = {
|
|
59
|
+
/** Where the app's project sends the person back to: one of that project's Redirect URLs. */
|
|
60
|
+
redirectTo: string;
|
|
61
|
+
/**
|
|
62
|
+
* The sign-in page starts the flow by itself rather than on a click. It then
|
|
63
|
+
* starts only on an autosolutions.app address, when the core's cookie shows
|
|
64
|
+
* a live session, at most once a minute, and not after a sign-out here, a
|
|
65
|
+
* "no access" answer (`holdAutomaticSignIn`) or an attempt that did not come
|
|
66
|
+
* back. A click clears all of that.
|
|
67
|
+
*/
|
|
68
|
+
automatic?: boolean;
|
|
69
|
+
/** Return the provider's address instead of going there (a popup flow, as in Ops Hub). */
|
|
70
|
+
skipBrowserRedirect?: boolean;
|
|
71
|
+
/** Extra scopes. The provider's own setting already asks for `openid email profile`. */
|
|
72
|
+
scopes?: string;
|
|
73
|
+
};
|
|
74
|
+
export type SignInResult = {
|
|
75
|
+
started: true;
|
|
76
|
+
url: string | null;
|
|
77
|
+
} | {
|
|
78
|
+
started: false;
|
|
79
|
+
reason: "off-domain" | "returning" | "held" | "too-soon" | "no-core-session";
|
|
80
|
+
} | {
|
|
81
|
+
started: false;
|
|
82
|
+
reason: "error";
|
|
83
|
+
error: unknown;
|
|
84
|
+
};
|
|
85
|
+
/**
|
|
86
|
+
* Start "Sign in with AutoSolutions". On a click, always; with `automatic`,
|
|
87
|
+
* only when the rules above allow it, so a sign-in page can call it as soon as
|
|
88
|
+
* it knows there is no session, and never loops.
|
|
89
|
+
*/
|
|
90
|
+
export declare function signInWithAutoSolutions(client: {
|
|
91
|
+
auth: FederatedSignIn;
|
|
92
|
+
}, options: SignInOptions): Promise<SignInResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Keep the automatic start from running until the person clicks: after a
|
|
95
|
+
* "no access" answer from the app's own rules, and before a sign-out here
|
|
96
|
+
* (`signOutWithAutoSolutions` does it).
|
|
97
|
+
*/
|
|
98
|
+
export declare function holdAutomaticSignIn(): void;
|
|
99
|
+
/**
|
|
100
|
+
* Whether this browser's session in the app came through AutoSolutions. With
|
|
101
|
+
* `user`, also that it is that person's (their AutoSolutions identity is the
|
|
102
|
+
* one the marker names). Needs `followCoreSession`, which sets the marker.
|
|
103
|
+
*/
|
|
104
|
+
export declare function cameThroughAutoSolutions(user?: UserLike): boolean;
|
|
105
|
+
export type FollowOptions = {
|
|
106
|
+
/**
|
|
107
|
+
* Called once this app has signed out (local scope) because the core did
|
|
108
|
+
* (`"core-signed-out"`) or because someone else is signed in at the core
|
|
109
|
+
* (`"different-person"`). The app's own SIGNED_OUT handling shows the
|
|
110
|
+
* sign-in page either way; use this to say why, or to navigate.
|
|
111
|
+
*/
|
|
112
|
+
onSignedOut?: (reason: "core-signed-out" | "different-person") => void;
|
|
113
|
+
/** How often to look while the page is open. Default once a minute. */
|
|
114
|
+
intervalMs?: number;
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Install once, at the app's root, on every address: it records which
|
|
118
|
+
* sessions came through AutoSolutions everywhere, and on an autosolutions.app
|
|
119
|
+
* address keeps those sessions in step with the core (see the top of this
|
|
120
|
+
* file). Returns a function that stops it.
|
|
121
|
+
*/
|
|
122
|
+
export declare function followCoreSession(client: {
|
|
123
|
+
auth: FederatedAuth;
|
|
124
|
+
}, options?: FollowOptions): () => void;
|
|
125
|
+
/**
|
|
126
|
+
* The Hub's sign-out page, which signs the person out of the core (so the Hub
|
|
127
|
+
* and every app follow) and then returns to `returnTo`, when the Hub allows
|
|
128
|
+
* that address. A relative `returnTo` is taken from this page's address.
|
|
129
|
+
*/
|
|
130
|
+
export declare function signOutEverywhereUrl(returnTo: string): string;
|
|
131
|
+
/**
|
|
132
|
+
* The app's sign-out for a session that came through AutoSolutions: hold the
|
|
133
|
+
* automatic start, sign out here (local scope), then go through the Hub's
|
|
134
|
+
* sign-out back to `returnTo` (the app's sign-in page). Returns false, and does
|
|
135
|
+
* nothing, for any other session: the app then signs out as it always has.
|
|
136
|
+
*/
|
|
137
|
+
export declare function signOutWithAutoSolutions(client: {
|
|
138
|
+
auth: FederatedAuth;
|
|
139
|
+
}, options: {
|
|
140
|
+
returnTo: string;
|
|
141
|
+
user?: UserLike;
|
|
142
|
+
}): Promise<boolean>;
|
|
143
|
+
export {};
|
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign in with AutoSolutions, for an app that keeps its own Supabase project.
|
|
3
|
+
*
|
|
4
|
+
* Four apps keep their own project, their own people and their own roles: Key
|
|
5
|
+
* Accounts, Asset Portal, Candidate Hub and Ops Hub. Each project adds the
|
|
6
|
+
* core as a custom OpenID Connect provider, `custom:autosolutions`; the core's
|
|
7
|
+
* OAuth 2.1 server sends people to the Hub's consent page
|
|
8
|
+
* (https://autosolutions.app/oauth/consent), and the app's project links them
|
|
9
|
+
* to their existing account by verified email, so their user id there does not
|
|
10
|
+
* change. See docs/CONTRACT.md, "Federated: own project, one sign-in".
|
|
11
|
+
*
|
|
12
|
+
* What this module does in the app's browser:
|
|
13
|
+
*
|
|
14
|
+
* 1. `signInWithAutoSolutions` starts the flow: on a click, or by itself on the
|
|
15
|
+
* sign-in page (`automatic`), only on an autosolutions.app address, only
|
|
16
|
+
* when the core's shared cookie shows a live session, at most once a
|
|
17
|
+
* minute, and never after a sign-out here, a "no access" answer or an
|
|
18
|
+
* attempt that did not come back, until the person clicks.
|
|
19
|
+
* 2. `followCoreSession` marks the session that comes back as one that came
|
|
20
|
+
* through AutoSolutions (`as-sso`, the core user id) and, on an
|
|
21
|
+
* autosolutions.app address, keeps that session in step with the core: on
|
|
22
|
+
* load, on focus, when the tab comes back into view and once a minute it
|
|
23
|
+
* reads the core's cookies on .autosolutions.app, and
|
|
24
|
+
* - the sign-out note is there and no live core session is shared: it signs
|
|
25
|
+
* out here (local scope), so the app shows its sign-in page;
|
|
26
|
+
* - the shared session is another person's: it signs out here, and the
|
|
27
|
+
* sign-in page starts again;
|
|
28
|
+
* - the same person, or no cookie and no note (blocked cookies): nothing.
|
|
29
|
+
* A session that did not come through AutoSolutions (Google, a password, an
|
|
30
|
+
* email link, a partner or a candidate) is never touched.
|
|
31
|
+
* 3. `signOutWithAutoSolutions` signs out here (local scope) and then at the
|
|
32
|
+
* Hub's /sign-out, which signs the person out of the core, so the Hub and
|
|
33
|
+
* every other app follow; `signOutEverywhereUrl` is that address.
|
|
34
|
+
* 4. `coreIdentity` reads the person's AutoSolutions identity from the app's
|
|
35
|
+
* own user: the core user id (`sub`) and the email the core vouched for.
|
|
36
|
+
*
|
|
37
|
+
* Every part is best effort: storage or cookie errors never stop a sign-in or
|
|
38
|
+
* a sign-out. Nothing here imports supabase-js or React; the functions take
|
|
39
|
+
* the app's own client.
|
|
40
|
+
*/
|
|
41
|
+
import { coreProjectRef, HUB_URL, PARENT_DOMAIN } from "./config.js";
|
|
42
|
+
import { joinChunks, parseCopy, parseSignedOutNote, sharedSessionCookieName, signedOutCookieName, signedOutCopy, } from "./session.js";
|
|
43
|
+
/** The provider identifier every federated project registers. */
|
|
44
|
+
export const AUTOSOLUTIONS_PROVIDER = "custom:autosolutions";
|
|
45
|
+
/** The Hub's sign-out page. */
|
|
46
|
+
export const SIGN_OUT_PATH = "/sign-out";
|
|
47
|
+
/** localStorage keys on the app's own origin. */
|
|
48
|
+
const PENDING = "as-sso-pending";
|
|
49
|
+
const MARKER = "as-sso";
|
|
50
|
+
const HOLD = "as-sso-hold";
|
|
51
|
+
const AUTO_AT = "as-sso-auto-at";
|
|
52
|
+
/** A started sign-in that has not come back within this long has been abandoned. */
|
|
53
|
+
const PENDING_FOR = 10 * 60_000;
|
|
54
|
+
/** The automatic start runs at most this often. */
|
|
55
|
+
const AUTO_EVERY = 60_000;
|
|
56
|
+
/** The server's clock and the browser's may differ by this much. */
|
|
57
|
+
const CLOCK_SKEW = 5 * 60_000;
|
|
58
|
+
/** followCoreSession looks this often while the page is open. */
|
|
59
|
+
const FOLLOW_EVERY = 60_000;
|
|
60
|
+
const text = (value) => (typeof value === "string" && value.length > 0 ? value : null);
|
|
61
|
+
/**
|
|
62
|
+
* The `custom:autosolutions` identity of a user of the app's project, or null
|
|
63
|
+
* when the person has never come in through AutoSolutions.
|
|
64
|
+
*/
|
|
65
|
+
export function coreIdentity(user) {
|
|
66
|
+
const identities = user && Array.isArray(user.identities) ? user.identities : [];
|
|
67
|
+
const identity = identities.find((candidate) => candidate?.provider === AUTOSOLUTIONS_PROVIDER);
|
|
68
|
+
if (!identity)
|
|
69
|
+
return null;
|
|
70
|
+
const data = identity.identity_data && typeof identity.identity_data === "object" ? identity.identity_data : {};
|
|
71
|
+
const sub = text(data["sub"]) ?? text(data["provider_id"]) ?? text(identity.id);
|
|
72
|
+
if (!sub)
|
|
73
|
+
return null;
|
|
74
|
+
const verified = data["email_verified"];
|
|
75
|
+
return {
|
|
76
|
+
sub,
|
|
77
|
+
email: text(data["email"]) ?? text(identity.email),
|
|
78
|
+
emailVerified: verified === true || verified === "true" ? true : verified === false || verified === "false" ? false : null,
|
|
79
|
+
identityId: text(identity.identity_id),
|
|
80
|
+
lastSignInAt: text(identity.last_sign_in_at),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function browser() {
|
|
84
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
85
|
+
}
|
|
86
|
+
function onParentDomain() {
|
|
87
|
+
if (!browser())
|
|
88
|
+
return false;
|
|
89
|
+
const host = window.location.hostname;
|
|
90
|
+
return host === PARENT_DOMAIN || host.endsWith(`.${PARENT_DOMAIN}`);
|
|
91
|
+
}
|
|
92
|
+
function read(key) {
|
|
93
|
+
try {
|
|
94
|
+
return window.localStorage.getItem(key);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function write(key, value) {
|
|
101
|
+
try {
|
|
102
|
+
window.localStorage.setItem(key, value);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Storage blocked: the sign-in still works, it is just not followed.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function remove(key) {
|
|
109
|
+
try {
|
|
110
|
+
window.localStorage.removeItem(key);
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
// Nothing to remove.
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** A sign-in started here that has not come back yet, or null. A stale one is forgotten. */
|
|
117
|
+
function pending() {
|
|
118
|
+
const raw = read(PENDING);
|
|
119
|
+
if (raw === null)
|
|
120
|
+
return null;
|
|
121
|
+
const at = Number(raw);
|
|
122
|
+
if (!Number.isFinite(at) || Date.now() - at > PENDING_FOR || at - Date.now() > CLOCK_SKEW) {
|
|
123
|
+
remove(PENDING);
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
return at;
|
|
127
|
+
}
|
|
128
|
+
function readCoreCookies() {
|
|
129
|
+
try {
|
|
130
|
+
const ref = coreProjectRef();
|
|
131
|
+
const jar = new Map();
|
|
132
|
+
for (const entry of document.cookie ? document.cookie.split("; ") : []) {
|
|
133
|
+
const index = entry.indexOf("=");
|
|
134
|
+
if (index > 0)
|
|
135
|
+
jar.set(entry.slice(0, index), entry.slice(index + 1));
|
|
136
|
+
}
|
|
137
|
+
const note = parseSignedOutNote(jar.get(signedOutCookieName(ref)));
|
|
138
|
+
const key = sharedSessionCookieName(ref);
|
|
139
|
+
if (!jar.has(`${key}.0`))
|
|
140
|
+
return { note: note !== null, shared: "absent", userId: null };
|
|
141
|
+
const copy = parseCopy(joinChunks(key, (name) => jar.get(name)));
|
|
142
|
+
// Another app may be halfway through writing its chunks: that is not "no session".
|
|
143
|
+
if (!copy)
|
|
144
|
+
return { note: note !== null, shared: "unreadable", userId: null };
|
|
145
|
+
return { note: note !== null, shared: signedOutCopy(copy, note) ? "signed-out" : "live", userId: copy.userId };
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/** The sign-in page has just come back from a provider: let supabase-js finish with it. */
|
|
152
|
+
function returning() {
|
|
153
|
+
try {
|
|
154
|
+
const search = new URLSearchParams(window.location.search ?? "");
|
|
155
|
+
const hash = new URLSearchParams(String(window.location.hash ?? "").replace(/^#/, ""));
|
|
156
|
+
return ["code", "error", "error_code", "error_description"].some((name) => search.has(name))
|
|
157
|
+
|| ["access_token", "error", "error_code", "error_description"].some((name) => hash.has(name));
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Start "Sign in with AutoSolutions". On a click, always; with `automatic`,
|
|
165
|
+
* only when the rules above allow it, so a sign-in page can call it as soon as
|
|
166
|
+
* it knows there is no session, and never loops.
|
|
167
|
+
*/
|
|
168
|
+
export async function signInWithAutoSolutions(client, options) {
|
|
169
|
+
if (options.automatic) {
|
|
170
|
+
if (!onParentDomain())
|
|
171
|
+
return { started: false, reason: "off-domain" };
|
|
172
|
+
if (returning())
|
|
173
|
+
return { started: false, reason: "returning" };
|
|
174
|
+
if (read(HOLD) !== null)
|
|
175
|
+
return { started: false, reason: "held" };
|
|
176
|
+
if (pending() !== null) {
|
|
177
|
+
// The last attempt went to the Hub and did not come back signed in (it
|
|
178
|
+
// was cancelled, refused, or left): wait for a click.
|
|
179
|
+
holdAutomaticSignIn();
|
|
180
|
+
remove(PENDING);
|
|
181
|
+
return { started: false, reason: "held" };
|
|
182
|
+
}
|
|
183
|
+
const last = Number(read(AUTO_AT));
|
|
184
|
+
if (Number.isFinite(last) && last > 0 && Date.now() - last < AUTO_EVERY)
|
|
185
|
+
return { started: false, reason: "too-soon" };
|
|
186
|
+
if (readCoreCookies()?.shared !== "live")
|
|
187
|
+
return { started: false, reason: "no-core-session" };
|
|
188
|
+
write(AUTO_AT, String(Date.now()));
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
remove(HOLD);
|
|
192
|
+
}
|
|
193
|
+
write(PENDING, String(Date.now()));
|
|
194
|
+
try {
|
|
195
|
+
const { data, error } = await client.auth.signInWithOAuth({
|
|
196
|
+
provider: AUTOSOLUTIONS_PROVIDER,
|
|
197
|
+
options: {
|
|
198
|
+
redirectTo: options.redirectTo,
|
|
199
|
+
...(options.skipBrowserRedirect ? { skipBrowserRedirect: true } : {}),
|
|
200
|
+
...(options.scopes ? { scopes: options.scopes } : {}),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
if (error) {
|
|
204
|
+
remove(PENDING);
|
|
205
|
+
return { started: false, reason: "error", error };
|
|
206
|
+
}
|
|
207
|
+
return { started: true, url: text(data?.url) };
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
remove(PENDING);
|
|
211
|
+
return { started: false, reason: "error", error };
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Keep the automatic start from running until the person clicks: after a
|
|
216
|
+
* "no access" answer from the app's own rules, and before a sign-out here
|
|
217
|
+
* (`signOutWithAutoSolutions` does it).
|
|
218
|
+
*/
|
|
219
|
+
export function holdAutomaticSignIn() {
|
|
220
|
+
if (browser())
|
|
221
|
+
write(HOLD, String(Date.now()));
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Whether this browser's session in the app came through AutoSolutions. With
|
|
225
|
+
* `user`, also that it is that person's (their AutoSolutions identity is the
|
|
226
|
+
* one the marker names). Needs `followCoreSession`, which sets the marker.
|
|
227
|
+
*/
|
|
228
|
+
export function cameThroughAutoSolutions(user) {
|
|
229
|
+
if (!browser())
|
|
230
|
+
return false;
|
|
231
|
+
const marker = read(MARKER);
|
|
232
|
+
if (marker === null)
|
|
233
|
+
return false;
|
|
234
|
+
if (user === undefined)
|
|
235
|
+
return true;
|
|
236
|
+
return coreIdentity(user)?.sub === marker;
|
|
237
|
+
}
|
|
238
|
+
/** Record how a session came: through AutoSolutions only when a sign-in started here came back with that identity. */
|
|
239
|
+
function noteAuthEvent(event, session) {
|
|
240
|
+
try {
|
|
241
|
+
if (event === "SIGNED_OUT") {
|
|
242
|
+
remove(MARKER);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const user = session?.user;
|
|
246
|
+
if (!user)
|
|
247
|
+
return;
|
|
248
|
+
const identity = coreIdentity(user);
|
|
249
|
+
const startedAt = pending();
|
|
250
|
+
if (startedAt !== null && identity) {
|
|
251
|
+
const last = identity.lastSignInAt ? Date.parse(identity.lastSignInAt) : Number.NaN;
|
|
252
|
+
// A person with the identity already linked who signs in some other way
|
|
253
|
+
// after an abandoned attempt: the identity's last sign-in is the old one.
|
|
254
|
+
if (Number.isNaN(last) || last >= startedAt - CLOCK_SKEW) {
|
|
255
|
+
write(MARKER, identity.sub);
|
|
256
|
+
remove(PENDING);
|
|
257
|
+
remove(HOLD);
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const marker = read(MARKER);
|
|
262
|
+
if (marker !== null && identity?.sub !== marker)
|
|
263
|
+
remove(MARKER);
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
// Never let the marker break authentication.
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Install once, at the app's root, on every address: it records which
|
|
271
|
+
* sessions came through AutoSolutions everywhere, and on an autosolutions.app
|
|
272
|
+
* address keeps those sessions in step with the core (see the top of this
|
|
273
|
+
* file). Returns a function that stops it.
|
|
274
|
+
*/
|
|
275
|
+
export function followCoreSession(client, options = {}) {
|
|
276
|
+
if (!browser())
|
|
277
|
+
return () => { };
|
|
278
|
+
let stopped = false;
|
|
279
|
+
let signingOut = false;
|
|
280
|
+
let user = undefined;
|
|
281
|
+
let unsubscribe = () => { };
|
|
282
|
+
const listeners = [];
|
|
283
|
+
let timer;
|
|
284
|
+
const check = () => {
|
|
285
|
+
if (stopped || signingOut || !onParentDomain())
|
|
286
|
+
return;
|
|
287
|
+
try {
|
|
288
|
+
if (!user)
|
|
289
|
+
return;
|
|
290
|
+
const marker = read(MARKER);
|
|
291
|
+
if (marker === null || coreIdentity(user)?.sub !== marker)
|
|
292
|
+
return;
|
|
293
|
+
const core = readCoreCookies();
|
|
294
|
+
if (!core)
|
|
295
|
+
return;
|
|
296
|
+
let reason = null;
|
|
297
|
+
if (core.note && (core.shared === "absent" || core.shared === "signed-out"))
|
|
298
|
+
reason = "core-signed-out";
|
|
299
|
+
else if (core.shared === "live" && core.userId !== null && core.userId !== marker)
|
|
300
|
+
reason = "different-person";
|
|
301
|
+
if (reason === null)
|
|
302
|
+
return;
|
|
303
|
+
const why = reason;
|
|
304
|
+
signingOut = true;
|
|
305
|
+
remove(MARKER);
|
|
306
|
+
const done = () => {
|
|
307
|
+
signingOut = false;
|
|
308
|
+
try {
|
|
309
|
+
options.onSignedOut?.(why);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
// The app's handler is its own business.
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
Promise.resolve()
|
|
316
|
+
.then(() => client.auth.signOut({ scope: "local" }))
|
|
317
|
+
.then(done, done);
|
|
318
|
+
}
|
|
319
|
+
catch {
|
|
320
|
+
// Never let following the core break the page.
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
try {
|
|
324
|
+
const subscription = client.auth.onAuthStateChange((event, session) => {
|
|
325
|
+
noteAuthEvent(event, session);
|
|
326
|
+
user = event === "SIGNED_OUT" ? null : session?.user ?? null;
|
|
327
|
+
// Outside the callback: supabase-js holds its lock while it runs, and a
|
|
328
|
+
// sign-out from inside it would wait for itself.
|
|
329
|
+
if (user)
|
|
330
|
+
setTimeout(check, 0);
|
|
331
|
+
});
|
|
332
|
+
unsubscribe = () => subscription?.data?.subscription?.unsubscribe?.();
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
// No auth events: nothing can be followed.
|
|
336
|
+
}
|
|
337
|
+
if (onParentDomain()) {
|
|
338
|
+
const wake = () => {
|
|
339
|
+
if (document.visibilityState === "hidden")
|
|
340
|
+
return;
|
|
341
|
+
check();
|
|
342
|
+
};
|
|
343
|
+
listeners.push([document, "visibilitychange", wake], [window, "focus", wake], [window, "pageshow", wake]);
|
|
344
|
+
for (const [target, type, listener] of listeners) {
|
|
345
|
+
try {
|
|
346
|
+
target.addEventListener(type, listener);
|
|
347
|
+
}
|
|
348
|
+
catch {
|
|
349
|
+
// A part that cannot be installed leaves the others working.
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
timer = setInterval(check, options.intervalMs ?? FOLLOW_EVERY);
|
|
353
|
+
}
|
|
354
|
+
return () => {
|
|
355
|
+
stopped = true;
|
|
356
|
+
if (timer !== undefined)
|
|
357
|
+
clearInterval(timer);
|
|
358
|
+
for (const [target, type, listener] of listeners) {
|
|
359
|
+
try {
|
|
360
|
+
target.removeEventListener(type, listener);
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
// Already gone.
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
try {
|
|
367
|
+
unsubscribe();
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
// Already gone.
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* The Hub's sign-out page, which signs the person out of the core (so the Hub
|
|
376
|
+
* and every app follow) and then returns to `returnTo`, when the Hub allows
|
|
377
|
+
* that address. A relative `returnTo` is taken from this page's address.
|
|
378
|
+
*/
|
|
379
|
+
export function signOutEverywhereUrl(returnTo) {
|
|
380
|
+
let target = returnTo;
|
|
381
|
+
try {
|
|
382
|
+
target = browser() ? new URL(returnTo, window.location.href).toString() : new URL(returnTo).toString();
|
|
383
|
+
}
|
|
384
|
+
catch {
|
|
385
|
+
// Not a URL: the Hub refuses it and stays on its own page.
|
|
386
|
+
}
|
|
387
|
+
return `${HUB_URL}${SIGN_OUT_PATH}?return_to=${encodeURIComponent(target)}`;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* The app's sign-out for a session that came through AutoSolutions: hold the
|
|
391
|
+
* automatic start, sign out here (local scope), then go through the Hub's
|
|
392
|
+
* sign-out back to `returnTo` (the app's sign-in page). Returns false, and does
|
|
393
|
+
* nothing, for any other session: the app then signs out as it always has.
|
|
394
|
+
*/
|
|
395
|
+
export async function signOutWithAutoSolutions(client, options) {
|
|
396
|
+
if (!cameThroughAutoSolutions(options.user))
|
|
397
|
+
return false;
|
|
398
|
+
holdAutomaticSignIn();
|
|
399
|
+
try {
|
|
400
|
+
await client.auth.signOut({ scope: "local" });
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
// Local sign-out removes the session whatever the server says.
|
|
404
|
+
}
|
|
405
|
+
remove(MARKER);
|
|
406
|
+
window.location.assign(signOutEverywhereUrl(options.returnTo));
|
|
407
|
+
return true;
|
|
408
|
+
}
|
package/dist/nav.d.ts
CHANGED
|
@@ -115,6 +115,8 @@ export type OsToolLink = {
|
|
|
115
115
|
* the catalogue already has whatever the map says of them (Influencer
|
|
116
116
|
* Manager, Marketing calendar, Paid media), so every app the app switcher
|
|
117
117
|
* offers is reachable from the menu too. Every link opens in the same tab.
|
|
118
|
+
* Label Maker's fixed address is label-maker.autosolutions.app since 0.3.1:
|
|
119
|
+
* the old label-maker-app.lovable.app address cannot read the shared session.
|
|
118
120
|
* Pricing researcher and Brand voice copywriter have no app to open yet:
|
|
119
121
|
* like every tool with none, they open their page in the Hub. The Hub itself
|
|
120
122
|
* (smart_comms_hub) is the rail's Hub, not a row.
|
package/dist/nav.js
CHANGED
|
@@ -183,6 +183,8 @@ const opsHub = (path) => ({ app: "ops_hub", url: OPS_HUB, path });
|
|
|
183
183
|
* the catalogue already has whatever the map says of them (Influencer
|
|
184
184
|
* Manager, Marketing calendar, Paid media), so every app the app switcher
|
|
185
185
|
* offers is reachable from the menu too. Every link opens in the same tab.
|
|
186
|
+
* Label Maker's fixed address is label-maker.autosolutions.app since 0.3.1:
|
|
187
|
+
* the old label-maker-app.lovable.app address cannot read the shared session.
|
|
186
188
|
* Pricing researcher and Brand voice copywriter have no app to open yet:
|
|
187
189
|
* like every tool with none, they open their page in the Hub. The Hub itself
|
|
188
190
|
* (smart_comms_hub) is the rail's Hub, not a row.
|
|
@@ -209,7 +211,7 @@ export const OS_TOOL_LINKS = {
|
|
|
209
211
|
"inventory-value": opsHub("/inventory-health?view=valuation"),
|
|
210
212
|
"action-queue": opsHub("/action-queue"),
|
|
211
213
|
"ordering-tool": opsHub("/ordering"),
|
|
212
|
-
"carton-label-generator": { app: "label_maker", url: "https://label-maker
|
|
214
|
+
"carton-label-generator": { app: "label_maker", url: "https://label-maker.autosolutions.app" },
|
|
213
215
|
"pm-copilot": { app: "pm_copilot", url: "https://pm-copilot.autosolutions.app" },
|
|
214
216
|
"candidate-hub": { app: "candidate_hub" },
|
|
215
217
|
};
|
package/dist/session.d.ts
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
/** Reassemble a chunked value from any source of name/value pairs. */
|
|
2
2
|
export declare function joinChunks(key: string, read: (name: string) => string | undefined): string | null;
|
|
3
|
+
export type SignedOut = {
|
|
4
|
+
expiresAt: number;
|
|
5
|
+
sessions: string[];
|
|
6
|
+
};
|
|
7
|
+
/** The sign-out note's value, read. autosolutions-os/federation reads it too, so the rule lives here once. */
|
|
8
|
+
export declare function parseSignedOutNote(raw: string | undefined): SignedOut | null;
|
|
9
|
+
/** A copy the sign-out ended: it expires no later than the sign-out's watermark, or it is one of the sessions it ended (a refresh of that session expires later). */
|
|
10
|
+
export declare function signedOutCopy(copy: Copy, note: SignedOut | null): boolean;
|
|
11
|
+
/** What the rules need from a stored session, or null when it is not one. */
|
|
12
|
+
export type Copy = {
|
|
13
|
+
raw: string;
|
|
14
|
+
expiresAt: number;
|
|
15
|
+
refreshToken: string;
|
|
16
|
+
userId: string | null;
|
|
17
|
+
sessionId: string | null;
|
|
18
|
+
};
|
|
19
|
+
export declare function parseCopy(raw: string | null | undefined): Copy | null;
|
|
3
20
|
/**
|
|
4
21
|
* Copy a session that arrived from another app into this origin's storage,
|
|
5
22
|
* or share this origin's when it is the newer one. Must run before the
|
|
@@ -40,4 +57,6 @@ export declare function followSharedSession(client: {
|
|
|
40
57
|
}, projectRef: string): void;
|
|
41
58
|
/** The storage key, for server code that needs to find the cookie by name. */
|
|
42
59
|
export declare function sharedSessionCookieName(projectRef: string): string;
|
|
60
|
+
/** The name of the note an explicit sign-out leaves, for code that follows the core from outside it (autosolutions-os/federation). */
|
|
61
|
+
export declare function signedOutCookieName(projectRef: string): string;
|
|
43
62
|
export {};
|
package/dist/session.js
CHANGED
|
@@ -107,7 +107,10 @@ function clearCookieChunks(key) {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
function readSignedOut(jar, key) {
|
|
110
|
-
|
|
110
|
+
return parseSignedOutNote(jar.get(signedOutName(key)));
|
|
111
|
+
}
|
|
112
|
+
/** The sign-out note's value, read. autosolutions-os/federation reads it too, so the rule lives here once. */
|
|
113
|
+
export function parseSignedOutNote(raw) {
|
|
111
114
|
if (raw === undefined || raw === "")
|
|
112
115
|
return null;
|
|
113
116
|
const [watermark = "", sessions = ""] = raw.split("|");
|
|
@@ -124,7 +127,7 @@ function writeSignedOut(key, expiresAt, sessionIds) {
|
|
|
124
127
|
document.cookie = `${signedOutName(key)}=${value}; Domain=.${PARENT_DOMAIN}; Path=/; Max-Age=${MAX_AGE}; SameSite=Lax; Secure`;
|
|
125
128
|
}
|
|
126
129
|
/** A copy the sign-out ended: it expires no later than the sign-out's watermark, or it is one of the sessions it ended (a refresh of that session expires later). */
|
|
127
|
-
function signedOutCopy(copy, note) {
|
|
130
|
+
export function signedOutCopy(copy, note) {
|
|
128
131
|
if (note === null)
|
|
129
132
|
return false;
|
|
130
133
|
return copy.expiresAt <= note.expiresAt || (copy.sessionId !== null && note.sessions.includes(copy.sessionId));
|
|
@@ -142,7 +145,7 @@ function sessionIdOf(accessToken) {
|
|
|
142
145
|
return null;
|
|
143
146
|
}
|
|
144
147
|
}
|
|
145
|
-
function parseCopy(raw) {
|
|
148
|
+
export function parseCopy(raw) {
|
|
146
149
|
if (!raw)
|
|
147
150
|
return null;
|
|
148
151
|
try {
|
|
@@ -543,3 +546,7 @@ function signOutEverywhere(auth, key, page) {
|
|
|
543
546
|
export function sharedSessionCookieName(projectRef) {
|
|
544
547
|
return storageKey(projectRef);
|
|
545
548
|
}
|
|
549
|
+
/** The name of the note an explicit sign-out leaves, for code that follows the core from outside it (autosolutions-os/federation). */
|
|
550
|
+
export function signedOutCookieName(projectRef) {
|
|
551
|
+
return signedOutName(storageKey(projectRef));
|
|
552
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "autosolutions-os",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "One sign-in, one company switcher, one app grid. What every AutoSolutions app imports instead of copying.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"./server": { "types": "./dist/server.d.ts", "import": "./dist/server.js" },
|
|
12
12
|
"./nav": { "types": "./dist/nav.d.ts", "import": "./dist/nav.js" },
|
|
13
13
|
"./frame": { "types": "./dist/frame.d.ts", "import": "./dist/frame.js" },
|
|
14
|
+
"./federation": { "types": "./dist/federation.d.ts", "import": "./dist/federation.js" },
|
|
14
15
|
"./chrome.css": "./dist/chrome.css"
|
|
15
16
|
},
|
|
16
17
|
"files": ["dist", "CHANGELOG.md"],
|