gt 2.25.1 → 2.25.2
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 +14 -0
- package/dist/auth/callbackPage.d.ts +15 -1
- package/dist/auth/callbackPage.js +141 -41
- package/dist/auth/callbackPage.js.map +1 -1
- package/dist/auth/loopback.d.ts +36 -2
- package/dist/auth/loopback.js +79 -24
- package/dist/auth/loopback.js.map +1 -1
- package/dist/auth/oauth.d.ts +2 -0
- package/dist/auth/oauth.js +57 -7
- package/dist/auth/oauth.js.map +1 -1
- package/dist/generated/version.d.ts +1 -1
- package/dist/generated/version.js +1 -1
- package/dist/generated/version.js.map +1 -1
- package/dist/setup/buildTools/{tanstackStart → shared}/edits.js +1 -1
- package/dist/setup/buildTools/shared/edits.js.map +1 -0
- package/dist/setup/buildTools/shared/jsx.d.ts +4 -0
- package/dist/setup/buildTools/shared/jsx.js +19 -0
- package/dist/setup/buildTools/shared/jsx.js.map +1 -0
- package/dist/setup/buildTools/shared/source.d.ts +12 -0
- package/dist/setup/buildTools/shared/source.js +40 -0
- package/dist/setup/buildTools/shared/source.js.map +1 -0
- package/dist/setup/buildTools/tanstackStart/index.js +5 -3
- package/dist/setup/buildTools/tanstackStart/index.js.map +1 -1
- package/dist/setup/buildTools/tanstackStart/middleware.d.ts +1 -1
- package/dist/setup/buildTools/tanstackStart/middleware.js +3 -2
- package/dist/setup/buildTools/tanstackStart/middleware.js.map +1 -1
- package/dist/setup/buildTools/tanstackStart/root.d.ts +1 -2
- package/dist/setup/buildTools/tanstackStart/root.js +5 -16
- package/dist/setup/buildTools/tanstackStart/root.js.map +1 -1
- package/dist/setup/buildTools/tanstackStart/router.d.ts +2 -2
- package/dist/setup/buildTools/tanstackStart/router.js +3 -2
- package/dist/setup/buildTools/tanstackStart/router.js.map +1 -1
- package/dist/setup/buildTools/tanstackStart/source.d.ts +3 -15
- package/dist/setup/buildTools/tanstackStart/source.js +2 -37
- package/dist/setup/buildTools/tanstackStart/source.js.map +1 -1
- package/dist/setup/setupViteSPA.d.ts +7 -6
- package/dist/setup/setupViteSPA.js +13 -12
- package/dist/setup/setupViteSPA.js.map +1 -1
- package/package.json +5 -5
- package/dist/auth/loopback.test.d.ts +0 -1
- package/dist/setup/buildTools/tanstackStart/edits.js.map +0 -1
- /package/dist/setup/buildTools/{tanstackStart → shared}/edits.d.ts +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# gtx-cli
|
|
2
2
|
|
|
3
|
+
## 2.25.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2341](https://github.com/generaltranslation/gt/pull/2341) [`60deece`](https://github.com/generaltranslation/gt/commit/60deececd8d1339aef7ef642d0e0f0f0b1d189d7) Thanks [@Kevin-Liu-01](https://github.com/Kevin-Liu-01)! - Restyle the page `gt login` shows in the browser after the loopback callback to match the dashboard's auth plate, centered on the plain ground: the GT mark, a heading with a status glyph, a lede, and a note naming the signed-in account. Nothing loads from the network. A denied request and a failed exchange get their own pages, and both say to run `npx gt login` again, which copies the command when clicked and shows a copy icon beside it that turns into a check for a moment after a copy (the page's one script, allowed by its hash in the Content-Security-Policy); the page calls a request denied only when the exchange read a validated denial, so it agrees with the terminal.
|
|
8
|
+
|
|
9
|
+
`gt login` returns to the shell as soon as the browser's callback is answered: a repeated callback request no longer holds a keep-alive socket open for the five second timeout. A browser that abandoned its first navigation while the exchange ran gets the result page on its repeat: `gt login` waits up to five seconds for that repeat, and exits at once when the first page arrived. A caller's AbortSignal now cancels the browser flow's token exchange and account lookup, reported as a cancellation.
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [[`ebb6c9e`](https://github.com/generaltranslation/gt/commit/ebb6c9efa403b2b6b2ec84521398a05825eb1ff0)]:
|
|
12
|
+
- @generaltranslation/supported-locales@2.1.41
|
|
13
|
+
- generaltranslation@9.5.5
|
|
14
|
+
- @generaltranslation/python-extractor@0.2.60
|
|
15
|
+
- @generaltranslation/vue-extractor@0.1.19
|
|
16
|
+
|
|
3
17
|
## 2.25.1
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
|
@@ -1 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
/** What the page says: a signed-in account, or why the login did not finish. */
|
|
2
|
+
export type CallbackPageView = {
|
|
3
|
+
ok: true;
|
|
4
|
+
account?: string;
|
|
5
|
+
} | {
|
|
6
|
+
ok: false;
|
|
7
|
+
reason: 'denied' | 'failed';
|
|
8
|
+
};
|
|
9
|
+
/** The part of a success view the login flow can add after the exchange. */
|
|
10
|
+
export type CallbackPageDetails = {
|
|
11
|
+
account?: string;
|
|
12
|
+
};
|
|
13
|
+
/** The policy the loopback server sends with the page. */
|
|
14
|
+
export declare const CALLBACK_PAGE_CSP: string;
|
|
15
|
+
export declare function renderCallbackPage(view: CallbackPageView): string;
|
|
@@ -1,60 +1,158 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
1
2
|
//#region src/auth/callbackPage.ts
|
|
2
3
|
const STYLES = `
|
|
3
4
|
:root {
|
|
4
5
|
color-scheme: light dark;
|
|
5
|
-
--
|
|
6
|
-
--
|
|
7
|
-
--
|
|
8
|
-
--
|
|
9
|
-
--
|
|
6
|
+
--ink: #070707;
|
|
7
|
+
--ink-2: #3a3d44;
|
|
8
|
+
--titanium: #6e737c;
|
|
9
|
+
--paper: #ffffff;
|
|
10
|
+
--success: #12a37a;
|
|
11
|
+
--danger: #e5484d;
|
|
10
12
|
}
|
|
11
13
|
@media (prefers-color-scheme: dark) {
|
|
12
14
|
:root {
|
|
13
|
-
--
|
|
14
|
-
--
|
|
15
|
-
--
|
|
16
|
-
--
|
|
17
|
-
--error: #f87171;
|
|
15
|
+
--ink: #f2f2f0;
|
|
16
|
+
--ink-2: #b9bcc3;
|
|
17
|
+
--titanium: #8a8f98;
|
|
18
|
+
--paper: #070707;
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
21
|
* { box-sizing: border-box; margin: 0; }
|
|
21
|
-
html, body { height: 100%; }
|
|
22
|
+
html, body { min-height: 100%; }
|
|
22
23
|
body {
|
|
23
24
|
display: flex;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
font: 14px/20px Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
25
|
+
flex-direction: column;
|
|
26
|
+
min-height: 100svh;
|
|
27
|
+
background: var(--paper);
|
|
28
|
+
color: var(--ink);
|
|
29
|
+
font: 15px/1.55 Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;
|
|
30
30
|
-webkit-font-smoothing: antialiased;
|
|
31
31
|
}
|
|
32
32
|
main {
|
|
33
|
+
display: flex;
|
|
34
|
+
flex: 1;
|
|
35
|
+
align-items: center;
|
|
36
|
+
justify-content: center;
|
|
37
|
+
padding: 24px 16px;
|
|
38
|
+
}
|
|
39
|
+
.plate {
|
|
33
40
|
width: 100%;
|
|
34
|
-
max-width:
|
|
35
|
-
|
|
41
|
+
max-width: 520px;
|
|
42
|
+
padding: 28px 20px;
|
|
43
|
+
background: var(--paper);
|
|
36
44
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
width: 48px;
|
|
40
|
-
height: 48px;
|
|
41
|
-
margin: 0 auto 16px;
|
|
45
|
+
@media (min-width: 640px) {
|
|
46
|
+
.plate { padding: 40px; }
|
|
42
47
|
}
|
|
43
|
-
|
|
44
|
-
.
|
|
48
|
+
svg { display: block; }
|
|
49
|
+
.mark { width: 25px; height: 16px; margin-bottom: 32px; }
|
|
45
50
|
h1 {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
display: flex;
|
|
52
|
+
align-items: center;
|
|
53
|
+
gap: 8px;
|
|
54
|
+
font-size: 24px;
|
|
55
|
+
line-height: 1.1;
|
|
56
|
+
font-weight: 500;
|
|
57
|
+
letter-spacing: -0.0195em;
|
|
58
|
+
text-wrap: balance;
|
|
59
|
+
}
|
|
60
|
+
.glyph { width: 24px; height: 24px; flex: none; }
|
|
61
|
+
.success { color: var(--success); }
|
|
62
|
+
.error { color: var(--danger); }
|
|
63
|
+
.lede { margin-top: 12px; color: var(--ink-2); }
|
|
64
|
+
.note {
|
|
65
|
+
margin-top: 32px;
|
|
66
|
+
font-size: 13px;
|
|
67
|
+
line-height: 1.45;
|
|
68
|
+
color: var(--titanium);
|
|
69
|
+
overflow-wrap: anywhere;
|
|
70
|
+
}
|
|
71
|
+
.note .ink { color: var(--ink); }
|
|
72
|
+
.gap {
|
|
73
|
+
font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
74
|
+
}
|
|
75
|
+
/* A button reset to inline text. */
|
|
76
|
+
.cmd {
|
|
77
|
+
all: unset;
|
|
78
|
+
font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
|
|
79
|
+
color: var(--ink);
|
|
80
|
+
cursor: pointer;
|
|
81
|
+
}
|
|
82
|
+
.cmd:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
|
|
83
|
+
/* The icon after the command, one monospace space from it. Both glyphs keep
|
|
84
|
+
the same box, so the line does not move when the check replaces the copy
|
|
85
|
+
icon. */
|
|
86
|
+
.cmd svg {
|
|
87
|
+
display: inline-block;
|
|
88
|
+
width: 13px;
|
|
89
|
+
height: 13px;
|
|
90
|
+
margin-left: 1ch;
|
|
91
|
+
vertical-align: -2px;
|
|
92
|
+
color: var(--titanium);
|
|
93
|
+
}
|
|
94
|
+
.cmd .done {
|
|
95
|
+
display: none;
|
|
96
|
+
color: var(--success);
|
|
97
|
+
}
|
|
98
|
+
.cmd.is-copied .copy { display: none; }
|
|
99
|
+
.cmd.is-copied .done { display: inline-block; }
|
|
100
|
+
.status {
|
|
101
|
+
position: absolute;
|
|
102
|
+
width: 1px;
|
|
103
|
+
height: 1px;
|
|
104
|
+
overflow: hidden;
|
|
105
|
+
clip-path: inset(50%);
|
|
106
|
+
white-space: nowrap;
|
|
51
107
|
}
|
|
52
|
-
p { color: var(--muted-foreground); }
|
|
53
108
|
`;
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
|
|
109
|
+
const COPY_SCRIPT = `const cmd = document.querySelector('.cmd');
|
|
110
|
+
const status = document.querySelector('.status');
|
|
111
|
+
let timer;
|
|
112
|
+
cmd.addEventListener('click', () => {
|
|
113
|
+
navigator.clipboard.writeText('npx gt login').then(() => {
|
|
114
|
+
cmd.classList.add('is-copied');
|
|
115
|
+
status.textContent = 'Copied';
|
|
116
|
+
clearTimeout(timer);
|
|
117
|
+
timer = setTimeout(() => {
|
|
118
|
+
cmd.classList.remove('is-copied');
|
|
119
|
+
status.textContent = '';
|
|
120
|
+
}, 1600);
|
|
121
|
+
}, () => {});
|
|
122
|
+
});`;
|
|
123
|
+
/** The policy the loopback server sends with the page. */
|
|
124
|
+
const CALLBACK_PAGE_CSP = `default-src 'none'; style-src 'unsafe-inline'; script-src 'sha256-${createHash("sha256").update(COPY_SCRIPT).digest("base64")}'`;
|
|
125
|
+
const GT_MARK = "<svg class=\"mark\" viewBox=\"-8 214 1213 771\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" d=\"M363 222.5L1197 222.5L1196.5 283L834 283.5L832.5 976L773 975.5L772.5 398L359.5 398L341.5 401L301.5 414L271.5 430L249.5 446L231 463.5L214 484.5L190 529.5L180 567.5L178 613.5L185 653.5L196 682.5L217 717.5L242.5 746L270.5 768L314.5 790L342.5 798L372.5 802L399.5 802L430.5 798L475.5 783L502.5 768L524 751.5L523.5 747L415.5 748L414.5 684L583 684.5L583 923.5L580.5 926L516.5 955L476.5 967L439.5 974L403.5 977L355.5 976L326.5 973L287.5 965L252.5 954L221.5 941L187.5 923L155.5 902L121.5 874L97 849.5L77 825.5L55 793.5L33 752.5L15 705.5L4 656.5L0 613.5L2 556.5L10 511.5L23 469.5L44 423.5L66 387.5L99 346.5L129.5 317L170.5 286L225.5 256L275.5 237L325.5 226L363 222.5Z M386.5 282L322.5 288L275.5 301L220.5 327L167.5 365L123 413.5L103 443.5L87 474.5L71 518.5L61 578.5L63 641.5L68 669.5L78 703.5L107 762.5L143 810.5L171.5 838L194.5 856L248.5 887L305.5 907L366.5 916L403.5 916L442.5 912L490.5 900L523.5 887L524 826.5L479.5 847L440.5 858L399.5 863L344.5 860L291.5 846L254.5 829L214.5 802L186 775.5L165 749.5L141 708.5L125 664.5L118 624.5L118 573.5L126 530.5L139 494.5L165 449.5L201.5 408L238.5 379L292.5 352L341.5 339L373.5 336L773 336.5L772.5 283L386.5 282Z M888 337.5L1197 337.5L1196.5 398L949 398.5L948.5 976L888 975.5L888 337.5Z M415 571.5L692 572.5L692 830.5L668 858.5L633.5 890L631 890.5L631 635.5L414.5 635L415 571.5Z\"/></svg>";
|
|
126
|
+
const COPY_ICON = "<svg class=\"copy\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg>";
|
|
127
|
+
const DONE_ICON = "<svg class=\"done\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"M20 6 9 17l-5-5\"/></svg>";
|
|
128
|
+
const CHECK_CIRCLE = "<svg class=\"glyph success\" viewBox=\"0 0 24 24\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12Zm13.36-1.814a.75.75 0 1 0-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 0 0-1.06 1.06l2.25 2.25a.75.75 0 0 0 1.14-.094l3.75-5.25Z\"/></svg>";
|
|
129
|
+
const X_CIRCLE = "<svg class=\"glyph error\" viewBox=\"0 0 24 24\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25Zm-1.72 6.97a.75.75 0 1 0-1.06 1.06L10.94 12l-1.72 1.72a.75.75 0 1 0 1.06 1.06L12 13.06l1.72 1.72a.75.75 0 1 0 1.06-1.06L13.06 12l1.72-1.72a.75.75 0 1 0-1.06-1.06L12 10.94l-1.72-1.72Z\"/></svg>";
|
|
130
|
+
function escapeHtml(value) {
|
|
131
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
132
|
+
}
|
|
133
|
+
const RETRY = `Run<span class="gap"> </span><button type="button" class="cmd" aria-label="Copy npx gt login">npx gt login${COPY_ICON}${DONE_ICON}</button><span class="gap"> </span>to try again.<span class="status" role="status"></span>`;
|
|
134
|
+
function copy(view) {
|
|
135
|
+
if (view.ok) return {
|
|
136
|
+
title: "Signed in to the gt CLI",
|
|
137
|
+
glyph: CHECK_CIRCLE,
|
|
138
|
+
lede: "You can close this tab and return to your terminal.",
|
|
139
|
+
note: view.account ? `Signed in as <span class="ink">${escapeHtml(view.account)}</span>.` : ""
|
|
140
|
+
};
|
|
141
|
+
if (view.reason === "denied") return {
|
|
142
|
+
title: "Request denied",
|
|
143
|
+
glyph: X_CIRCLE,
|
|
144
|
+
lede: "You did not authorize the CLI, so nothing changed. You can close this tab.",
|
|
145
|
+
note: RETRY
|
|
146
|
+
};
|
|
147
|
+
return {
|
|
148
|
+
title: "Sign-in failed",
|
|
149
|
+
glyph: X_CIRCLE,
|
|
150
|
+
lede: "The CLI could not finish signing you in. Your terminal shows the reason.",
|
|
151
|
+
note: RETRY
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function renderCallbackPage(view) {
|
|
155
|
+
const { title, glyph, lede, note } = copy(view);
|
|
58
156
|
return `<!doctype html>
|
|
59
157
|
<html lang="en">
|
|
60
158
|
<head>
|
|
@@ -65,14 +163,16 @@ function renderCallbackPage(success) {
|
|
|
65
163
|
</head>
|
|
66
164
|
<body>
|
|
67
165
|
<main>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
<
|
|
71
|
-
</
|
|
166
|
+
<div class="plate">
|
|
167
|
+
${GT_MARK}
|
|
168
|
+
<h1>${glyph}<span>${title}</span></h1>
|
|
169
|
+
<p class="lede">${lede}</p>${note ? `\n<p class="note">${note}</p>` : ""}
|
|
170
|
+
</div>
|
|
171
|
+
</main>${note === RETRY ? `\n<script>${COPY_SCRIPT}<\/script>` : ""}
|
|
72
172
|
</body>
|
|
73
173
|
</html>`;
|
|
74
174
|
}
|
|
75
175
|
//#endregion
|
|
76
|
-
export { renderCallbackPage };
|
|
176
|
+
export { CALLBACK_PAGE_CSP, renderCallbackPage };
|
|
77
177
|
|
|
78
178
|
//# sourceMappingURL=callbackPage.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"callbackPage.js","names":[],"sources":["../../src/auth/callbackPage.ts"],"sourcesContent":["// Mirrors the terminal state of the dashboard's device-approval screen\n// (gt-cloud apps/dashboard/.../signin/device/_components/DeviceApproval.tsx):\n// a 48px lucide status icon, 18px semibold heading, 14px muted body, centered\n// in a 400px column. Tokens are copied from gt-cloud packages/ui shared.css\n// because this page is served from 127.0.0.1 with no external assets.\nconst STYLES = `\n :root {\n color-scheme: light dark;\n --background: #fff;\n --foreground: #09090b;\n --muted-foreground: #71717a;\n --success: #1cca5b;\n --error: #dc2626;\n }\n @media (prefers-color-scheme: dark) {\n :root {\n --background: #09090b;\n --foreground: #fafafa;\n --muted-foreground: #a1a1aa;\n --success: #387836;\n --error: #f87171;\n }\n }\n * { box-sizing: border-box; margin: 0; }\n html, body { height: 100%; }\n body {\n display: flex;\n align-items: center;\n justify-content: center;\n padding: 48px 24px;\n background: var(--background);\n color: var(--foreground);\n font: 14px/20px Inter, ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif;\n -webkit-font-smoothing: antialiased;\n }\n main {\n width: 100%;\n max-width: 400px;\n text-align: center;\n }\n svg {\n display: block;\n width: 48px;\n height: 48px;\n margin: 0 auto 16px;\n }\n .success { color: var(--success); }\n .error { color: var(--error); }\n h1 {\n font-size: 18px;\n line-height: 28px;\n font-weight: 600;\n letter-spacing: -0.01em;\n margin-bottom: 8px;\n }\n p { color: var(--muted-foreground); }\n`;\n\nconst CIRCLE_CHECK =\n '<svg class=\"success\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"m9 12 2 2 4-4\"/></svg>';\nconst CIRCLE_X =\n '<svg class=\"error\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"m15 9-6 6m0-6 6 6\"/></svg>';\n\nexport function renderCallbackPage(success: boolean): string {\n const title = success\n ? 'Successfully authenticated gt CLI'\n : 'Authentication failed';\n return `<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<title>${title}</title>\n<style>${STYLES}</style>\n</head>\n<body>\n<main>\n${success ? CIRCLE_CHECK : CIRCLE_X}\n<h1>${title}</h1>\n<p>You may now close this tab and return to the terminal.</p>\n</main>\n</body>\n</html>`;\n}\n"],"mappings":";AAKA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqDf,MAAM,eACJ;AACF,MAAM,WACJ;AAEF,SAAgB,mBAAmB,SAA0B;CAC3D,MAAM,QAAQ,UACV,sCACA;AACJ,QAAO;;;;;SAKA,MAAM;SACN,OAAO;;;;EAId,UAAU,eAAe,SAAS;MAC9B,MAAM"}
|
|
1
|
+
{"version":3,"file":"callbackPage.js","names":[],"sources":["../../src/auth/callbackPage.ts"],"sourcesContent":["import { createHash } from 'node:crypto';\n\n// The page the loopback server shows once `gt login` has finished. It is\n// served from 127.0.0.1 under CALLBACK_PAGE_CSP, so everything is inline: the brand deck's tokens (paper and ink with a\n// prefers-color-scheme swap, ink-2 for the sentence, titanium for the note,\n// the status hues), the GT mark and a Heroicons 24/solid status glyph. The\n// composition is the dashboard's auth plate in its centered form: the plate\n// alone on the plain ground, centered both ways, with the mark, a 24px\n// heading with the glyph, a lede, and a 13px note naming the account. No\n// field, no footer, nothing from the network and no webfont; Inter is used\n// when it is installed. The one script copies the retry command on click; the\n// policy allows it by its hash and allows no other script.\n/** What the page says: a signed-in account, or why the login did not finish. */\nexport type CallbackPageView =\n | { ok: true; account?: string }\n | { ok: false; reason: 'denied' | 'failed' };\n\n/** The part of a success view the login flow can add after the exchange. */\nexport type CallbackPageDetails = { account?: string };\n\nconst STYLES = `\n :root {\n color-scheme: light dark;\n --ink: #070707;\n --ink-2: #3a3d44;\n --titanium: #6e737c;\n --paper: #ffffff;\n --success: #12a37a;\n --danger: #e5484d;\n }\n @media (prefers-color-scheme: dark) {\n :root {\n --ink: #f2f2f0;\n --ink-2: #b9bcc3;\n --titanium: #8a8f98;\n --paper: #070707;\n }\n }\n * { box-sizing: border-box; margin: 0; }\n html, body { min-height: 100%; }\n body {\n display: flex;\n flex-direction: column;\n min-height: 100svh;\n background: var(--paper);\n color: var(--ink);\n font: 15px/1.55 Inter, ui-sans-serif, system-ui, -apple-system, sans-serif;\n -webkit-font-smoothing: antialiased;\n }\n main {\n display: flex;\n flex: 1;\n align-items: center;\n justify-content: center;\n padding: 24px 16px;\n }\n .plate {\n width: 100%;\n max-width: 520px;\n padding: 28px 20px;\n background: var(--paper);\n }\n @media (min-width: 640px) {\n .plate { padding: 40px; }\n }\n svg { display: block; }\n .mark { width: 25px; height: 16px; margin-bottom: 32px; }\n h1 {\n display: flex;\n align-items: center;\n gap: 8px;\n font-size: 24px;\n line-height: 1.1;\n font-weight: 500;\n letter-spacing: -0.0195em;\n text-wrap: balance;\n }\n .glyph { width: 24px; height: 24px; flex: none; }\n .success { color: var(--success); }\n .error { color: var(--danger); }\n .lede { margin-top: 12px; color: var(--ink-2); }\n .note {\n margin-top: 32px;\n font-size: 13px;\n line-height: 1.45;\n color: var(--titanium);\n overflow-wrap: anywhere;\n }\n .note .ink { color: var(--ink); }\n .gap {\n font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;\n }\n /* A button reset to inline text. */\n .cmd {\n all: unset;\n font: 13px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;\n color: var(--ink);\n cursor: pointer;\n }\n .cmd:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }\n /* The icon after the command, one monospace space from it. Both glyphs keep\n the same box, so the line does not move when the check replaces the copy\n icon. */\n .cmd svg {\n display: inline-block;\n width: 13px;\n height: 13px;\n margin-left: 1ch;\n vertical-align: -2px;\n color: var(--titanium);\n }\n .cmd .done {\n display: none;\n color: var(--success);\n }\n .cmd.is-copied .copy { display: none; }\n .cmd.is-copied .done { display: inline-block; }\n .status {\n position: absolute;\n width: 1px;\n height: 1px;\n overflow: hidden;\n clip-path: inset(50%);\n white-space: nowrap;\n }\n`;\n\n// Copies the retry command and shows a check for 1.6s, announced to screen\n// readers.\nconst COPY_SCRIPT = `const cmd = document.querySelector('.cmd');\nconst status = document.querySelector('.status');\nlet timer;\ncmd.addEventListener('click', () => {\n navigator.clipboard.writeText('npx gt login').then(() => {\n cmd.classList.add('is-copied');\n status.textContent = 'Copied';\n clearTimeout(timer);\n timer = setTimeout(() => {\n cmd.classList.remove('is-copied');\n status.textContent = '';\n }, 1600);\n }, () => {});\n});`;\n\n/** The policy the loopback server sends with the page. */\nexport const CALLBACK_PAGE_CSP = `default-src 'none'; style-src 'unsafe-inline'; script-src 'sha256-${createHash('sha256').update(COPY_SCRIPT).digest('base64')}'`;\n\n// The General Translation mark, from the brand asset set.\nconst GT_MARK =\n '<svg class=\"mark\" viewBox=\"-8 214 1213 771\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" d=\"M363 222.5L1197 222.5L1196.5 283L834 283.5L832.5 976L773 975.5L772.5 398L359.5 398L341.5 401L301.5 414L271.5 430L249.5 446L231 463.5L214 484.5L190 529.5L180 567.5L178 613.5L185 653.5L196 682.5L217 717.5L242.5 746L270.5 768L314.5 790L342.5 798L372.5 802L399.5 802L430.5 798L475.5 783L502.5 768L524 751.5L523.5 747L415.5 748L414.5 684L583 684.5L583 923.5L580.5 926L516.5 955L476.5 967L439.5 974L403.5 977L355.5 976L326.5 973L287.5 965L252.5 954L221.5 941L187.5 923L155.5 902L121.5 874L97 849.5L77 825.5L55 793.5L33 752.5L15 705.5L4 656.5L0 613.5L2 556.5L10 511.5L23 469.5L44 423.5L66 387.5L99 346.5L129.5 317L170.5 286L225.5 256L275.5 237L325.5 226L363 222.5Z M386.5 282L322.5 288L275.5 301L220.5 327L167.5 365L123 413.5L103 443.5L87 474.5L71 518.5L61 578.5L63 641.5L68 669.5L78 703.5L107 762.5L143 810.5L171.5 838L194.5 856L248.5 887L305.5 907L366.5 916L403.5 916L442.5 912L490.5 900L523.5 887L524 826.5L479.5 847L440.5 858L399.5 863L344.5 860L291.5 846L254.5 829L214.5 802L186 775.5L165 749.5L141 708.5L125 664.5L118 624.5L118 573.5L126 530.5L139 494.5L165 449.5L201.5 408L238.5 379L292.5 352L341.5 339L373.5 336L773 336.5L772.5 283L386.5 282Z M888 337.5L1197 337.5L1196.5 398L949 398.5L948.5 976L888 975.5L888 337.5Z M415 571.5L692 572.5L692 830.5L668 858.5L633.5 890L631 890.5L631 635.5L414.5 635L415 571.5Z\"/></svg>';\n\n// Lucide copy and check (ISC), the control icons the landing uses.\nconst COPY_ICON =\n '<svg class=\"copy\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><rect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"/><path d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"/></svg>';\nconst DONE_ICON =\n '<svg class=\"done\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\" stroke-linecap=\"round\" stroke-linejoin=\"round\" aria-hidden=\"true\"><path d=\"M20 6 9 17l-5-5\"/></svg>';\n\n// Heroicons 24/solid check-circle and x-circle (MIT, Tailwind Labs).\nconst CHECK_CIRCLE =\n '<svg class=\"glyph success\" viewBox=\"0 0 24 24\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M2.25 12c0-5.385 4.365-9.75 9.75-9.75s9.75 4.365 9.75 9.75-4.365 9.75-9.75 9.75S2.25 17.385 2.25 12Zm13.36-1.814a.75.75 0 1 0-1.22-.872l-3.236 4.53L9.53 12.22a.75.75 0 0 0-1.06 1.06l2.25 2.25a.75.75 0 0 0 1.14-.094l3.75-5.25Z\"/></svg>';\nconst X_CIRCLE =\n '<svg class=\"glyph error\" viewBox=\"0 0 24 24\" fill=\"currentColor\" aria-hidden=\"true\"><path fill-rule=\"evenodd\" clip-rule=\"evenodd\" d=\"M12 2.25c-5.385 0-9.75 4.365-9.75 9.75s4.365 9.75 9.75 9.75 9.75-4.365 9.75-9.75S17.385 2.25 12 2.25Zm-1.72 6.97a.75.75 0 1 0-1.06 1.06L10.94 12l-1.72 1.72a.75.75 0 1 0 1.06 1.06L12 13.06l1.72 1.72a.75.75 0 1 0 1.06-1.06L13.06 12l1.72-1.72a.75.75 0 1 0-1.06-1.06L12 10.94l-1.72-1.72Z\"/></svg>';\n\nfunction escapeHtml(value: string): string {\n return value\n .replace(/&/g, '&')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/\"/g, '"');\n}\n\n// The spaces around the command are monospace, so the gap before and after\n// it matches the gap between its words.\nconst RETRY = `Run<span class=\"gap\"> </span><button type=\"button\" class=\"cmd\" aria-label=\"Copy npx gt login\">npx gt login${COPY_ICON}${DONE_ICON}</button><span class=\"gap\"> </span>to try again.<span class=\"status\" role=\"status\"></span>`;\n\nfunction copy(view: CallbackPageView): {\n title: string;\n glyph: string;\n lede: string;\n note: string;\n} {\n if (view.ok) {\n return {\n title: 'Signed in to the gt CLI',\n glyph: CHECK_CIRCLE,\n lede: 'You can close this tab and return to your terminal.',\n note: view.account\n ? `Signed in as <span class=\"ink\">${escapeHtml(view.account)}</span>.`\n : '',\n };\n }\n if (view.reason === 'denied') {\n return {\n title: 'Request denied',\n glyph: X_CIRCLE,\n lede: 'You did not authorize the CLI, so nothing changed. You can close this tab.',\n note: RETRY,\n };\n }\n return {\n title: 'Sign-in failed',\n glyph: X_CIRCLE,\n lede: 'The CLI could not finish signing you in. Your terminal shows the reason.',\n note: RETRY,\n };\n}\n\nexport function renderCallbackPage(view: CallbackPageView): string {\n const { title, glyph, lede, note } = copy(view);\n return `<!doctype html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n<title>${title}</title>\n<style>${STYLES}</style>\n</head>\n<body>\n<main>\n<div class=\"plate\">\n${GT_MARK}\n<h1>${glyph}<span>${title}</span></h1>\n<p class=\"lede\">${lede}</p>${note ? `\\n<p class=\"note\">${note}</p>` : ''}\n</div>\n</main>${note === RETRY ? `\\n<script>${COPY_SCRIPT}</script>` : ''}\n</body>\n</html>`;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declare const LOOPBACK_CALLBACK_PATH = "/callback";
|
|
3
|
+
/**
|
|
4
|
+
* How long a settled callback's page stays available to a browser that
|
|
5
|
+
* repeats the navigation after the exchange (a first request it aborted, a
|
|
6
|
+
* refresh), while the process is still running.
|
|
7
|
+
*/
|
|
8
|
+
export declare const CALLBACK_REPEAT_WINDOW_MS = 10000;
|
|
9
|
+
/**
|
|
10
|
+
* How long the process stays open for the browser's repeat when the first
|
|
11
|
+
* request was abandoned before its page was sent, so the page the browser
|
|
12
|
+
* never received can still reach it.
|
|
13
|
+
*/
|
|
14
|
+
export declare const CALLBACK_RETRY_HOLD_MS = 5000;
|
|
15
|
+
export type CallbackWaitOptions<T> = {
|
|
16
|
+
/** What the page says about a success, such as the signed-in account. */
|
|
17
|
+
describe?: (result: T) => CallbackPageDetails;
|
|
18
|
+
/**
|
|
19
|
+
* Whether a failed exchange was the user's own denial; the page reads the
|
|
20
|
+
* exchange's error, so it agrees with the terminal. Without it every
|
|
21
|
+
* failure is shown as failed.
|
|
22
|
+
*/
|
|
23
|
+
failure?: (error: unknown) => 'denied' | 'failed';
|
|
24
|
+
};
|
|
2
25
|
export type LoopbackServer = {
|
|
3
26
|
/** Loopback redirect URI bound to an ephemeral port, e.g. http://127.0.0.1:53211/callback */
|
|
4
27
|
redirectUri: string;
|
|
5
|
-
/**
|
|
6
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Shows the result page only after the callback has been validated and
|
|
30
|
+
* saved. `describe` adds what the page says about a success, such as the
|
|
31
|
+
* signed-in account, and `failure` says whether a failure was a denial.
|
|
32
|
+
*/
|
|
33
|
+
waitForCallback: <T>(onCallback: (url: URL) => Promise<T>, timeoutMs?: number, options?: CallbackWaitOptions<T>) => Promise<T>;
|
|
34
|
+
/**
|
|
35
|
+
* Stops the server. Once a callback has settled, repeats of that callback
|
|
36
|
+
* are still answered for CALLBACK_REPEAT_WINDOW_MS while the process runs;
|
|
37
|
+
* the listener holds the process open only for CALLBACK_RETRY_HOLD_MS, and
|
|
38
|
+
* only when the first page was never sent. A command that exits on its own
|
|
39
|
+
* (a failed `gt login` exits at once) ends the window early.
|
|
40
|
+
*/
|
|
7
41
|
close: () => void;
|
|
8
42
|
};
|
|
9
43
|
export declare function startLoopbackServer(): Promise<LoopbackServer>;
|
package/dist/auth/loopback.js
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
import { renderCallbackPage } from "./callbackPage.js";
|
|
1
|
+
import { CALLBACK_PAGE_CSP, renderCallbackPage } from "./callbackPage.js";
|
|
2
2
|
import { createDiagnosticMessage } from "generaltranslation/diagnostics";
|
|
3
3
|
import { createServer } from "node:http";
|
|
4
4
|
//#region src/auth/loopback.ts
|
|
5
5
|
const LOOPBACK_CALLBACK_PATH = "/callback";
|
|
6
6
|
const DEFAULT_CALLBACK_TIMEOUT_MS = 300 * 1e3;
|
|
7
|
+
/**
|
|
8
|
+
* How long a settled callback's page stays available to a browser that
|
|
9
|
+
* repeats the navigation after the exchange (a first request it aborted, a
|
|
10
|
+
* refresh), while the process is still running.
|
|
11
|
+
*/
|
|
12
|
+
const CALLBACK_REPEAT_WINDOW_MS = 1e4;
|
|
13
|
+
/**
|
|
14
|
+
* How long the process stays open for the browser's repeat when the first
|
|
15
|
+
* request was abandoned before its page was sent, so the page the browser
|
|
16
|
+
* never received can still reach it.
|
|
17
|
+
*/
|
|
18
|
+
const CALLBACK_RETRY_HOLD_MS = 5e3;
|
|
7
19
|
function listen(server) {
|
|
8
20
|
return new Promise((resolve, reject) => {
|
|
9
21
|
server.once("error", reject);
|
|
@@ -23,58 +35,101 @@ async function startLoopbackServer() {
|
|
|
23
35
|
const server = createServer();
|
|
24
36
|
const origin = `http://127.0.0.1:${await listen(server)}`;
|
|
25
37
|
let timeout;
|
|
38
|
+
let repeatWindow;
|
|
39
|
+
let hold;
|
|
40
|
+
const release = () => {
|
|
41
|
+
clearTimeout(hold);
|
|
42
|
+
server.unref();
|
|
43
|
+
};
|
|
44
|
+
const closeAfterRepeats = (firstPageSent) => {
|
|
45
|
+
if (repeatWindow) return;
|
|
46
|
+
repeatWindow = setTimeout(() => server.close(), CALLBACK_REPEAT_WINDOW_MS);
|
|
47
|
+
repeatWindow.unref();
|
|
48
|
+
if (firstPageSent) release();
|
|
49
|
+
else hold = setTimeout(release, CALLBACK_RETRY_HOLD_MS);
|
|
50
|
+
};
|
|
26
51
|
return {
|
|
27
52
|
redirectUri: `${origin}${LOOPBACK_CALLBACK_PATH}`,
|
|
28
|
-
waitForCallback(onCallback, timeoutMs = DEFAULT_CALLBACK_TIMEOUT_MS) {
|
|
53
|
+
waitForCallback(onCallback, timeoutMs = DEFAULT_CALLBACK_TIMEOUT_MS, options) {
|
|
29
54
|
return new Promise((resolve, reject) => {
|
|
30
|
-
let
|
|
55
|
+
let timedOut = false;
|
|
56
|
+
let outcome;
|
|
31
57
|
timeout = setTimeout(() => {
|
|
32
|
-
|
|
58
|
+
timedOut = true;
|
|
33
59
|
server.close();
|
|
34
60
|
reject(new Error(createDiagnosticMessage({
|
|
35
61
|
whatHappened: "Timed out waiting for authentication",
|
|
36
62
|
fix: "Run `gt login` again"
|
|
37
63
|
})));
|
|
38
64
|
}, timeoutMs);
|
|
65
|
+
const respond = (response, page) => {
|
|
66
|
+
response.writeHead(200, {
|
|
67
|
+
"cache-control": "no-store",
|
|
68
|
+
connection: "close",
|
|
69
|
+
"content-security-policy": CALLBACK_PAGE_CSP,
|
|
70
|
+
"content-type": "text/html; charset=utf-8",
|
|
71
|
+
"x-content-type-options": "nosniff"
|
|
72
|
+
});
|
|
73
|
+
response.end(page);
|
|
74
|
+
};
|
|
39
75
|
server.on("request", async (request, response) => {
|
|
40
76
|
const target = request.url ?? "/";
|
|
41
77
|
const url = URL.canParse(target, origin) ? new URL(target, origin) : void 0;
|
|
42
|
-
if (!url ||
|
|
78
|
+
if (!url || timedOut || request.method !== "GET" || url.origin !== origin || url.pathname !== "/callback" || outcome && outcome.href !== url.href) {
|
|
43
79
|
response.writeHead(404, {
|
|
44
80
|
"cache-control": "no-store",
|
|
81
|
+
connection: "close",
|
|
45
82
|
"content-type": "text/plain; charset=utf-8"
|
|
46
83
|
});
|
|
47
84
|
response.end("Not found");
|
|
48
85
|
return;
|
|
49
86
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
"x-content-type-options": "nosniff"
|
|
57
|
-
});
|
|
58
|
-
try {
|
|
59
|
-
const result = await onCallback(url);
|
|
60
|
-
response.end(renderCallbackPage(true));
|
|
61
|
-
resolve(result);
|
|
62
|
-
} catch (error) {
|
|
63
|
-
response.end(renderCallbackPage(false));
|
|
64
|
-
reject(error);
|
|
65
|
-
} finally {
|
|
66
|
-
server.close();
|
|
87
|
+
if (outcome) {
|
|
88
|
+
const page = await outcome.page;
|
|
89
|
+
if (response.destroyed) return;
|
|
90
|
+
respond(response, page);
|
|
91
|
+
release();
|
|
92
|
+
return;
|
|
67
93
|
}
|
|
94
|
+
clearTimeout(timeout);
|
|
95
|
+
outcome = {
|
|
96
|
+
href: url.href,
|
|
97
|
+
page: (async () => {
|
|
98
|
+
let page;
|
|
99
|
+
let firstPageSent = false;
|
|
100
|
+
try {
|
|
101
|
+
const result = await onCallback(url);
|
|
102
|
+
page = renderCallbackPage({
|
|
103
|
+
ok: true,
|
|
104
|
+
...options?.describe?.(result)
|
|
105
|
+
});
|
|
106
|
+
firstPageSent = !response.destroyed;
|
|
107
|
+
respond(response, page);
|
|
108
|
+
resolve(result);
|
|
109
|
+
} catch (error) {
|
|
110
|
+
page = renderCallbackPage({
|
|
111
|
+
ok: false,
|
|
112
|
+
reason: options?.failure?.(error) ?? "failed"
|
|
113
|
+
});
|
|
114
|
+
firstPageSent = !response.destroyed;
|
|
115
|
+
respond(response, page);
|
|
116
|
+
reject(error);
|
|
117
|
+
} finally {
|
|
118
|
+
closeAfterRepeats(firstPageSent);
|
|
119
|
+
}
|
|
120
|
+
return page;
|
|
121
|
+
})()
|
|
122
|
+
};
|
|
68
123
|
});
|
|
69
124
|
});
|
|
70
125
|
},
|
|
71
126
|
close() {
|
|
72
127
|
clearTimeout(timeout);
|
|
73
|
-
server.close();
|
|
128
|
+
if (!repeatWindow) server.close();
|
|
74
129
|
}
|
|
75
130
|
};
|
|
76
131
|
}
|
|
77
132
|
//#endregion
|
|
78
|
-
export { LOOPBACK_CALLBACK_PATH, startLoopbackServer };
|
|
133
|
+
export { CALLBACK_REPEAT_WINDOW_MS, CALLBACK_RETRY_HOLD_MS, LOOPBACK_CALLBACK_PATH, startLoopbackServer };
|
|
79
134
|
|
|
80
135
|
//# sourceMappingURL=loopback.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loopback.js","names":[],"sources":["../../src/auth/loopback.ts"],"sourcesContent":["import { createServer, type Server } from 'node:http';\nimport { createDiagnosticMessage } from 'generaltranslation/diagnostics';\nimport { renderCallbackPage } from './callbackPage.js';\n\nexport const LOOPBACK_CALLBACK_PATH = '/callback';\nconst DEFAULT_CALLBACK_TIMEOUT_MS = 5 * 60 * 1000;\n\nexport type LoopbackServer = {\n /** Loopback redirect URI bound to an ephemeral port, e.g. http://127.0.0.1:53211/callback */\n redirectUri: string;\n /** Shows the result page only after the callback has been validated and saved. */\n waitForCallback: <T>(\n onCallback: (url: URL) => Promise<T>,\n timeoutMs?: number\n ) => Promise<T>;\n close: () => void;\n};\n\nfunction listen(server: Server): Promise<number> {\n return new Promise((resolve, reject) => {\n server.once('error', reject);\n // RFC 8252 §7.3: bind the loopback interface on an ephemeral port; the\n // authorization server matches loopback redirect URIs ignoring the port.\n server.listen(0, '127.0.0.1', () => {\n server.off('error', reject);\n const address = server.address();\n if (!address || typeof address === 'string') {\n server.close();\n reject(\n new Error(\n createDiagnosticMessage({\n whatHappened: 'Could not resolve the loopback callback address',\n })\n )\n );\n return;\n }\n resolve(address.port);\n });\n });\n}\n\nexport async function startLoopbackServer(): Promise<LoopbackServer> {\n const server = createServer();\n const port = await listen(server);\n const origin = `http://127.0.0.1:${port}`;\n let timeout: NodeJS.Timeout | undefined;\n\n return {\n redirectUri: `${origin}${LOOPBACK_CALLBACK_PATH}`,\n waitForCallback<T>(\n onCallback: (url: URL) => Promise<T>,\n timeoutMs = DEFAULT_CALLBACK_TIMEOUT_MS\n ) {\n return new Promise<T>((resolve, reject) => {\n let settled = false;\n timeout = setTimeout(() => {\n settled = true;\n server.close();\n reject(\n new Error(\n createDiagnosticMessage({\n whatHappened: 'Timed out waiting for authentication',\n fix: 'Run `gt login` again',\n })\n )\n );\n }, timeoutMs);\n\n server.on('request', async (request, response) => {\n const target = request.url ?? '/';\n const url = URL.canParse(target, origin)\n ? new URL(target, origin)\n : undefined;\n if (\n !url ||\n settled ||\n request.method !== 'GET' ||\n url.origin !== origin ||\n url.pathname !== LOOPBACK_CALLBACK_PATH\n ) {\n response.writeHead(404, {\n 'cache-control': 'no-store',\n 'content-type': 'text/plain; charset=utf-8',\n });\n response.end('Not found');\n return;\n }\n\n settled = true;\n clearTimeout(timeout);\n response.writeHead(200, {\n 'cache-control': 'no-store',\n 'content-security-policy':\n \"default-src 'none'; style-src 'unsafe-inline'\",\n 'content-type': 'text/html; charset=utf-8',\n 'x-content-type-options': 'nosniff',\n });\n try {\n const result = await onCallback(url);\n response.end(renderCallbackPage(true));\n resolve(result);\n } catch (error) {\n response.end(renderCallbackPage(false));\n reject(error);\n } finally {\n server.close();\n }\n });\n });\n },\n close() {\n clearTimeout(timeout);\n server.close();\n },\n };\n}\n"],"mappings":";;;;AAIA,MAAa,yBAAyB;AACtC,MAAM,8BAA8B,MAAS;AAa7C,SAAS,OAAO,QAAiC;AAC/C,QAAO,IAAI,SAAS,SAAS,WAAW;AACtC,SAAO,KAAK,SAAS,OAAO;AAG5B,SAAO,OAAO,GAAG,mBAAmB;AAClC,UAAO,IAAI,SAAS,OAAO;GAC3B,MAAM,UAAU,OAAO,SAAS;AAChC,OAAI,CAAC,WAAW,OAAO,YAAY,UAAU;AAC3C,WAAO,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"sources":["../../src/auth/loopback.ts"],"sourcesContent":["import { createServer, type Server, type ServerResponse } from 'node:http';\nimport { createDiagnosticMessage } from 'generaltranslation/diagnostics';\nimport {\n CALLBACK_PAGE_CSP,\n renderCallbackPage,\n type CallbackPageDetails,\n} from './callbackPage.js';\n\nexport const LOOPBACK_CALLBACK_PATH = '/callback';\nconst DEFAULT_CALLBACK_TIMEOUT_MS = 5 * 60 * 1000;\n/**\n * How long a settled callback's page stays available to a browser that\n * repeats the navigation after the exchange (a first request it aborted, a\n * refresh), while the process is still running.\n */\nexport const CALLBACK_REPEAT_WINDOW_MS = 10_000;\n/**\n * How long the process stays open for the browser's repeat when the first\n * request was abandoned before its page was sent, so the page the browser\n * never received can still reach it.\n */\nexport const CALLBACK_RETRY_HOLD_MS = 5_000;\n\nexport type CallbackWaitOptions<T> = {\n /** What the page says about a success, such as the signed-in account. */\n describe?: (result: T) => CallbackPageDetails;\n /**\n * Whether a failed exchange was the user's own denial; the page reads the\n * exchange's error, so it agrees with the terminal. Without it every\n * failure is shown as failed.\n */\n failure?: (error: unknown) => 'denied' | 'failed';\n};\n\nexport type LoopbackServer = {\n /** Loopback redirect URI bound to an ephemeral port, e.g. http://127.0.0.1:53211/callback */\n redirectUri: string;\n /**\n * Shows the result page only after the callback has been validated and\n * saved. `describe` adds what the page says about a success, such as the\n * signed-in account, and `failure` says whether a failure was a denial.\n */\n waitForCallback: <T>(\n onCallback: (url: URL) => Promise<T>,\n timeoutMs?: number,\n options?: CallbackWaitOptions<T>\n ) => Promise<T>;\n /**\n * Stops the server. Once a callback has settled, repeats of that callback\n * are still answered for CALLBACK_REPEAT_WINDOW_MS while the process runs;\n * the listener holds the process open only for CALLBACK_RETRY_HOLD_MS, and\n * only when the first page was never sent. A command that exits on its own\n * (a failed `gt login` exits at once) ends the window early.\n */\n close: () => void;\n};\n\nfunction listen(server: Server): Promise<number> {\n return new Promise((resolve, reject) => {\n server.once('error', reject);\n // RFC 8252 §7.3: bind the loopback interface on an ephemeral port; the\n // authorization server matches loopback redirect URIs ignoring the port.\n server.listen(0, '127.0.0.1', () => {\n server.off('error', reject);\n const address = server.address();\n if (!address || typeof address === 'string') {\n server.close();\n reject(\n new Error(\n createDiagnosticMessage({\n whatHappened: 'Could not resolve the loopback callback address',\n })\n )\n );\n return;\n }\n resolve(address.port);\n });\n });\n}\n\nexport async function startLoopbackServer(): Promise<LoopbackServer> {\n const server = createServer();\n const port = await listen(server);\n const origin = `http://127.0.0.1:${port}`;\n let timeout: NodeJS.Timeout | undefined;\n let repeatWindow: NodeJS.Timeout | undefined;\n let hold: NodeJS.Timeout | undefined;\n // Lets the process exit without waiting for the repeat window.\n const release = () => {\n clearTimeout(hold);\n server.unref();\n };\n // After a callback settles, the listener answers its repeats for a short\n // while and then closes. A first page the browser received needs no\n // repeat, so the listener is unref'd at once and a finished CLI exits\n // without waiting. A first page the browser abandoned before it was sent\n // keeps the process open until the repeat is answered or the hold ends.\n const closeAfterRepeats = (firstPageSent: boolean) => {\n if (repeatWindow) return;\n repeatWindow = setTimeout(() => server.close(), CALLBACK_REPEAT_WINDOW_MS);\n repeatWindow.unref();\n if (firstPageSent) release();\n else hold = setTimeout(release, CALLBACK_RETRY_HOLD_MS);\n };\n\n return {\n redirectUri: `${origin}${LOOPBACK_CALLBACK_PATH}`,\n waitForCallback<T>(\n onCallback: (url: URL) => Promise<T>,\n timeoutMs = DEFAULT_CALLBACK_TIMEOUT_MS,\n options?: CallbackWaitOptions<T>\n ) {\n return new Promise<T>((resolve, reject) => {\n let timedOut = false;\n // Set by the first callback request: the page every request for that\n // same callback receives once the exchange has settled.\n let outcome: { href: string; page: Promise<string> } | undefined;\n timeout = setTimeout(() => {\n timedOut = true;\n server.close();\n reject(\n new Error(\n createDiagnosticMessage({\n whatHappened: 'Timed out waiting for authentication',\n fix: 'Run `gt login` again',\n })\n )\n );\n }, timeoutMs);\n\n // Every response closes its connection. A repeat of the callback can\n // be answered after the login has finished, and Node leaves a\n // keep-alive socket open after a response until the keep-alive\n // timeout, which would hold the process open.\n const respond = (response: ServerResponse, page: string) => {\n response.writeHead(200, {\n 'cache-control': 'no-store',\n connection: 'close',\n 'content-security-policy': CALLBACK_PAGE_CSP,\n 'content-type': 'text/html; charset=utf-8',\n 'x-content-type-options': 'nosniff',\n });\n response.end(page);\n };\n\n server.on('request', async (request, response) => {\n const target = request.url ?? '/';\n const url = URL.canParse(target, origin)\n ? new URL(target, origin)\n : undefined;\n if (\n !url ||\n timedOut ||\n request.method !== 'GET' ||\n url.origin !== origin ||\n url.pathname !== LOOPBACK_CALLBACK_PATH ||\n (outcome && outcome.href !== url.href)\n ) {\n response.writeHead(404, {\n 'cache-control': 'no-store',\n connection: 'close',\n 'content-type': 'text/plain; charset=utf-8',\n });\n response.end('Not found');\n return;\n }\n\n // Browsers abort and repeat a navigation (a redirect issued twice,\n // a refresh while the exchange runs or just after it), so a repeat\n // of the same callback gets the same page instead of a 404.\n if (outcome) {\n const page = await outcome.page;\n // A repeat the browser abandoned too gets nothing, and the hold\n // stays for the next one.\n if (response.destroyed) return;\n respond(response, page);\n release();\n return;\n }\n\n clearTimeout(timeout);\n outcome = {\n href: url.href,\n page: (async () => {\n let page: string;\n // Read when the exchange settles: a browser that aborted this\n // navigation has closed the connection by then.\n let firstPageSent = false;\n try {\n const result = await onCallback(url);\n page = renderCallbackPage({\n ok: true,\n ...options?.describe?.(result),\n });\n firstPageSent = !response.destroyed;\n respond(response, page);\n resolve(result);\n } catch (error) {\n // The page says why in the coarsest terms: a denial is the\n // user's own choice, anything else is reported in the terminal.\n // The exchange's error decides, not the callback's own error\n // parameter, which a callback with a bad state also carries.\n const reason = options?.failure?.(error) ?? 'failed';\n page = renderCallbackPage({ ok: false, reason });\n firstPageSent = !response.destroyed;\n respond(response, page);\n reject(error);\n } finally {\n closeAfterRepeats(firstPageSent);\n }\n return page;\n })(),\n };\n });\n });\n },\n close() {\n clearTimeout(timeout);\n // a settled callback's repeat window closes the server on its own\n if (!repeatWindow) server.close();\n },\n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|
openBrowser?: (url: string) => Promise<unknown>;
|
|
29
29
|
timeoutMs?: number;
|
|
30
30
|
};
|
|
31
|
+
/** How long the callback page waits to learn the account's name. */
|
|
32
|
+
export declare const ACCOUNT_LOOKUP_TIMEOUT_MS = 3000;
|
|
31
33
|
/** Browser S256/loopback, or device login for SSH, --no-browser and bind failure. */
|
|
32
34
|
export declare function login(options?: LoginOptions): Promise<OAuthTokens>;
|
|
33
35
|
/** Whether a stored login exists for this API; does not refresh or contact the server. */
|