@platforma-sdk/pl-cli 0.13.5 → 0.14.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.
Files changed (45) hide show
  1. package/README.md +208 -0
  2. package/dist/address.d.ts +6 -0
  3. package/dist/base_command.d.ts +10 -3
  4. package/dist/cli.js +649 -230
  5. package/dist/cli.js.map +1 -1
  6. package/dist/cmd/login.d.ts +21 -0
  7. package/dist/cmd/logout.d.ts +7 -0
  8. package/dist/cmd/session/list.d.ts +6 -0
  9. package/dist/cmd/session/remove.d.ts +7 -0
  10. package/dist/cmd/session/select.d.ts +9 -0
  11. package/dist/cmd-opts.d.ts +8 -3
  12. package/dist/connection.d.ts +59 -3
  13. package/dist/index.js +1 -1
  14. package/dist/instance.d.ts +7 -0
  15. package/dist/jwt.d.ts +12 -0
  16. package/dist/lib.d.ts +2 -1
  17. package/dist/project_ops-CHhJu5k5.js +524 -0
  18. package/dist/project_ops-CHhJu5k5.js.map +1 -0
  19. package/dist/prompt.d.ts +5 -0
  20. package/dist/session_store.d.ts +112 -0
  21. package/dist/sso.d.ts +30 -0
  22. package/package.json +3 -3
  23. package/src/address.ts +15 -0
  24. package/src/base_command.ts +24 -6
  25. package/src/cli.ts +74 -3
  26. package/src/cmd/admin/delete-user.ts +7 -0
  27. package/src/cmd/login.ts +262 -0
  28. package/src/cmd/logout.ts +55 -0
  29. package/src/cmd/session/list.ts +71 -0
  30. package/src/cmd/session/remove.ts +29 -0
  31. package/src/cmd/session/select.ts +36 -0
  32. package/src/cmd-opts.ts +24 -15
  33. package/src/commands.test.ts +637 -0
  34. package/src/connection.ts +275 -15
  35. package/src/instance.ts +19 -0
  36. package/src/jwt.ts +36 -0
  37. package/src/lib.ts +12 -0
  38. package/src/prompt.ts +54 -0
  39. package/src/session_store.test.ts +237 -0
  40. package/src/session_store.ts +315 -0
  41. package/src/sso.test.ts +269 -0
  42. package/src/sso.ts +336 -0
  43. package/src/test_backend.ts +151 -0
  44. package/dist/output-D41Wn0wM.js +0 -229
  45. package/dist/output-D41Wn0wM.js.map +0 -1
package/README.md ADDED
@@ -0,0 +1,208 @@
1
+ # @platforma-sdk/pl-cli
2
+
3
+ Command-line client for a Platforma Backend: projects, admin operations and maintenance.
4
+
5
+ ```sh
6
+ npx @platforma-sdk/pl-cli --help
7
+ pl-cli <command> --help
8
+ ```
9
+
10
+ Give the Backend address with `-a, --address` or `PL_ADDRESS`. After `pl-cli login`, commands
11
+ without an address use the address of the default session (see [Sessions](#sessions)).
12
+
13
+ ## Sign In
14
+
15
+ Sign in one time with `pl-cli login`. pl-cli stores the session. Later commands to the same Backend
16
+ use the stored session and do not ask for credentials.
17
+
18
+ `pl-cli login` and stored sessions need **Backend v3.6.0 or newer**. On an older Backend,
19
+ `pl-cli login` stops with a message before it asks for credentials, and commands without
20
+ credentials stop with the same message. Give `--user` and `--password` with each command: this
21
+ works with older Backends too. `pl-cli login --list-methods` works with all Backends.
22
+
23
+ You always select the login method (identity provider, password or token method) by its ID.
24
+ pl-cli never selects one for you. First show the methods of the Backend:
25
+
26
+ ```sh
27
+ pl-cli login -a https://platforma.example.com --list-methods
28
+ ```
29
+
30
+ ```
31
+ ID KIND TITLE ISSUER
32
+ ----------- ----- ------------- ------------------------
33
+ corp-sso sso Corporate SSO https://corp.example.com
34
+ ldap basic LDAP
35
+ ```
36
+
37
+ Then sign in with one of them:
38
+
39
+ ```sh
40
+ pl-cli login -a https://platforma.example.com --method corp-sso # SSO in the browser
41
+ pl-cli login -a localhost:6345 --method ldap -u alice # pl-cli asks for the password
42
+ pl-cli project list # uses the default session
43
+ ```
44
+
45
+ ### SSO
46
+
47
+ `pl-cli login` uses the same flow as the Desktop App:
48
+
49
+ 1. pl-cli gets the SSO settings and a one-time nonce from the Backend.
50
+ 2. pl-cli listens on `127.0.0.1`, on the first free redirect port that the Backend advertises.
51
+ 3. pl-cli opens the sign-in page of the identity provider in the browser and prints its URL.
52
+ With `--no-browser`, pl-cli only prints the URL.
53
+ 4. The identity provider sends the browser back to `http://127.0.0.1:<port>/callback` with a code.
54
+ 5. pl-cli exchanges the code for tokens (PKCE, S256). It sends the client secret only when the
55
+ Backend gives one (confidential clients, for example Google).
56
+ 6. pl-cli sends the token response to the Backend and gets a Platforma token.
57
+
58
+ The browser must reach the redirect port on the computer where pl-cli runs. When you work on a
59
+ remote host over SSH, forward that port (for example `ssh -L 8085:127.0.0.1:8085 host`), or run
60
+ `pl-cli login` on your own computer through an SSH tunnel to the Backend.
61
+
62
+ `pl-cli login` waits 300 seconds for the sign-in. Change this with `--timeout <seconds>`.
63
+
64
+ ### Login Options
65
+
66
+ | Option | Result |
67
+ | --------------------------- | ------------------------------------------------------------------------------------------- |
68
+ | `--method <id>` | Required. The login method with this ID. Without it, pl-cli shows the methods and stops |
69
+ | `--list-methods` | Shows ID, kind, title and SSO issuer of each method (`-f json` for JSON), then stops |
70
+ | `-u, --user <login>` | Login for a password method (static user list, LDAP). pl-cli asks for it if it is not given |
71
+ | `-p, --password <password>` | Password, or the token of a token method. pl-cli asks for it (hidden input) if not given |
72
+ | `--no-browser` | SSO: only print the sign-in URL |
73
+ | `--timeout <seconds>` | SSO: how long to wait for the sign-in (default 300) |
74
+
75
+ `--user` and `--password` do not select the method; they only give the credentials for the
76
+ selected password or token method.
77
+
78
+ If the Backend needs no authentication, `pl-cli login` says so and stores nothing.
79
+
80
+ Credentials in the address URL (`-a http://alice:password@host:6345`) count as `--user` and
81
+ `--password` for the selected method. pl-cli never stores or prints them: the session file, the
82
+ messages and `pl-cli session list` show the address without them.
83
+
84
+ ## Credential Precedence
85
+
86
+ For each command, pl-cli selects credentials in this order:
87
+
88
+ 1. The Backend requires no authentication: anonymous access.
89
+ 2. `--admin-user` with `--admin-password` (admin commands), or `--user` with `--password`
90
+ (`PL_ADMIN_USER`/`PL_ADMIN_PASSWORD`, `PL_USER`/`PL_PASSWORD`): pl-cli signs in with them.
91
+ It does not read or change stored sessions.
92
+ 3. `--admin-user` or `--user` without a password: the stored session of that login.
93
+ 4. No credentials: the current stored session of the Backend instance.
94
+
95
+ Without `--address`, the address comes from the default session, and rules 3 and 4 use its
96
+ instance (rule 4: the default session). Admin credentials with a password need `--address`.
97
+
98
+ If no rule applies, the command stops and tells you to run `pl-cli login` or
99
+ `pl-cli session select`.
100
+
101
+ Credentials in the address URL (`http://alice:password@host:6345`) count as `--user` and
102
+ `--password`. Environment variables count as the flags. Thus a `PL_USER` or `PL_ADMIN_USER` in
103
+ your shell selects the stored session of that login, and `PL_USER` with `PL_PASSWORD` bypasses the
104
+ stored sessions. When pl-cli takes credentials from the environment, it prints a one-line note to
105
+ stderr.
106
+
107
+ Admin commands accept the stored session of an admin user. The Backend checks the role.
108
+
109
+ ## Sessions
110
+
111
+ pl-cli keys a session by the **instance ID** that the Backend returns in its ping answer, not by the
112
+ address. Several Backends can answer on the same local address, for example through SSH tunnels.
113
+ Before each command, pl-cli pings the address, reads the instance ID and selects the session of that
114
+ instance.
115
+
116
+ One instance can have sessions of several logins. pl-cli marks two kinds of session:
117
+
118
+ - The **current** session of an instance: commands with `--address` and without credentials use
119
+ the current session of the instance at that address.
120
+ - The **default** session (one for all instances): commands **without** `--address` use it. pl-cli
121
+ pings the address stored in the default session and uses the session only if the Backend there
122
+ has the same instance ID. If not (for example, an SSH tunnel now points to another Backend),
123
+ the command stops and sends no token.
124
+
125
+ A new `pl-cli login` and `pl-cli session select` make the session both the current session of its
126
+ instance and the default session. When you remove a session, it stops being current and default;
127
+ pl-cli does not switch to another login without your command.
128
+
129
+ The default session stores the address as `scheme://host:port` only. If you need address options
130
+ (for example `?http-proxy=…`), give `--address` with each command.
131
+
132
+ | Command | Result |
133
+ | ------------------------------------------------ | ------------------------------------------------------------------------------------ |
134
+ | `pl-cli session list [-f json]` | Shows default, current, instance ID, login, address, login method, expiry. No tokens |
135
+ | `pl-cli session select <login> -a <address>` | Makes the session of `<login>` current on that Backend and the default session |
136
+ | `pl-cli session remove <login> -a <address>` | Removes the session of `<login>` |
137
+ | `pl-cli logout [-a <address>] [--login <login>]` | Removes the default session, or the current session (or `<login>`) of that Backend |
138
+
139
+ pl-cli does not send a stored token to an instance other than the one that issued it (the `iss`
140
+ claim of the token). `pl-cli login` stores nothing if the token is not for the instance of the
141
+ ping answer. If the full connection reports another instance ID than the ping, the command stops.
142
+
143
+ The instance ID is not a proof of identity: any server can return any ID. Only TLS (`https://`
144
+ addresses) authenticates the server. Use `https://` when the connection leaves your computer,
145
+ or use an SSH tunnel.
146
+
147
+ `session select` and `session remove` also accept `--instance <id>` instead of an address. Use it
148
+ when the Backend is not reachable.
149
+
150
+ `logout` removes the session from the file only. The Backend has no call to close a session, so
151
+ the token stays valid on the Backend until it expires.
152
+
153
+ ### What pl-cli Stores
154
+
155
+ pl-cli stores only the Platforma token that the Backend issues. It never stores a password or the
156
+ tokens of the identity provider. For SSO sessions, the Backend keeps the refresh token of the
157
+ identity provider and uses it when it renews the Platforma token.
158
+
159
+ ### Session File
160
+
161
+ The file is `~/.platforma/pl-cli.sessions` (JSON). Set `PL_CLI_SESSIONS_FILE` to use another path.
162
+
163
+ - On Linux and macOS, the file has mode `0600`. If pl-cli creates the directory, the directory has
164
+ mode `0700`. If the file has a wider mode, pl-cli changes it to `0600` and shows a warning.
165
+ - If the directory exists and other users can write to it, pl-cli shows a warning: they could
166
+ replace the file.
167
+ - On Windows, Node.js cannot set these modes. The file is in your user profile, where by default
168
+ only you and administrators can read it.
169
+ - Each change takes the lock file `pl-cli.sessions.lock`, so that two pl-cli processes do not lose a
170
+ change. A lock older than 30 seconds is removed as stale.
171
+
172
+ Format (version 1):
173
+
174
+ ```json
175
+ {
176
+ "version": 1,
177
+ "default": { "instanceId": "<instance ID>", "login": "alice" },
178
+ "instances": {
179
+ "<instance ID>": {
180
+ "current": "alice",
181
+ "sessions": {
182
+ "alice": {
183
+ "login": "alice",
184
+ "address": "https://platforma.example.com",
185
+ "token": "<Platforma JWT>",
186
+ "loginMethod": { "id": "sso", "kind": "sso" },
187
+ "createdAt": "2026-12-02T10:00:00.000Z",
188
+ "updatedAt": "2026-12-02T10:00:00.000Z"
189
+ }
190
+ }
191
+ }
192
+ }
193
+ }
194
+ ```
195
+
196
+ ### Expiry and Renewal
197
+
198
+ - When the token is past half of its lifetime, pl-cli renews it before the command, as the Desktop
199
+ App does, and saves the new token.
200
+ - When the token has expired, pl-cli does not send it. It tells you to run `pl-cli login`.
201
+ - When the Backend refuses the token (for example, the SSO session ended at the identity provider),
202
+ pl-cli tells you to run `pl-cli login`.
203
+
204
+ ## Library Use
205
+
206
+ `createPlConnection` (exported by the package) uses the same precedence and the same session file.
207
+ Give `sessionStore: null` to use only explicit credentials, without reading or writing the file.
208
+ Give `sessionStore: new SessionStore(path)` to use another file.
@@ -0,0 +1,6 @@
1
+ /** Returns the Backend address as `scheme://host:port` only. Drops the user name and password,
2
+ * and the query string and fragment, which can hold proxy credentials
3
+ * (`?http-proxy=http://user:password@proxy:3128`). An address without a scheme (`host:6345`)
4
+ * cannot hold credentials and is returned as it is. Use it for every address that pl-cli stores
5
+ * or prints. */
6
+ export declare function redactAddress(address: string): string;
@@ -3,10 +3,16 @@ import { PlClient, SignedResourceId } from '@milaboratories/pl-client';
3
3
  * Low-level: get an authenticated PlClient without resolving a project list.
4
4
  * Use this for commands that navigate to multiple users (e.g. admin copy-project).
5
5
  *
6
+ * Credential precedence: `--admin-user` with `--admin-password` first, then `--user` with
7
+ * `--password`. A login without a password (`--admin-user` or `--user` alone) selects the stored
8
+ * session of that login. With no credentials, the current stored session of the Backend instance
9
+ * is used. Without an address, the default stored session gives the address (not with
10
+ * `--admin-password`). See {@link createPlConnection}.
11
+ *
6
12
  * Callers own the returned client and must `await pl.close()` when done.
7
13
  */
8
14
  export declare function connectClient(flags: {
9
- address: string;
15
+ address?: string;
10
16
  user?: string;
11
17
  password?: string;
12
18
  adminUser?: string;
@@ -14,13 +20,14 @@ export declare function connectClient(flags: {
14
20
  }): Promise<PlClient>;
15
21
  /**
16
22
  * Connect and resolve the project list for a single user.
17
- * In admin mode (--admin-user + --admin-password + --target-user), operates on the target user's data.
23
+ * In admin mode (--target-user with --admin-user + --admin-password, or with the stored session of
24
+ * an admin user), operates on the target user's data.
18
25
  * In user mode, operates on the authenticated user's own data.
19
26
  *
20
27
  * Callers own the returned client and must `await pl.close()` when done.
21
28
  */
22
29
  export declare function connect(flags: {
23
- address: string;
30
+ address?: string;
24
31
  user?: string;
25
32
  password?: string;
26
33
  adminUser?: string;