@basementuniverse/jsonpad-cli 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,43 @@ and this package adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  Dates are npm publish dates.
9
9
 
10
+ ## [1.4.0] - 2026-09-18
11
+
12
+ Needs `@basementuniverse/jsonpad-sdk` 2.1.0, and the JSONPad API release with
13
+ identity email addresses, password reset and provider sign-in.
14
+
15
+ ### Added
16
+
17
+ - `--email` on `jsonpad identities create`, `update`, `register` and
18
+ `self update`, with `--no-email` to remove an address. Identities show their
19
+ email address, whether it's verified, and whether they have a password.
20
+ - `jsonpad identities login --email`, to log in with an email address instead
21
+ of a name, and `jsonpad identities logout --all`, to end an identity's
22
+ sessions everywhere.
23
+ - `jsonpad identities password-reset request|confirm` and
24
+ `jsonpad identities email-verification request|confirm`, for issuing
25
+ single-use tokens and using them.
26
+ - `jsonpad identities providers`, listing the sign-in providers enabled for an
27
+ identity group, and `jsonpad identities self providers` with `list` and
28
+ `unlink`, for the accounts an identity can sign in with.
29
+ - The identity event types added by the API: sessions revoked, password reset,
30
+ email verification and linked accounts.
31
+
32
+ ### Changed
33
+
34
+ - `jsonpad identities self update` takes `--current-password`, which the API
35
+ now needs before it changes an identity's password or email address (unless
36
+ the identity has no password). It's read from
37
+ `JSONPAD_IDENTITY_CURRENT_PASSWORD`, or asked for in a terminal.
38
+
39
+ ## [1.3.0] - 2026-09-16
40
+
41
+ ### Removed
42
+
43
+ - The `xmlhttprequest-ssl` dependency. `jsonpad listen` needed it only because
44
+ `@basementuniverse/jsonpad-realtime-sdk` couldn't connect from Node before
45
+ version 1.6.0.
46
+
10
47
  ## [1.2.0] - 2026-09-16
11
48
 
12
49
  ### Added
package/README.md CHANGED
@@ -6,6 +6,9 @@ Use it to manage lists, indexes, items and identities from the command line,
6
6
  and to sync schema documents (lists and their indexes) with your account, e.g.
7
7
  in a deploy script or CI.
8
8
 
9
+ There's a guide to it in the JSONPad docs:
10
+ [Command line tool](https://jsonpad.io/docs/command-line-tool).
11
+
9
12
  ## Install
10
13
 
11
14
  The tool needs Node.js 22.12 or later.
@@ -95,7 +98,7 @@ jsonpad items data patch recipes pancakes --patch @changes.json
95
98
  jsonpad items data delete recipes pancakes /ingredients/0
96
99
 
97
100
  # Identities, by id or group/name. Passwords are asked for, or read from stdin
98
- jsonpad identities create --group staff --name ada
101
+ jsonpad identities create --group staff --name ada --email ada@example.com
99
102
  jsonpad identities get staff/ada
100
103
  jsonpad identities update staff/ada --display-name Ada --password
101
104
  ```
@@ -179,7 +182,54 @@ jsonpad identities logout
179
182
  `login -o env` sets `JSONPAD_IDENTITY_TOKEN` (and `JSONPAD_IDENTITY_GROUP`).
180
183
  While it's set, commands that work with items send the identity along with
181
184
  the API token. Passwords are asked for in a terminal, or read from stdin or
182
- `JSONPAD_IDENTITY_PASSWORD`.
185
+ `JSONPAD_IDENTITY_PASSWORD`. An identity can log in with its email address
186
+ instead of its name (`--email`), and `logout --all` ends its sessions
187
+ everywhere.
188
+
189
+ Changing an identity's own password or email address needs its current
190
+ password, which is asked for with `--current-password`, or read from
191
+ `JSONPAD_IDENTITY_CURRENT_PASSWORD`:
192
+
193
+ ```bash
194
+ jsonpad identities self update --email zed@example.com --current-password
195
+ jsonpad identities self providers # accounts it can sign in with
196
+ jsonpad identities self providers unlink google
197
+ ```
198
+
199
+ ### Password reset and email verification
200
+
201
+ JSONPad never sends email. It issues a single-use token, and your app sends it
202
+ to whoever owns the identity, with your own branding. Both halves of that flow
203
+ are here, for a server or a support script:
204
+
205
+ ```bash
206
+ # Issue a reset token: by id, group/name, or --email
207
+ jsonpad identities password-reset request players/zed
208
+ jsonpad identities password-reset request --group players --email zed@example.com
209
+
210
+ # ...send it to them, then set the new password with it
211
+ jsonpad identities password-reset confirm <token>
212
+
213
+ # The same for verifying an email address
214
+ jsonpad identities email-verification request players/zed
215
+ jsonpad identities email-verification confirm <token>
216
+ ```
217
+
218
+ Requesting a token needs the API token's `reset-password` or `verify-email`
219
+ permission. If the identity group sends tokens to a webhook, the request
220
+ answers `delivery: webhook` and the token goes there instead.
221
+
222
+ ### Signing in with Google, GitHub and others
223
+
224
+ Providers are set up per identity group in the dashboard, and the sign-in
225
+ itself happens in a browser. From here you can see what's enabled, and manage
226
+ the accounts an identity can sign in with:
227
+
228
+ ```bash
229
+ jsonpad identities providers --group players
230
+ jsonpad identities self providers
231
+ jsonpad identities self providers unlink github
232
+ ```
183
233
 
184
234
  ### Realtime events
185
235
 
@@ -314,9 +364,9 @@ The schema commands (`sync-schema`, `export-schema`, `move-lists`) and
314
364
 
315
365
  ## Moving from the SDK's `jsonpad` command
316
366
 
317
- Versions 1.12 and 1.13 of `@basementuniverse/jsonpad-sdk` include a `jsonpad`
318
- command. It's deprecated, and it will be removed in SDK 2.0.0. This package has
319
- the same commands, options, output and exit codes, so to switch, replace
367
+ Versions 1.12 to 1.14 of `@basementuniverse/jsonpad-sdk` included a `jsonpad`
368
+ command, which was removed in SDK 2.0.0. This package has the same commands,
369
+ options, output and exit codes, so to switch, replace
320
370
  `npx @basementuniverse/jsonpad-sdk` with `npx @basementuniverse/jsonpad-cli`.
321
371
 
322
372
  If you installed the SDK globally to get the command, uninstall it first.
@@ -339,15 +389,10 @@ npm run reference # regenerates REFERENCE.md after changing a command
339
389
  `npm test` fails when `REFERENCE.md` is out of date.
340
390
 
341
391
  The parity tests check that the commands ported from the SDK behave exactly as
342
- the SDK's `bin/jsonpad.js` does. They run each scenario against a fake API and
343
- compare the output, exit code, requests and files against golden files in
344
- `test/parity/golden`. Those files were recorded from the SDK's command with:
345
-
346
- ```bash
347
- npm run record-parity -- ../jsonpad-sdk-js/bin/jsonpad.js
348
- ```
349
-
350
- Only re-record them to add scenarios while the SDK still has its command.
392
+ the SDK's command did. They run each scenario against a fake API and compare
393
+ the output, exit code, requests and files against golden files in
394
+ `test/parity/golden`, which were recorded from the SDK's command before it was
395
+ removed.
351
396
 
352
397
  ### Publishing
353
398
 
package/REFERENCE.md CHANGED
@@ -58,7 +58,14 @@ Run `jsonpad <command> --help` for the same information in a terminal.
58
58
  - [`jsonpad identities logout`](#jsonpad-identities-logout): Log out the identity in JSONPAD_IDENTITY_TOKEN, so that its token stops working
59
59
  - [`jsonpad identities self get`](#jsonpad-identities-self-get): Show the identity in JSONPAD_IDENTITY_TOKEN (the default when no command is given)
60
60
  - [`jsonpad identities self update`](#jsonpad-identities-self-update): Change the identity in JSONPAD_IDENTITY_TOKEN. Fields that aren't given are left as they are
61
+ - [`jsonpad identities self providers list`](#jsonpad-identities-self-providers-list): List the accounts the identity can sign in with (the default when no command is given)
62
+ - [`jsonpad identities self providers unlink`](#jsonpad-identities-self-providers-unlink): Stop the identity signing in with a provider account
61
63
  - [`jsonpad identities self delete`](#jsonpad-identities-self-delete): Delete the identity in JSONPAD_IDENTITY_TOKEN
64
+ - [`jsonpad identities password-reset request`](#jsonpad-identities-password-reset-request): Issue a password reset token for an identity
65
+ - [`jsonpad identities password-reset confirm`](#jsonpad-identities-password-reset-confirm): Set a new password with a reset token
66
+ - [`jsonpad identities email-verification request`](#jsonpad-identities-email-verification-request): Issue an email verification token for an identity
67
+ - [`jsonpad identities email-verification confirm`](#jsonpad-identities-email-verification-confirm): Verify an identity's email address with a token
68
+ - [`jsonpad identities providers`](#jsonpad-identities-providers): List an identity group's sign-in providers
62
69
  - [`jsonpad identities stats`](#jsonpad-identities-stats): Show an identity's events, by day
63
70
  - [`jsonpad identities events`](#jsonpad-identities-events): List an identity's events, newest first by default
64
71
  - [`jsonpad identities event`](#jsonpad-identities-event): Show one of an identity's events
@@ -1050,6 +1057,7 @@ jsonpad identities create [options]
1050
1057
  | `--group <group>` | The group, which separates identities with the same name |
1051
1058
  | `--name <name>` | The name, used to log in |
1052
1059
  | `--display-name <name>` | The name to show |
1060
+ | `--email <email>` | The email address, which can also be used to log in |
1053
1061
  | `--tags <tags>` | Comma-separated tags, replacing any it has (repeatable; pass "" to remove them all) |
1054
1062
  | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1055
1063
  | `--json` | Output JSON (the same as --output json) |
@@ -1072,8 +1080,10 @@ jsonpad identities update [options] <identity>
1072
1080
  | `--group <group>` | The group, which separates identities with the same name |
1073
1081
  | `--name <name>` | The name, used to log in |
1074
1082
  | `--display-name <name>` | The name to show |
1083
+ | `--email <email>` | The email address, which can also be used to log in |
1075
1084
  | `--tags <tags>` | Comma-separated tags, replacing any it has (repeatable; pass "" to remove them all) |
1076
1085
  | `--no-display-name` | Remove the display name |
1086
+ | `--no-email` | Remove the email address |
1077
1087
  | `--password` | Change the password: asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD |
1078
1088
  | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1079
1089
  | `--json` | Output JSON (the same as --output json) |
@@ -1108,6 +1118,7 @@ jsonpad identities register [options]
1108
1118
  | `--group <group>` | The group |
1109
1119
  | `--name <name>` | The name, used to log in |
1110
1120
  | `--display-name <name>` | The name to show |
1121
+ | `--email <email>` | The email address, which can also be used to log in |
1111
1122
  | `--tags <tags>` | Comma-separated tags, replacing any it has (repeatable; pass "" to remove them all) |
1112
1123
  | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1113
1124
  | `--json` | Output JSON (the same as --output json) |
@@ -1125,6 +1136,7 @@ jsonpad identities login [options]
1125
1136
  | --- | --- |
1126
1137
  | `--group <group>` | The identity's group |
1127
1138
  | `--name <name>` | The identity's name |
1139
+ | `--email <email>` | The identity's email address, instead of a name |
1128
1140
  | `-o, --output <format>` | Output format: table (the default in a terminal), json (the default otherwise), env (export commands for a POSIX shell), or token (just the token) (one of: table, json, env, token) |
1129
1141
 
1130
1142
  ### `jsonpad identities logout`
@@ -1135,6 +1147,10 @@ Log out the identity in JSONPAD_IDENTITY_TOKEN, so that its token stops working
1135
1147
  jsonpad identities logout [options]
1136
1148
  ```
1137
1149
 
1150
+ | Option | Description |
1151
+ | --- | --- |
1152
+ | `--all` | Log the identity out everywhere, ending its sessions on every device |
1153
+
1138
1154
  ### `jsonpad identities self get`
1139
1155
 
1140
1156
  Show the identity in JSONPAD_IDENTITY_TOKEN (the default when no command is given)
@@ -1162,11 +1178,44 @@ jsonpad identities self update [options]
1162
1178
  | `--name <name>` | The name, used to log in |
1163
1179
  | `--display-name <name>` | The name to show |
1164
1180
  | `--no-display-name` | Remove the display name |
1181
+ | `--email <email>` | The email address, which needs verifying again |
1182
+ | `--no-email` | Remove the email address |
1165
1183
  | `--password` | Change the password: asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD |
1184
+ | `--current-password` | The identity's current password, needed to change its password or email address. Asked for in a terminal, or read from JSONPAD_IDENTITY_CURRENT_PASSWORD |
1185
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1186
+ | `--json` | Output JSON (the same as --output json) |
1187
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1188
+
1189
+ ### `jsonpad identities self providers list`
1190
+
1191
+ List the accounts the identity can sign in with (the default when no command is given)
1192
+
1193
+ ```
1194
+ jsonpad identities self providers list [options]
1195
+ ```
1196
+
1197
+ | Option | Description |
1198
+ | --- | --- |
1166
1199
  | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1167
1200
  | `--json` | Output JSON (the same as --output json) |
1168
1201
  | `-q, --quiet` | Only output ids (the same as --output id) |
1169
1202
 
1203
+ ### `jsonpad identities self providers unlink`
1204
+
1205
+ Stop the identity in JSONPAD_IDENTITY_TOKEN signing in with a provider account. An identity's last way of signing in can't be removed: set a password first, or link another account
1206
+
1207
+ ```
1208
+ jsonpad identities self providers unlink [options] <provider>
1209
+ ```
1210
+
1211
+ | Argument | Description |
1212
+ | --- | --- |
1213
+ | `provider` | The provider, e.g. google |
1214
+
1215
+ | Option | Description |
1216
+ | --- | --- |
1217
+ | `-y, --yes` | Don't ask for confirmation |
1218
+
1170
1219
  ### `jsonpad identities self delete`
1171
1220
 
1172
1221
  Delete the identity in JSONPAD_IDENTITY_TOKEN
@@ -1179,6 +1228,97 @@ jsonpad identities self delete [options]
1179
1228
  | --- | --- |
1180
1229
  | `-y, --yes` | Don't ask for confirmation |
1181
1230
 
1231
+ ### `jsonpad identities password-reset request`
1232
+
1233
+ Issue a single-use password reset token for an identity, to send to whoever owns it. JSONPad never sends email itself. The token is returned unless the identity group delivers tokens to a webhook. This needs the token's reset-password permission
1234
+
1235
+ ```
1236
+ jsonpad identities password-reset request [options] [identity]
1237
+ ```
1238
+
1239
+ | Argument | Description |
1240
+ | --- | --- |
1241
+ | `identity` (optional) | The identity: its id, group/name, or the name of an identity without a group |
1242
+
1243
+ | Option | Description |
1244
+ | --- | --- |
1245
+ | `--group <group>` | The identity's group |
1246
+ | `--email <email>` | The identity's email address, instead of a name |
1247
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1248
+ | `--json` | Output JSON (the same as --output json) |
1249
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1250
+
1251
+ ### `jsonpad identities password-reset confirm`
1252
+
1253
+ Set a new password for the identity the reset token was issued for, which logs it out everywhere. The password is asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD
1254
+
1255
+ ```
1256
+ jsonpad identities password-reset confirm [options] <token>
1257
+ ```
1258
+
1259
+ | Argument | Description |
1260
+ | --- | --- |
1261
+ | `token` | The reset token, which can only be used once |
1262
+
1263
+ | Option | Description |
1264
+ | --- | --- |
1265
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1266
+ | `--json` | Output JSON (the same as --output json) |
1267
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1268
+
1269
+ ### `jsonpad identities email-verification request`
1270
+
1271
+ Issue a single-use email verification token for an identity, to send to the address being verified. This needs the token's verify-email permission
1272
+
1273
+ ```
1274
+ jsonpad identities email-verification request [options] [identity]
1275
+ ```
1276
+
1277
+ | Argument | Description |
1278
+ | --- | --- |
1279
+ | `identity` (optional) | The identity: its id, group/name, or the name of an identity without a group |
1280
+
1281
+ | Option | Description |
1282
+ | --- | --- |
1283
+ | `--group <group>` | The identity's group |
1284
+ | `--email <email>` | The identity's email address, instead of a name |
1285
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1286
+ | `--json` | Output JSON (the same as --output json) |
1287
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1288
+
1289
+ ### `jsonpad identities email-verification confirm`
1290
+
1291
+ Mark an identity's email address as verified, using a verification token
1292
+
1293
+ ```
1294
+ jsonpad identities email-verification confirm [options] <token>
1295
+ ```
1296
+
1297
+ | Argument | Description |
1298
+ | --- | --- |
1299
+ | `token` | The verification token, which can only be used once |
1300
+
1301
+ | Option | Description |
1302
+ | --- | --- |
1303
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1304
+ | `--json` | Output JSON (the same as --output json) |
1305
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1306
+
1307
+ ### `jsonpad identities providers`
1308
+
1309
+ List the sign-in providers enabled for an identity group, e.g. to see what an app's sign-in page would show. Set them up in the dashboard
1310
+
1311
+ ```
1312
+ jsonpad identities providers [options]
1313
+ ```
1314
+
1315
+ | Option | Description |
1316
+ | --- | --- |
1317
+ | `--group <group>` | The identity group |
1318
+ | `-o, --output <format>` | Output format (default: table in a terminal, otherwise json) (one of: table, json, ndjson, id) |
1319
+ | `--json` | Output JSON (the same as --output json) |
1320
+ | `-q, --quiet` | Only output ids (the same as --output id) |
1321
+
1182
1322
  ### `jsonpad identities stats`
1183
1323
 
1184
1324
  Show an identity's events, by day
@@ -1211,7 +1351,7 @@ jsonpad identities events [options] <identity>
1211
1351
 
1212
1352
  | Option | Description |
1213
1353
  | --- | --- |
1214
- | `--type <type>` | Only events of this type (one of: identity-created, identity-updated, identity-deleted, identity-registered, identity-logged-in, identity-logged-out, identity-updated-self, identity-deleted-self) |
1354
+ | `--type <type>` | Only events of this type (one of: identity-created, identity-updated, identity-deleted, identity-registered, identity-logged-in, identity-logged-out, identity-updated-self, identity-deleted-self, identity-sessions-revoked, identity-password-reset-requested, identity-password-reset, identity-email-verification-requested, identity-email-verified, identity-provider-linked, identity-provider-unlinked) |
1215
1355
  | `--start-at <date>` | Only events at or after this date, e.g. 2026-09-01 |
1216
1356
  | `--end-at <date>` | Only events at or before this date |
1217
1357
  | `--include-snapshot` | Include the identity as it was after each event |
@@ -1,8 +1,8 @@
1
1
  import { Option } from 'commander';
2
2
  import { apiError, apiErrorExitCode, CliError } from '../errors.js';
3
3
  import { canPrompt, confirm, promptSecret, readStdin } from '../input.js';
4
- import { addOutputOptions, formatTimestamp, printRecord, renderDetails, resolveOutputFormat, } from '../output.js';
5
- import { addAllOption, addPagingOptions, addTaggedOption, addTagsOption, nullable, removeUndefined, printPages, request, resolveIdentityId, } from '../resources.js';
4
+ import { addOutputOptions, formatTimestamp, printRecord, printRecords, renderDetails, resolveOutputFormat, } from '../output.js';
5
+ import { addAllOption, addPagingOptions, addTaggedOption, addTagsOption, nullable, removeUndefined, printPages, request, resolveIdentityId, UUID, } from '../resources.js';
6
6
  import { JSONPadError } from '../sdk.js';
7
7
  import { defineEventCommands, defineStatsCommand } from './history.js';
8
8
  const ORDER_FIELDS = [
@@ -14,6 +14,22 @@ const ORDER_FIELDS = [
14
14
  'activated',
15
15
  ];
16
16
  const IDENTITY_ARGUMENT_DESCRIPTION = 'The identity: its id, group/name, or the name of an identity without a group';
17
+ /**
18
+ * The sign-in providers identities can use, for showing a name rather than an
19
+ * id. An unknown provider (one added to the API since this release) is shown
20
+ * as it comes
21
+ */
22
+ const PROVIDER_NAMES = {
23
+ google: 'Google',
24
+ github: 'GitHub',
25
+ apple: 'Apple',
26
+ microsoft: 'Microsoft',
27
+ discord: 'Discord',
28
+ facebook: 'Facebook',
29
+ };
30
+ export function providerLabel(provider) {
31
+ return PROVIDER_NAMES[provider] ?? provider;
32
+ }
17
33
  export function identityLabel(identity) {
18
34
  return identity.group ? `${identity.group}/${identity.name}` : identity.name;
19
35
  }
@@ -26,6 +42,32 @@ function identityOutput(context) {
26
42
  ['Group', identity.group || dim('(none)')],
27
43
  ['Name', identity.name],
28
44
  ['Display name', identity.displayName ?? dim('(none)')],
45
+ ...(identity.email === undefined
46
+ ? []
47
+ : [
48
+ [
49
+ 'Email',
50
+ identity.email
51
+ ? `${identity.email}${identity.emailVerified ? '' : dim(' (not verified)')}`
52
+ : dim('(none)'),
53
+ ],
54
+ ]),
55
+ ...(identity.hasPassword === undefined
56
+ ? []
57
+ : [['Password', identity.hasPassword ? 'Set' : dim('not set')]]),
58
+ ...(identity.sessionCount === undefined
59
+ ? []
60
+ : [['Sessions', String(identity.sessionCount)]]),
61
+ ...(identity.providers?.length
62
+ ? [
63
+ [
64
+ 'Sign-in accounts',
65
+ identity.providers
66
+ .map(account => providerLabel(account.provider))
67
+ .join(', '),
68
+ ],
69
+ ]
70
+ : []),
29
71
  [
30
72
  'Tags',
31
73
  identity.tags?.length ? identity.tags.join(', ') : dim('(none)'),
@@ -74,15 +116,33 @@ async function readPassword(context, label) {
74
116
  }
75
117
  return password;
76
118
  }
119
+ /**
120
+ * Read an identity's current password, which the API needs before it changes
121
+ * a password or an email address
122
+ */
123
+ async function readCurrentPassword(context) {
124
+ const password = context.env.JSONPAD_IDENTITY_CURRENT_PASSWORD ??
125
+ (canPrompt(context)
126
+ ? await promptSecret(context, 'Current password: ')
127
+ : undefined);
128
+ if (!password) {
129
+ throw new CliError(canPrompt(context)
130
+ ? 'The current password is needed'
131
+ : 'The current password is needed. Set JSONPAD_IDENTITY_CURRENT_PASSWORD');
132
+ }
133
+ return password;
134
+ }
77
135
  function addIdentityFieldOptions(command, update) {
78
136
  command
79
137
  .option('--group <group>', 'The group, which separates identities with the same name')
80
138
  .option('--name <name>', 'The name, used to log in')
81
- .option('--display-name <name>', 'The name to show');
139
+ .option('--display-name <name>', 'The name to show')
140
+ .option('--email <email>', 'The email address, which can also be used to log in');
82
141
  addTagsOption(command);
83
142
  if (update) {
84
143
  command
85
144
  .option('--no-display-name', 'Remove the display name')
145
+ .option('--no-email', 'Remove the email address')
86
146
  .option('--password', 'Change the password: asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD');
87
147
  }
88
148
  return command;
@@ -135,6 +195,7 @@ export function defineIdentities(command, context) {
135
195
  group: options.group || undefined,
136
196
  name: options.name,
137
197
  displayName: options.displayName || undefined,
198
+ email: options.email || undefined,
138
199
  tags: options.tags,
139
200
  password,
140
201
  })));
@@ -154,6 +215,7 @@ export function defineIdentities(command, context) {
154
215
  group: options.group,
155
216
  name: options.name,
156
217
  displayName: nullable(options.displayName),
218
+ email: nullable(options.email),
157
219
  tags: options.tags,
158
220
  password,
159
221
  })));
@@ -181,6 +243,8 @@ export function defineIdentities(command, context) {
181
243
  context.error(`Deleted the identity ${context.colours.bold(reference)}`);
182
244
  });
183
245
  defineIdentityModeCommands(command, context);
246
+ defineTokenCommands(command, context);
247
+ defineProviderCommands(command, context);
184
248
  const target = {
185
249
  arguments: [['<identity>', IDENTITY_ARGUMENT_DESCRIPTION]],
186
250
  resolve: async (jsonpad, [reference]) => [
@@ -204,6 +268,13 @@ export function defineIdentities(command, context) {
204
268
  'identity-logged-out',
205
269
  'identity-updated-self',
206
270
  'identity-deleted-self',
271
+ 'identity-sessions-revoked',
272
+ 'identity-password-reset-requested',
273
+ 'identity-password-reset',
274
+ 'identity-email-verification-requested',
275
+ 'identity-email-verified',
276
+ 'identity-provider-linked',
277
+ 'identity-provider-unlinked',
207
278
  ],
208
279
  fetchEvents: (jsonpad, [id], parameters) => jsonpad.fetchIdentityEvents(id, parameters),
209
280
  fetchEvent: (jsonpad, [id], eventId, parameters) => jsonpad.fetchIdentityEvent(id, eventId, parameters),
@@ -234,7 +305,8 @@ function defineIdentityModeCommands(command, context) {
234
305
  .description("Register an identity, as an app would for a new user. Unlike create, this needs the token's register permission. The password is asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD")
235
306
  .option('--group <group>', 'The group')
236
307
  .option('--name <name>', 'The name, used to log in')
237
- .option('--display-name <name>', 'The name to show'))).action(async (options) => {
308
+ .option('--display-name <name>', 'The name to show')
309
+ .option('--email <email>', 'The email address, which can also be used to log in'))).action(async (options) => {
238
310
  const format = resolveOutputFormat(context, options);
239
311
  if (!options.name) {
240
312
  throw new CliError('An identity needs a name: pass --name');
@@ -245,6 +317,7 @@ function defineIdentityModeCommands(command, context) {
245
317
  group: options.group || undefined,
246
318
  name: options.name,
247
319
  displayName: options.displayName || undefined,
320
+ email: options.email || undefined,
248
321
  password,
249
322
  }), NO_IDENTITY));
250
323
  printRecord(context, format, identity, identityOutput(context));
@@ -255,22 +328,29 @@ function defineIdentityModeCommands(command, context) {
255
328
  .description('Log in as an identity, and output its identity token. Set JSONPAD_IDENTITY_TOKEN (and JSONPAD_IDENTITY_GROUP) to it to act as the identity when working with items, e.g. eval "$(jsonpad identities login --name ada -o env)". The password is asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD')
256
329
  .option('--group <group>', "The identity's group")
257
330
  .option('--name <name>', "The identity's name")
331
+ .option('--email <email>', "The identity's email address, instead of a name")
258
332
  .addOption(new Option('-o, --output <format>', 'Output format: table (the default in a terminal), json (the default otherwise), env (export commands for a POSIX shell), or token (just the token)').choices(['table', 'json', 'env', 'token']))
259
333
  .action(async (options) => {
260
- if (!options.name) {
261
- throw new CliError("Pass the identity's name with --name");
334
+ if (!options.name && !options.email) {
335
+ throw new CliError("Pass the identity's name with --name, or its email address with --email");
336
+ }
337
+ if (options.name && options.email) {
338
+ throw new CliError('Pass either --name or --email, not both');
262
339
  }
263
340
  const format = options.output ?? (context.stdout.isTTY ? 'table' : 'json');
341
+ const nameOrEmail = (options.name ?? options.email);
264
342
  const label = options.group
265
- ? `${options.group}/${options.name}`
266
- : options.name;
343
+ ? `${options.group}/${nameOrEmail}`
344
+ : nameOrEmail;
267
345
  const password = await readPassword(context, label);
268
346
  const jsonpad = context.createClient();
269
347
  const [identity, token] = await request(context, async () => {
270
348
  try {
271
349
  return await jsonpad.loginIdentity(removeUndefined({
272
350
  group: options.group || undefined,
273
- name: options.name,
351
+ ...(options.email
352
+ ? { email: options.email }
353
+ : { name: options.name }),
274
354
  password,
275
355
  }), NO_IDENTITY);
276
356
  }
@@ -303,18 +383,21 @@ function defineIdentityModeCommands(command, context) {
303
383
  break;
304
384
  case 'table':
305
385
  context.log(renderIdentityWithToken(context, identity, token));
306
- context.error(context.colours.dim(`To act as ${label}, set JSONPAD_IDENTITY_TOKEN${group ? ' and JSONPAD_IDENTITY_GROUP' : ''}, e.g. with: eval "$(jsonpad identities login ${group ? `--group ${shellQuote(group)} ` : ''}--name ${shellQuote(options.name)} -o env)"`));
386
+ context.error(context.colours.dim(`To act as ${label}, set JSONPAD_IDENTITY_TOKEN${group ? ' and JSONPAD_IDENTITY_GROUP' : ''}, e.g. with: eval "$(jsonpad identities login ${group ? `--group ${shellQuote(group)} ` : ''}${options.email
387
+ ? `--email ${shellQuote(options.email)}`
388
+ : `--name ${shellQuote(options.name)}`} -o env)"`));
307
389
  break;
308
390
  }
309
391
  });
310
392
  command
311
393
  .command('logout')
312
394
  .description('Log out the identity in JSONPAD_IDENTITY_TOKEN, so that its token stops working')
313
- .action(async () => {
395
+ .option('--all', 'Log the identity out everywhere, ending its sessions on every device')
396
+ .action(async (options) => {
314
397
  const jsonpad = context.createClient();
315
398
  requireIdentity(context);
316
- await request(context, () => jsonpad.logoutIdentity());
317
- context.error('Logged out. Unset JSONPAD_IDENTITY_TOKEN and JSONPAD_IDENTITY_GROUP');
399
+ await request(context, () => jsonpad.logoutIdentity(undefined, { all: options.all }));
400
+ context.error(`${options.all ? 'Logged out everywhere' : 'Logged out'}. Unset JSONPAD_IDENTITY_TOKEN and JSONPAD_IDENTITY_GROUP`);
318
401
  });
319
402
  const self = command
320
403
  .command('self')
@@ -334,20 +417,76 @@ function defineIdentityModeCommands(command, context) {
334
417
  .option('--name <name>', 'The name, used to log in')
335
418
  .option('--display-name <name>', 'The name to show')
336
419
  .option('--no-display-name', 'Remove the display name')
337
- .option('--password', 'Change the password: asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD')).action(async (options) => {
420
+ .option('--email <email>', 'The email address, which needs verifying again')
421
+ .option('--no-email', 'Remove the email address')
422
+ .option('--password', 'Change the password: asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD')
423
+ .option('--current-password', "The identity's current password, needed to change its password or email address. Asked for in a terminal, or read from JSONPAD_IDENTITY_CURRENT_PASSWORD")).action(async (options) => {
338
424
  const format = resolveOutputFormat(context, options);
339
425
  const jsonpad = context.createClient();
340
426
  requireIdentity(context);
427
+ // The API needs the current password to change a password or an email
428
+ // address, unless the identity has no password (e.g. it signed in with
429
+ // Google)
430
+ const currentPassword = options.currentPassword
431
+ ? await readCurrentPassword(context)
432
+ : undefined;
341
433
  const password = options.password
342
434
  ? await readPassword(context, 'this identity')
343
435
  : undefined;
344
436
  const identity = await request(context, () => jsonpad.updateSelfIdentity(removeUndefined({
345
437
  name: options.name,
346
438
  displayName: nullable(options.displayName),
439
+ email: nullable(options.email),
347
440
  password,
441
+ currentPassword,
348
442
  })));
349
443
  printRecord(context, format, identity, identityOutput(context));
350
444
  });
445
+ const providers = self
446
+ .command('providers')
447
+ .description('The provider accounts (e.g. Google) the identity in JSONPAD_IDENTITY_TOKEN can sign in with');
448
+ addOutputOptions(providers
449
+ .command('list', { isDefault: true })
450
+ .alias('ls')
451
+ .description('List the accounts the identity can sign in with (the default when no command is given)')).action(async (options) => {
452
+ const format = resolveOutputFormat(context, options);
453
+ const jsonpad = context.createClient();
454
+ requireIdentity(context);
455
+ const accounts = await request(context, () => jsonpad.fetchSelfIdentityProviders());
456
+ printRecords(context, format, accounts, {
457
+ id: account => account.provider,
458
+ columns: [
459
+ {
460
+ header: 'PROVIDER',
461
+ value: account => providerLabel(account.provider),
462
+ },
463
+ { header: 'EMAIL', value: account => account.email ?? '-' },
464
+ { header: 'NAME', value: account => account.name ?? '-' },
465
+ {
466
+ header: 'LAST SIGNED IN',
467
+ value: account => account.lastLoginAt ? formatTimestamp(account.lastLoginAt) : '-',
468
+ },
469
+ ],
470
+ empty: 'This identity has no provider accounts',
471
+ });
472
+ });
473
+ providers
474
+ .command('unlink')
475
+ .alias('rm')
476
+ .summary('Stop the identity signing in with a provider account')
477
+ .description("Stop the identity in JSONPAD_IDENTITY_TOKEN signing in with a provider account. An identity's last way of signing in can't be removed: set a password first, or link another account")
478
+ .argument('<provider>', 'The provider, e.g. google')
479
+ .option('-y, --yes', "Don't ask for confirmation")
480
+ .action(async (provider, options) => {
481
+ const jsonpad = context.createClient();
482
+ requireIdentity(context);
483
+ await confirm(context, {
484
+ question: `Stop signing in with ${providerLabel(provider)}?`,
485
+ yes: options.yes,
486
+ });
487
+ await request(context, () => jsonpad.unlinkSelfIdentityProvider(provider));
488
+ context.error(`Unlinked the ${context.colours.bold(providerLabel(provider))} account`);
489
+ });
351
490
  self
352
491
  .command('delete')
353
492
  .alias('rm')
@@ -366,6 +505,173 @@ function defineIdentityModeCommands(command, context) {
366
505
  context.error('Deleted the identity. Unset JSONPAD_IDENTITY_TOKEN and JSONPAD_IDENTITY_GROUP');
367
506
  });
368
507
  }
508
+ /**
509
+ * How an identity is chosen for a password reset or email verification token:
510
+ * by the same reference as elsewhere, or by email address
511
+ */
512
+ function identityTokenRequest(reference, options) {
513
+ if (reference && options.email) {
514
+ throw new CliError('Pass either an identity or --email, not both');
515
+ }
516
+ if (options.email) {
517
+ return removeUndefined({
518
+ group: options.group || undefined,
519
+ email: options.email,
520
+ });
521
+ }
522
+ if (!reference) {
523
+ throw new CliError('Pass the identity (its id, group/name, or name), or its email address with --email');
524
+ }
525
+ if (UUID.test(reference)) {
526
+ return { identityId: reference };
527
+ }
528
+ // Names can't contain a slash, but groups can
529
+ const slash = reference.lastIndexOf('/');
530
+ return removeUndefined({
531
+ group: slash === -1 ? options.group || undefined : reference.slice(0, slash),
532
+ name: reference.slice(slash + 1),
533
+ });
534
+ }
535
+ /**
536
+ * Print an issued password reset or email verification token
537
+ */
538
+ function printIssuedToken(context, format, issued, noun) {
539
+ const { dim } = context.colours;
540
+ if (issued.delivery === 'webhook') {
541
+ if (format === 'table') {
542
+ context.log(renderDetails([['Delivery', "sent to the identity group's webhook"]]));
543
+ context.error(dim(`The ${noun} was sent to the webhook, so it isn't returned here. The same response is given whether or not an identity matched.`));
544
+ }
545
+ else {
546
+ printRecord(context, format, issued, {
547
+ id: () => '',
548
+ details: () => [],
549
+ data: () => issued.response,
550
+ });
551
+ }
552
+ return;
553
+ }
554
+ if (!issued.token && format === 'table') {
555
+ context.error(`No activated, unlocked identity matched, so no ${noun} was issued`);
556
+ }
557
+ printRecord(context, format, issued, {
558
+ data: () => issued.response,
559
+ id: () => issued.identity?.id ?? '',
560
+ details: () => [
561
+ [
562
+ noun === 'reset token' ? 'Reset token' : 'Verification token',
563
+ issued.token ?? dim('(none)'),
564
+ ],
565
+ [
566
+ 'Expires',
567
+ issued.expiresAt ? formatTimestamp(issued.expiresAt) : dim('(none)'),
568
+ ],
569
+ [
570
+ 'Identity',
571
+ issued.identity
572
+ ? `${identityLabel(issued.identity)} (${issued.identity.id})`
573
+ : dim('(no match)'),
574
+ ],
575
+ ['Email', issued.identity?.email ?? dim('(none)')],
576
+ ],
577
+ });
578
+ }
579
+ /**
580
+ * Password reset and email verification tokens: JSONPad issues them, and your
581
+ * app sends them on
582
+ */
583
+ function defineTokenCommands(command, context) {
584
+ const kinds = [
585
+ {
586
+ name: 'password-reset',
587
+ summary: 'Issue and use password reset tokens',
588
+ noun: 'reset token',
589
+ requestSummary: 'Issue a password reset token for an identity',
590
+ requestDescription: "Issue a single-use password reset token for an identity, to send to whoever owns it. JSONPad never sends email itself. The token is returned unless the identity group delivers tokens to a webhook. This needs the token's reset-password permission",
591
+ confirmSummary: 'Set a new password with a reset token',
592
+ confirmDescription: 'Set a new password for the identity the reset token was issued for, which logs it out everywhere. The password is asked for in a terminal, or read from stdin or JSONPAD_IDENTITY_PASSWORD',
593
+ request: (jsonpad, data) => jsonpad.requestIdentityPasswordReset(data),
594
+ },
595
+ {
596
+ name: 'email-verification',
597
+ summary: 'Issue and use email verification tokens',
598
+ noun: 'verification token',
599
+ requestSummary: 'Issue an email verification token for an identity',
600
+ requestDescription: "Issue a single-use email verification token for an identity, to send to the address being verified. This needs the token's verify-email permission",
601
+ confirmSummary: "Verify an identity's email address with a token",
602
+ confirmDescription: "Mark an identity's email address as verified, using a verification token",
603
+ request: (jsonpad, data) => jsonpad.requestIdentityEmailVerification(data),
604
+ },
605
+ ];
606
+ for (const kind of kinds) {
607
+ const group = command.command(kind.name).description(kind.summary);
608
+ addOutputOptions(group
609
+ .command('request')
610
+ .summary(kind.requestSummary)
611
+ .description(kind.requestDescription)
612
+ .argument('[identity]', IDENTITY_ARGUMENT_DESCRIPTION)
613
+ .option('--group <group>', "The identity's group")
614
+ .option('--email <email>', "The identity's email address, instead of a name")).action(async (reference, options) => {
615
+ const format = resolveOutputFormat(context, options);
616
+ const jsonpad = context.createClient();
617
+ const issued = (await request(context, () => kind.request(jsonpad, identityTokenRequest(reference, options))));
618
+ printIssuedToken(context, format, {
619
+ token: issued.resetToken ?? issued.verificationToken ?? null,
620
+ expiresAt: issued.expiresAt ?? null,
621
+ identity: issued.identity ?? null,
622
+ delivery: issued.delivery,
623
+ response: issued,
624
+ }, kind.noun);
625
+ });
626
+ const confirm = group
627
+ .command('confirm')
628
+ .summary(kind.confirmSummary)
629
+ .description(kind.confirmDescription)
630
+ .argument('<token>', `The ${kind.noun}, which can only be used once`);
631
+ addOutputOptions(confirm).action(async (token, options) => {
632
+ const format = resolveOutputFormat(context, options);
633
+ const jsonpad = context.createClient();
634
+ const identity = await request(context, async () => {
635
+ if (kind.name === 'password-reset') {
636
+ const password = await readPassword(context, 'the identity');
637
+ return jsonpad.confirmIdentityPasswordReset({
638
+ resetToken: token,
639
+ password,
640
+ });
641
+ }
642
+ return jsonpad.confirmIdentityEmailVerification({
643
+ verificationToken: token,
644
+ });
645
+ });
646
+ printRecord(context, format, identity, identityOutput(context));
647
+ if (kind.name === 'password-reset') {
648
+ context.error(context.colours.dim('The identity has been logged out everywhere, and can log in with its new password'));
649
+ }
650
+ });
651
+ }
652
+ }
653
+ /**
654
+ * Signing in with Google, GitHub and the rest
655
+ */
656
+ function defineProviderCommands(command, context) {
657
+ addOutputOptions(command
658
+ .command('providers')
659
+ .summary("List an identity group's sign-in providers")
660
+ .description("List the sign-in providers enabled for an identity group, e.g. to see what an app's sign-in page would show. Set them up in the dashboard")
661
+ .option('--group <group>', 'The identity group')).action(async (options) => {
662
+ const format = resolveOutputFormat(context, options);
663
+ const jsonpad = context.createClient();
664
+ const providers = await request(context, () => jsonpad.fetchIdentityOAuthProviders(options.group || undefined));
665
+ printRecords(context, format, providers, {
666
+ id: provider => provider.provider,
667
+ columns: [
668
+ { header: 'PROVIDER', value: provider => provider.provider },
669
+ { header: 'NAME', value: provider => provider.name },
670
+ ],
671
+ empty: 'No sign-in providers are enabled for this identity group',
672
+ });
673
+ });
674
+ }
369
675
  function renderIdentityWithToken(context, identity, token) {
370
676
  const rows = identityOutput(context).details(identity);
371
677
  return renderDetails([...rows, ['Token', token]]);
package/build/realtime.js CHANGED
@@ -1,27 +1,7 @@
1
- import { createRequire } from 'node:module';
2
1
  import realtime from '@basementuniverse/jsonpad-realtime-sdk';
3
- // Like the SDK, the realtime SDK is a UMD build, so an ES module only sees its
2
+ // The realtime SDK's Node build is CommonJS, so an ES module only sees its
4
3
  // exports object as the default import
5
- const RealtimeClientClass = realtime.default;
6
- /**
7
- * The realtime SDK's build contains socket.io-client's browser build, which
8
- * connects with XMLHttpRequest. Node doesn't have one, so without this the
9
- * client never connects (and never says why). xmlhttprequest-ssl is what
10
- * socket.io-client's own Node build uses
11
- */
12
- function provideXMLHttpRequest() {
13
- if (!('XMLHttpRequest' in globalThis)) {
14
- const require = createRequire(import.meta.url);
15
- globalThis.XMLHttpRequest =
16
- require('xmlhttprequest-ssl').XMLHttpRequest;
17
- }
18
- }
19
- export const JSONPadRealtime = new Proxy(RealtimeClientClass, {
20
- construct(target, args) {
21
- provideXMLHttpRequest();
22
- return Reflect.construct(target, args);
23
- },
24
- });
4
+ export const JSONPadRealtime = realtime.default;
25
5
  export const REALTIME_EVENT_TYPES = [
26
6
  'list-created',
27
7
  'list-updated',
@@ -108,7 +108,7 @@ export async function request(context, send) {
108
108
  export function sdkPointer(pointer) {
109
109
  return pointer === undefined ? undefined : pointer.replace(/^\/+/, '');
110
110
  }
111
- const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
111
+ export const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
112
112
  /**
113
113
  * Find an identity's id from what was passed on the command line: its id,
114
114
  * group/name, or the name of an identity with no group
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@basementuniverse/jsonpad-cli",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Command line tool for JSONPad",
5
5
  "author": "Gordon Larrigan <gordonlarrigan@gmail.com> (https://gordonlarrigan.com)",
6
6
  "license": "MIT",
@@ -35,14 +35,12 @@
35
35
  "build": "rm -rf build && tsc -p tsconfig.build.json && chmod +x build/cli.js",
36
36
  "typecheck": "tsc --noEmit",
37
37
  "test": "npm run build && node --test \"test/**/*.test.ts\"",
38
- "reference": "node scripts/generate-reference.ts",
39
- "record-parity": "npm run build && node scripts/record-parity.ts"
38
+ "reference": "node scripts/generate-reference.ts"
40
39
  },
41
40
  "dependencies": {
42
41
  "@basementuniverse/jsonpad-realtime-sdk": "^1.6.0",
43
- "@basementuniverse/jsonpad-sdk": "^1.14.1",
44
- "commander": "^15.0.0",
45
- "xmlhttprequest-ssl": "~2.1.2"
42
+ "@basementuniverse/jsonpad-sdk": "^2.1.0",
43
+ "commander": "^15.0.0"
46
44
  },
47
45
  "devDependencies": {
48
46
  "@types/node": "^22.20.3",