@platforma-sdk/pl-cli 0.13.6 → 0.14.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/README.md +208 -0
- package/dist/address.d.ts +6 -0
- package/dist/base_command.d.ts +10 -3
- package/dist/cli.js +649 -230
- package/dist/cli.js.map +1 -1
- package/dist/cmd/login.d.ts +21 -0
- package/dist/cmd/logout.d.ts +7 -0
- package/dist/cmd/session/list.d.ts +6 -0
- package/dist/cmd/session/remove.d.ts +7 -0
- package/dist/cmd/session/select.d.ts +9 -0
- package/dist/cmd-opts.d.ts +8 -3
- package/dist/connection.d.ts +59 -3
- package/dist/index.js +1 -1
- package/dist/instance.d.ts +7 -0
- package/dist/jwt.d.ts +12 -0
- package/dist/lib.d.ts +2 -1
- package/dist/project_ops-CHhJu5k5.js +524 -0
- package/dist/project_ops-CHhJu5k5.js.map +1 -0
- package/dist/prompt.d.ts +5 -0
- package/dist/session_store.d.ts +112 -0
- package/dist/sso.d.ts +30 -0
- package/package.json +3 -3
- package/src/address.ts +15 -0
- package/src/base_command.ts +24 -6
- package/src/cli.ts +74 -3
- package/src/cmd/admin/delete-user.ts +7 -0
- package/src/cmd/login.ts +262 -0
- package/src/cmd/logout.ts +55 -0
- package/src/cmd/session/list.ts +71 -0
- package/src/cmd/session/remove.ts +29 -0
- package/src/cmd/session/select.ts +36 -0
- package/src/cmd-opts.ts +24 -15
- package/src/commands.test.ts +637 -0
- package/src/connection.ts +275 -15
- package/src/instance.ts +19 -0
- package/src/jwt.ts +36 -0
- package/src/lib.ts +12 -0
- package/src/prompt.ts +54 -0
- package/src/session_store.test.ts +237 -0
- package/src/session_store.ts +315 -0
- package/src/sso.test.ts +269 -0
- package/src/sso.ts +336 -0
- package/src/test_backend.ts +151 -0
- package/dist/output-D41Wn0wM.js +0 -229
- package/dist/output-D41Wn0wM.js.map +0 -1
package/src/cmd/login.ts
ADDED
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { Command, Option } from "commander";
|
|
2
|
+
import {
|
|
3
|
+
UnauthenticatedPlClient,
|
|
4
|
+
plAddressToConfig,
|
|
5
|
+
type AuthInformation,
|
|
6
|
+
type LoginMethod,
|
|
7
|
+
} from "@milaboratories/pl-client";
|
|
8
|
+
import { addOptions, AddressOptions } from "../cmd-opts";
|
|
9
|
+
import { redactAddress } from "../address";
|
|
10
|
+
import {
|
|
11
|
+
OldBackendMessage,
|
|
12
|
+
MinSessionBackendVersion,
|
|
13
|
+
probeInstance,
|
|
14
|
+
requireInstanceId,
|
|
15
|
+
} from "../connection";
|
|
16
|
+
import { parsePlToken } from "../jwt";
|
|
17
|
+
import { formatTable, outputJson, outputText } from "../output";
|
|
18
|
+
import { promptLine } from "../prompt";
|
|
19
|
+
import { SessionStore, type SessionLoginKind } from "../session_store";
|
|
20
|
+
import { openInBrowser, runOidcLogin, sanitizeForTerminal } from "../sso";
|
|
21
|
+
|
|
22
|
+
export default function loginCommand(): Command {
|
|
23
|
+
const cmd = new Command("login").description(
|
|
24
|
+
"Sign in to a Backend and store the session. Later commands to the same Backend instance use " +
|
|
25
|
+
"the stored session. Select the login method with --method <id>; see the IDs with " +
|
|
26
|
+
`--list-methods. Needs Backend v${MinSessionBackendVersion} or newer.`,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
addOptions(cmd, AddressOptions({ required: true }));
|
|
30
|
+
cmd.addOption(
|
|
31
|
+
new Option(
|
|
32
|
+
"--method <id>",
|
|
33
|
+
"ID of the login method (SSO provider, password or token method). Required to sign in",
|
|
34
|
+
),
|
|
35
|
+
);
|
|
36
|
+
cmd.addOption(
|
|
37
|
+
new Option("--list-methods", "Show the login methods of the Backend and exit; do not sign in"),
|
|
38
|
+
);
|
|
39
|
+
cmd.addOption(
|
|
40
|
+
new Option("-f, --format <format>", "Output format of --list-methods")
|
|
41
|
+
.choices(["text", "json"])
|
|
42
|
+
.default("text"),
|
|
43
|
+
);
|
|
44
|
+
cmd.addOption(
|
|
45
|
+
new Option("-u, --user <user>", "Login for a password method. Asked for when not given").env(
|
|
46
|
+
"PL_USER",
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
cmd.addOption(
|
|
50
|
+
new Option(
|
|
51
|
+
"-p, --password <password>",
|
|
52
|
+
"Password, or the token for a token method. Asked for when not given",
|
|
53
|
+
).env("PL_PASSWORD"),
|
|
54
|
+
);
|
|
55
|
+
cmd.addOption(
|
|
56
|
+
new Option("--no-browser", "Do not open a browser for SSO; only print the sign-in URL"),
|
|
57
|
+
);
|
|
58
|
+
cmd.addOption(
|
|
59
|
+
new Option("--timeout <seconds>", "How long to wait for the SSO sign-in to complete")
|
|
60
|
+
.default(300)
|
|
61
|
+
.argParser((v) => {
|
|
62
|
+
const n = Number(v);
|
|
63
|
+
if (!Number.isFinite(n) || n <= 0) throw new Error("--timeout must be a positive number");
|
|
64
|
+
return n;
|
|
65
|
+
}),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
cmd.action(async (flags) => {
|
|
69
|
+
if (flags.listMethods) await listMethods(flags);
|
|
70
|
+
else await login(flags);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
return cmd;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface LoginFlags {
|
|
77
|
+
address: string;
|
|
78
|
+
user?: string;
|
|
79
|
+
password?: string;
|
|
80
|
+
method?: string;
|
|
81
|
+
browser?: boolean;
|
|
82
|
+
timeout?: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Formats login methods as a table: ID, KIND, TITLE, ISSUER (SSO only). */
|
|
86
|
+
export function formatLoginMethods(methods: LoginMethod[]): string {
|
|
87
|
+
if (methods.length === 0) return "The Backend advertises no login methods.";
|
|
88
|
+
return formatTable(
|
|
89
|
+
["ID", "KIND", "TITLE", "ISSUER"],
|
|
90
|
+
methods.map((m) => [
|
|
91
|
+
sanitizeForTerminal(m.id),
|
|
92
|
+
m.kind,
|
|
93
|
+
sanitizeForTerminal(m.title),
|
|
94
|
+
m.kind === "sso" ? sanitizeForTerminal(m.issuer) : "",
|
|
95
|
+
]),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Prints the login methods of the Backend at `flags.address`. Does not sign in. */
|
|
100
|
+
export async function listMethods(flags: {
|
|
101
|
+
address: string;
|
|
102
|
+
format?: "text" | "json";
|
|
103
|
+
}): Promise<void> {
|
|
104
|
+
// Only the method list is needed: no instance ID, so this works with any Backend.
|
|
105
|
+
const unauth = await UnauthenticatedPlClient.build(plAddressToConfig(flags.address));
|
|
106
|
+
try {
|
|
107
|
+
const methods = unauth.loginMethods();
|
|
108
|
+
if (flags.format === "json")
|
|
109
|
+
outputJson(
|
|
110
|
+
methods.map((m) =>
|
|
111
|
+
m.kind === "sso"
|
|
112
|
+
? { id: m.id, kind: m.kind, title: m.title, issuer: m.issuer }
|
|
113
|
+
: { id: m.id, kind: m.kind, title: m.title },
|
|
114
|
+
),
|
|
115
|
+
);
|
|
116
|
+
else outputText(formatLoginMethods(methods));
|
|
117
|
+
} finally {
|
|
118
|
+
await unauth.ll.close();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Signs in to the Backend at `flags.address` and stores the session. */
|
|
123
|
+
export async function login(
|
|
124
|
+
flags: LoginFlags,
|
|
125
|
+
store: SessionStore = new SessionStore(),
|
|
126
|
+
): Promise<void> {
|
|
127
|
+
const config = plAddressToConfig(flags.address);
|
|
128
|
+
// Never store or print the credentials of an address URL.
|
|
129
|
+
const address = redactAddress(flags.address);
|
|
130
|
+
const { unauth, instanceId: probedId } = await probeInstance(config);
|
|
131
|
+
try {
|
|
132
|
+
if (!(await unauth.requireAuth())) {
|
|
133
|
+
outputText(`The Backend at ${address} needs no authentication. No session was stored.`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
// Backends older than v3.6.0 have no typed login methods (auth:v2) and issue tokens without
|
|
137
|
+
// the instance ID. Stop before any prompt or login call.
|
|
138
|
+
if (!unauth.hasCapability("auth:v2")) throw new Error(OldBackendMessage);
|
|
139
|
+
const instanceId = requireInstanceId(probedId);
|
|
140
|
+
|
|
141
|
+
const method = pickMethod(unauth, flags, address);
|
|
142
|
+
const kind: SessionLoginKind = method.kind;
|
|
143
|
+
|
|
144
|
+
// Credentials in the address URL count as --user/--password, as for other commands.
|
|
145
|
+
const credentials = {
|
|
146
|
+
user: flags.user ?? config.user,
|
|
147
|
+
password: flags.password ?? config.password,
|
|
148
|
+
};
|
|
149
|
+
const authInformation =
|
|
150
|
+
method.kind === "sso"
|
|
151
|
+
? await ssoLogin(unauth, method, flags)
|
|
152
|
+
: await passwordLogin(unauth, method, credentials);
|
|
153
|
+
|
|
154
|
+
const token = authInformation.jwtToken;
|
|
155
|
+
if (token === undefined) throw new Error("The Backend returned no token.");
|
|
156
|
+
const claims = parsePlToken(token);
|
|
157
|
+
// Sessions are keyed by the instance ID from the ping answer. A token of another instance
|
|
158
|
+
// would be stored under the wrong key, so store nothing.
|
|
159
|
+
if (claims.issuer === undefined) throw new Error(OldBackendMessage);
|
|
160
|
+
if (claims.issuer !== instanceId)
|
|
161
|
+
throw new Error(
|
|
162
|
+
`The token is for instance ${claims.issuer}, but the ping answer names ` +
|
|
163
|
+
`instance ${instanceId}. No session was stored. Make sure that one Backend answers at ` +
|
|
164
|
+
`${address} and try again.`,
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const now = new Date().toISOString();
|
|
168
|
+
store.put(instanceId, {
|
|
169
|
+
login: claims.login,
|
|
170
|
+
address,
|
|
171
|
+
token,
|
|
172
|
+
loginMethod: { id: method.id, kind },
|
|
173
|
+
createdAt: now,
|
|
174
|
+
updatedAt: now,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const expiry =
|
|
178
|
+
claims.expiresAt === undefined ? "" : ` until ${new Date(claims.expiresAt).toISOString()}`;
|
|
179
|
+
outputText(
|
|
180
|
+
`Signed in to ${address} (instance ${instanceId}) as "${claims.login}". ` +
|
|
181
|
+
`The session is stored${expiry} in ${store.path}.`,
|
|
182
|
+
);
|
|
183
|
+
} finally {
|
|
184
|
+
await unauth.ll.close();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Selects the login method named by `--method`. The method is never chosen implicitly, so
|
|
189
|
+
* the user always knows which identity provider gets the sign-in. */
|
|
190
|
+
function pickMethod(
|
|
191
|
+
unauth: UnauthenticatedPlClient,
|
|
192
|
+
flags: LoginFlags,
|
|
193
|
+
address: string,
|
|
194
|
+
): LoginMethod {
|
|
195
|
+
const methods = unauth.loginMethods();
|
|
196
|
+
if (methods.length === 0)
|
|
197
|
+
throw new Error(
|
|
198
|
+
"The Backend advertises no login method that pl-cli can use. Use --user/--password per command.",
|
|
199
|
+
);
|
|
200
|
+
if (flags.method === undefined)
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Select a login method with --method <id>. Login methods of the Backend:\n\n` +
|
|
203
|
+
`${formatLoginMethods(methods)}\n\n` +
|
|
204
|
+
`Run: pl-cli login -a ${address} --method <id>`,
|
|
205
|
+
);
|
|
206
|
+
const found = methods.find((m) => m.id === flags.method);
|
|
207
|
+
if (found === undefined)
|
|
208
|
+
throw new Error(
|
|
209
|
+
`The Backend has no login method "${flags.method}". Login methods of the Backend:\n\n` +
|
|
210
|
+
formatLoginMethods(methods),
|
|
211
|
+
);
|
|
212
|
+
return found;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
async function passwordLogin(
|
|
216
|
+
unauth: UnauthenticatedPlClient,
|
|
217
|
+
method: LoginMethod,
|
|
218
|
+
credentials: { user?: string; password?: string },
|
|
219
|
+
): Promise<AuthInformation> {
|
|
220
|
+
// A token method takes only the token; the login comes from the token that the Backend returns.
|
|
221
|
+
const isToken = method.kind === "token";
|
|
222
|
+
const user = isToken
|
|
223
|
+
? (credentials.user ?? "")
|
|
224
|
+
: (credentials.user ?? (await promptLine("Login: ")));
|
|
225
|
+
const password =
|
|
226
|
+
credentials.password ??
|
|
227
|
+
(await promptLine(isToken ? "Access token: " : "Password: ", { hidden: true }));
|
|
228
|
+
return await unauth.login(user, password, method.id);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
async function ssoLogin(
|
|
232
|
+
unauth: UnauthenticatedPlClient,
|
|
233
|
+
method: Extract<LoginMethod, { kind: "sso" }>,
|
|
234
|
+
flags: LoginFlags,
|
|
235
|
+
): Promise<AuthInformation> {
|
|
236
|
+
const signal = AbortSignal.timeout((flags.timeout ?? 300) * 1000);
|
|
237
|
+
const attempt = await unauth.beginSSOLogin(method.id);
|
|
238
|
+
const tokenResponse = await runOidcLogin({
|
|
239
|
+
method,
|
|
240
|
+
nonce: attempt.nonce,
|
|
241
|
+
clientSecret: attempt.clientSecret,
|
|
242
|
+
signal,
|
|
243
|
+
openUrl: async (url) => {
|
|
244
|
+
const redirect = new URL(url).searchParams.get("redirect_uri") ?? "";
|
|
245
|
+
process.stderr.write(
|
|
246
|
+
`Sign in with ${sanitizeForTerminal(method.title)} in the browser. If no browser opens, open this URL:\n\n` +
|
|
247
|
+
` ${url}\n\n` +
|
|
248
|
+
`The browser must reach ${redirect} on this computer ` +
|
|
249
|
+
`(when you work over SSH, forward that port).\n` +
|
|
250
|
+
`Waiting for the sign-in (up to ${flags.timeout ?? 300} s)...\n`,
|
|
251
|
+
);
|
|
252
|
+
if (flags.browser !== false && !(await openInBrowser(url)))
|
|
253
|
+
process.stderr.write("Could not open a browser; open the URL above.\n");
|
|
254
|
+
},
|
|
255
|
+
}).catch((err: unknown) => {
|
|
256
|
+
if (signal.aborted)
|
|
257
|
+
throw new Error(`SSO sign-in did not complete in ${flags.timeout ?? 300} s.`, { cause: err });
|
|
258
|
+
throw err;
|
|
259
|
+
});
|
|
260
|
+
// The identity provider tokens go to the Backend only. pl-cli keeps the Platforma token.
|
|
261
|
+
return await unauth.loginSSO({ tokenResponse, idP: method.id });
|
|
262
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Command, Option } from "commander";
|
|
2
|
+
import { redactAddress } from "../address";
|
|
3
|
+
import { addOptions, AddressOptions } from "../cmd-opts";
|
|
4
|
+
import { instanceIdOf } from "../instance";
|
|
5
|
+
import { outputText } from "../output";
|
|
6
|
+
import { SessionStore } from "../session_store";
|
|
7
|
+
|
|
8
|
+
export default function logoutCommand(): Command {
|
|
9
|
+
const cmd = new Command("logout").description(
|
|
10
|
+
"Remove a stored session. With --address: the current session of that Backend (found by its " +
|
|
11
|
+
"instance ID), or the session of --login there. Without --address: the default session, " +
|
|
12
|
+
"or with --login, the session of that login on the default session's instance. The " +
|
|
13
|
+
"Backend keeps the session until its token expires.",
|
|
14
|
+
);
|
|
15
|
+
addOptions(cmd, AddressOptions());
|
|
16
|
+
cmd.addOption(
|
|
17
|
+
new Option(
|
|
18
|
+
"--login <login>",
|
|
19
|
+
"Remove the session of this login (without --address: on the default session's instance)",
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
cmd.action(async (flags: { address?: string; login?: string }) => {
|
|
24
|
+
await logout(flags);
|
|
25
|
+
});
|
|
26
|
+
return cmd;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function logout(
|
|
30
|
+
flags: { address?: string; login?: string },
|
|
31
|
+
store: SessionStore = new SessionStore(),
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
let instanceId: string;
|
|
34
|
+
let where: string;
|
|
35
|
+
if (flags.address !== undefined) {
|
|
36
|
+
instanceId = await instanceIdOf(flags.address);
|
|
37
|
+
where = `the Backend at ${redactAddress(flags.address)} (instance ${instanceId})`;
|
|
38
|
+
} else {
|
|
39
|
+
// No Backend call: the default session names its instance.
|
|
40
|
+
const def = store.getDefault();
|
|
41
|
+
if (def === undefined)
|
|
42
|
+
throw new Error("No default session. Give --address (or PL_ADDRESS) of the Backend.");
|
|
43
|
+
instanceId = def.instanceId;
|
|
44
|
+
where = `instance ${instanceId}`;
|
|
45
|
+
}
|
|
46
|
+
const login =
|
|
47
|
+
flags.login ?? (flags.address === undefined ? store.getDefault()?.session.login : undefined);
|
|
48
|
+
const removed = store.remove(instanceId, login);
|
|
49
|
+
if (removed === undefined) {
|
|
50
|
+
const which = login === undefined ? "No current session" : `No session of "${login}"`;
|
|
51
|
+
outputText(`${which} is stored for ${where}.`);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
outputText(`Removed the session of "${removed.login}" on instance ${instanceId}.`);
|
|
55
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Command, Option } from "commander";
|
|
2
|
+
import { redactAddress } from "../../address";
|
|
3
|
+
import { parsePlToken } from "../../jwt";
|
|
4
|
+
import { formatDate, formatTable, outputJson, outputText } from "../../output";
|
|
5
|
+
import { SessionStore } from "../../session_store";
|
|
6
|
+
|
|
7
|
+
export default function sessionListCommand(): Command {
|
|
8
|
+
const cmd = new Command("list").description(
|
|
9
|
+
"List stored sessions: instance ID, login, address, login method and expiry. Never prints " +
|
|
10
|
+
"tokens. DEFAULT: the session that commands without --address use. CURRENT: the session " +
|
|
11
|
+
"that commands use when --address selects its instance.",
|
|
12
|
+
);
|
|
13
|
+
cmd.addOption(
|
|
14
|
+
new Option("-f, --format <format>", "Output format").choices(["text", "json"]).default("text"),
|
|
15
|
+
);
|
|
16
|
+
cmd.action((flags: { format: "text" | "json" }) => {
|
|
17
|
+
sessionList(flags);
|
|
18
|
+
});
|
|
19
|
+
return cmd;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function sessionList(
|
|
23
|
+
flags: { format: "text" | "json" },
|
|
24
|
+
store: SessionStore = new SessionStore(),
|
|
25
|
+
): void {
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const rows = store.list().map(({ instanceId, current, default: isDefault, session }) => {
|
|
28
|
+
let expiresAt: number | undefined;
|
|
29
|
+
try {
|
|
30
|
+
expiresAt = parsePlToken(session.token).expiresAt;
|
|
31
|
+
} catch {
|
|
32
|
+
expiresAt = undefined;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
instanceId,
|
|
36
|
+
default: isDefault,
|
|
37
|
+
current,
|
|
38
|
+
login: session.login,
|
|
39
|
+
// Files written by earlier versions can hold an address with credentials.
|
|
40
|
+
address: redactAddress(session.address),
|
|
41
|
+
loginMethod: session.loginMethod,
|
|
42
|
+
expiresAt: expiresAt === undefined ? undefined : new Date(expiresAt).toISOString(),
|
|
43
|
+
expired: expiresAt !== undefined && expiresAt <= now,
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (flags.format === "json") {
|
|
48
|
+
outputJson(rows);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (rows.length === 0) {
|
|
52
|
+
outputText(`No stored sessions in ${store.path}.`);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
outputText(
|
|
56
|
+
formatTable(
|
|
57
|
+
["DEFAULT", "CURRENT", "INSTANCE", "LOGIN", "ADDRESS", "METHOD", "EXPIRES"],
|
|
58
|
+
rows.map((r) => [
|
|
59
|
+
r.default ? "*" : "",
|
|
60
|
+
r.current ? "*" : "",
|
|
61
|
+
r.instanceId,
|
|
62
|
+
r.login,
|
|
63
|
+
r.address,
|
|
64
|
+
r.loginMethod === undefined ? "" : `${r.loginMethod.id} (${r.loginMethod.kind})`,
|
|
65
|
+
r.expiresAt === undefined
|
|
66
|
+
? "(unknown)"
|
|
67
|
+
: formatDate(new Date(r.expiresAt)) + (r.expired ? " (expired)" : ""),
|
|
68
|
+
]),
|
|
69
|
+
),
|
|
70
|
+
);
|
|
71
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Command } from "commander";
|
|
2
|
+
import { resolveInstance } from "../../instance";
|
|
3
|
+
import { outputText } from "../../output";
|
|
4
|
+
import { SessionStore } from "../../session_store";
|
|
5
|
+
import { InstanceOptions } from "./select";
|
|
6
|
+
|
|
7
|
+
export default function sessionRemoveCommand(): Command {
|
|
8
|
+
const cmd = new Command("remove").description(
|
|
9
|
+
"Remove the stored session of <login>. With --instance, works when the Backend is not reachable.",
|
|
10
|
+
);
|
|
11
|
+
cmd.argument("<login>", "Login of a stored session");
|
|
12
|
+
for (const opt of InstanceOptions()) cmd.addOption(opt);
|
|
13
|
+
cmd.action(async (login: string, flags: { instance?: string; address?: string }) => {
|
|
14
|
+
await sessionRemove(login, flags);
|
|
15
|
+
});
|
|
16
|
+
return cmd;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export async function sessionRemove(
|
|
20
|
+
login: string,
|
|
21
|
+
flags: { instance?: string; address?: string },
|
|
22
|
+
store: SessionStore = new SessionStore(),
|
|
23
|
+
): Promise<void> {
|
|
24
|
+
const instanceId = await resolveInstance(flags);
|
|
25
|
+
const removed = store.remove(instanceId, login);
|
|
26
|
+
if (removed === undefined)
|
|
27
|
+
throw new Error(`No stored session for login "${login}" on instance ${instanceId}.`);
|
|
28
|
+
outputText(`Removed the session of "${login}" on instance ${instanceId}.`);
|
|
29
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Command, Option } from "commander";
|
|
2
|
+
import { resolveInstance } from "../../instance";
|
|
3
|
+
import { outputText } from "../../output";
|
|
4
|
+
import { SessionStore } from "../../session_store";
|
|
5
|
+
|
|
6
|
+
/** Options that name the instance: an explicit ID, or the address of a running Backend. */
|
|
7
|
+
export const InstanceOptions = (): Option[] => [
|
|
8
|
+
new Option("--instance <id>", "Instance ID of the Backend (see 'pl-cli session list')"),
|
|
9
|
+
new Option(
|
|
10
|
+
"-a, --address <url>",
|
|
11
|
+
"Address of the Backend; its ping answer gives the instance ID",
|
|
12
|
+
).env("PL_ADDRESS"),
|
|
13
|
+
];
|
|
14
|
+
|
|
15
|
+
export default function sessionSelectCommand(): Command {
|
|
16
|
+
const cmd = new Command("select").description(
|
|
17
|
+
"Make the stored session of <login> the current session of its Backend instance. " +
|
|
18
|
+
"Commands without credentials use the current session.",
|
|
19
|
+
);
|
|
20
|
+
cmd.argument("<login>", "Login of a stored session");
|
|
21
|
+
for (const opt of InstanceOptions()) cmd.addOption(opt);
|
|
22
|
+
cmd.action(async (login: string, flags: { instance?: string; address?: string }) => {
|
|
23
|
+
await sessionSelect(login, flags);
|
|
24
|
+
});
|
|
25
|
+
return cmd;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function sessionSelect(
|
|
29
|
+
login: string,
|
|
30
|
+
flags: { instance?: string; address?: string },
|
|
31
|
+
store: SessionStore = new SessionStore(),
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const instanceId = await resolveInstance(flags);
|
|
34
|
+
store.select(instanceId, login);
|
|
35
|
+
outputText(`The session of "${login}" is now current on instance ${instanceId}.`);
|
|
36
|
+
}
|
package/src/cmd-opts.ts
CHANGED
|
@@ -9,12 +9,17 @@ export function addOptions(cmd: Command, ...optionGroups: Option[][]): Command {
|
|
|
9
9
|
return cmd;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
/** Server address
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
.
|
|
17
|
-
|
|
12
|
+
/** Server address. Optional by default: without it, commands use the address of the default
|
|
13
|
+
* stored session (see `pl-cli session select`). */
|
|
14
|
+
export const AddressOptions = (opts: { required?: boolean } = {}): Option[] => {
|
|
15
|
+
const option = opts.required
|
|
16
|
+
? new Option("-a, --address <url>", "Platforma server address").env("PL_ADDRESS")
|
|
17
|
+
: new Option(
|
|
18
|
+
"-a, --address <url>",
|
|
19
|
+
"Platforma server address. Default: the address of the default stored session",
|
|
20
|
+
).env("PL_ADDRESS");
|
|
21
|
+
return [opts.required ? option.makeOptionMandatory() : option];
|
|
22
|
+
};
|
|
18
23
|
|
|
19
24
|
export const GlobalOptions = (
|
|
20
25
|
formatDefault: OutputFormat = "text",
|
|
@@ -27,18 +32,22 @@ export const GlobalOptions = (
|
|
|
27
32
|
];
|
|
28
33
|
|
|
29
34
|
export const UserAuthOptions = (): Option[] => [
|
|
30
|
-
new Option(
|
|
35
|
+
new Option(
|
|
36
|
+
"-u, --user <user>",
|
|
37
|
+
"Username for authentication. Without --password, selects the stored session of this login",
|
|
38
|
+
).env("PL_USER"),
|
|
31
39
|
new Option("-p, --password <password>", "Password for authentication").env("PL_PASSWORD"),
|
|
32
40
|
];
|
|
33
41
|
|
|
34
|
-
/** Admin credentials only (for purely admin commands like copy-project).
|
|
42
|
+
/** Admin credentials only (for purely admin commands like copy-project).
|
|
43
|
+
* Without them, the command uses the stored session (see `pl-cli login`); the Backend checks
|
|
44
|
+
* that its user has the admin role. */
|
|
35
45
|
export const AdminAuthOptions = (): Option[] => [
|
|
36
|
-
new Option(
|
|
37
|
-
|
|
38
|
-
.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.makeOptionMandatory(),
|
|
46
|
+
new Option(
|
|
47
|
+
"--admin-user <user>",
|
|
48
|
+
"Admin/controller username. Without --admin-password, selects the stored session of this login",
|
|
49
|
+
).env("PL_ADMIN_USER"),
|
|
50
|
+
new Option("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD"),
|
|
42
51
|
];
|
|
43
52
|
|
|
44
53
|
/** Admin credentials + target user (for regular commands that can optionally operate on another user). */
|
|
@@ -49,6 +58,6 @@ export const AdminTargetOptions = (): Option[] => [
|
|
|
49
58
|
new Option("--admin-password <password>", "Admin/controller password").env("PL_ADMIN_PASSWORD"),
|
|
50
59
|
new Option(
|
|
51
60
|
"--target-user <user>",
|
|
52
|
-
"Operate on this user's data (requires admin credentials)",
|
|
61
|
+
"Operate on this user's data (requires admin credentials or an admin's stored session)",
|
|
53
62
|
).env("PL_TARGET_USER"),
|
|
54
63
|
];
|