@agent-watch-ai/edge 0.2.5 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +512 -0
- package/README.md +217 -96
- package/SECURITY.md +69 -0
- package/dist/billing/session-billing.d.ts +19 -0
- package/dist/billing/session-billing.js +52 -0
- package/dist/cli/args.d.ts +10 -0
- package/dist/cli/args.js +20 -0
- package/dist/cli/constants/cli.constants.d.ts +50 -4
- package/dist/cli/constants/cli.constants.js +54 -5
- package/dist/cli/context.d.ts +33 -2
- package/dist/cli/context.js +84 -14
- package/dist/cli/doctor.js +309 -32
- package/dist/cli/hook-check.d.ts +23 -0
- package/dist/cli/hook-check.js +114 -0
- package/dist/cli/hook.js +118 -7
- package/dist/cli/index.d.ts +3 -1
- package/dist/cli/index.js +3 -1
- package/dist/cli/misc.d.ts +11 -0
- package/dist/cli/misc.js +80 -4
- package/dist/cli/npx-install.d.ts +37 -0
- package/dist/cli/npx-install.js +79 -0
- package/dist/cli/open-browser.d.ts +39 -0
- package/dist/cli/open-browser.js +62 -0
- package/dist/cli/setup.d.ts +17 -8
- package/dist/cli/setup.js +439 -98
- package/dist/cli/status.js +82 -13
- package/dist/cli/toggle.d.ts +9 -0
- package/dist/cli/toggle.js +113 -0
- package/dist/cli/types/cli.types.d.ts +37 -3
- package/dist/cli/ui.d.ts +7 -0
- package/dist/cli/ui.js +10 -1
- package/dist/cli/uninstall.js +39 -2
- package/dist/cli/update.d.ts +17 -0
- package/dist/cli/update.js +76 -0
- package/dist/cli.js +100 -13
- package/dist/config/config-store.d.ts +57 -0
- package/dist/config/config-store.js +250 -12
- package/dist/config/config.d.ts +53 -6
- package/dist/config/config.js +66 -6
- package/dist/config/constants/config.constants.d.ts +113 -6
- package/dist/config/constants/config.constants.js +126 -8
- package/dist/config/destination.d.ts +24 -0
- package/dist/config/destination.js +33 -0
- package/dist/config/index.d.ts +7 -4
- package/dist/config/index.js +5 -3
- package/dist/config/repo-config.d.ts +11 -7
- package/dist/config/repo-config.js +64 -15
- package/dist/config/root-config.d.ts +72 -0
- package/dist/config/root-config.js +170 -0
- package/dist/config/schemas/config.schema.d.ts +721 -84
- package/dist/config/schemas/config.schema.js +388 -15
- package/dist/config/types/config.types.d.ts +15 -1
- package/dist/config/types/destination.types.d.ts +8 -0
- package/dist/core/async.d.ts +36 -0
- package/dist/core/async.js +68 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/object.d.ts +5 -0
- package/dist/core/object.js +9 -4
- package/dist/core/pipe.d.ts +0 -13
- package/dist/core/pipe.js +1 -9
- package/dist/core/types/core.types.d.ts +9 -0
- package/dist/core/types/core.types.js +0 -3
- package/dist/core/which.d.ts +14 -0
- package/dist/core/which.js +43 -3
- package/dist/credentials/budget.d.ts +9 -0
- package/dist/credentials/budget.js +16 -0
- package/dist/credentials/command-runner.d.ts +16 -0
- package/dist/credentials/command-runner.js +37 -0
- package/dist/credentials/constants/credentials.constants.d.ts +111 -0
- package/dist/credentials/constants/credentials.constants.js +112 -0
- package/dist/credentials/credential-status.d.ts +14 -0
- package/dist/credentials/credential-status.js +34 -0
- package/dist/credentials/credentials.d.ts +72 -0
- package/dist/credentials/credentials.js +253 -0
- package/dist/credentials/device-api.d.ts +64 -0
- package/dist/credentials/device-api.js +150 -0
- package/dist/credentials/device-state.d.ts +31 -0
- package/dist/credentials/device-state.js +51 -0
- package/dist/credentials/identity.d.ts +62 -0
- package/dist/credentials/identity.js +90 -0
- package/dist/credentials/index.d.ts +7 -0
- package/dist/credentials/index.js +5 -0
- package/dist/credentials/migration.d.ts +46 -0
- package/dist/credentials/migration.js +164 -0
- package/dist/credentials/schemas/device-api.schema.d.ts +77 -0
- package/dist/credentials/schemas/device-api.schema.js +33 -0
- package/dist/credentials/schemas/device-state.schema.d.ts +51 -0
- package/dist/credentials/schemas/device-state.schema.js +20 -0
- package/dist/credentials/secret-store.d.ts +54 -0
- package/dist/credentials/secret-store.js +217 -0
- package/dist/credentials/sign-in.d.ts +18 -0
- package/dist/credentials/sign-in.js +86 -0
- package/dist/credentials/token-refresher.d.ts +39 -0
- package/dist/credentials/token-refresher.js +185 -0
- package/dist/credentials/types/credentials.types.d.ts +195 -0
- package/dist/credentials/types/credentials.types.js +1 -0
- package/dist/enforcement/constants/enforcement.constants.d.ts +47 -1
- package/dist/enforcement/constants/enforcement.constants.js +56 -0
- package/dist/enforcement/decision-cache.d.ts +20 -1
- package/dist/enforcement/decision-cache.js +65 -2
- package/dist/enforcement/decision-client.d.ts +9 -8
- package/dist/enforcement/decision-client.js +62 -16
- package/dist/enforcement/enforcement.d.ts +23 -2
- package/dist/enforcement/enforcement.js +103 -15
- package/dist/enforcement/index.d.ts +4 -3
- package/dist/enforcement/index.js +3 -2
- package/dist/enforcement/types/enforcement.types.d.ts +64 -0
- package/dist/enrollment/constants/enrollment.constants.d.ts +2 -6
- package/dist/enrollment/constants/enrollment.constants.js +2 -6
- package/dist/enrollment/enrollment.d.ts +16 -0
- package/dist/enrollment/enrollment.js +54 -0
- package/dist/enrollment/index.d.ts +3 -7
- package/dist/enrollment/index.js +1 -1
- package/dist/enrollment/types/enrollment.types.d.ts +9 -13
- package/dist/events/constants/enrich.constants.d.ts +15 -0
- package/dist/events/constants/enrich.constants.js +15 -0
- package/dist/events/constants/events.constants.d.ts +6 -2
- package/dist/events/constants/events.constants.js +5 -1
- package/dist/events/enrich.js +92 -10
- package/dist/events/event-id.d.ts +0 -8
- package/dist/events/event-id.js +0 -10
- package/dist/events/index.d.ts +2 -2
- package/dist/events/index.js +1 -1
- package/dist/events/product-event.d.ts +9 -6
- package/dist/events/types/repo-snapshot.types.d.ts +7 -0
- package/dist/events/types/session-billing.types.d.ts +16 -0
- package/dist/events/types/session-billing.types.js +1 -0
- package/dist/feature/constants/feature.constants.d.ts +7 -0
- package/dist/feature/constants/feature.constants.js +7 -0
- package/dist/feature/index.d.ts +1 -1
- package/dist/feature/index.js +1 -1
- package/dist/feature/ticket-candidates.d.ts +14 -0
- package/dist/feature/ticket-candidates.js +26 -8
- package/dist/git/blob-hash.d.ts +21 -0
- package/dist/git/blob-hash.js +69 -0
- package/dist/git/checkout-root.d.ts +27 -0
- package/dist/git/checkout-root.js +75 -0
- package/dist/git/constants/git.constants.d.ts +160 -0
- package/dist/git/constants/git.constants.js +181 -0
- package/dist/git/fingerprint.d.ts +74 -0
- package/dist/git/fingerprint.js +219 -0
- package/dist/git/gate-checkout.d.ts +44 -0
- package/dist/git/gate-checkout.js +201 -0
- package/dist/git/git-context.d.ts +45 -0
- package/dist/git/git-context.js +61 -3
- package/dist/git/index.d.ts +4 -2
- package/dist/git/index.js +2 -1
- package/dist/git/manifest-repository.d.ts +13 -0
- package/dist/git/manifest-repository.js +92 -0
- package/dist/git/repo-snapshot.d.ts +29 -3
- package/dist/git/repo-snapshot.js +110 -7
- package/dist/git/repository-root.d.ts +39 -0
- package/dist/git/repository-root.js +131 -0
- package/dist/git/types/git.types.d.ts +55 -0
- package/dist/git/types/repository-root.types.d.ts +11 -0
- package/dist/git/types/repository-root.types.js +1 -0
- package/dist/git/types/snapshot.types.d.ts +9 -0
- package/dist/pipeline/constants/pipeline.constants.d.ts +7 -0
- package/dist/pipeline/constants/pipeline.constants.js +7 -0
- package/dist/pipeline/hook-pipeline.js +360 -32
- package/dist/pipeline/types/pipeline.types.d.ts +39 -4
- package/dist/privacy/constants/privacy.constants.js +15 -1
- package/dist/privacy/index.d.ts +1 -0
- package/dist/privacy/index.js +1 -0
- package/dist/privacy/product-capture.d.ts +12 -0
- package/dist/privacy/product-capture.js +73 -0
- package/dist/privacy/sanitizer.d.ts +21 -3
- package/dist/privacy/sanitizer.js +52 -8
- package/dist/process/ancestors.d.ts +39 -0
- package/dist/process/ancestors.js +121 -0
- package/dist/process/constants/process.constants.d.ts +34 -0
- package/dist/process/constants/process.constants.js +34 -0
- package/dist/process/index.d.ts +7 -0
- package/dist/process/index.js +2 -0
- package/dist/process/types/process.types.d.ts +23 -0
- package/dist/process/types/process.types.js +1 -0
- package/dist/providers/antigravity/antigravity.adapter.d.ts +12 -1
- package/dist/providers/antigravity/antigravity.adapter.js +21 -3
- package/dist/providers/antigravity/antigravity.detect.d.ts +0 -9
- package/dist/providers/antigravity/antigravity.detect.js +1 -1
- package/dist/providers/antigravity/antigravity.provider.js +3 -2
- package/dist/providers/antigravity/constants/antigravity.constants.d.ts +2 -0
- package/dist/providers/antigravity/constants/antigravity.constants.js +2 -0
- package/dist/providers/claude/claude.adapter.d.ts +11 -1
- package/dist/providers/claude/claude.adapter.js +26 -5
- package/dist/providers/claude/claude.otel.d.ts +32 -18
- package/dist/providers/claude/claude.otel.js +81 -6
- package/dist/providers/claude/claude.provider.js +5 -2
- package/dist/providers/claude/constants/claude.constants.d.ts +2 -0
- package/dist/providers/claude/constants/claude.constants.js +2 -0
- package/dist/providers/claude/constants/claude.otel.constants.d.ts +4 -0
- package/dist/providers/claude/constants/claude.otel.constants.js +8 -0
- package/dist/providers/codex/codex.adapter.d.ts +15 -1
- package/dist/providers/codex/codex.adapter.js +29 -5
- package/dist/providers/codex/codex.otel.d.ts +24 -2
- package/dist/providers/codex/codex.otel.js +147 -30
- package/dist/providers/codex/codex.provider.js +5 -2
- package/dist/providers/codex/constants/codex.constants.d.ts +2 -0
- package/dist/providers/codex/constants/codex.constants.js +16 -0
- package/dist/providers/codex/constants/codex.otel.constants.d.ts +5 -0
- package/dist/providers/codex/constants/codex.otel.constants.js +5 -0
- package/dist/providers/constants/provider.constants.d.ts +0 -3
- package/dist/providers/constants/provider.constants.js +0 -3
- package/dist/providers/cursor/constants/cursor.constants.d.ts +7 -0
- package/dist/providers/cursor/constants/cursor.constants.js +7 -0
- package/dist/providers/cursor/cursor.adapter.d.ts +33 -1
- package/dist/providers/cursor/cursor.adapter.js +52 -3
- package/dist/providers/cursor/cursor.detect.d.ts +0 -7
- package/dist/providers/cursor/cursor.detect.js +1 -1
- package/dist/providers/cursor/cursor.provider.js +5 -2
- package/dist/providers/gemini/constants/gemini.constants.d.ts +9 -0
- package/dist/providers/gemini/constants/gemini.constants.js +9 -0
- package/dist/providers/gemini/constants/gemini.otel.constants.d.ts +3 -1
- package/dist/providers/gemini/constants/gemini.otel.constants.js +3 -0
- package/dist/providers/gemini/gemini.adapter.d.ts +14 -1
- package/dist/providers/gemini/gemini.adapter.js +25 -5
- package/dist/providers/gemini/gemini.detect.d.ts +0 -7
- package/dist/providers/gemini/gemini.detect.js +1 -1
- package/dist/providers/gemini/gemini.otel.d.ts +19 -20
- package/dist/providers/gemini/gemini.otel.js +121 -17
- package/dist/providers/gemini/gemini.provider.js +2 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +1 -1
- package/dist/providers/loaders.d.ts +10 -0
- package/dist/providers/loaders.js +40 -0
- package/dist/providers/provider.d.ts +11 -0
- package/dist/providers/provider.js +1 -1
- package/dist/providers/registry.d.ts +11 -8
- package/dist/providers/registry.js +11 -15
- package/dist/providers/shared/constants/hook-refusal.constants.d.ts +7 -0
- package/dist/providers/shared/constants/hook-refusal.constants.js +7 -0
- package/dist/providers/shared/constants/shell-directories.constants.d.ts +30 -0
- package/dist/providers/shared/constants/shell-directories.constants.js +39 -0
- package/dist/providers/shared/constants/tooling.constants.d.ts +18 -2
- package/dist/providers/shared/constants/tooling.constants.js +19 -3
- package/dist/providers/shared/event-builder.d.ts +12 -14
- package/dist/providers/shared/event-builder.js +20 -14
- package/dist/providers/shared/exporter-allowlist.d.ts +20 -0
- package/dist/providers/shared/exporter-allowlist.js +49 -0
- package/dist/providers/shared/hook-config.d.ts +2 -21
- package/dist/providers/shared/hook-config.js +5 -4
- package/dist/providers/shared/hook-refusal.d.ts +12 -0
- package/dist/providers/shared/hook-refusal.js +17 -1
- package/dist/providers/shared/install-record.d.ts +12 -2
- package/dist/providers/shared/install-record.js +5 -2
- package/dist/providers/shared/shell-directories.d.ts +24 -0
- package/dist/providers/shared/shell-directories.js +90 -0
- package/dist/providers/shared/tooling.d.ts +12 -1
- package/dist/providers/shared/tooling.js +18 -0
- package/dist/providers/shared/types/adapter.types.d.ts +1 -2
- package/dist/providers/types/provider.types.d.ts +57 -1
- package/dist/snapshot/constants/snapshot.constants.d.ts +32 -0
- package/dist/snapshot/constants/snapshot.constants.js +33 -0
- package/dist/snapshot/snapshot-event.js +52 -11
- package/dist/snapshot/snapshot-pipeline.js +1 -1
- package/dist/snapshot/types/snapshot.types.d.ts +2 -0
- package/dist/storage/atomic-file.d.ts +7 -0
- package/dist/storage/atomic-file.js +1 -1
- package/dist/storage/constants/storage.constants.d.ts +17 -0
- package/dist/storage/constants/storage.constants.js +17 -0
- package/dist/storage/disabled.d.ts +13 -0
- package/dist/storage/disabled.js +24 -0
- package/dist/storage/index.d.ts +2 -0
- package/dist/storage/index.js +2 -0
- package/dist/storage/paths.js +3 -2
- package/dist/storage/schemas/storage.schema.d.ts +72 -0
- package/dist/storage/schemas/storage.schema.js +6 -0
- package/dist/storage/sweep-marker.d.ts +19 -0
- package/dist/storage/sweep-marker.js +55 -0
- package/dist/storage/types/storage.types.d.ts +2 -0
- package/dist/storage/uninstalled.d.ts +58 -0
- package/dist/storage/uninstalled.js +107 -0
- package/dist/transport/auth-block.d.ts +66 -0
- package/dist/transport/auth-block.js +129 -0
- package/dist/transport/constants/transport.constants.d.ts +82 -3
- package/dist/transport/constants/transport.constants.js +83 -4
- package/dist/transport/cooldown.js +8 -1
- package/dist/transport/delivery.d.ts +14 -6
- package/dist/transport/delivery.js +80 -55
- package/dist/transport/http-transport.d.ts +1 -0
- package/dist/transport/http-transport.js +52 -10
- package/dist/transport/index.d.ts +6 -3
- package/dist/transport/index.js +3 -0
- package/dist/transport/queue-partition.d.ts +91 -0
- package/dist/transport/queue-partition.js +143 -0
- package/dist/transport/queue.d.ts +99 -20
- package/dist/transport/queue.js +210 -69
- package/dist/transport/response-body.d.ts +25 -0
- package/dist/transport/response-body.js +64 -0
- package/dist/transport/send.d.ts +11 -0
- package/dist/transport/send.js +17 -0
- package/dist/transport/types/delivery-flow.types.d.ts +19 -0
- package/dist/transport/types/delivery-flow.types.js +1 -0
- package/dist/transport/types/transport.types.d.ts +45 -0
- package/dist/turns/claude-transcript.d.ts +19 -1
- package/dist/turns/claude-transcript.js +46 -1
- package/dist/turns/codex-originator.d.ts +11 -0
- package/dist/turns/codex-originator.js +31 -0
- package/dist/turns/codex-transcript.d.ts +30 -0
- package/dist/turns/codex-transcript.js +88 -0
- package/dist/turns/constants/turns.constants.d.ts +141 -5
- package/dist/turns/constants/turns.constants.js +155 -5
- package/dist/turns/file-identity.d.ts +27 -0
- package/dist/turns/file-identity.js +48 -0
- package/dist/turns/gemini-transcript.d.ts +30 -0
- package/dist/turns/gemini-transcript.js +96 -0
- package/dist/turns/index.d.ts +3 -3
- package/dist/turns/index.js +1 -1
- package/dist/turns/inherited-checkout.d.ts +54 -0
- package/dist/turns/inherited-checkout.js +155 -0
- package/dist/turns/native-session-titles.d.ts +26 -0
- package/dist/turns/native-session-titles.js +102 -0
- package/dist/turns/transcript.d.ts +3 -2
- package/dist/turns/transcript.js +5 -1
- package/dist/turns/turn-state.d.ts +224 -3
- package/dist/turns/turn-state.js +456 -21
- package/dist/turns/turn-summary.d.ts +1 -14
- package/dist/turns/turn-summary.js +87 -51
- package/dist/turns/turn-tracker.d.ts +21 -2
- package/dist/turns/turn-tracker.js +379 -56
- package/dist/turns/types/transcript.types.d.ts +5 -0
- package/dist/turns/types/turn-state.types.d.ts +116 -5
- package/dist/turns/types/turn-summary.types.d.ts +64 -14
- package/dist/turns/types/turn-tracker.types.d.ts +27 -1
- package/dist/turns/work-checkout.d.ts +65 -0
- package/dist/turns/work-checkout.js +666 -0
- package/dist/untraced/constants/untraced.constants.d.ts +31 -0
- package/dist/untraced/constants/untraced.constants.js +63 -0
- package/dist/untraced/untraced.d.ts +35 -0
- package/dist/untraced/untraced.js +105 -0
- package/dist/update/constants/update.constants.d.ts +18 -0
- package/dist/update/constants/update.constants.js +20 -0
- package/dist/update/update-check.d.ts +52 -0
- package/dist/update/update-check.js +113 -0
- package/docs/AUDIT_PROMPT.md +129 -0
- package/docs/DATA_HANDLING.md +648 -0
- package/docs/ENTERPRISE_DEPLOYMENT.md +179 -0
- package/docs/PERFORMANCE.md +81 -0
- package/package.json +14 -4
- package/dist/enrollment/enrollment-provider.d.ts +0 -5
- package/dist/enrollment/manual-enrollment.d.ts +0 -32
- package/dist/enrollment/manual-enrollment.js +0 -72
- package/example/README.md +0 -43
- package/example/server.mjs +0 -240
- /package/dist/{enrollment/enrollment-provider.js → config/types/destination.types.js} +0 -0
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { loadConfig, updateConfig } from '../config/config-store.js';
|
|
3
|
+
import { CONFIG_LOCK_POLL_MS, CONFIG_LOCK_WAIT_MS, DEVICE_KEY } from '../config/constants/config.constants.js';
|
|
4
|
+
import { applyRootOverride } from '../config/root-config.js';
|
|
5
|
+
import { pollUntil } from '../core/async.js';
|
|
6
|
+
import { debugLog, warnLog } from '../core/logger.js';
|
|
7
|
+
import { omitKeys } from '../core/object.js';
|
|
8
|
+
import { acquireLock } from '../storage/lock.js';
|
|
9
|
+
import { configScopes, isUninstalled, markUninstalled } from '../storage/uninstalled.js';
|
|
10
|
+
import { identityPaths } from '../transport/queue-partition.js';
|
|
11
|
+
import { revokeDevice } from './device-api.js';
|
|
12
|
+
import { readDeviceState } from './device-state.js';
|
|
13
|
+
import { ENROLL_LOCK_PREFIX } from './constants/credentials.constants.js';
|
|
14
|
+
import { credentialId, deviceFor, deviceRefs, identityOf } from './identity.js';
|
|
15
|
+
import { secretLocation, secretStoreFor } from './secret-store.js';
|
|
16
|
+
import { migrateInstallKey } from './migration.js';
|
|
17
|
+
import { cachedAccessToken, currentAccessToken } from './token-refresher.js';
|
|
18
|
+
/**
|
|
19
|
+
* The bearer to send for this config, doing the network work it needs.
|
|
20
|
+
*
|
|
21
|
+
* A config whose device is listed sends that device's access token, refreshed
|
|
22
|
+
* when due, and never its install key (which stays in the file for Codex and
|
|
23
|
+
* Gemini, AWT-357). An install key with no device yet is offered for exchange
|
|
24
|
+
* first and presented itself only while that fails. A device the gateway
|
|
25
|
+
* refused is enrolled again when `migrateInstallKey` allows it, and its install
|
|
26
|
+
* key is never sent in its place. Undefined means "nothing to send with": the
|
|
27
|
+
* caller queues. So is an uninstalled scope: neither its install key nor a
|
|
28
|
+
* token goes out until setup runs for it again.
|
|
29
|
+
*
|
|
30
|
+
* Never throws. A keychain, a token cache or a config file that cannot be
|
|
31
|
+
* written is a reason to queue this run's records, never to lose them or to
|
|
32
|
+
* fail the hook.
|
|
33
|
+
*
|
|
34
|
+
* @param ctx - Paths, clock, timeout, deadline, fetch and keychain runner.
|
|
35
|
+
* @param config - Effective config.
|
|
36
|
+
* @returns The bearer, or undefined.
|
|
37
|
+
*/
|
|
38
|
+
export async function resolveBearer(ctx, config) {
|
|
39
|
+
try {
|
|
40
|
+
if (await isUninstalled(ctx.paths, ctx.scope))
|
|
41
|
+
return undefined;
|
|
42
|
+
if (deviceFor(config)) {
|
|
43
|
+
const token = await currentAccessToken(ctx, config);
|
|
44
|
+
if (token || !config.token || !(await readDeviceState(identityPaths(ctx.paths, config.token).deviceStateFile)).refusal)
|
|
45
|
+
return token;
|
|
46
|
+
const renewed = await migrateInstallKey(ctx, config);
|
|
47
|
+
return renewed ? await currentAccessToken(ctx, { ...config, device: renewed }) : undefined;
|
|
48
|
+
}
|
|
49
|
+
if (!config.token)
|
|
50
|
+
return undefined;
|
|
51
|
+
const device = await migrateInstallKey(ctx, config);
|
|
52
|
+
if (!device)
|
|
53
|
+
return config.token;
|
|
54
|
+
return await currentAccessToken(ctx, { ...config, device });
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
debugLog('credentials: resolving the bearer failed; queueing instead:', error);
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The bearer to send without any network or keychain work: a cached access
|
|
63
|
+
* token that has not expired, or the install key of an identity not yet
|
|
64
|
+
* exchanged, and nothing for an uninstalled scope. For the prompt path, whose
|
|
65
|
+
* budget is a few hundred milliseconds.
|
|
66
|
+
*
|
|
67
|
+
* @param ctx - Paths and clock.
|
|
68
|
+
* @param config - Effective config.
|
|
69
|
+
* @returns The bearer, or undefined (never a throw).
|
|
70
|
+
*/
|
|
71
|
+
export async function cachedBearer(ctx, config) {
|
|
72
|
+
try {
|
|
73
|
+
if (await isUninstalled(ctx.paths, ctx.scope))
|
|
74
|
+
return undefined;
|
|
75
|
+
return deviceFor(config) ? await cachedAccessToken(ctx, config) : config.token;
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
debugLog('credentials: reading the cached token failed:', error);
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Remove a device secret and its cached token, unless the config still names
|
|
84
|
+
* that device anywhere: a root may share it with the machine, or with the
|
|
85
|
+
* entry setup just gave a new key.
|
|
86
|
+
*
|
|
87
|
+
* @param ctx - Paths, environment, timeout and keychain runner.
|
|
88
|
+
* @param device - The device a new key displaced.
|
|
89
|
+
* @returns True when it was removed.
|
|
90
|
+
*/
|
|
91
|
+
export async function forgetDeviceIfUnused(ctx, device) {
|
|
92
|
+
const { config } = await loadConfig(ctx.paths);
|
|
93
|
+
if (deviceRefs(config).some((ref) => ref.id === device.id))
|
|
94
|
+
return false;
|
|
95
|
+
await forgetSecret(ctx, device);
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Switch a displaced device off on the platform, then forget it here, unless the
|
|
100
|
+
* config still names it. For a sign-in that replaces one: its secret is about to
|
|
101
|
+
* be the last copy, so revoking after forgetting would be impossible.
|
|
102
|
+
*
|
|
103
|
+
* @param ctx - Paths, environment, timeout, fetch and keychain runner.
|
|
104
|
+
* @param device - The device a new sign-in displaced.
|
|
105
|
+
* @returns True when it was removed from this machine.
|
|
106
|
+
*/
|
|
107
|
+
export async function revokeDeviceIfUnused(ctx, device) {
|
|
108
|
+
const { config } = await loadConfig(ctx.paths);
|
|
109
|
+
if (deviceRefs(config).some((ref) => ref.id === device.id))
|
|
110
|
+
return false;
|
|
111
|
+
const secret = await secretStoreFor(device.store, ctx).get(device.id).catch(() => undefined);
|
|
112
|
+
const revoked = secret && device.endpoint ? (await revokeDevice({ endpoint: device.endpoint, deviceSecret: secret, timeoutMs: ctx.timeoutMs, fetchFn: ctx.fetchFn })).ok : false;
|
|
113
|
+
if (!revoked)
|
|
114
|
+
warnLog('the device this sign-in replaced could not be revoked; it may still be active on the platform. An admin can revoke it in Settings → Install.');
|
|
115
|
+
await forgetSecret(ctx, device);
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Switch off every device credential this machine holds, then remove it: the
|
|
120
|
+
* references from the config, and for each device the secret from its store
|
|
121
|
+
* and the cached access token.
|
|
122
|
+
*
|
|
123
|
+
* For uninstall. The marker goes down first, under every install key's enroll
|
|
124
|
+
* lock, so a hook mid-exchange either attached its device before (and it is
|
|
125
|
+
* collected here) or sees the marker before attaching (and revokes it itself).
|
|
126
|
+
* The references then leave the config under the config lock as one write; the
|
|
127
|
+
* gateway is called outside both locks, so a slow platform never holds them.
|
|
128
|
+
* Revocation comes before the secret is deleted, because a deleted secret
|
|
129
|
+
* cannot be revoked from here. A device the config names again by then (a
|
|
130
|
+
* concurrent setup) is left alone. One device's failure never stops another's.
|
|
131
|
+
*
|
|
132
|
+
* @param ctx - Paths, environment, timeout, fetch and keychain runner.
|
|
133
|
+
* @returns Whether the marker and the config were written, and one outcome per device.
|
|
134
|
+
*/
|
|
135
|
+
export async function forgetDeviceCredentials(ctx) {
|
|
136
|
+
const before = await loadConfig(ctx.paths);
|
|
137
|
+
const keys = [...new Set([before.config.token, ...Object.values(before.config.roots ?? {}).map((root) => root.token)].filter((key) => Boolean(key)))];
|
|
138
|
+
const releases = await holdEnrollLocks(ctx, keys.map(credentialId).sort());
|
|
139
|
+
let held = new Map();
|
|
140
|
+
let invalid = false;
|
|
141
|
+
let marked = true;
|
|
142
|
+
let written = 'unchanged';
|
|
143
|
+
try {
|
|
144
|
+
marked = await markUninstalled(ctx.paths, before.state === 'ok' ? configScopes(before.config.roots) : undefined, ctx.env.now()).then(() => true, (error) => {
|
|
145
|
+
debugLog('uninstall: writing the marker failed:', error);
|
|
146
|
+
return false;
|
|
147
|
+
});
|
|
148
|
+
written = await updateConfig(ctx.paths, (loaded) => {
|
|
149
|
+
invalid = loaded.state === 'invalid';
|
|
150
|
+
held = loaded.state === 'ok' ? heldDevices(loaded.config) : new Map();
|
|
151
|
+
if (held.size === 0)
|
|
152
|
+
return undefined;
|
|
153
|
+
const roots = loaded.config.roots ? Object.fromEntries(Object.entries(loaded.config.roots).map(([key, root]) => [key, omitKeys(root, DEVICE_KEY)])) : undefined;
|
|
154
|
+
return { ...omitKeys(loaded.config, DEVICE_KEY), ...(roots ? { roots } : {}) };
|
|
155
|
+
}, { waitMs: CONFIG_LOCK_WAIT_MS, now: ctx.env.now });
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
await Promise.all(releases.map((release) => release()));
|
|
159
|
+
}
|
|
160
|
+
if (written === 'busy' || invalid)
|
|
161
|
+
return { marked, config: written === 'busy' ? 'busy' : 'invalid', revocations: [] };
|
|
162
|
+
const { config } = await loadConfig(ctx.paths);
|
|
163
|
+
const named = new Set(deviceRefs(config).map((ref) => ref.id));
|
|
164
|
+
const revocations = await Promise.all([...held.values()].filter((device) => !named.has(device.id)).map((device) => settleDevice(ctx, device)));
|
|
165
|
+
return { marked, config: 'ok', revocations };
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Each install key's enroll lock, waiting a while for one a hook holds. A lock
|
|
169
|
+
* still taken after the wait is skipped: the exchange looks at the marker again
|
|
170
|
+
* before it attaches anything, so the lock narrows the race, it does not close it.
|
|
171
|
+
*
|
|
172
|
+
* @param ctx - Paths and clock.
|
|
173
|
+
* @param ids - The keys' device ids, sorted.
|
|
174
|
+
* @returns The releases of the locks taken.
|
|
175
|
+
*/
|
|
176
|
+
async function holdEnrollLocks(ctx, ids) {
|
|
177
|
+
const releases = [];
|
|
178
|
+
for (const id of ids) {
|
|
179
|
+
const release = await pollUntil(() => acquireLock(ctx.paths.locksDir, ENROLL_LOCK_PREFIX + id, ctx.env.now).catch(() => undefined), CONFIG_LOCK_WAIT_MS, CONFIG_LOCK_POLL_MS);
|
|
180
|
+
if (release)
|
|
181
|
+
releases.push(release);
|
|
182
|
+
}
|
|
183
|
+
return releases;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Each distinct device in the file, with the endpoint to revoke it at: the one
|
|
187
|
+
* it was enrolled with, else the one the first entry naming it sends to, as
|
|
188
|
+
* `applyRootOverride` resolves that entry.
|
|
189
|
+
*
|
|
190
|
+
* @param config - The machine's global config, roots included.
|
|
191
|
+
* @returns The devices by id.
|
|
192
|
+
*/
|
|
193
|
+
function heldDevices(config) {
|
|
194
|
+
const found = new Map();
|
|
195
|
+
const entries = [{ device: config.device, endpoint: config.endpoint }, ...Object.entries(config.roots ?? {}).map(([key, root]) => ({ device: root.device, endpoint: applyRootOverride(config, key).config.endpoint }))];
|
|
196
|
+
for (const { device, endpoint } of entries) {
|
|
197
|
+
if (!device || found.get(device.id)?.endpoint)
|
|
198
|
+
continue;
|
|
199
|
+
const revokeAt = device.endpoint ?? endpoint ?? undefined;
|
|
200
|
+
found.set(device.id, { ...device, ...(revokeAt ? { endpoint: revokeAt } : {}) });
|
|
201
|
+
}
|
|
202
|
+
return found;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Revoke one device, then remove it from this machine. Never throws: whatever
|
|
206
|
+
* fails is in the outcome, never the secret.
|
|
207
|
+
*
|
|
208
|
+
* @param ctx - Paths, environment, timeout, fetch and keychain runner.
|
|
209
|
+
* @param device - The device, with the endpoint to revoke it at.
|
|
210
|
+
* @returns What happened.
|
|
211
|
+
*/
|
|
212
|
+
async function settleDevice(ctx, device) {
|
|
213
|
+
const revocation = await revokeHeld(ctx, device).catch(() => ({ reason: 'revoking it failed' }));
|
|
214
|
+
const removed = await forgetSecret(ctx, device).then(() => true, (error) => {
|
|
215
|
+
debugLog('uninstall: removing a device credential failed:', error);
|
|
216
|
+
return false;
|
|
217
|
+
});
|
|
218
|
+
const reasons = [revocation.reason, removed ? undefined : 'it could not be removed from this machine'].filter(Boolean);
|
|
219
|
+
return {
|
|
220
|
+
revoked: revocation.reason === undefined,
|
|
221
|
+
removed,
|
|
222
|
+
...(reasons.length > 0 ? { reason: reasons.join('; ') } : {}),
|
|
223
|
+
...(removed ? {} : { leftover: secretLocation(device.store, ctx, device.id) })
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Ask the platform to switch one device off.
|
|
228
|
+
*
|
|
229
|
+
* @param ctx - Environment, timeout, fetch and keychain runner.
|
|
230
|
+
* @param device - The device, with the endpoint to revoke it at.
|
|
231
|
+
* @returns Nothing when revoked, else why not.
|
|
232
|
+
*/
|
|
233
|
+
async function revokeHeld(ctx, device) {
|
|
234
|
+
if (!device.endpoint)
|
|
235
|
+
return { reason: 'no endpoint is configured' };
|
|
236
|
+
const secret = await secretStoreFor(device.store, ctx).get(device.id).catch(() => undefined);
|
|
237
|
+
if (!secret)
|
|
238
|
+
return { reason: 'the secret could not be read' };
|
|
239
|
+
const result = await revokeDevice({ endpoint: device.endpoint, deviceSecret: secret, timeoutMs: ctx.timeoutMs, fetchFn: ctx.fetchFn });
|
|
240
|
+
if (result.ok)
|
|
241
|
+
return {};
|
|
242
|
+
return { reason: result.status === undefined ? 'the platform could not be reached' : `the platform answered ${result.status}` };
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Delete one device's secret and cached access token.
|
|
246
|
+
*
|
|
247
|
+
* @param ctx - Paths, environment, timeout and keychain runner.
|
|
248
|
+
* @param device - The device.
|
|
249
|
+
*/
|
|
250
|
+
async function forgetSecret(ctx, device) {
|
|
251
|
+
await secretStoreFor(device.store, ctx).remove(device.id);
|
|
252
|
+
await fs.rm(identityPaths(ctx.paths, identityOf({ device })).accessTokenFile, { force: true });
|
|
253
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { DeviceApiResult, EnrollInput, IssuedDeviceCredential, PollSessionInput, RefreshedAccessToken, RefreshInput, RevokeInput, StartedEnrollmentSession, StartSessionInput } from './types/credentials.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Trade the install key for a device credential of this machine's own.
|
|
4
|
+
*
|
|
5
|
+
* MIGRATION WINDOW ONLY, like the route it calls: AWT-275 replaces it with a
|
|
6
|
+
* sign-in. The developer is whatever this machine reports and the gateway
|
|
7
|
+
* records it as unverified.
|
|
8
|
+
*
|
|
9
|
+
* @param input - Gateway base URL, install key, who and what this machine is, timeout.
|
|
10
|
+
* @returns The credential, or why not.
|
|
11
|
+
*/
|
|
12
|
+
export declare function enrollDevice(input: EnrollInput): Promise<DeviceApiResult<IssuedDeviceCredential>>;
|
|
13
|
+
/**
|
|
14
|
+
* Open a browser sign-in session. Public on the gateway: the organization's id
|
|
15
|
+
* is all it takes, and no install key travels on this path.
|
|
16
|
+
*
|
|
17
|
+
* @param input - Gateway base URL, organization, installation, machine label, timeout.
|
|
18
|
+
* @returns The codes to show the developer, or why not.
|
|
19
|
+
*/
|
|
20
|
+
export declare function startEnrollmentSession(input: StartSessionInput): Promise<DeviceApiResult<StartedEnrollmentSession>>;
|
|
21
|
+
/**
|
|
22
|
+
* Ask once whether the developer has approved the session.
|
|
23
|
+
*
|
|
24
|
+
* `error` carries the RFC 8628 answer of a `400` (`authorization_pending`,
|
|
25
|
+
* `slow_down`, `access_denied`, `expired_token`); the caller decides which of
|
|
26
|
+
* those means keep waiting.
|
|
27
|
+
*
|
|
28
|
+
* @param input - Gateway base URL, device code, installation, timeout.
|
|
29
|
+
* @returns The device credential once approved, else why not.
|
|
30
|
+
*/
|
|
31
|
+
export declare function pollEnrollmentSession(input: PollSessionInput): Promise<DeviceApiResult<IssuedDeviceCredential> & {
|
|
32
|
+
readonly error?: string;
|
|
33
|
+
}>;
|
|
34
|
+
/**
|
|
35
|
+
* A new access token for the device secret.
|
|
36
|
+
*
|
|
37
|
+
* The secret travels in the body, never as a header a proxy might log, and no
|
|
38
|
+
* bearer is sent: the secret is the credential.
|
|
39
|
+
*
|
|
40
|
+
* @param input - Gateway base URL, device secret, timeout.
|
|
41
|
+
* @returns The token, or why not.
|
|
42
|
+
*/
|
|
43
|
+
export declare function refreshAccessToken(input: RefreshInput): Promise<DeviceApiResult<RefreshedAccessToken>>;
|
|
44
|
+
/**
|
|
45
|
+
* Switch this device's credential off on the platform.
|
|
46
|
+
*
|
|
47
|
+
* The gateway answers `204` for any well-formed secret, known or not, so a
|
|
48
|
+
* `204` is the only answer that means "no longer usable"; anything else, a
|
|
49
|
+
* `429` included, leaves the credential live and the caller says so.
|
|
50
|
+
*
|
|
51
|
+
* @param input - Gateway base URL, device secret, timeout.
|
|
52
|
+
* @returns ok on a `204`, else the status when there was one.
|
|
53
|
+
*/
|
|
54
|
+
export declare function revokeDevice(input: RevokeInput): Promise<DeviceApiResult<undefined>>;
|
|
55
|
+
/**
|
|
56
|
+
* The reason a `401` names, when it is one the edge acts on.
|
|
57
|
+
*
|
|
58
|
+
* The gateway's refusal body is `{ message: <reason> }`; anything else, or a
|
|
59
|
+
* body too large to be one, is no reason.
|
|
60
|
+
*
|
|
61
|
+
* @param response - A refusal.
|
|
62
|
+
* @returns The reason, or undefined.
|
|
63
|
+
*/
|
|
64
|
+
export declare function readRefusal(response: Response): Promise<string | undefined>;
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { joinUrl } from '../config/config.js';
|
|
2
|
+
import { asRecord } from '../core/object.js';
|
|
3
|
+
import { AUTHORIZATION_HEADER, CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE, USER_AGENT, USER_AGENT_HEADER } from '../transport/constants/transport.constants.js';
|
|
4
|
+
import { discardResponseBody, readCappedJson } from '../transport/response-body.js';
|
|
5
|
+
import { ENROLL_PATH, ENROLLMENT_SESSION_PATH, ENROLLMENT_SESSION_TOKEN_PATH, KNOWN_REFUSALS, REVOKE_PATH, TOKEN_PATH } from './constants/credentials.constants.js';
|
|
6
|
+
import { issuedDeviceCredentialSchema, pollErrorSchema, refreshedAccessTokenSchema, startedEnrollmentSessionSchema } from './schemas/device-api.schema.js';
|
|
7
|
+
/**
|
|
8
|
+
* Trade the install key for a device credential of this machine's own.
|
|
9
|
+
*
|
|
10
|
+
* MIGRATION WINDOW ONLY, like the route it calls: AWT-275 replaces it with a
|
|
11
|
+
* sign-in. The developer is whatever this machine reports and the gateway
|
|
12
|
+
* records it as unverified.
|
|
13
|
+
*
|
|
14
|
+
* @param input - Gateway base URL, install key, who and what this machine is, timeout.
|
|
15
|
+
* @returns The credential, or why not.
|
|
16
|
+
*/
|
|
17
|
+
export async function enrollDevice(input) {
|
|
18
|
+
return post(input, ENROLL_PATH, { developerId: input.developerId, name: input.name }, issuedDeviceCredentialSchema, input.installKey);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Open a browser sign-in session. Public on the gateway: the organization's id
|
|
22
|
+
* is all it takes, and no install key travels on this path.
|
|
23
|
+
*
|
|
24
|
+
* @param input - Gateway base URL, organization, installation, machine label, timeout.
|
|
25
|
+
* @returns The codes to show the developer, or why not.
|
|
26
|
+
*/
|
|
27
|
+
export async function startEnrollmentSession(input) {
|
|
28
|
+
const body = { organizationId: input.organizationId, installationId: input.installationId, name: input.name };
|
|
29
|
+
return post(input, ENROLLMENT_SESSION_PATH, body, startedEnrollmentSessionSchema);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Ask once whether the developer has approved the session.
|
|
33
|
+
*
|
|
34
|
+
* `error` carries the RFC 8628 answer of a `400` (`authorization_pending`,
|
|
35
|
+
* `slow_down`, `access_denied`, `expired_token`); the caller decides which of
|
|
36
|
+
* those means keep waiting.
|
|
37
|
+
*
|
|
38
|
+
* @param input - Gateway base URL, device code, installation, timeout.
|
|
39
|
+
* @returns The device credential once approved, else why not.
|
|
40
|
+
*/
|
|
41
|
+
export async function pollEnrollmentSession(input) {
|
|
42
|
+
try {
|
|
43
|
+
const response = await send(input, ENROLLMENT_SESSION_TOKEN_PATH, { deviceCode: input.deviceCode, installationId: input.installationId });
|
|
44
|
+
const json = await readCappedJson(response).catch(() => undefined);
|
|
45
|
+
if (response.ok) {
|
|
46
|
+
const parsed = issuedDeviceCredentialSchema.safeParse(json);
|
|
47
|
+
return parsed.success ? { ok: true, value: parsed.data } : { ok: false, status: response.status };
|
|
48
|
+
}
|
|
49
|
+
const failure = pollErrorSchema.safeParse(json);
|
|
50
|
+
return { ok: false, status: response.status, ...(failure.success ? { error: failure.data.error } : {}) };
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
// Never the error text: it can quote the request, and the request carries the device code.
|
|
54
|
+
return { ok: false };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A new access token for the device secret.
|
|
59
|
+
*
|
|
60
|
+
* The secret travels in the body, never as a header a proxy might log, and no
|
|
61
|
+
* bearer is sent: the secret is the credential.
|
|
62
|
+
*
|
|
63
|
+
* @param input - Gateway base URL, device secret, timeout.
|
|
64
|
+
* @returns The token, or why not.
|
|
65
|
+
*/
|
|
66
|
+
export async function refreshAccessToken(input) {
|
|
67
|
+
return post(input, TOKEN_PATH, { deviceSecret: input.deviceSecret }, refreshedAccessTokenSchema);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Switch this device's credential off on the platform.
|
|
71
|
+
*
|
|
72
|
+
* The gateway answers `204` for any well-formed secret, known or not, so a
|
|
73
|
+
* `204` is the only answer that means "no longer usable"; anything else, a
|
|
74
|
+
* `429` included, leaves the credential live and the caller says so.
|
|
75
|
+
*
|
|
76
|
+
* @param input - Gateway base URL, device secret, timeout.
|
|
77
|
+
* @returns ok on a `204`, else the status when there was one.
|
|
78
|
+
*/
|
|
79
|
+
export async function revokeDevice(input) {
|
|
80
|
+
try {
|
|
81
|
+
const response = await send(input, REVOKE_PATH, { deviceSecret: input.deviceSecret });
|
|
82
|
+
discardResponseBody(response);
|
|
83
|
+
return response.status === 204 ? { ok: true, value: undefined } : { ok: false, status: response.status };
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Never the error text: it can quote the request, and the request carries a secret.
|
|
87
|
+
return { ok: false };
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The reason a `401` names, when it is one the edge acts on.
|
|
92
|
+
*
|
|
93
|
+
* The gateway's refusal body is `{ message: <reason> }`; anything else, or a
|
|
94
|
+
* body too large to be one, is no reason.
|
|
95
|
+
*
|
|
96
|
+
* @param response - A refusal.
|
|
97
|
+
* @returns The reason, or undefined.
|
|
98
|
+
*/
|
|
99
|
+
export async function readRefusal(response) {
|
|
100
|
+
try {
|
|
101
|
+
const message = asRecord(await readCappedJson(response))?.['message'];
|
|
102
|
+
return typeof message === 'string' && KNOWN_REFUSALS.has(message) ? message : undefined;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function post(input, route, body, schema, bearer) {
|
|
109
|
+
try {
|
|
110
|
+
const response = await send(input, route, body, bearer);
|
|
111
|
+
if (response.status === 401) {
|
|
112
|
+
const refusal = await readRefusal(response);
|
|
113
|
+
return { ok: false, status: response.status, ...(refusal ? { refusal } : {}) };
|
|
114
|
+
}
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
discardResponseBody(response);
|
|
117
|
+
return { ok: false, status: response.status };
|
|
118
|
+
}
|
|
119
|
+
const parsed = schema.safeParse(await readCappedJson(response).catch(() => undefined));
|
|
120
|
+
return parsed.success ? { ok: true, value: parsed.data } : { ok: false, status: response.status };
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
// Never the error text: it can quote the request, and the request carries a secret.
|
|
124
|
+
return { ok: false };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* POST a JSON body to one device route. The one place the request is built, so
|
|
129
|
+
* every route refuses redirects and keeps to its timeout the same way.
|
|
130
|
+
*
|
|
131
|
+
* @param input - Gateway base URL, timeout, fetch.
|
|
132
|
+
* @param route - The route path.
|
|
133
|
+
* @param body - The JSON body; it may carry a secret.
|
|
134
|
+
* @param bearer - Sent as `Authorization: Bearer`, when given.
|
|
135
|
+
* @returns The response; throws on a network failure or timeout.
|
|
136
|
+
*/
|
|
137
|
+
function send(input, route, body, bearer) {
|
|
138
|
+
return (input.fetchFn ?? fetch)(joinUrl(input.endpoint, route), {
|
|
139
|
+
method: 'POST',
|
|
140
|
+
headers: {
|
|
141
|
+
[CONTENT_TYPE_HEADER]: JSON_CONTENT_TYPE,
|
|
142
|
+
[USER_AGENT_HEADER]: USER_AGENT,
|
|
143
|
+
...(bearer ? { [AUTHORIZATION_HEADER]: `Bearer ${bearer}` } : {})
|
|
144
|
+
},
|
|
145
|
+
body: JSON.stringify(body),
|
|
146
|
+
// A redirect would carry a credential somewhere nothing configured.
|
|
147
|
+
redirect: 'error',
|
|
148
|
+
signal: AbortSignal.timeout(Math.max(1, Math.floor(input.timeoutMs)))
|
|
149
|
+
});
|
|
150
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { AccessTokenCache, DeviceState } from './types/credentials.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The cached access token, when there is a readable one.
|
|
4
|
+
*
|
|
5
|
+
* @param file - The identity's access token file.
|
|
6
|
+
* @returns The cache entry, or undefined.
|
|
7
|
+
*/
|
|
8
|
+
export declare function readAccessToken(file: string): Promise<AccessTokenCache | undefined>;
|
|
9
|
+
/**
|
|
10
|
+
* Keep the access token for every process on this machine. 0600: it is a
|
|
11
|
+
* bearer for up to an hour (R8 allows that on disk; the device secret never is).
|
|
12
|
+
*
|
|
13
|
+
* @param file - The identity's access token file.
|
|
14
|
+
* @param cache - The entry.
|
|
15
|
+
*/
|
|
16
|
+
export declare function writeAccessToken(file: string, cache: AccessTokenCache): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* What this identity remembers about its device credential.
|
|
19
|
+
*
|
|
20
|
+
* @param file - The identity's device state file.
|
|
21
|
+
* @returns The state; empty when there is none.
|
|
22
|
+
*/
|
|
23
|
+
export declare function readDeviceState(file: string): Promise<DeviceState>;
|
|
24
|
+
/**
|
|
25
|
+
* Persist the device state. Never throws: it runs on the hook path, and a
|
|
26
|
+
* diagnosis that cannot be written must not fail the agent's turn.
|
|
27
|
+
*
|
|
28
|
+
* @param file - The identity's device state file.
|
|
29
|
+
* @param state - The state.
|
|
30
|
+
*/
|
|
31
|
+
export declare function writeDeviceState(file: string, state: DeviceState): Promise<void>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { readJsonFile } from '../storage/json-file.js';
|
|
2
|
+
import { writeFileAtomic } from '../storage/atomic-file.js';
|
|
3
|
+
import { SECRET_FILE_MODE } from '../storage/constants/storage.constants.js';
|
|
4
|
+
import { accessTokenCacheSchema, deviceStateSchema } from './schemas/device-state.schema.js';
|
|
5
|
+
/**
|
|
6
|
+
* The cached access token, when there is a readable one.
|
|
7
|
+
*
|
|
8
|
+
* @param file - The identity's access token file.
|
|
9
|
+
* @returns The cache entry, or undefined.
|
|
10
|
+
*/
|
|
11
|
+
export async function readAccessToken(file) {
|
|
12
|
+
const read = await readJsonFile(file);
|
|
13
|
+
const parsed = read.state === 'ok' ? accessTokenCacheSchema.safeParse(read.value) : undefined;
|
|
14
|
+
return parsed?.success ? parsed.data : undefined;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Keep the access token for every process on this machine. 0600: it is a
|
|
18
|
+
* bearer for up to an hour (R8 allows that on disk; the device secret never is).
|
|
19
|
+
*
|
|
20
|
+
* @param file - The identity's access token file.
|
|
21
|
+
* @param cache - The entry.
|
|
22
|
+
*/
|
|
23
|
+
export async function writeAccessToken(file, cache) {
|
|
24
|
+
await writeFileAtomic(file, JSON.stringify(cache), SECRET_FILE_MODE);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* What this identity remembers about its device credential.
|
|
28
|
+
*
|
|
29
|
+
* @param file - The identity's device state file.
|
|
30
|
+
* @returns The state; empty when there is none.
|
|
31
|
+
*/
|
|
32
|
+
export async function readDeviceState(file) {
|
|
33
|
+
const read = await readJsonFile(file);
|
|
34
|
+
const parsed = read.state === 'ok' ? deviceStateSchema.safeParse(read.value) : undefined;
|
|
35
|
+
return parsed?.success ? parsed.data : {};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Persist the device state. Never throws: it runs on the hook path, and a
|
|
39
|
+
* diagnosis that cannot be written must not fail the agent's turn.
|
|
40
|
+
*
|
|
41
|
+
* @param file - The identity's device state file.
|
|
42
|
+
* @param state - The state.
|
|
43
|
+
*/
|
|
44
|
+
export async function writeDeviceState(file, state) {
|
|
45
|
+
try {
|
|
46
|
+
await writeFileAtomic(file, JSON.stringify(state), SECRET_FILE_MODE);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// See above.
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { CredentialFields, DeviceCredentialRef } from './types/credentials.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Who a config sends as, for naming its queue partition and state files.
|
|
4
|
+
*
|
|
5
|
+
* An install key wins while there is one: a fresh `setup --token` on a migrated
|
|
6
|
+
* machine is a new identity until it too is exchanged. Never a bearer to send —
|
|
7
|
+
* see `resolveBearer` for that.
|
|
8
|
+
*
|
|
9
|
+
* @param config - The credential fields of an effective config.
|
|
10
|
+
* @returns The install key, `device:<id>`, or undefined when there is neither.
|
|
11
|
+
*/
|
|
12
|
+
export declare function identityOf(config: CredentialFields): string | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* Whether a config holds any credential at all.
|
|
15
|
+
*
|
|
16
|
+
* @param config - The credential fields of an effective config.
|
|
17
|
+
* @returns True for an install key or a device credential.
|
|
18
|
+
*/
|
|
19
|
+
export declare function hasCredential(config: CredentialFields): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The device id an install key's credential is filed under.
|
|
22
|
+
*
|
|
23
|
+
* The same digest that already names the key's partition, so nothing moves
|
|
24
|
+
* when the key is exchanged.
|
|
25
|
+
*
|
|
26
|
+
* @param installKey - The install key being exchanged.
|
|
27
|
+
* @returns Twelve hex characters.
|
|
28
|
+
*/
|
|
29
|
+
export declare function credentialId(installKey: string): string;
|
|
30
|
+
/**
|
|
31
|
+
* The digest naming an identity on disk; never the identity itself, which may
|
|
32
|
+
* be a bearer.
|
|
33
|
+
*
|
|
34
|
+
* @param identity - What `identityOf` returned.
|
|
35
|
+
* @returns The fingerprint.
|
|
36
|
+
*/
|
|
37
|
+
export declare function identityFingerprint(identity: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* The device credential that stands for this config's identity, if any.
|
|
40
|
+
*
|
|
41
|
+
* The install key stays beside its device in the config through the migration
|
|
42
|
+
* window, because Codex and Gemini still present it (AWT-357). A device whose
|
|
43
|
+
* id is not that key's is a leftover from an earlier key and stands for
|
|
44
|
+
* nothing.
|
|
45
|
+
*
|
|
46
|
+
* @param config - The credential fields of an effective config.
|
|
47
|
+
* @returns The device, or undefined when the config sends as an install key or has nothing.
|
|
48
|
+
*/
|
|
49
|
+
export declare function deviceFor(config: CredentialFields): DeviceCredentialRef | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Every device reference a config file holds: the machine entry's and each
|
|
52
|
+
* root's, in that order, repeats included.
|
|
53
|
+
*
|
|
54
|
+
* The one place that walks the file for devices, so "is this id still used"
|
|
55
|
+
* has one answer for the exchange, setup and uninstall.
|
|
56
|
+
*
|
|
57
|
+
* @param config - The machine's global config, roots included.
|
|
58
|
+
* @returns The references.
|
|
59
|
+
*/
|
|
60
|
+
export declare function deviceRefs(config: CredentialFields & {
|
|
61
|
+
readonly roots?: Readonly<Record<string, CredentialFields>>;
|
|
62
|
+
}): readonly DeviceCredentialRef[];
|