@jinshuju/cli 0.4.0 → 0.5.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/README.md +16 -17
- package/dist/auth.d.ts +5 -2
- package/dist/auth.js +17 -14
- package/dist/cli.js +1 -5
- package/dist/commands/entry.js +19 -4
- package/dist/commands/index.js +1 -1
- package/dist/commands/local.js +18 -17
- package/dist/config.d.ts +16 -13
- package/dist/config.js +13 -26
- package/dist/http.js +1 -5
- package/dist/local.d.ts +1 -3
- package/dist/local.js +48 -57
- package/dist/options.d.ts +1 -1
- package/dist/options.js +3 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -29,30 +29,27 @@ its execute bit — `npm run build` restores it.
|
|
|
29
29
|
|
|
30
30
|
## Authentication
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
an
|
|
34
|
-
|
|
35
|
-
Write them to the config file (`~/.jinshuju/config.json`, mode 600):
|
|
32
|
+
Two kinds of credential are supported: an interactive browser login (OAuth),
|
|
33
|
+
and an access token (personal or account). Both go in through `auth login`:
|
|
36
34
|
|
|
37
35
|
```bash
|
|
38
|
-
jinshuju
|
|
39
|
-
jinshuju
|
|
40
|
-
jinshuju config set api_secret xxx
|
|
36
|
+
jinshuju auth login # browser login
|
|
37
|
+
jinshuju auth login --access-token xxx # access token, checked before it is stored
|
|
41
38
|
```
|
|
42
39
|
|
|
43
|
-
|
|
40
|
+
Either one is written to the config file (`~/.jinshuju/config.json`, mode 600)
|
|
41
|
+
and replaces whatever credential was stored before. `jinshuju auth logout`
|
|
42
|
+
forgets it (revoking it first, for a browser session).
|
|
43
|
+
|
|
44
|
+
For CI and scripts, an environment variable works without touching the file:
|
|
44
45
|
|
|
45
46
|
```bash
|
|
46
47
|
export JINSHUJU_ACCESS_TOKEN=xxx
|
|
47
|
-
# or
|
|
48
|
-
export JINSHUJU_API_KEY=xxx
|
|
49
|
-
export JINSHUJU_API_SECRET=xxx
|
|
50
48
|
```
|
|
51
49
|
|
|
52
|
-
The **precedence** is
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
login is a surprise nobody wants. `jinshuju auth status` reports which
|
|
50
|
+
The **precedence** is `JINSHUJU_ACCESS_TOKEN`, then the stored credential. A
|
|
51
|
+
token set for this run always beats a stored login: acting as last week's
|
|
52
|
+
login instead is a surprise nobody wants. `jinshuju auth status` reports which
|
|
56
53
|
credential is in use and where it came from:
|
|
57
54
|
|
|
58
55
|
```
|
|
@@ -60,8 +57,8 @@ $ jinshuju auth status
|
|
|
60
57
|
Authenticated with an access token (from env).
|
|
61
58
|
```
|
|
62
59
|
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
`config set` holds only `host`, `auth_host` and `client_id`; credentials are
|
|
61
|
+
not config.
|
|
65
62
|
|
|
66
63
|
## Creating forms
|
|
67
64
|
|
|
@@ -242,6 +239,8 @@ jinshuju entry search repair --scope-filter 'entries_count gt 100' # picks whi
|
|
|
242
239
|
|
|
243
240
|
jinshuju entry stats --from 2026-09-01 # how much each form received in the period
|
|
244
241
|
jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10
|
|
242
|
+
jinshuju entry stats --from 2026-09-28 --by day # the same, split into each day, in one call
|
|
243
|
+
jinshuju entry stats --from 2026-01-01 --by month # or by week / month: at most 92 days, 53 weeks, 24 months
|
|
245
244
|
```
|
|
246
245
|
|
|
247
246
|
A form that could not be searched stays in the result **with the reason**,
|
package/dist/auth.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ export type OAuthLoginOptions = LoadConfigOptions & {
|
|
|
11
11
|
authHost?: string;
|
|
12
12
|
clientId?: string;
|
|
13
13
|
scopes?: string;
|
|
14
|
-
openBrowser?: boolean;
|
|
15
14
|
timeoutMs?: number;
|
|
16
15
|
port?: number;
|
|
17
16
|
};
|
|
@@ -35,5 +34,9 @@ export declare function buildAuthorizationUrl(params: {
|
|
|
35
34
|
}): string;
|
|
36
35
|
export declare function loginWithOAuth(options?: OAuthLoginOptions, opener?: BrowserOpener): Promise<OAuthLoginResult>;
|
|
37
36
|
export declare function refreshOAuthToken(config: LoadedConfig): Promise<OAuthConfig>;
|
|
38
|
-
|
|
37
|
+
/**
|
|
38
|
+
* Revokes a stored OAuth session, then forgets whatever credential is stored.
|
|
39
|
+
* An access token has no revoke endpoint here; it is only forgotten.
|
|
40
|
+
*/
|
|
41
|
+
export declare function logout(config: LoadedConfig): Promise<void>;
|
|
39
42
|
export declare function shouldRefresh(auth: OAuthConfig, skewMs?: number): boolean;
|
package/dist/auth.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createHash, randomBytes, timingSafeEqual } from 'node:crypto';
|
|
|
2
2
|
import { createServer } from 'node:http';
|
|
3
3
|
import { execFile } from 'node:child_process';
|
|
4
4
|
import { AuthError } from './errors.js';
|
|
5
|
-
import {
|
|
5
|
+
import { clearCredential, defaultScopes, loadConfig, saveOAuthConfig } from './config.js';
|
|
6
6
|
function base64Url(bytes) {
|
|
7
7
|
return bytes.toString('base64').replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_');
|
|
8
8
|
}
|
|
@@ -47,8 +47,7 @@ export async function loginWithOAuth(options = {}, opener = openUrl) {
|
|
|
47
47
|
// state, a refusal — and a rejection nobody is awaiting yet is reported as
|
|
48
48
|
// unhandled. It is awaited right below; this only says so in the meantime.
|
|
49
49
|
callback.code.catch(() => undefined);
|
|
50
|
-
|
|
51
|
-
await opener(authorizeUrl);
|
|
50
|
+
await opener(authorizeUrl);
|
|
52
51
|
const code = await callback.code;
|
|
53
52
|
const token = await exchangeAuthorizationCode(config.authHost, clientId, callback.redirectUri, code, verifier);
|
|
54
53
|
const auth = toOAuthConfig(config, clientId, token);
|
|
@@ -67,17 +66,21 @@ export async function refreshOAuthToken(config) {
|
|
|
67
66
|
saveOAuthConfig(config.configPath, auth);
|
|
68
67
|
return auth;
|
|
69
68
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Revokes a stored OAuth session, then forgets whatever credential is stored.
|
|
71
|
+
* An access token has no revoke endpoint here; it is only forgotten.
|
|
72
|
+
*/
|
|
73
|
+
export async function logout(config) {
|
|
74
|
+
if (config.auth?.access_token) {
|
|
75
|
+
const url = new URL('/oauth/revoke', config.auth.auth_host);
|
|
76
|
+
await fetch(url, {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json' },
|
|
79
|
+
body: new URLSearchParams({ client_id: config.auth.client_id, token: config.auth.access_token }),
|
|
80
|
+
signal: AbortSignal.timeout(config.timeoutMs ?? TOKEN_TIMEOUT_MS)
|
|
81
|
+
}).catch(() => undefined);
|
|
82
|
+
}
|
|
83
|
+
clearCredential(config.configPath);
|
|
81
84
|
}
|
|
82
85
|
export function shouldRefresh(auth, skewMs = 60_000) {
|
|
83
86
|
if (!auth.expires_at)
|
package/dist/cli.js
CHANGED
|
@@ -46,11 +46,7 @@ async function runRemote(command, words, flags, runtime, stdin) {
|
|
|
46
46
|
new JinshujuHttpClient(loadConfig({
|
|
47
47
|
configPath: options.config ?? defaultConfigPath,
|
|
48
48
|
env: runtime.env,
|
|
49
|
-
cli: {
|
|
50
|
-
apiKey: options.api_key,
|
|
51
|
-
apiSecret: options.api_secret,
|
|
52
|
-
host: options.host
|
|
53
|
-
}
|
|
49
|
+
cli: { host: options.host }
|
|
54
50
|
}));
|
|
55
51
|
// A command that needs more than one round trip, or has to decide how many,
|
|
56
52
|
// runs itself and answers with what should be printed.
|
package/dist/commands/entry.js
CHANGED
|
@@ -412,10 +412,13 @@ export const ENTRY = [
|
|
|
412
412
|
{
|
|
413
413
|
path: ['entry', 'stats'],
|
|
414
414
|
summary: 'Count submissions per form over a date range',
|
|
415
|
+
// Split, each bucket's forms are the answer; a table of bucket totals drops them.
|
|
416
|
+
text: { essentialLists: ['data'] },
|
|
415
417
|
description: 'Not the question `entry count` answers. These are submissions as they happened: an import ' +
|
|
416
418
|
'lands on the day it ran whatever dates its rows carry, and deletions are never subtracted, ' +
|
|
417
419
|
'so this is how much arrived rather than how much is still there. A whole day is the ' +
|
|
418
|
-
'smallest window; both ends are inclusive and days are cut in the reported time zone.'
|
|
420
|
+
'smallest window; both ends are inclusive and days are cut in the reported time zone. For a ' +
|
|
421
|
+
'day-by-day, weekly or monthly answer pass --by once rather than one call per period.',
|
|
419
422
|
options: [
|
|
420
423
|
{ name: '--from', type: 'string', placeholder: '<YYYY-MM-DD>', description: 'First day to count, inclusive' },
|
|
421
424
|
{
|
|
@@ -435,7 +438,16 @@ export const ENTRY = [
|
|
|
435
438
|
name: '--limit',
|
|
436
439
|
type: 'integer',
|
|
437
440
|
placeholder: '<n>',
|
|
438
|
-
description: 'How many forms to list, most submissions first (default 100, max 100)'
|
|
441
|
+
description: 'How many forms to list, most submissions first (default 100, or 10 per bucket with --by; max 100)'
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
name: '--by',
|
|
445
|
+
type: 'string',
|
|
446
|
+
choices: ['day', 'week', 'month'],
|
|
447
|
+
placeholder: '<unit>',
|
|
448
|
+
description: 'Also split the range into buckets: every one, empty ones with total 0, --limit per bucket. ' +
|
|
449
|
+
'Weeks start on Monday; the first and last are clipped to the range. ' +
|
|
450
|
+
'At most 92 days, 53 weeks or 24 months'
|
|
439
451
|
}
|
|
440
452
|
],
|
|
441
453
|
request: (input) => ({
|
|
@@ -445,12 +457,15 @@ export const ENTRY = [
|
|
|
445
457
|
from: requiredOption(input, 'from'),
|
|
446
458
|
to: input.options.to,
|
|
447
459
|
kind: input.options.kind,
|
|
448
|
-
limit: input.options.limit === undefined ? undefined : String(input.options.limit)
|
|
460
|
+
limit: input.options.limit === undefined ? undefined : String(input.options.limit),
|
|
461
|
+
by: input.options.by
|
|
449
462
|
}
|
|
450
463
|
}),
|
|
451
464
|
examples: [
|
|
452
465
|
'jinshuju entry stats --from 2026-09-01',
|
|
453
|
-
'jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10'
|
|
466
|
+
'jinshuju entry stats --from 2026-09-01 --to 2026-09-07 --kind form --limit 10',
|
|
467
|
+
'jinshuju entry stats --from 2026-09-28 --by day',
|
|
468
|
+
'jinshuju entry stats --from 2026-01-01 --by month'
|
|
454
469
|
]
|
|
455
470
|
},
|
|
456
471
|
{
|
package/dist/commands/index.js
CHANGED
|
@@ -24,7 +24,7 @@ export const RESOURCES = [
|
|
|
24
24
|
{
|
|
25
25
|
name: 'auth',
|
|
26
26
|
summary: 'Manage authentication',
|
|
27
|
-
note: 'login is the browser flow. To use a token instead: `jinshuju
|
|
27
|
+
note: 'login is the browser flow. To use a token instead: `jinshuju auth login --access-token <token>`, ' +
|
|
28
28
|
'or set JINSHUJU_ACCESS_TOKEN. `auth status --verify` says which account the credential belongs to.'
|
|
29
29
|
},
|
|
30
30
|
{ name: 'account', summary: 'Account, members, invoices and what the account paid' },
|
package/dist/commands/local.js
CHANGED
|
@@ -20,20 +20,21 @@ function local(...names) {
|
|
|
20
20
|
export const LOCAL = [
|
|
21
21
|
{
|
|
22
22
|
path: ['auth', 'login'],
|
|
23
|
-
summary: 'Log in through the browser and store the
|
|
24
|
-
description: '
|
|
25
|
-
'session to the config file.
|
|
26
|
-
'
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
summary: 'Log in through the browser, or with an access token, and store the credential',
|
|
24
|
+
description: 'Without --access-token: opens the authorization page, waits for the redirect on a loopback ' +
|
|
25
|
+
'port, and writes the session to the config file. With it: checks the token with one call, ' +
|
|
26
|
+
'then stores it; the OAuth options are ignored. Either replaces whatever credential was stored ' +
|
|
27
|
+
'before. JINSHUJU_ACCESS_TOKEN, if set, still outranks what this stores.',
|
|
28
|
+
options: local('--access-token', '--auth-host', '--client-id', '--scopes', '--no-open', '--host'),
|
|
29
|
+
examples: ['jinshuju auth login', 'jinshuju auth login --no-open', 'jinshuju auth login --access-token <token>']
|
|
29
30
|
},
|
|
30
31
|
{
|
|
31
32
|
path: ['auth', 'status'],
|
|
32
33
|
summary: 'Show which credential is in use, and where it came from',
|
|
33
|
-
description: 'The precedence is
|
|
34
|
+
description: 'The precedence is JINSHUJU_ACCESS_TOKEN, then the credential `auth login` stored. ' +
|
|
34
35
|
'--verify spends one call to confirm the credential still works, and reports the account it ' +
|
|
35
36
|
'belongs to — with more than one configured, nothing else here says which is in play.',
|
|
36
|
-
options: local('--verify', '--
|
|
37
|
+
options: local('--verify', '--host', '--auth-host', '--client-id'),
|
|
37
38
|
examples: ['jinshuju auth status', 'jinshuju auth status --verify']
|
|
38
39
|
},
|
|
39
40
|
{
|
|
@@ -44,33 +45,33 @@ export const LOCAL = [
|
|
|
44
45
|
},
|
|
45
46
|
{
|
|
46
47
|
path: ['auth', 'logout'],
|
|
47
|
-
summary: '
|
|
48
|
-
description:
|
|
48
|
+
summary: 'Forget the stored credential, revoking it first if it is a browser session',
|
|
49
|
+
description: 'An access token cannot be revoked from here, only forgotten. JINSHUJU_ACCESS_TOKEN is not ' +
|
|
50
|
+
"this command's to drop.",
|
|
49
51
|
options: local('--auth-host', '--client-id')
|
|
50
52
|
},
|
|
51
53
|
{
|
|
52
54
|
path: ['config', 'get'],
|
|
53
55
|
summary: 'Read one configuration value',
|
|
54
|
-
description: `Keys: ${CONFIG_KEYS.join(', ')}.
|
|
56
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. Credentials are not config: \`auth login\` stores them.`,
|
|
55
57
|
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
56
|
-
|
|
57
|
-
examples: ['jinshuju config get api_key', 'jinshuju config get access_token --show-secret']
|
|
58
|
+
examples: ['jinshuju config get host']
|
|
58
59
|
},
|
|
59
60
|
{
|
|
60
61
|
path: ['config', 'set'],
|
|
61
62
|
summary: 'Write one configuration value',
|
|
62
|
-
description: `Keys: ${CONFIG_KEYS.join(', ')}. The file is written with mode 600.
|
|
63
|
-
'
|
|
63
|
+
description: `Keys: ${CONFIG_KEYS.join(', ')}. The file is written with mode 600. JINSHUJU_HOST, ` +
|
|
64
|
+
'JINSHUJU_AUTH_HOST and JINSHUJU_OAUTH_CLIENT_ID outrank whatever is stored here.',
|
|
64
65
|
args: [
|
|
65
66
|
{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` },
|
|
66
67
|
{ name: 'value', required: true, description: 'The value to store' }
|
|
67
68
|
],
|
|
68
|
-
examples: ['jinshuju config set
|
|
69
|
+
examples: ['jinshuju config set host https://jinshuju.net']
|
|
69
70
|
},
|
|
70
71
|
{
|
|
71
72
|
path: ['config', 'unset'],
|
|
72
73
|
summary: 'Remove one configuration value',
|
|
73
74
|
args: [{ name: 'key', required: true, description: `One of ${CONFIG_KEYS.join(', ')}` }],
|
|
74
|
-
examples: ['jinshuju config unset
|
|
75
|
+
examples: ['jinshuju config unset host']
|
|
75
76
|
}
|
|
76
77
|
];
|
package/dist/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type ConfigKey = '
|
|
1
|
+
export type ConfigKey = 'host' | 'auth_host' | 'client_id';
|
|
2
2
|
export type ConfigSource = 'cli' | 'env' | 'file' | 'missing';
|
|
3
3
|
export type OAuthConfig = {
|
|
4
4
|
type: 'oauth';
|
|
@@ -9,14 +9,17 @@ export type OAuthConfig = {
|
|
|
9
9
|
expires_at?: string;
|
|
10
10
|
scope?: string;
|
|
11
11
|
};
|
|
12
|
+
/** An access token stored by `auth login --access-token`. It takes the same slot as an OAuth session. */
|
|
13
|
+
export type TokenConfig = {
|
|
14
|
+
type: 'access_token';
|
|
15
|
+
access_token: string;
|
|
16
|
+
};
|
|
12
17
|
export type LoadedConfig = {
|
|
13
18
|
/**
|
|
14
|
-
* A personal or account access token, sent as a bearer
|
|
15
|
-
*
|
|
19
|
+
* A personal or account access token, sent as a bearer: JINSHUJU_ACCESS_TOKEN,
|
|
20
|
+
* or the one `auth login --access-token` stored.
|
|
16
21
|
*/
|
|
17
22
|
accessToken?: string;
|
|
18
|
-
apiKey?: string;
|
|
19
|
-
apiSecret?: string;
|
|
20
23
|
host: string;
|
|
21
24
|
authHost: string;
|
|
22
25
|
clientId?: string;
|
|
@@ -26,8 +29,6 @@ export type LoadedConfig = {
|
|
|
26
29
|
configPath: string;
|
|
27
30
|
sources: {
|
|
28
31
|
accessToken: ConfigSource;
|
|
29
|
-
apiKey: ConfigSource;
|
|
30
|
-
apiSecret: ConfigSource;
|
|
31
32
|
host: ConfigSource;
|
|
32
33
|
authHost: ConfigSource;
|
|
33
34
|
clientId: ConfigSource;
|
|
@@ -38,9 +39,6 @@ export type LoadConfigOptions = {
|
|
|
38
39
|
configPath?: string;
|
|
39
40
|
env?: NodeJS.ProcessEnv | Record<string, string | undefined>;
|
|
40
41
|
cli?: {
|
|
41
|
-
accessToken?: string;
|
|
42
|
-
apiKey?: string;
|
|
43
|
-
apiSecret?: string;
|
|
44
42
|
host?: string;
|
|
45
43
|
authHost?: string;
|
|
46
44
|
clientId?: string;
|
|
@@ -51,16 +49,21 @@ export declare const defaultHost = "https://jinshuju.net";
|
|
|
51
49
|
export declare const defaultAuthHost = "https://account.jinshuju.net";
|
|
52
50
|
export declare const defaultOAuthClientId = "jinshuju_cli_public";
|
|
53
51
|
export declare const defaultScopes = "public forms read_entries write_entries form_setting read_contacts users";
|
|
52
|
+
/**
|
|
53
|
+
* One credential at a time: a login of either kind replaces whatever the last
|
|
54
|
+
* one stored, so the file never holds two that disagree about who is calling.
|
|
55
|
+
*/
|
|
54
56
|
export type RawConfig = Partial<Record<ConfigKey, string>> & {
|
|
55
|
-
auth?: OAuthConfig;
|
|
57
|
+
auth?: OAuthConfig | TokenConfig;
|
|
56
58
|
};
|
|
57
59
|
export declare function loadConfig(options?: LoadConfigOptions): LoadedConfig;
|
|
58
60
|
export declare function setConfigValue(configPath: string, key: ConfigKey, value: string): void;
|
|
59
61
|
export declare function unsetConfigValue(configPath: string, key: ConfigKey): void;
|
|
60
62
|
export declare function saveOAuthConfig(configPath: string, auth: OAuthConfig): void;
|
|
61
|
-
export declare function
|
|
63
|
+
export declare function saveAccessToken(configPath: string, accessToken: string): void;
|
|
64
|
+
/** Forgets the stored credential, whichever kind it is. */
|
|
65
|
+
export declare function clearCredential(configPath: string): void;
|
|
62
66
|
export declare function getConfig(configPath: string): RawConfig;
|
|
63
|
-
export declare function maskSecret(value: string | undefined): string | undefined;
|
|
64
67
|
/** Every key the config file holds, in the order help should list them. */
|
|
65
68
|
export declare const CONFIG_KEYS: readonly ConfigKey[];
|
|
66
69
|
export declare function assertConfigKey(value: string): asserts value is ConfigKey;
|
package/dist/config.js
CHANGED
|
@@ -53,19 +53,16 @@ export function loadConfig(options = {}) {
|
|
|
53
53
|
const configPath = options.configPath ?? defaultConfigPath;
|
|
54
54
|
const env = options.env ?? process.env;
|
|
55
55
|
const file = readConfigFile(configPath);
|
|
56
|
-
const
|
|
57
|
-
const
|
|
58
|
-
const
|
|
56
|
+
const auth = file.auth?.type === 'oauth' ? file.auth : undefined;
|
|
57
|
+
const storedToken = file.auth?.type === 'access_token' ? file.auth.access_token : undefined;
|
|
58
|
+
const accessToken = pickValue(undefined, env.JINSHUJU_ACCESS_TOKEN, storedToken);
|
|
59
59
|
const host = pickValue(options.cli?.host, env.JINSHUJU_HOST, file.host, defaultHost);
|
|
60
|
-
const authHost = pickValue(options.cli?.authHost, env.JINSHUJU_AUTH_HOST, file.auth_host ??
|
|
60
|
+
const authHost = pickValue(options.cli?.authHost, env.JINSHUJU_AUTH_HOST, file.auth_host ?? auth?.auth_host, defaultAuthHost);
|
|
61
61
|
const defaultClientId = authHost.value === defaultAuthHost ? defaultOAuthClientId : undefined;
|
|
62
|
-
const clientId = pickValue(options.cli?.clientId, env.JINSHUJU_OAUTH_CLIENT_ID, file.client_id ??
|
|
63
|
-
const auth = file.auth?.type === 'oauth' ? file.auth : undefined;
|
|
62
|
+
const clientId = pickValue(options.cli?.clientId, env.JINSHUJU_OAUTH_CLIENT_ID, file.client_id ?? auth?.client_id, defaultClientId);
|
|
64
63
|
const timeoutMs = parseTimeout(env.JINSHUJU_TIMEOUT_MS);
|
|
65
64
|
return {
|
|
66
65
|
accessToken: accessToken.value,
|
|
67
|
-
apiKey: apiKey.value,
|
|
68
|
-
apiSecret: apiSecret.value,
|
|
69
66
|
host: host.value,
|
|
70
67
|
authHost: authHost.value,
|
|
71
68
|
clientId: clientId.value,
|
|
@@ -74,8 +71,6 @@ export function loadConfig(options = {}) {
|
|
|
74
71
|
configPath,
|
|
75
72
|
sources: {
|
|
76
73
|
accessToken: accessToken.source,
|
|
77
|
-
apiKey: apiKey.source,
|
|
78
|
-
apiSecret: apiSecret.source,
|
|
79
74
|
host: host.source,
|
|
80
75
|
authHost: authHost.source,
|
|
81
76
|
clientId: clientId.source,
|
|
@@ -108,7 +103,13 @@ export function saveOAuthConfig(configPath, auth) {
|
|
|
108
103
|
config.client_id = auth.client_id;
|
|
109
104
|
writeConfigFile(configPath, config);
|
|
110
105
|
}
|
|
111
|
-
export function
|
|
106
|
+
export function saveAccessToken(configPath, accessToken) {
|
|
107
|
+
const config = readConfigFile(configPath);
|
|
108
|
+
config.auth = { type: 'access_token', access_token: accessToken };
|
|
109
|
+
writeConfigFile(configPath, config);
|
|
110
|
+
}
|
|
111
|
+
/** Forgets the stored credential, whichever kind it is. */
|
|
112
|
+
export function clearCredential(configPath) {
|
|
112
113
|
const config = readConfigFile(configPath);
|
|
113
114
|
delete config.auth;
|
|
114
115
|
writeConfigFile(configPath, config);
|
|
@@ -116,22 +117,8 @@ export function clearOAuthConfig(configPath) {
|
|
|
116
117
|
export function getConfig(configPath) {
|
|
117
118
|
return readConfigFile(configPath);
|
|
118
119
|
}
|
|
119
|
-
export function maskSecret(value) {
|
|
120
|
-
if (!value)
|
|
121
|
-
return undefined;
|
|
122
|
-
if (value.length <= 8)
|
|
123
|
-
return '••••';
|
|
124
|
-
return `${value.slice(0, 4)}…${value.slice(-4)}`;
|
|
125
|
-
}
|
|
126
120
|
/** Every key the config file holds, in the order help should list them. */
|
|
127
|
-
export const CONFIG_KEYS = [
|
|
128
|
-
'access_token',
|
|
129
|
-
'api_key',
|
|
130
|
-
'api_secret',
|
|
131
|
-
'host',
|
|
132
|
-
'auth_host',
|
|
133
|
-
'client_id'
|
|
134
|
-
];
|
|
121
|
+
export const CONFIG_KEYS = ['host', 'auth_host', 'client_id'];
|
|
135
122
|
export function assertConfigKey(value) {
|
|
136
123
|
if (!CONFIG_KEYS.includes(value)) {
|
|
137
124
|
throw new UsageError(`Config key must be one of ${CONFIG_KEYS.join(', ')}`);
|
package/dist/http.js
CHANGED
|
@@ -217,16 +217,12 @@ export class JinshujuHttpClient {
|
|
|
217
217
|
async authorizationHeader() {
|
|
218
218
|
if (this.config.accessToken)
|
|
219
219
|
return `Bearer ${this.config.accessToken}`;
|
|
220
|
-
if (this.config.apiKey && this.config.apiSecret) {
|
|
221
|
-
const credentials = Buffer.from(`${this.config.apiKey}:${this.config.apiSecret}`).toString('base64');
|
|
222
|
-
return `Basic ${credentials}`;
|
|
223
|
-
}
|
|
224
220
|
if (this.config.auth?.access_token) {
|
|
225
221
|
if (shouldRefresh(this.config.auth) && this.config.auth.refresh_token) {
|
|
226
222
|
this.config.auth = await refreshOAuthToken(this.config);
|
|
227
223
|
}
|
|
228
224
|
return `Bearer ${this.config.auth.access_token}`;
|
|
229
225
|
}
|
|
230
|
-
throw new AuthError('Missing authentication. Run `jinshuju auth login
|
|
226
|
+
throw new AuthError('Missing authentication. Run `jinshuju auth login` or `jinshuju auth login --access-token <token>`, or set JINSHUJU_ACCESS_TOKEN.');
|
|
231
227
|
}
|
|
232
228
|
}
|
package/dist/local.d.ts
CHANGED
|
@@ -9,15 +9,13 @@ import { type CliResult, type CliRuntime } from './result.js';
|
|
|
9
9
|
type LocalOptions = {
|
|
10
10
|
output: OutputFormat;
|
|
11
11
|
configPath: string;
|
|
12
|
-
|
|
13
|
-
apiSecret?: string;
|
|
12
|
+
accessToken?: string;
|
|
14
13
|
host?: string;
|
|
15
14
|
authHost?: string;
|
|
16
15
|
clientId?: string;
|
|
17
16
|
scopes?: string;
|
|
18
17
|
noOpen: boolean;
|
|
19
18
|
verify: boolean;
|
|
20
|
-
showSecret: boolean;
|
|
21
19
|
};
|
|
22
20
|
export declare function localOptions(flags: Record<string, unknown>, stdin: () => string): LocalOptions;
|
|
23
21
|
export declare function runLocal(words: readonly string[], flags: Record<string, unknown>, runtime: CliRuntime, stdin: () => string): Promise<CliResult>;
|
package/dist/local.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { loginWithOAuth,
|
|
1
|
+
import { loginWithOAuth, logout, refreshOAuthToken } from './auth.js';
|
|
2
2
|
import { bindOptions } from './args.js';
|
|
3
|
-
import { assertConfigKey, defaultConfigPath, getConfig, loadConfig,
|
|
3
|
+
import { assertConfigKey, CONFIG_KEYS, defaultConfigPath, getConfig, loadConfig, saveAccessToken, setConfigValue, unsetConfigValue } from './config.js';
|
|
4
|
+
import { AuthError } from './errors.js';
|
|
4
5
|
import { JinshujuHttpClient } from './http.js';
|
|
5
6
|
import { GLOBAL_OPTIONS, LOCAL_OPTIONS, UsageError } from './options.js';
|
|
6
7
|
import { format } from './render.js';
|
|
@@ -11,15 +12,13 @@ export function localOptions(flags, stdin) {
|
|
|
11
12
|
return {
|
|
12
13
|
output: bound.output ?? 'text',
|
|
13
14
|
configPath: bound.config ?? defaultConfigPath,
|
|
14
|
-
|
|
15
|
-
apiSecret: bound.api_secret,
|
|
15
|
+
accessToken: bound.access_token,
|
|
16
16
|
host: bound.host,
|
|
17
17
|
authHost: bound.auth_host,
|
|
18
18
|
clientId: bound.client_id,
|
|
19
19
|
scopes: bound.scopes,
|
|
20
20
|
noOpen: Boolean(bound.no_open),
|
|
21
|
-
verify: Boolean(bound.verify)
|
|
22
|
-
showSecret: Boolean(bound.show_secret)
|
|
21
|
+
verify: Boolean(bound.verify)
|
|
23
22
|
};
|
|
24
23
|
}
|
|
25
24
|
export async function runLocal(words, flags, runtime, stdin) {
|
|
@@ -49,24 +48,24 @@ function requireArg(value, name) {
|
|
|
49
48
|
throw new UsageError(`Missing argument <${name}>`);
|
|
50
49
|
return value;
|
|
51
50
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
})));
|
|
51
|
+
/** A client that sends `accessToken` when one is given, and the configured credential otherwise. */
|
|
52
|
+
function createClient(options, runtime, accessToken) {
|
|
53
|
+
if (runtime.client)
|
|
54
|
+
return runtime.client;
|
|
55
|
+
const config = loadConfig({ configPath: options.configPath, env: runtime.env, cli: { host: options.host } });
|
|
56
|
+
return new JinshujuHttpClient(accessToken ? { ...config, accessToken } : config);
|
|
59
57
|
}
|
|
60
58
|
async function authLogin(options, runtime) {
|
|
59
|
+
if (options.accessToken !== undefined)
|
|
60
|
+
return await tokenLogin(options.accessToken, options, runtime);
|
|
61
61
|
const result = await loginWithOAuth({
|
|
62
62
|
configPath: options.configPath,
|
|
63
63
|
env: runtime.env,
|
|
64
64
|
host: options.host,
|
|
65
65
|
authHost: options.authHost,
|
|
66
66
|
clientId: options.clientId,
|
|
67
|
-
scopes: options.scopes
|
|
68
|
-
|
|
69
|
-
});
|
|
67
|
+
scopes: options.scopes
|
|
68
|
+
}, options.noOpen ? printLoginUrl : undefined);
|
|
70
69
|
const payload = {
|
|
71
70
|
authenticated: true,
|
|
72
71
|
mode: 'oauth',
|
|
@@ -79,39 +78,39 @@ async function authLogin(options, runtime) {
|
|
|
79
78
|
return ok(format(payload, options.output, 0));
|
|
80
79
|
return ok(`Authenticated with OAuth.\nConfig: ${options.configPath}`);
|
|
81
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* A token is checked before it is stored: one pasted wrong would otherwise sit
|
|
83
|
+
* in the config until the next command failed with it, far from where it was
|
|
84
|
+
* typed. The same call says whose token it is.
|
|
85
|
+
*/
|
|
86
|
+
/** --no-open: the URL goes to stderr, so stdout still carries only the result. */
|
|
87
|
+
async function printLoginUrl(url) {
|
|
88
|
+
process.stderr.write(`Open this URL in a browser to log in:\n${url}\n`);
|
|
89
|
+
}
|
|
90
|
+
async function tokenLogin(accessToken, options, runtime) {
|
|
91
|
+
if (!accessToken)
|
|
92
|
+
throw new UsageError('--access-token needs a token');
|
|
93
|
+
const account = await identify(createClient(options, runtime, accessToken));
|
|
94
|
+
saveAccessToken(options.configPath, accessToken);
|
|
95
|
+
if (options.output !== 'text')
|
|
96
|
+
return ok(format({ authenticated: true, mode: 'access_token', account }, options.output, 0));
|
|
97
|
+
return ok(`Authenticated with an access token.${describeAccount(account)}\nConfig: ${options.configPath}`);
|
|
98
|
+
}
|
|
82
99
|
async function authStatus(options, runtime) {
|
|
83
100
|
const config = loadConfig({
|
|
84
101
|
configPath: options.configPath,
|
|
85
102
|
env: runtime.env,
|
|
86
|
-
cli: {
|
|
87
|
-
apiKey: options.apiKey,
|
|
88
|
-
apiSecret: options.apiSecret,
|
|
89
|
-
host: options.host,
|
|
90
|
-
authHost: options.authHost,
|
|
91
|
-
clientId: options.clientId
|
|
92
|
-
}
|
|
103
|
+
cli: { host: options.host, authHost: options.authHost, clientId: options.clientId }
|
|
93
104
|
});
|
|
94
|
-
const authenticated = Boolean(config.accessToken ||
|
|
95
|
-
const mode = config.accessToken
|
|
96
|
-
? 'access_token'
|
|
97
|
-
: config.apiKey && config.apiSecret
|
|
98
|
-
? 'api_key_secret'
|
|
99
|
-
: config.auth?.access_token
|
|
100
|
-
? 'oauth'
|
|
101
|
-
: 'none';
|
|
105
|
+
const authenticated = Boolean(config.accessToken || config.auth?.access_token);
|
|
106
|
+
const mode = config.accessToken ? 'access_token' : config.auth?.access_token ? 'oauth' : 'none';
|
|
102
107
|
const payload = {
|
|
103
108
|
authenticated,
|
|
104
109
|
mode,
|
|
105
110
|
host: config.host,
|
|
106
111
|
auth_host: config.authHost,
|
|
107
112
|
sources: config.sources,
|
|
108
|
-
source: mode === 'access_token'
|
|
109
|
-
? config.sources.accessToken
|
|
110
|
-
: mode === 'api_key_secret'
|
|
111
|
-
? config.sources.apiKey
|
|
112
|
-
: mode === 'oauth'
|
|
113
|
-
? config.sources.auth
|
|
114
|
-
: 'missing',
|
|
113
|
+
source: mode === 'access_token' ? config.sources.accessToken : mode === 'oauth' ? config.sources.auth : 'missing',
|
|
115
114
|
oauth: config.auth
|
|
116
115
|
? {
|
|
117
116
|
client_id: config.auth.client_id,
|
|
@@ -129,14 +128,16 @@ async function authStatus(options, runtime) {
|
|
|
129
128
|
const full = { ...payload, ...(account ? { account } : {}) };
|
|
130
129
|
if (options.output !== 'text')
|
|
131
130
|
return ok(format(full, options.output, 0));
|
|
132
|
-
const whose =
|
|
131
|
+
const whose = describeAccount(account);
|
|
133
132
|
if (mode === 'access_token')
|
|
134
133
|
return ok(`Authenticated with an access token (from ${config.sources.accessToken}).${whose}`);
|
|
135
134
|
if (mode === 'oauth')
|
|
136
135
|
return ok(`Authenticated with OAuth.${whose}`);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
return ok('Missing authentication. Run `jinshuju auth login` or `jinshuju auth login --access-token <token>`, ' +
|
|
137
|
+
'or set JINSHUJU_ACCESS_TOKEN.');
|
|
138
|
+
}
|
|
139
|
+
function describeAccount(account) {
|
|
140
|
+
return account ? ` Account: ${account.name}${account.plan ? ` (${account.plan})` : ''}.` : '';
|
|
140
141
|
}
|
|
141
142
|
/**
|
|
142
143
|
* Who the credential belongs to. Reading the account proves the credential works
|
|
@@ -158,6 +159,8 @@ async function authRefresh(options, runtime) {
|
|
|
158
159
|
env: runtime.env,
|
|
159
160
|
cli: { host: options.host, authHost: options.authHost, clientId: options.clientId }
|
|
160
161
|
});
|
|
162
|
+
if (!config.auth && config.accessToken)
|
|
163
|
+
throw new AuthError('An access token cannot be refreshed; log in again with a new one if it stopped working.');
|
|
161
164
|
const auth = await refreshOAuthToken(config);
|
|
162
165
|
if (options.output !== 'text')
|
|
163
166
|
return ok(format({ authenticated: true, mode: 'oauth', expires_at: auth.expires_at, scope: auth.scope }, options.output, 0));
|
|
@@ -165,7 +168,7 @@ async function authRefresh(options, runtime) {
|
|
|
165
168
|
}
|
|
166
169
|
async function authLogout(options, runtime) {
|
|
167
170
|
const config = loadConfig({ configPath: options.configPath, env: runtime.env });
|
|
168
|
-
await
|
|
171
|
+
await logout(config);
|
|
169
172
|
return ok(options.output !== 'text' ? format({ authenticated: false }, options.output, 0) : 'Logged out.');
|
|
170
173
|
}
|
|
171
174
|
function configGet(positionals, options) {
|
|
@@ -173,20 +176,8 @@ function configGet(positionals, options) {
|
|
|
173
176
|
if (requestedKey)
|
|
174
177
|
assertConfigKey(requestedKey);
|
|
175
178
|
const config = getConfig(options.configPath);
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
let payload;
|
|
179
|
-
if (requestedKey) {
|
|
180
|
-
const configKey = requestedKey;
|
|
181
|
-
payload = { [configKey]: renderValue(configKey, config[configKey]) };
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
payload = {
|
|
185
|
-
access_token: renderValue('access_token', config.access_token),
|
|
186
|
-
api_key: renderValue('api_key', config.api_key),
|
|
187
|
-
api_secret: renderValue('api_secret', config.api_secret)
|
|
188
|
-
};
|
|
189
|
-
}
|
|
179
|
+
const keys = requestedKey ? [requestedKey] : CONFIG_KEYS;
|
|
180
|
+
const payload = Object.fromEntries(keys.map((key) => [key, config[key]]));
|
|
190
181
|
if (options.output !== 'text')
|
|
191
182
|
return ok(format(payload, options.output, 0));
|
|
192
183
|
return ok(Object.entries(payload)
|
package/dist/options.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export declare const PAGINATION_OPTIONS: readonly OptionSpec[];
|
|
|
42
42
|
export declare const JSON_OPTION: OptionSpec;
|
|
43
43
|
export declare const MINE_OPTION: OptionSpec;
|
|
44
44
|
export { UsageError } from './errors.js';
|
|
45
|
-
/** `--
|
|
45
|
+
/** `--auth-host` reads back as `auth_host`. */
|
|
46
46
|
export declare function optionKey(spec: OptionSpec): string;
|
|
47
47
|
export interface FilterCondition {
|
|
48
48
|
readonly field: string;
|
package/dist/options.js
CHANGED
|
@@ -24,15 +24,13 @@ export const GLOBAL_OPTIONS = [
|
|
|
24
24
|
* in it is a flag nobody knows about.
|
|
25
25
|
*/
|
|
26
26
|
export const LOCAL_OPTIONS = [
|
|
27
|
-
{ name: '--
|
|
28
|
-
{ name: '--api-secret', type: 'string', placeholder: '<secret>', description: 'Override API secret' },
|
|
27
|
+
{ name: '--access-token', type: 'string', placeholder: '<token>', description: 'Log in with an access token' },
|
|
29
28
|
{ name: '--host', type: 'string', placeholder: '<url>', description: 'API host' },
|
|
30
29
|
{ name: '--auth-host', type: 'string', placeholder: '<url>', description: 'OAuth host' },
|
|
31
30
|
{ name: '--client-id', type: 'string', placeholder: '<id>', description: 'OAuth public client id' },
|
|
32
31
|
{ name: '--scopes', type: 'string', placeholder: '<scopes>', description: 'Space-separated OAuth scopes' },
|
|
33
32
|
{ name: '--no-open', type: 'boolean', description: 'Print the login URL instead of opening a browser' },
|
|
34
|
-
{ name: '--verify', type: 'boolean', description: 'Verify the credentials with a lightweight call' }
|
|
35
|
-
{ name: '--show-secret', type: 'boolean', description: 'Show secrets unmasked' }
|
|
33
|
+
{ name: '--verify', type: 'boolean', description: 'Verify the credentials with a lightweight call' }
|
|
36
34
|
];
|
|
37
35
|
/** The data container a command acts on. Mutually exclusive; both map to form_token. */
|
|
38
36
|
export const CONTAINER_OPTIONS = [
|
|
@@ -120,7 +118,7 @@ export const MINE_OPTION = {
|
|
|
120
118
|
description: 'Switch to what the current user submitted'
|
|
121
119
|
};
|
|
122
120
|
export { UsageError } from './errors.js';
|
|
123
|
-
/** `--
|
|
121
|
+
/** `--auth-host` reads back as `auth_host`. */
|
|
124
122
|
export function optionKey(spec) {
|
|
125
123
|
return spec.name.replace(/^--/, '').replace(/-/g, '_');
|
|
126
124
|
}
|