@blackbelt-technology/pi-dashboard-gmail-plugin 0.9.0 → 0.11.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/package.json +9 -4
- package/src/bridge/attachments.ts +4 -1
- package/src/bridge/gmail-api.ts +35 -4
- package/src/bridge/lease-client.ts +9 -2
- package/src/bridge/tools.ts +217 -9
- package/src/client/AutoSetup.tsx +787 -0
- package/src/client/GmailSettings.tsx +702 -176
- package/src/client/auto-setup.ts +124 -0
- package/src/client/ui.ts +24 -0
- package/src/client/wizard.ts +80 -20
- package/src/configSchema.json +14 -1
- package/src/i18n.ts +439 -9
- package/src/server/accounts.ts +37 -2
- package/src/server/google-oauth.ts +10 -2
- package/src/server/index.ts +7 -0
- package/src/server/lease.ts +9 -2
- package/src/server/routes.ts +132 -25
- package/src/server/setup/browser-service.ts +55 -0
- package/src/server/setup/capture.ts +96 -0
- package/src/server/setup/cdp-page.ts +308 -0
- package/src/server/setup/errors.ts +36 -0
- package/src/server/setup/local.ts +27 -0
- package/src/server/setup/mask.ts +11 -0
- package/src/server/setup/run.ts +723 -0
- package/src/server/setup/setup-routes.ts +123 -0
- package/src/server/setup/steps.ts +458 -0
- package/src/server/store-client.ts +30 -0
- package/src/shared/auto-setup.ts +132 -0
- package/src/shared/client-json.ts +3 -1
- package/src/shared/protocol.ts +2 -0
- package/src/shared/scopes.ts +20 -4
- package/src/shared/setup.ts +43 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Automatic-setup client data (pure, no React): hand-off reason → i18n key +
|
|
3
|
+
* English sentence, checkpoint copy keys, start-error mapping, the publishing-links
|
|
4
|
+
* check and the unverified-app notice rule. `AUTO_SETUP_ERROR_EN` is spread into
|
|
5
|
+
* `ERROR_EN` (wizard.ts) so every dynamic key carries an English `t()` fallback.
|
|
6
|
+
* See change: add-gmail-auto-setup.
|
|
7
|
+
*/
|
|
8
|
+
import type { Audience, CheckpointKind, HandoffReason, PublishLinks, SetupRunState } from "../shared/auto-setup.js";
|
|
9
|
+
|
|
10
|
+
/** i18n key per hand-off reason (design D3/D8/D9). */
|
|
11
|
+
export const HANDOFF_KEY: Record<HandoffReason, string> = {
|
|
12
|
+
unexpected_page: "handoffUnexpectedPage",
|
|
13
|
+
locale: "handoffLocale",
|
|
14
|
+
domain_blocked: "handoffDomainBlocked",
|
|
15
|
+
publish_cancelled: "handoffPublishCancelled",
|
|
16
|
+
timeout: "handoffTimeout",
|
|
17
|
+
secret_capture_failed: "handoffSecretCaptureFailed",
|
|
18
|
+
client_in_use: "handoffClientInUse",
|
|
19
|
+
browser_closed: "handoffBrowserClosed",
|
|
20
|
+
disabled: "handoffDisabled",
|
|
21
|
+
unloaded: "handoffUnloaded",
|
|
22
|
+
"no-chrome": "handoffNoChrome",
|
|
23
|
+
"no-display": "handoffNoDisplay",
|
|
24
|
+
"owned-by-other-server": "handoffOwnedByOtherServer",
|
|
25
|
+
"profile-in-use": "handoffProfileInUse",
|
|
26
|
+
"launch-failed": "handoffLaunchFailed",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** Key for a hand-off reason; a reason a newer server adds falls back to the generic sentence. */
|
|
30
|
+
export function handoffKey(reason: string): string {
|
|
31
|
+
return (HANDOFF_KEY as Record<string, string>)[reason] ?? "handoffGeneric";
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** i18n key per human checkpoint (design D5). */
|
|
35
|
+
export const CHECKPOINT_KEY: Record<CheckpointKind, string> = {
|
|
36
|
+
sign_in: "autoCpSignIn",
|
|
37
|
+
console_terms: "autoCpTerms",
|
|
38
|
+
policy: "autoCpPolicy",
|
|
39
|
+
publish: "autoCpPublish",
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/** Run states in which the run is still going (polled every second). */
|
|
43
|
+
const LIVE_STATES: readonly SetupRunState[] = ["starting", "running", "waiting_user", "waiting_decision", "busy"];
|
|
44
|
+
export const isLive = (state: SetupRunState): boolean => LIVE_STATES.includes(state);
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* The unverified-app sentence applies unless the audience is known to be Internal
|
|
48
|
+
* (design D11): External and "unknown" (no run observed on this page) both show it.
|
|
49
|
+
*/
|
|
50
|
+
export function showUnverifiedNotice(audience: Audience | null | undefined): boolean {
|
|
51
|
+
return audience !== "internal";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type LinkError = "invalid_url" | "not_https" | "outside_domain" | "invalid_domain";
|
|
55
|
+
|
|
56
|
+
/** Mirror of the server rule (design D7): `https:` and host under the authorized domain. */
|
|
57
|
+
export function validateLinks(links: PublishLinks): Partial<Record<keyof PublishLinks, LinkError>> {
|
|
58
|
+
const errors: Partial<Record<keyof PublishLinks, LinkError>> = {};
|
|
59
|
+
const domain = links.authorizedDomain.trim().toLowerCase();
|
|
60
|
+
if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/.test(domain)) errors.authorizedDomain = "invalid_domain";
|
|
61
|
+
for (const key of ["homePage", "privacyPolicy"] as const) {
|
|
62
|
+
let url: URL;
|
|
63
|
+
try {
|
|
64
|
+
url = new URL(links[key].trim());
|
|
65
|
+
} catch {
|
|
66
|
+
errors[key] = "invalid_url";
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
if (url.protocol !== "https:") errors[key] = "not_https";
|
|
70
|
+
else if (!errors.authorizedDomain && url.hostname !== domain && !url.hostname.endsWith(`.${domain}`)) errors[key] = "outside_domain";
|
|
71
|
+
}
|
|
72
|
+
return errors;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const LINK_ERROR_KEY: Record<LinkError, string> = {
|
|
76
|
+
invalid_url: "errLinkInvalid",
|
|
77
|
+
not_https: "errLinkHttps",
|
|
78
|
+
outside_domain: "errLinkDomain",
|
|
79
|
+
invalid_domain: "errLinkAuthorizedDomain",
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/** i18n key for a rejected `POST setup/run` / action; unknown codes render the raw server message. */
|
|
83
|
+
export function startErrorKey(code: string | undefined): string | null {
|
|
84
|
+
switch (code) {
|
|
85
|
+
case "run_active":
|
|
86
|
+
return "autoErrRunActive";
|
|
87
|
+
case "remote_dashboard":
|
|
88
|
+
return "autoErrRemote";
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** English sentences (the `t()` fallbacks for every dynamic key above). */
|
|
95
|
+
export const AUTO_SETUP_ERROR_EN: Record<string, string> = {
|
|
96
|
+
handoffGeneric: "Automatic setup stopped. Continue with the manual steps below.",
|
|
97
|
+
handoffUnexpectedPage:
|
|
98
|
+
"Google's console showed a page the automatic setup did not expect, so it stopped instead of guessing. Continue by hand from the highlighted step.",
|
|
99
|
+
handoffLocale:
|
|
100
|
+
"The console is not in English, so the automatic setup could not read it reliably. Switch the Google account's language to English, or continue by hand from the highlighted step.",
|
|
101
|
+
handoffDomainBlocked:
|
|
102
|
+
"The dedicated browser refused to open the Google Cloud console or Google sign-in. Add the domains listed below to the Browser plugin's allowed domains, or continue by hand.",
|
|
103
|
+
handoffPublishCancelled: "The publish confirmation was cancelled in the browser window. Continue by hand from the highlighted step.",
|
|
104
|
+
handoffTimeout: "The automatic setup waited 15 minutes for a step on your side and gave up. Continue by hand, or start it again.",
|
|
105
|
+
handoffSecretCaptureFailed:
|
|
106
|
+
"The OAuth client was created, but its secret could not be read. The secret cannot be shown again, so add a new one to that client.",
|
|
107
|
+
handoffClientInUse:
|
|
108
|
+
"An OAuth client with connected accounts is already configured, and the automatic setup never replaces a working client. Revoke the accounts first, or keep the current client.",
|
|
109
|
+
handoffBrowserClosed: "The dedicated browser window was closed. Continue by hand, or start the automatic setup again.",
|
|
110
|
+
handoffDisabled: "The dedicated browser is turned off in the Browser plugin settings. Turn it on, or continue by hand.",
|
|
111
|
+
handoffUnloaded: "The Browser plugin is not loaded, so the dedicated browser is not available. Continue by hand.",
|
|
112
|
+
handoffNoChrome: "No Chrome installation was found for the dedicated browser. Install Chrome, or continue by hand.",
|
|
113
|
+
handoffNoDisplay: "This computer has no display for the dedicated browser window. Continue by hand.",
|
|
114
|
+
handoffOwnedByOtherServer: "Another dashboard server on this computer owns the dedicated browser. Continue by hand.",
|
|
115
|
+
handoffProfileInUse: "The dedicated browser profile is open in another Chrome window. Close it, or continue by hand.",
|
|
116
|
+
handoffLaunchFailed: "The dedicated browser could not be started. Continue by hand.",
|
|
117
|
+
autoErrRunActive: "An automatic setup is already running on this dashboard.",
|
|
118
|
+
autoErrRemote: "Automatic setup only starts from the computer that runs the dashboard.",
|
|
119
|
+
autoErrGeneric: "The automatic setup request failed.",
|
|
120
|
+
errLinkInvalid: "Enter a full web address, for example https://example.com/privacy.",
|
|
121
|
+
errLinkHttps: "The address must start with https://.",
|
|
122
|
+
errLinkDomain: "The address must be on the authorized domain.",
|
|
123
|
+
errLinkAuthorizedDomain: "Enter a domain name, for example github.com.",
|
|
124
|
+
};
|
package/src/client/ui.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client helpers shared by `GmailSettings.tsx` and `AutoSetup.tsx`: the plugin
|
|
3
|
+
* REST helper and the secondary-button / text class recipes.
|
|
4
|
+
* See change: add-gmail-auto-setup.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const API = "/api/plugins/gmail";
|
|
8
|
+
|
|
9
|
+
/** JSON helper for the plugin routes; a failed reply throws `{ code: body.error, status, detail: body.message }`. */
|
|
10
|
+
export async function api<T>(path: string, init?: RequestInit): Promise<T> {
|
|
11
|
+
const res = await fetch(`${API}${path}`, {
|
|
12
|
+
...init,
|
|
13
|
+
headers: init?.body ? { "content-type": "application/json" } : undefined,
|
|
14
|
+
});
|
|
15
|
+
const body = (await res.json().catch(() => ({}))) as T & { error?: string; message?: string };
|
|
16
|
+
if (!res.ok) throw Object.assign(new Error(body.error ?? `HTTP ${res.status}`), { code: body.error, status: res.status, detail: body.message });
|
|
17
|
+
return body;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Secondary button recipe (`ui-contract.md`); `tap-target` = 44 px touch, 32 px from sm.
|
|
21
|
+
export const btn =
|
|
22
|
+
"focus-ring tap-target rounded-md border border-[var(--border-secondary)] bg-[var(--bg-tertiary)] px-2.5 text-xs font-semibold text-[var(--text-primary)] hover:bg-[var(--bg-hover)] disabled:opacity-50";
|
|
23
|
+
export const muted = "text-xs text-[var(--text-secondary)]";
|
|
24
|
+
export const errorText = "text-xs text-[var(--severity-error-fg)]";
|
package/src/client/wizard.ts
CHANGED
|
@@ -4,43 +4,90 @@
|
|
|
4
4
|
* the commands. See change: add-gmail-plugin.
|
|
5
5
|
* Plus the sign-in error table (`errorKey`, `ERROR_EN`).
|
|
6
6
|
* See change: improve-gmail-settings-ux.
|
|
7
|
+
* `ERROR_EN` also carries the hand-off reason sentences. See change: add-gmail-auto-setup.
|
|
7
8
|
*/
|
|
8
9
|
import { type FlowCode, knownFlowCode } from "../shared/flow-codes.js";
|
|
10
|
+
import { classifyProjectId, isEmail, normalizeEmail, normalizeProjectId } from "../shared/setup.js";
|
|
11
|
+
import { AUTO_SETUP_ERROR_EN } from "./auto-setup.js";
|
|
9
12
|
|
|
10
13
|
export type WizardStep = 1 | 2 | 3 | 4 | 5 | 6;
|
|
11
14
|
|
|
12
15
|
const CONSOLE = "https://console.cloud.google.com";
|
|
13
16
|
|
|
14
17
|
export interface ConsoleLinks {
|
|
18
|
+
/** Never project-scoped: carries only `authuser`. */
|
|
15
19
|
projectCreate: string;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
+
/** Console project picker (find the project id); never project-scoped. */
|
|
21
|
+
projectList: string;
|
|
22
|
+
/** Project-scoped links: `null` until the project id is valid. */
|
|
23
|
+
gmailApi: string | null;
|
|
24
|
+
branding: string | null;
|
|
25
|
+
audience: string | null;
|
|
26
|
+
clientCreate: string | null;
|
|
20
27
|
}
|
|
21
28
|
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
/** The owner account when it is a valid email (normalized), else `undefined`. */
|
|
30
|
+
function validOwner(owner: string | undefined): string | undefined {
|
|
31
|
+
if (!owner) return undefined;
|
|
32
|
+
const email = normalizeEmail(owner);
|
|
33
|
+
return isEmail(email) ? email : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Console deep links (design D3). Project-scoped links exist only for a valid
|
|
38
|
+
* project id and carry `project`; every non-null link carries `authuser` when
|
|
39
|
+
* the owner account is a valid email. Query values are URL-encoded.
|
|
40
|
+
*/
|
|
41
|
+
export function consoleLinks(projectId: string, owner?: string): ConsoleLinks {
|
|
42
|
+
const email = validOwner(owner);
|
|
43
|
+
const id = classifyProjectId(projectId) === "ok" ? normalizeProjectId(projectId) : undefined;
|
|
44
|
+
const url = (path: string, withProject: boolean): string => {
|
|
45
|
+
const q = new URLSearchParams();
|
|
46
|
+
if (withProject && id) q.set("project", id);
|
|
47
|
+
if (email) q.set("authuser", email);
|
|
48
|
+
const qs = q.toString();
|
|
49
|
+
return `${CONSOLE}${path}${qs ? `?${qs}` : ""}`;
|
|
50
|
+
};
|
|
25
51
|
return {
|
|
26
|
-
projectCreate:
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
52
|
+
projectCreate: url("/projectcreate", false),
|
|
53
|
+
projectList: url("/cloud-resource-manager", false),
|
|
54
|
+
gmailApi: id ? url("/apis/library/gmail.googleapis.com", true) : null,
|
|
55
|
+
branding: id ? url("/auth/branding", true) : null,
|
|
56
|
+
audience: id ? url("/auth/audience", true) : null,
|
|
57
|
+
clientCreate: id ? url("/auth/clients/create", true) : null,
|
|
31
58
|
};
|
|
32
59
|
}
|
|
33
60
|
|
|
34
|
-
|
|
61
|
+
export interface GcloudCommands {
|
|
62
|
+
/** `gcloud auth login <email>` — only with a valid owner; run it once first. */
|
|
63
|
+
authLogin: string | null;
|
|
64
|
+
projectsList: string;
|
|
65
|
+
/** `null` until the project id is valid (never a placeholder). */
|
|
66
|
+
create: string | null;
|
|
67
|
+
enable: string | null;
|
|
68
|
+
}
|
|
35
69
|
|
|
36
|
-
|
|
37
|
-
|
|
70
|
+
/**
|
|
71
|
+
* POSIX-shell quote for a value the user will paste into a terminal. `isEmail` admits shell
|
|
72
|
+
* metacharacters in the local part (`a;b@x.co`, `$(id)@x.co`), so anything outside the plain
|
|
73
|
+
* address alphabet is single-quoted (embedded `'` as `'\''`); ordinary addresses stay bare.
|
|
74
|
+
*/
|
|
75
|
+
function shellQuote(v: string): string {
|
|
76
|
+
return /^[A-Za-z0-9@._+-]+$/.test(v) ? v : `'${v.replaceAll("'", "'\\''")}'`;
|
|
38
77
|
}
|
|
39
78
|
|
|
40
|
-
/** Copyable gcloud commands (
|
|
41
|
-
export function gcloudCommands(projectId: string):
|
|
42
|
-
const
|
|
43
|
-
|
|
79
|
+
/** Copyable gcloud commands (design D3); `--account <email>` on every command but `auth login`. */
|
|
80
|
+
export function gcloudCommands(projectId: string, owner?: string): GcloudCommands {
|
|
81
|
+
const valid = validOwner(owner);
|
|
82
|
+
const email = valid === undefined ? undefined : shellQuote(valid);
|
|
83
|
+
const id = classifyProjectId(projectId) === "ok" ? normalizeProjectId(projectId) : undefined;
|
|
84
|
+
const acct = (cmd: string): string => (email ? `${cmd} --account ${email}` : cmd);
|
|
85
|
+
return {
|
|
86
|
+
authLogin: email ? `gcloud auth login ${email}` : null,
|
|
87
|
+
projectsList: acct("gcloud projects list"),
|
|
88
|
+
create: id ? acct(`gcloud projects create ${id}`) : null,
|
|
89
|
+
enable: id ? acct(`gcloud services enable gmail.googleapis.com --project ${id}`) : null,
|
|
90
|
+
};
|
|
44
91
|
}
|
|
45
92
|
|
|
46
93
|
/**
|
|
@@ -109,7 +156,7 @@ export const ERROR_EN: Record<string, string> = {
|
|
|
109
156
|
errRedirectMismatch: "Google rejected the redirect address. Create a Desktop app client, not a Web client (step 4).",
|
|
110
157
|
errInvalidClient: "Google does not recognise the OAuth client. Download its JSON again and upload it (step 5).",
|
|
111
158
|
errScopeMissing:
|
|
112
|
-
"Google did not grant the Gmail permission.
|
|
159
|
+
"Google did not grant the Gmail permission. Google asks for it either as a checkbox you must tick, or on a second screen (\"wants additional access\") you must accept — finishing after the first screen only signs you in. Remove this app's access at myaccount.google.com/connections, then add the account again.",
|
|
113
160
|
errAccountMismatch: "You signed in with a different Google account. Re-authenticate with the account shown in the row.",
|
|
114
161
|
errMissingRefresh: "Google returned no long-lived token. Revoke the app at myaccount.google.com/permissions, then add the account again.",
|
|
115
162
|
errEmailUnverified: "Google reports this account's email as unverified. Verify it with Google, then try again.",
|
|
@@ -125,6 +172,8 @@ export const ERROR_EN: Record<string, string> = {
|
|
|
125
172
|
errCancelled: "Sign-in was cancelled.",
|
|
126
173
|
errAborted: "Sign-in was aborted.",
|
|
127
174
|
errGeneric: "Sign-in failed. Try again; if it keeps failing, check the setup steps above.",
|
|
175
|
+
// Automatic setup: hand-off reasons + start/link errors (change: add-gmail-auto-setup).
|
|
176
|
+
...AUTO_SETUP_ERROR_EN,
|
|
128
177
|
};
|
|
129
178
|
|
|
130
179
|
/** i18n key for a terminal flow error; unknown input → `errGeneric` (exact match only). */
|
|
@@ -134,3 +183,14 @@ export function errorKey(code: string | undefined | null): string {
|
|
|
134
183
|
const known = knownFlowCode(code);
|
|
135
184
|
return known ? CODE_KEY[known] : "errGeneric";
|
|
136
185
|
}
|
|
186
|
+
|
|
187
|
+
/** English fallbacks for copy that mirrors a catalog key (the E30 test pins their content). */
|
|
188
|
+
export const CONSENT_HINT_EN =
|
|
189
|
+
"Google asks for the Gmail permission either as a checkbox (tick it) or on a second screen saying the app \"wants additional access\" (accept it). Stopping after the first screen only signs you in.";
|
|
190
|
+
|
|
191
|
+
export const AUDIENCE_HELP_EN =
|
|
192
|
+
"Internal admits only accounts of the project's own Workspace organization: choose it only when every account you will connect belongs to that organization. Otherwise choose External and add every address you will connect as a test user. In Testing, tokens expire after 7 days. Publishing the app removes the 7-day token expiry. An unverified app shows Google's warning and admits at most 100 users. Accounts added while in Testing need one re-auth after publishing.";
|
|
193
|
+
|
|
194
|
+
/** Sign-in notice for an External or unknown audience (design D11). See change: add-gmail-auto-setup. */
|
|
195
|
+
export const UNVERIFIED_HINT_EN =
|
|
196
|
+
"Google may show “Google hasn’t verified this app”. Choose Advanced, then continue to the app.";
|
package/src/configSchema.json
CHANGED
|
@@ -1 +1,14 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"type": "object",
|
|
3
|
+
"properties": {
|
|
4
|
+
"projectId": {
|
|
5
|
+
"type": "string",
|
|
6
|
+
"description": "Google Cloud project id of the OAuth client (saved by the setup wizard)."
|
|
7
|
+
},
|
|
8
|
+
"ownerAccount": {
|
|
9
|
+
"type": "string",
|
|
10
|
+
"description": "Email of the Google account that owns the project; sets authuser on console links and --account on gcloud commands."
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"additionalProperties": false
|
|
14
|
+
}
|