@sentio/cli 4.4.0-rc.1 → 4.4.0-rc.3
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/dist/index.js +24 -11
- package/package.json +1 -1
- package/src/commands/login-server.ts +29 -14
package/dist/index.js
CHANGED
|
@@ -46183,10 +46183,18 @@ function isAccessTokenExpired(expiresAt) {
|
|
|
46183
46183
|
}
|
|
46184
46184
|
//#endregion
|
|
46185
46185
|
//#region src/commands/login-server.ts
|
|
46186
|
+
function loginResultPage(message) {
|
|
46187
|
+
return `<!doctype html>
|
|
46188
|
+
<html lang="en">
|
|
46189
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sentio CLI</title></head>
|
|
46190
|
+
<body><p>${message}</p></body>
|
|
46191
|
+
</html>`;
|
|
46192
|
+
}
|
|
46186
46193
|
function startServer(params) {
|
|
46187
46194
|
return new Promise((resolve, reject) => {
|
|
46188
46195
|
const app = (0, import_express.default)();
|
|
46189
|
-
let
|
|
46196
|
+
let redemption;
|
|
46197
|
+
let responses = 0;
|
|
46190
46198
|
let settled = false;
|
|
46191
46199
|
const controller = new AbortController();
|
|
46192
46200
|
const finish = (error) => {
|
|
@@ -46205,26 +46213,31 @@ function startServer(params) {
|
|
|
46205
46213
|
process.once("SIGINT", interrupted);
|
|
46206
46214
|
app.get("/callback", async (req, res) => {
|
|
46207
46215
|
res.setHeader("Cache-Control", "no-store");
|
|
46216
|
+
res.type("html");
|
|
46208
46217
|
if (typeof req.query.state !== "string" || req.query.state !== params.state) {
|
|
46209
|
-
res.status(400).end("Invalid authorization state");
|
|
46210
|
-
return;
|
|
46211
|
-
}
|
|
46212
|
-
if (redeeming) {
|
|
46213
|
-
res.status(409).end("Authorization already in progress");
|
|
46218
|
+
res.status(400).end(loginResultPage("Invalid authorization state"));
|
|
46214
46219
|
return;
|
|
46215
46220
|
}
|
|
46216
|
-
|
|
46221
|
+
responses++;
|
|
46217
46222
|
const respond = (message, error) => {
|
|
46218
|
-
|
|
46223
|
+
let completed = false;
|
|
46224
|
+
const complete = () => {
|
|
46225
|
+
if (completed) return;
|
|
46226
|
+
completed = true;
|
|
46227
|
+
if (--responses === 0) finish(error);
|
|
46228
|
+
};
|
|
46219
46229
|
if (res.destroyed) return complete();
|
|
46220
46230
|
res.once("close", complete);
|
|
46221
|
-
res.status(error ? 400 : 200).end(message, complete);
|
|
46231
|
+
res.status(error ? 400 : 200).end(loginResultPage(message), complete);
|
|
46222
46232
|
};
|
|
46223
46233
|
try {
|
|
46224
46234
|
if (req.query.error) throw new Error("Authorization was not completed");
|
|
46225
46235
|
if (typeof req.query.code !== "string" || !req.query.code) throw new Error("Missing authorization code");
|
|
46226
|
-
|
|
46227
|
-
|
|
46236
|
+
redemption ??= exchangeCodeAndSave(getFinalizedHost(params.sentioHost), req.query.code, params.codeVerifier, params.authConfig, controller.signal).then((username) => {
|
|
46237
|
+
console.log(chalk.green(`Login success with ${username}`));
|
|
46238
|
+
return username;
|
|
46239
|
+
});
|
|
46240
|
+
await redemption;
|
|
46228
46241
|
respond("Login success, please go back to CLI to continue");
|
|
46229
46242
|
} catch (error) {
|
|
46230
46243
|
respond("Login failed. Check the terminal and try again.", error);
|
package/package.json
CHANGED
|
@@ -17,10 +17,20 @@ interface AuthParams {
|
|
|
17
17
|
timeoutMs?: number
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
// Only fixed messages are rendered; OAuth parameters never appear in the page.
|
|
21
|
+
function loginResultPage(message: string): string {
|
|
22
|
+
return `<!doctype html>
|
|
23
|
+
<html lang="en">
|
|
24
|
+
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sentio CLI</title></head>
|
|
25
|
+
<body><p>${message}</p></body>
|
|
26
|
+
</html>`
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
export function startServer(params: AuthParams): Promise<void> {
|
|
21
30
|
return new Promise((resolve, reject) => {
|
|
22
31
|
const app = express()
|
|
23
|
-
let
|
|
32
|
+
let redemption: Promise<string> | undefined
|
|
33
|
+
let responses = 0
|
|
24
34
|
let settled = false
|
|
25
35
|
const controller = new AbortController()
|
|
26
36
|
const finish = (error?: Error) => {
|
|
@@ -42,34 +52,39 @@ export function startServer(params: AuthParams): Promise<void> {
|
|
|
42
52
|
process.once('SIGINT', interrupted)
|
|
43
53
|
app.get('/callback', async (req, res) => {
|
|
44
54
|
res.setHeader('Cache-Control', 'no-store')
|
|
55
|
+
res.type('html')
|
|
45
56
|
if (typeof req.query.state !== 'string' || req.query.state !== params.state) {
|
|
46
|
-
res.status(400).end('Invalid authorization state')
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
if (redeeming) {
|
|
50
|
-
res.status(409).end('Authorization already in progress')
|
|
57
|
+
res.status(400).end(loginResultPage('Invalid authorization state'))
|
|
51
58
|
return
|
|
52
59
|
}
|
|
53
|
-
|
|
60
|
+
responses++
|
|
54
61
|
const respond = (message: string, error?: Error) => {
|
|
55
|
-
//
|
|
56
|
-
//
|
|
57
|
-
|
|
62
|
+
// Browsers may interrupt and retry the navigation during redemption.
|
|
63
|
+
// Flush every waiting response before closing the shared listener.
|
|
64
|
+
let completed = false
|
|
65
|
+
const complete = () => {
|
|
66
|
+
if (completed) return
|
|
67
|
+
completed = true
|
|
68
|
+
if (--responses === 0) finish(error)
|
|
69
|
+
}
|
|
58
70
|
if (res.destroyed) return complete()
|
|
59
71
|
res.once('close', complete)
|
|
60
|
-
res.status(error ? 400 : 200).end(message, complete)
|
|
72
|
+
res.status(error ? 400 : 200).end(loginResultPage(message), complete)
|
|
61
73
|
}
|
|
62
74
|
try {
|
|
63
75
|
if (req.query.error) throw new Error('Authorization was not completed')
|
|
64
76
|
if (typeof req.query.code !== 'string' || !req.query.code) throw new Error('Missing authorization code')
|
|
65
|
-
|
|
77
|
+
redemption ??= exchangeCodeAndSave(
|
|
66
78
|
getFinalizedHost(params.sentioHost),
|
|
67
79
|
req.query.code,
|
|
68
80
|
params.codeVerifier,
|
|
69
81
|
params.authConfig,
|
|
70
82
|
controller.signal
|
|
71
|
-
)
|
|
72
|
-
|
|
83
|
+
).then((username) => {
|
|
84
|
+
console.log(chalk.green(`Login success with ${username}`))
|
|
85
|
+
return username
|
|
86
|
+
})
|
|
87
|
+
await redemption
|
|
73
88
|
respond('Login success, please go back to CLI to continue')
|
|
74
89
|
} catch (error) {
|
|
75
90
|
respond('Login failed. Check the terminal and try again.', error as Error)
|