@bakers-build/addon-dropsh 0.10.1-next.8 → 0.11.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/dist/src/auth-token.d.ts +13 -0
- package/dist/src/auth-token.js +59 -0
- package/dist/src/index.js +21 -14
- package/package.json +4 -4
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Command } from 'commander';
|
|
2
|
+
import type { CommandContext } from 'dropsh';
|
|
3
|
+
export interface AuthTokenDeps {
|
|
4
|
+
contextFactory: () => Promise<CommandContext>;
|
|
5
|
+
stdout(text: string): void;
|
|
6
|
+
stderr(text: string): void;
|
|
7
|
+
setExitCode(code: number): void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `auth token`: the profile's adapter refreshes inside its own margin and stores
|
|
11
|
+
* the rotation; this prints only the resulting access token, never a refresh token.
|
|
12
|
+
*/
|
|
13
|
+
export declare function registerAuthTokenCommand(program: Command, deps: AuthTokenDeps): void;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AuthError, exitCodeFor } from 'dropsh/plugin';
|
|
2
|
+
/** The `exp` claim of a JWT access token as an ISO 8601 instant. */
|
|
3
|
+
function expiresAt(token) {
|
|
4
|
+
let exp;
|
|
5
|
+
try {
|
|
6
|
+
exp = JSON.parse(Buffer.from(token.split('.')[1] ?? '', 'base64url').toString()).exp;
|
|
7
|
+
}
|
|
8
|
+
catch { }
|
|
9
|
+
if (typeof exp !== 'number')
|
|
10
|
+
throw new AuthError('access token carries no expiry');
|
|
11
|
+
return new Date(exp * 1000).toISOString();
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `auth token`: the profile's adapter refreshes inside its own margin and stores
|
|
15
|
+
* the rotation; this prints only the resulting access token, never a refresh token.
|
|
16
|
+
*/
|
|
17
|
+
export function registerAuthTokenCommand(program, deps) {
|
|
18
|
+
program.commands
|
|
19
|
+
.find((entry) => entry.name() === 'auth')
|
|
20
|
+
?.command('token')
|
|
21
|
+
.description('Print a short-lived access token as JSON for direct API calls')
|
|
22
|
+
.action(async () => {
|
|
23
|
+
try {
|
|
24
|
+
const ctx = await deps.contextFactory();
|
|
25
|
+
const request = await ctx.auth.apply({
|
|
26
|
+
method: 'GET',
|
|
27
|
+
url: ctx.baseUrl,
|
|
28
|
+
headers: {},
|
|
29
|
+
});
|
|
30
|
+
const header = Object.entries(request.headers ?? {}).find(([name]) => name.toLowerCase() === 'authorization')?.[1];
|
|
31
|
+
const token = /^Bearer (\S+)$/.exec(header ?? '')?.[1];
|
|
32
|
+
if (!token)
|
|
33
|
+
throw new AuthError('auth profile does not issue a bearer token');
|
|
34
|
+
deps.stdout(`${JSON.stringify({
|
|
35
|
+
base_url: ctx.baseUrl,
|
|
36
|
+
token,
|
|
37
|
+
token_type: 'Bearer',
|
|
38
|
+
expires_at: expiresAt(token),
|
|
39
|
+
})}\n`);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
const code = error.code;
|
|
43
|
+
let message = (error instanceof Error ? error.message : String(error)).replaceAll('dropsh auth login', 'bakers auth login');
|
|
44
|
+
// A rejected refresh says why, not what to do; every auth failure names the fix.
|
|
45
|
+
if (code === 'E_AUTH' && !message.includes('bakers auth login'))
|
|
46
|
+
message += " Run 'bakers auth login'.";
|
|
47
|
+
// No `details`: AuthError details carry client ids and token endpoints.
|
|
48
|
+
deps.stderr(`${JSON.stringify({
|
|
49
|
+
error: {
|
|
50
|
+
code: typeof code === 'string' && code.startsWith('E_')
|
|
51
|
+
? code
|
|
52
|
+
: 'E_UNKNOWN',
|
|
53
|
+
message,
|
|
54
|
+
},
|
|
55
|
+
})}\n`);
|
|
56
|
+
deps.setExitCode(exitCodeFor(error));
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
package/dist/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { createBbConnectionContext, discoverBbWorkspaces, loadBbConnectionBootst
|
|
|
2
2
|
import { toolPlugin } from '@dropsh/plugin-tool';
|
|
3
3
|
import { Help } from 'commander';
|
|
4
4
|
import { buildProgram as buildDropshProgram } from 'dropsh';
|
|
5
|
+
import { registerAuthTokenCommand } from './auth-token.js';
|
|
5
6
|
import { registerTemplateCommand } from './template.js';
|
|
6
7
|
import { hideUnpublishedComments } from './unpublished-comments.js';
|
|
7
8
|
export function dropshConfigHint(resolvedPath) {
|
|
@@ -155,6 +156,24 @@ export async function dispatchBbDropsh(host, argv, options = {}) {
|
|
|
155
156
|
}
|
|
156
157
|
: {}),
|
|
157
158
|
});
|
|
159
|
+
const deps = {
|
|
160
|
+
contextFactory: () => {
|
|
161
|
+
if (!connection)
|
|
162
|
+
throw new Error('no connection loaded');
|
|
163
|
+
return createBbConnectionContext(connection, {
|
|
164
|
+
...opts,
|
|
165
|
+
...(args.profile ? { profile: args.profile } : {}),
|
|
166
|
+
wrapHttp: hideUnpublishedComments,
|
|
167
|
+
});
|
|
168
|
+
},
|
|
169
|
+
stdout: options.stdout ?? ((s) => process.stdout.write(s)),
|
|
170
|
+
stderr: options.stderr ?? ((s) => process.stderr.write(s)),
|
|
171
|
+
setExitCode: (code) => {
|
|
172
|
+
process.exitCode = code;
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
// Registered even for --help, which loads no connection, so help lists it.
|
|
176
|
+
registerAuthTokenCommand(program, deps);
|
|
158
177
|
if (options.publicCommand) {
|
|
159
178
|
program.name('bakers');
|
|
160
179
|
const command = program.commands.find((entry) => entry.name() === (options.publicCommand === 'tools' ? 'tool' : 'auth'));
|
|
@@ -168,20 +187,8 @@ export async function dispatchBbDropsh(host, argv, options = {}) {
|
|
|
168
187
|
});
|
|
169
188
|
}
|
|
170
189
|
}
|
|
171
|
-
if (connection)
|
|
172
|
-
registerTemplateCommand(program,
|
|
173
|
-
contextFactory: () => createBbConnectionContext(connection, {
|
|
174
|
-
...opts,
|
|
175
|
-
...(args.profile ? { profile: args.profile } : {}),
|
|
176
|
-
wrapHttp: hideUnpublishedComments,
|
|
177
|
-
}),
|
|
178
|
-
stdout: options.stdout ?? ((s) => process.stdout.write(s)),
|
|
179
|
-
stderr: options.stderr ?? ((s) => process.stderr.write(s)),
|
|
180
|
-
setExitCode: (code) => {
|
|
181
|
-
process.exitCode = code;
|
|
182
|
-
},
|
|
183
|
-
});
|
|
184
|
-
}
|
|
190
|
+
if (connection)
|
|
191
|
+
registerTemplateCommand(program, deps);
|
|
185
192
|
// Auth commands perform their own reload; bind it explicitly to the same path.
|
|
186
193
|
const forwarded = connection
|
|
187
194
|
? ['--config', connection.config_path, ...args.forwarded]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bakers-build/addon-dropsh",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "bakers.build tools and auth command plugins using the shared connection config.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"dropsh": "^0.8.2"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@bakers-build/core": "^0.
|
|
30
|
+
"@bakers-build/core": "^0.11.0"
|
|
31
31
|
},
|
|
32
32
|
"bakersBuild": {
|
|
33
|
-
"commit": "
|
|
34
|
-
"channel": "
|
|
33
|
+
"commit": "4e1d1a59439a6a0019f3b5b213d9cc9f09d14381",
|
|
34
|
+
"channel": "latest"
|
|
35
35
|
}
|
|
36
36
|
}
|