@astrale-os/cli 0.8.0-alpha.0 → 0.8.1-alpha.2
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 +4 -1
- package/dist/astrale.js +62968 -41240
- package/dist/public/connect-core.js +55030 -0
- package/dist/public/keys/index.js +45631 -0
- package/dist/public/paths/index.js +43990 -0
- package/dist/types/admin/instance/model.d.ts +40 -0
- package/dist/types/connect-core.d.ts +22 -0
- package/dist/types/connection/auth.d.ts +33 -0
- package/dist/types/errors.d.ts +23 -0
- package/dist/types/identity/registry.d.ts +25 -0
- package/dist/types/keys/algorithm.d.ts +6 -0
- package/dist/types/keys/credential.d.ts +21 -0
- package/dist/types/keys/index.d.ts +4 -0
- package/dist/types/keys/pair.d.ts +34 -0
- package/dist/types/lib/admin-target.d.ts +74 -0
- package/dist/types/lib/ca-fetch.d.ts +2 -0
- package/dist/types/lib/config.d.ts +71 -0
- package/dist/types/lib/format.d.ts +1 -0
- package/dist/types/lib/idp-session.d.ts +33 -0
- package/dist/types/lib/idp.d.ts +397 -0
- package/dist/types/lib/instance-target.d.ts +36 -0
- package/dist/types/lib/instance.d.ts +186 -0
- package/dist/types/lib/log.d.ts +30 -0
- package/dist/types/lib/login-flow.d.ts +49 -0
- package/dist/types/lib/meta.d.ts +36 -0
- package/dist/types/lib/output.d.ts +83 -0
- package/dist/types/lib/table.d.ts +18 -0
- package/dist/types/lib/validation.d.ts +11 -0
- package/dist/types/paths/index.d.ts +2 -0
- package/dist/types/state/exchange-credentials.d.ts +25 -0
- package/dist/types/state/files.d.ts +9 -0
- package/dist/types/state/identities.d.ts +42 -0
- package/dist/types/state/index.d.ts +8 -0
- package/dist/types/state/paths.d.ts +32 -0
- package/package.json +30 -8
- package/src/admin/.spec/api.d.ts +3 -0
- package/src/admin/.spec/architecture.md +15 -0
- package/src/admin/.spec/icon.svg +3 -0
- package/src/admin/.spec/layout.ts +6 -0
- package/src/admin/catalog/.spec/api.d.ts +65 -0
- package/src/admin/catalog/.spec/architecture.md +6 -0
- package/src/admin/catalog/.spec/icon.svg +3 -0
- package/src/admin/catalog/.spec/layout.ts +6 -0
- package/src/admin/catalog/__tests__/client.test.ts +171 -0
- package/src/admin/catalog/client.ts +214 -0
- package/src/admin/catalog/index.ts +13 -0
- package/src/admin/catalog/model.ts +42 -0
- package/src/admin/catalog/tsconfig.json +8 -0
- package/src/admin/graph/.spec/api.d.ts +28 -0
- package/src/admin/graph/.spec/architecture.md +5 -0
- package/src/admin/graph/.spec/icon.svg +3 -0
- package/src/admin/graph/.spec/layout.ts +3 -0
- package/src/admin/graph/index.ts +6 -0
- package/src/admin/graph/nodes.ts +56 -0
- package/src/admin/index.ts +2 -0
- package/src/admin/instance/.spec/api.d.ts +66 -0
- package/src/admin/instance/.spec/architecture.md +10 -0
- package/src/admin/instance/.spec/icon.svg +3 -0
- package/src/admin/instance/.spec/layout.ts +6 -0
- package/src/admin/instance/__tests__/client.test.ts +289 -0
- package/src/admin/instance/client.ts +298 -0
- package/src/admin/instance/index.ts +16 -0
- package/src/admin/instance/model.ts +65 -0
- package/src/admin/instance/tsconfig.json +8 -0
- package/src/commands/__tests__/admin-instance.test.ts +3 -36
- package/src/commands/__tests__/call.test.ts +21 -68
- package/src/commands/__tests__/domain-install-owned.test.ts +26 -27
- package/src/commands/__tests__/domain-list.test.ts +2 -6
- package/src/commands/__tests__/logs.test.ts +50 -98
- package/src/commands/__tests__/ls.test.ts +21 -17
- package/src/commands/__tests__/read-commands.test.ts +171 -101
- package/src/commands/__tests__/view.test.ts +100 -51
- package/src/commands/admin/status.ts +3 -3
- package/src/commands/admin/use.ts +29 -6
- package/src/commands/auth/login.ts +1 -1
- package/src/commands/auth/logout.ts +4 -2
- package/src/commands/auth/status.ts +2 -2
- package/src/commands/auth/token.ts +2 -2
- package/src/commands/browser.ts +1 -1
- package/src/commands/call.ts +55 -91
- package/src/commands/describe.ts +52 -147
- package/src/commands/domain/install.ts +81 -50
- package/src/commands/domain/list.ts +4 -8
- package/src/commands/domain/publish.ts +9 -31
- package/src/commands/get.ts +32 -65
- package/src/commands/identity/__tests__/register.test.ts +93 -0
- package/src/commands/identity/create.ts +2 -2
- package/src/commands/identity/delete.ts +2 -2
- package/src/commands/identity/export.ts +7 -33
- package/src/commands/identity/import.ts +21 -70
- package/src/commands/identity/list.ts +2 -2
- package/src/commands/identity/register.ts +148 -117
- package/src/commands/identity/sync.ts +2 -2
- package/src/commands/identity/unsync.ts +2 -2
- package/src/commands/identity/use.ts +2 -2
- package/src/commands/identity/whoami.ts +2 -2
- package/src/commands/idp/add.ts +1 -1
- package/src/commands/idp/list.ts +1 -1
- package/src/commands/idp/refresh.ts +1 -1
- package/src/commands/idp/remove.ts +2 -2
- package/src/commands/idp/show.ts +1 -1
- package/src/commands/instance/active.ts +2 -2
- package/src/commands/instance/bookmark.ts +8 -2
- package/src/commands/instance/create.ts +1 -1
- package/src/commands/instance/delete.ts +4 -10
- package/src/commands/instance/forget.ts +1 -1
- package/src/commands/instance/list.ts +3 -3
- package/src/commands/instance/status.ts +5 -7
- package/src/commands/instance/use.ts +3 -3
- package/src/commands/logs.ts +152 -279
- package/src/commands/ls.ts +78 -204
- package/src/commands/mutate.ts +70 -139
- package/src/commands/query.ts +100 -278
- package/src/commands/session/analyze.ts +1 -1
- package/src/commands/session/list.ts +1 -1
- package/src/commands/setup.ts +1 -1
- package/src/commands/status.ts +1 -1
- package/src/commands/studio.ts +1 -1
- package/src/commands/token.ts +26 -21
- package/src/commands/update.ts +1 -1
- package/src/commands/use.ts +2 -3
- package/src/commands/view-serve.ts +1 -1
- package/src/commands/view.ts +67 -109
- package/src/connect-core.test.ts +38 -0
- package/src/connect-core.ts +42 -0
- package/src/connection/.spec/api.d.ts +111 -0
- package/src/connection/.spec/architecture.md +41 -0
- package/src/connection/.spec/capabilities/connection.ts +13 -0
- package/src/connection/.spec/examples/call.ts +7 -0
- package/src/connection/.spec/flows/command.ts +20 -0
- package/src/connection/.spec/flows/session.ts +61 -0
- package/src/connection/.spec/laws/connection.ts +181 -0
- package/src/connection/.spec/layout.ts +19 -0
- package/src/{kernel → connection}/__tests__/auth.test.ts +26 -2
- package/src/connection/__tests__/ca-fetch.test.ts +53 -0
- package/src/connection/__tests__/call.test.ts +17 -0
- package/src/connection/__tests__/credential.test.ts +109 -0
- package/src/connection/__tests__/errors.test.ts +96 -0
- package/src/connection/__tests__/exchange.test.ts +224 -0
- package/src/connection/__tests__/fixtures/localhost-cert.pem +19 -0
- package/src/connection/__tests__/fixtures/localhost-key.base64 +1 -0
- package/src/connection/__tests__/managed.test.ts +39 -0
- package/src/connection/__tests__/self.test.ts +51 -0
- package/src/connection/__tests__/session.test.ts +140 -0
- package/src/connection/__tests__/target.test.ts +93 -0
- package/src/{kernel → connection}/auth.ts +8 -8
- package/src/connection/call.ts +7 -0
- package/src/{kernel/run.ts → connection/command.ts} +22 -13
- package/src/connection/credential.ts +114 -0
- package/src/{kernel → connection}/errors.ts +32 -9
- package/src/connection/exchange.ts +274 -0
- package/src/connection/index.ts +8 -0
- package/src/connection/self.ts +120 -0
- package/src/connection/session.ts +176 -0
- package/src/connection/target.ts +111 -0
- package/src/connection/tsconfig.json +6 -0
- package/src/graph/.spec/api.d.ts +34 -0
- package/src/graph/.spec/laws/documents.ts +24 -0
- package/src/graph/.spec/laws/projection.ts +13 -0
- package/src/graph/.spec/layout.ts +6 -0
- package/src/graph/__tests__/mutation.test.ts +43 -0
- package/src/graph/__tests__/projection.test.ts +22 -0
- package/src/graph/__tests__/query.test.ts +154 -0
- package/src/graph/index.ts +3 -0
- package/src/graph/mutation.ts +18 -0
- package/src/graph/projection.ts +21 -0
- package/src/graph/query.ts +108 -0
- package/src/graph/tsconfig.json +6 -0
- package/src/identity/.history/ard/bootstrap-root-transfer.md +57 -0
- package/src/identity/.history/ard/domain-mediated-registration.md +36 -0
- package/src/identity/.spec/api.d.ts +124 -0
- package/src/identity/.spec/architecture.md +33 -0
- package/src/identity/.spec/capabilities/identity.ts +7 -0
- package/src/identity/.spec/flows/bootstrap-root.ts +79 -0
- package/src/identity/.spec/flows/domain-mediated-registration.ts +51 -0
- package/src/identity/.spec/flows/import.ts +29 -0
- package/src/identity/.spec/laws/identity.ts +56 -0
- package/src/identity/.spec/layout.ts +13 -0
- package/src/identity/.spec/schemas/identity-export-v1.schema.json +25 -0
- package/src/identity/__tests__/fixtures/registry-journey.ts +53 -0
- package/src/identity/__tests__/registration.test.ts +64 -0
- package/src/identity/__tests__/registry.test.ts +70 -0
- package/src/identity/__tests__/transfer.test.ts +198 -0
- package/src/identity/index.ts +23 -0
- package/src/identity/registration.ts +72 -0
- package/src/identity/registry.ts +172 -0
- package/src/identity/transfer.ts +187 -0
- package/src/identity/tsconfig.json +6 -0
- package/src/index.ts +1 -0
- package/src/keys/.spec/api.d.ts +74 -0
- package/src/keys/.spec/architecture.md +13 -0
- package/src/keys/.spec/laws/keys.ts +43 -0
- package/src/keys/.spec/layout.ts +6 -0
- package/src/keys/__tests__/algorithm.test.ts +39 -0
- package/src/keys/__tests__/keys.test.ts +168 -0
- package/src/keys/algorithm.ts +33 -0
- package/src/keys/credential.ts +112 -0
- package/src/keys/index.ts +14 -0
- package/src/keys/pair.ts +219 -0
- package/src/keys/tsconfig.json +6 -0
- package/src/lib/__tests__/admin-target.test.ts +67 -7
- package/src/lib/__tests__/binary.test.ts +2 -3
- package/src/lib/__tests__/command-dx.test.ts +2 -2
- package/src/lib/__tests__/config.test.ts +3 -2
- package/src/lib/__tests__/idp.test.ts +4 -6
- package/src/lib/__tests__/instance-target.test.ts +7 -4
- package/src/lib/__tests__/local-status.test.ts +1 -1
- package/src/lib/__tests__/log.test.ts +31 -0
- package/src/lib/__tests__/meta.test.ts +55 -0
- package/src/lib/__tests__/sdk-deps.test.ts +1 -1
- package/src/lib/__tests__/self.test.ts +8 -230
- package/src/lib/__tests__/use-target.test.ts +1 -1
- package/src/lib/__tests__/view-open-intent.test.ts +78 -104
- package/src/lib/__tests__/view-session.test.ts +99 -0
- package/src/lib/admin-domain.ts +39 -25
- package/src/lib/admin-instance.ts +38 -43
- package/src/lib/admin-target.ts +67 -13
- package/src/lib/binary.ts +13 -34
- package/src/lib/browser.ts +1 -1
- package/src/{kernel → lib}/ca-fetch.ts +35 -27
- package/src/lib/config.ts +1 -1
- package/src/lib/idp-session.ts +1 -1
- package/src/lib/idp.ts +3 -8
- package/src/lib/instance-target.ts +12 -8
- package/src/lib/instance.ts +34 -2
- package/src/lib/local-status.ts +2 -2
- package/src/lib/log.ts +3 -1
- package/src/lib/login-flow.ts +32 -5
- package/src/lib/meta.ts +15 -11
- package/src/lib/provision-instance.ts +4 -12
- package/src/lib/self.ts +5 -147
- package/src/lib/update.ts +1 -1
- package/src/lib/use-target.ts +1 -1
- package/src/lib/validation.ts +2 -2
- package/src/lib/view/open-intent.ts +7 -37
- package/src/lib/view/port-allocation.ts +1 -1
- package/src/lib/view/resolve.ts +52 -45
- package/src/lib/view/server.ts +16 -33
- package/src/lib/view/session.ts +51 -22
- package/src/paths/.spec/api.d.ts +13 -0
- package/src/paths/.spec/architecture.md +5 -0
- package/src/paths/.spec/layout.ts +3 -0
- package/src/paths/index.ts +13 -0
- package/src/program/.spec/api.d.ts +42 -0
- package/src/program/.spec/architecture.md +18 -0
- package/src/program/.spec/laws/program.ts +29 -0
- package/src/program/.spec/layout.ts +14 -0
- package/src/program/__tests__/program.test.ts +359 -0
- package/src/program/build.ts +190 -0
- package/src/program/command.ts +42 -0
- package/src/program/index.ts +2 -0
- package/src/program/options.ts +40 -0
- package/src/{registry.ts → program/registry.ts} +5 -1
- package/src/program/tsconfig.json +6 -0
- package/src/setup/steps/admin.ts +18 -3
- package/src/setup/steps/instance.ts +1 -1
- package/src/state/.spec/api.d.ts +133 -0
- package/src/state/.spec/architecture.md +31 -0
- package/src/state/.spec/capabilities/state.ts +7 -0
- package/src/state/.spec/flows/identity-update.ts +22 -0
- package/src/state/.spec/laws/state.ts +73 -0
- package/src/state/.spec/layout.ts +14 -0
- package/src/state/__tests__/exchange-credentials.test.ts +188 -0
- package/src/state/__tests__/files.test.ts +113 -0
- package/src/state/__tests__/fixtures/identity-transition.ts +37 -0
- package/src/state/__tests__/identities.test.ts +142 -0
- package/src/state/__tests__/paths.test.ts +27 -0
- package/src/state/exchange-credentials.ts +219 -0
- package/src/state/files.ts +129 -0
- package/src/state/identities.ts +236 -0
- package/src/state/index.ts +29 -0
- package/src/state/paths.ts +61 -0
- package/src/state/tsconfig.json +6 -0
- package/src/telemetry/__tests__/redact.test.ts +3 -3
- package/src/telemetry/__tests__/trigger.test.ts +1 -1
- package/src/telemetry/settings.ts +1 -1
- package/src/telemetry/store.ts +1 -1
- package/src/test-utils.ts +1 -1
- package/studio/client/dist/assets/elk-api-D0cBetPW.js +1 -0
- package/studio/client/dist/assets/elk-worker.min-C9JGDOE-.js +6312 -0
- package/studio/client/dist/assets/index-BQnJ5sgd.css +1 -0
- package/studio/client/dist/assets/index-ChOr3yP0.css +1 -0
- package/studio/client/dist/assets/index-Dspir4w7.js +81 -0
- package/studio/client/dist/assets/index-bVD2KJgz.js +8 -0
- package/studio/client/dist/index.html +2 -2
- package/studio/package.json +10 -1
- package/studio/server/introspect/extractor.ts +1 -1
- package/studio/server/state/views.test.ts +3 -7
- package/studio/server/state/views.ts +23 -30
- package/studio/tsconfig.json +23 -0
- package/tsconfig.json +1 -1
- package/viewer/dist/main.js +65 -39
- package/viewer/tsconfig.json +13 -0
- package/src/command.ts +0 -42
- package/src/commands/__tests__/help-contract.test.ts +0 -164
- package/src/kernel/__tests__/client-owned-lookup.test.ts +0 -51
- package/src/kernel/__tests__/errors.test.ts +0 -43
- package/src/kernel/__tests__/expand.test.ts +0 -123
- package/src/kernel/client.ts +0 -214
- package/src/kernel/expand.ts +0 -192
- package/src/kernel/graph.ts +0 -96
- package/src/kernel/index.ts +0 -29
- package/src/kernel/options.ts +0 -22
- package/src/kernel/types.ts +0 -14
- package/src/lib/__tests__/domain-identity.test.ts +0 -60
- package/src/lib/__tests__/fs-atomic.test.ts +0 -104
- package/src/lib/__tests__/identity.test.ts +0 -79
- package/src/lib/__tests__/keys.test.ts +0 -129
- package/src/lib/domain-identity.ts +0 -49
- package/src/lib/env.ts +0 -49
- package/src/lib/fs-atomic.ts +0 -126
- package/src/lib/identity.ts +0 -256
- package/src/lib/keys.ts +0 -294
- package/src/lib/paths.ts +0 -11
- package/src/program.ts +0 -213
- package/studio/client/dist/assets/index-BaqEuIQJ.js +0 -109
- package/studio/client/dist/assets/index-jRdExahh.css +0 -1
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { compactDecrypt, CompactEncrypt, type JWK } from 'jose'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
|
|
4
|
+
import { AstraleError } from '../errors'
|
|
5
|
+
import { acceptKeypair, importKeypair, readKeypair } from '../keys/index'
|
|
6
|
+
import { validateName, validateUrl } from '../lib/validation'
|
|
7
|
+
import {
|
|
8
|
+
atomicWrite,
|
|
9
|
+
readIdentityStore,
|
|
10
|
+
updateIdentityStore,
|
|
11
|
+
type Identity,
|
|
12
|
+
type IdentityMode,
|
|
13
|
+
type IdentityStoreOptions,
|
|
14
|
+
} from '../state/index'
|
|
15
|
+
|
|
16
|
+
const IDENTITY_EXPORT_VERSION = 1 as const
|
|
17
|
+
const JwkSchema = z.record(z.string(), z.unknown())
|
|
18
|
+
const IdentityExportFields = {
|
|
19
|
+
subject: z.string().min(1),
|
|
20
|
+
mode: z.enum(['local', 'remote']).optional(),
|
|
21
|
+
kid: z.string().min(1).optional(),
|
|
22
|
+
issuer: z.string().url().optional(),
|
|
23
|
+
privateJwk: JwkSchema,
|
|
24
|
+
publicJwk: JwkSchema,
|
|
25
|
+
}
|
|
26
|
+
const LegacyIdentityExportSchema = z.object(IdentityExportFields).strict()
|
|
27
|
+
const IdentityExportV1Schema = z
|
|
28
|
+
.object({ version: z.literal(IDENTITY_EXPORT_VERSION), ...IdentityExportFields })
|
|
29
|
+
.strict()
|
|
30
|
+
|
|
31
|
+
export interface IdentityFileOptions {
|
|
32
|
+
readonly state?: IdentityStoreOptions
|
|
33
|
+
readonly keysDir?: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface IdentityExport {
|
|
37
|
+
readonly version: 1
|
|
38
|
+
readonly subject: string
|
|
39
|
+
readonly mode: IdentityMode
|
|
40
|
+
readonly kid?: string
|
|
41
|
+
readonly issuer?: string
|
|
42
|
+
readonly privateJwk: JWK
|
|
43
|
+
readonly publicJwk: JWK
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface IdentityImportOptions extends IdentityFileOptions {
|
|
47
|
+
readonly name?: string
|
|
48
|
+
readonly issuer?: string
|
|
49
|
+
readonly replace?: boolean
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function isEncryptedIdentityExport(raw: string): boolean {
|
|
53
|
+
const content = raw.trim()
|
|
54
|
+
return !content.startsWith('{') && content.split('.').length === 5
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function decodeIdentityExport(
|
|
58
|
+
raw: string,
|
|
59
|
+
passphrase?: string,
|
|
60
|
+
): Promise<IdentityExport> {
|
|
61
|
+
let json = raw
|
|
62
|
+
if (isEncryptedIdentityExport(raw)) {
|
|
63
|
+
if (passphrase === undefined) {
|
|
64
|
+
throw invalidExport('This encrypted identity export requires a passphrase.')
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const { plaintext } = await compactDecrypt(raw.trim(), new TextEncoder().encode(passphrase), {
|
|
68
|
+
keyManagementAlgorithms: ['PBES2-HS256+A128KW'],
|
|
69
|
+
contentEncryptionAlgorithms: ['A256GCM'],
|
|
70
|
+
})
|
|
71
|
+
json = new TextDecoder().decode(plaintext)
|
|
72
|
+
} catch {
|
|
73
|
+
throw invalidExport('Could not decrypt the identity export.')
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let input: unknown
|
|
78
|
+
try {
|
|
79
|
+
input = JSON.parse(json)
|
|
80
|
+
} catch {
|
|
81
|
+
throw invalidExport('The identity export is not valid JSON.')
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let decoded: z.infer<typeof LegacyIdentityExportSchema>
|
|
85
|
+
try {
|
|
86
|
+
decoded = hasVersion(input)
|
|
87
|
+
? IdentityExportV1Schema.parse(input)
|
|
88
|
+
: LegacyIdentityExportSchema.parse(input)
|
|
89
|
+
} catch {
|
|
90
|
+
throw invalidExport('The identity export has an invalid or unsupported shape.')
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const pair = await acceptKeypair({
|
|
94
|
+
privateJwk: decoded.privateJwk,
|
|
95
|
+
publicJwk: decoded.publicJwk,
|
|
96
|
+
})
|
|
97
|
+
if (decoded.kid && pair.kid && decoded.kid !== pair.kid) {
|
|
98
|
+
throw invalidExport('The envelope key ID differs from its JWK key ID.')
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
version: IDENTITY_EXPORT_VERSION,
|
|
103
|
+
subject: decoded.subject,
|
|
104
|
+
mode: decoded.mode ?? 'local',
|
|
105
|
+
kid: decoded.kid ?? pair.kid,
|
|
106
|
+
issuer: decoded.issuer,
|
|
107
|
+
privateJwk: pair.privateJwk,
|
|
108
|
+
publicJwk: pair.publicJwk,
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export async function encodeIdentityExport(
|
|
113
|
+
envelope: IdentityExport,
|
|
114
|
+
passphrase?: string,
|
|
115
|
+
): Promise<string> {
|
|
116
|
+
const plaintext = JSON.stringify(envelope, null, 2)
|
|
117
|
+
if (passphrase === undefined) return plaintext
|
|
118
|
+
return new CompactEncrypt(new TextEncoder().encode(plaintext))
|
|
119
|
+
.setProtectedHeader({ alg: 'PBES2-HS256+A128KW', enc: 'A256GCM' })
|
|
120
|
+
.encrypt(new TextEncoder().encode(passphrase))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export async function exportIdentity(
|
|
124
|
+
name: string,
|
|
125
|
+
options: IdentityFileOptions = {},
|
|
126
|
+
): Promise<IdentityExport> {
|
|
127
|
+
const store = await readIdentityStore(options.state)
|
|
128
|
+
const identity = store.identities[name]
|
|
129
|
+
if (!identity)
|
|
130
|
+
throw new Error(`Identity "${name}" not found. Run: astrale identity create ${name}`)
|
|
131
|
+
if ((identity.source ?? 'key') !== 'key') {
|
|
132
|
+
throw new Error(`Identity "${name}" is IdP-backed and has no transferable local keypair`)
|
|
133
|
+
}
|
|
134
|
+
const pair = await readKeypair(identity.subject, options.keysDir)
|
|
135
|
+
return {
|
|
136
|
+
version: IDENTITY_EXPORT_VERSION,
|
|
137
|
+
subject: identity.subject,
|
|
138
|
+
mode: identity.mode ?? 'local',
|
|
139
|
+
kid: identity.kid ?? pair.kid,
|
|
140
|
+
issuer: identity.issuer,
|
|
141
|
+
privateJwk: pair.privateJwk,
|
|
142
|
+
publicJwk: pair.publicJwk,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export async function importIdentity(
|
|
147
|
+
envelope: IdentityExport,
|
|
148
|
+
options: IdentityImportOptions = {},
|
|
149
|
+
): Promise<Identity> {
|
|
150
|
+
const name = options.name ?? envelope.subject
|
|
151
|
+
validateName(name, 'Identity')
|
|
152
|
+
if (options.issuer !== undefined) validateUrl(options.issuer)
|
|
153
|
+
|
|
154
|
+
return updateIdentityStore(async (store) => {
|
|
155
|
+
const existing = store.identities[name]
|
|
156
|
+
if (existing && !options.replace) throw new Error(`Identity "${name}" already exists`)
|
|
157
|
+
if (existing && (existing.source ?? 'key') !== 'key') {
|
|
158
|
+
throw new Error(`Identity "${name}" already exists and is IdP-backed`)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const pair = await importKeypair(envelope.subject, envelope, options.keysDir)
|
|
162
|
+
const identity: Identity = {
|
|
163
|
+
subject: envelope.subject,
|
|
164
|
+
createdAt: existing?.createdAt ?? (options.state?.now?.() ?? new Date()).toISOString(),
|
|
165
|
+
source: 'key',
|
|
166
|
+
mode: envelope.mode,
|
|
167
|
+
kid: envelope.kid ?? pair.kid,
|
|
168
|
+
issuer: options.issuer ?? envelope.issuer,
|
|
169
|
+
}
|
|
170
|
+
return {
|
|
171
|
+
next: { ...store, identities: { ...store.identities, [name]: identity } },
|
|
172
|
+
value: identity,
|
|
173
|
+
}
|
|
174
|
+
}, options.state)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export async function writeIdentityExport(path: string, content: string): Promise<void> {
|
|
178
|
+
await atomicWrite(path, content)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function hasVersion(input: unknown): input is { readonly version: unknown } {
|
|
182
|
+
return typeof input === 'object' && input !== null && Object.hasOwn(input, 'version')
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function invalidExport(message: string): AstraleError {
|
|
186
|
+
return new AstraleError('INVALID_IDENTITY_EXPORT', message)
|
|
187
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { JWK } from 'jose'
|
|
2
|
+
|
|
3
|
+
type AuthOptions = {
|
|
4
|
+
readonly issuer?: string
|
|
5
|
+
readonly subject?: string
|
|
6
|
+
readonly kid?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type AuthBinding = {
|
|
10
|
+
readonly credential: string
|
|
11
|
+
readonly publicKey: { readonly jwk: JWK }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type KeypairPaths = {
|
|
15
|
+
readonly privatePath: string
|
|
16
|
+
readonly publicPath: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface Keypair {
|
|
20
|
+
readonly privateJwk: JWK
|
|
21
|
+
readonly publicJwk: JWK
|
|
22
|
+
readonly kid?: string
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface KeypairInput {
|
|
26
|
+
readonly privateJwk: unknown
|
|
27
|
+
readonly publicJwk: unknown
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function keypairPaths(subject: string, keysDir?: string): KeypairPaths
|
|
31
|
+
|
|
32
|
+
export function fileExists(path: string): Promise<boolean>
|
|
33
|
+
|
|
34
|
+
export function listIdentityKeys(keysDir?: string): Promise<string[]>
|
|
35
|
+
|
|
36
|
+
export function generateEd25519Jwk(
|
|
37
|
+
kid: string,
|
|
38
|
+
): Promise<{ readonly privateJwk: JWK; readonly publicJwk: JWK }>
|
|
39
|
+
|
|
40
|
+
export function persistKeypair(
|
|
41
|
+
subject: string,
|
|
42
|
+
opts?: { readonly keysDir?: string; readonly kid?: string },
|
|
43
|
+
): Promise<{ readonly publicJwk: JWK; readonly privateJwk: JWK; readonly kid: string }>
|
|
44
|
+
|
|
45
|
+
/** Admit supported private/public JWKs and prove that they form one pair. */
|
|
46
|
+
export function acceptKeypair(input: KeypairInput): Promise<Keypair>
|
|
47
|
+
|
|
48
|
+
/** Decode and cryptographically prove a persisted subject keypair. */
|
|
49
|
+
export function readKeypair(subject: string, keysDir?: string): Promise<Keypair>
|
|
50
|
+
|
|
51
|
+
/** Admit and atomically persist one externally supplied subject keypair. */
|
|
52
|
+
export function importKeypair(
|
|
53
|
+
subject: string,
|
|
54
|
+
input: KeypairInput,
|
|
55
|
+
keysDir?: string,
|
|
56
|
+
): Promise<Keypair>
|
|
57
|
+
|
|
58
|
+
export function removeKeypair(subject: string, keysDir?: string): Promise<void>
|
|
59
|
+
|
|
60
|
+
export function persistAuth(keysDir?: string, opts?: AuthOptions): Promise<AuthBinding>
|
|
61
|
+
|
|
62
|
+
export function loadAuth(keysDir?: string, opts?: AuthOptions): Promise<AuthBinding>
|
|
63
|
+
|
|
64
|
+
export function resolveAuth(keysDir?: string, opts?: AuthOptions): Promise<AuthBinding>
|
|
65
|
+
|
|
66
|
+
export function signAs(
|
|
67
|
+
subject: string,
|
|
68
|
+
keysDir?: string,
|
|
69
|
+
opts?: {
|
|
70
|
+
readonly issuer?: string
|
|
71
|
+
readonly audience?: string
|
|
72
|
+
readonly subject?: string
|
|
73
|
+
},
|
|
74
|
+
): Promise<string>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# CLI key material
|
|
2
|
+
|
|
3
|
+
This Node-only module owns the retained `@astrale-os/cli/keys` subpath. Manager keys remain on their
|
|
4
|
+
legacy filenames; every other identity uses its own subject-named pair. Writes are private and
|
|
5
|
+
atomic through State. Signing admits the selected private JWK algorithm and never substitutes
|
|
6
|
+
manager material for a missing identity key.
|
|
7
|
+
|
|
8
|
+
Externally supplied or persisted pairs cross a proof-loss boundary here. Keys accepts only supported
|
|
9
|
+
algorithms, keeps paths inside the selected key directory, rejects private material in the public
|
|
10
|
+
file, and proves private/public correspondence before returning or replacing files.
|
|
11
|
+
|
|
12
|
+
The retained Node-only subpath contains the explicit raw-key operations required by import/export
|
|
13
|
+
and scaffold consumers. Ordinary connection code receives signed credentials or public material.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { defineLaw } from '@astrale-os/spec/authoring'
|
|
2
|
+
|
|
3
|
+
export const CLI_KEYS_IDENTITY_ISOLATED = defineLaw({
|
|
4
|
+
id: 'CLI-KEYS-IDENTITY-ISOLATED',
|
|
5
|
+
statement:
|
|
6
|
+
'Each subject signs only with its own mode-0600 keypair; missing subject material fails even when manager keys exist.',
|
|
7
|
+
tests: [
|
|
8
|
+
{ file: '__tests__/keys.test.ts', id: 'TEST-CLI-KEYS-PRIVATE-MODE' },
|
|
9
|
+
{ file: '__tests__/keys.test.ts', id: 'TEST-CLI-KEYS-NO-MANAGER-FALLBACK' },
|
|
10
|
+
],
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
export const CLI_KEYS_LEGACY_READABLE = defineLaw({
|
|
14
|
+
id: 'CLI-KEYS-LEGACY-READABLE',
|
|
15
|
+
statement:
|
|
16
|
+
'Manager filenames and unstamped supported legacy algorithms remain readable without changing another identity key coordinate.',
|
|
17
|
+
tests: [
|
|
18
|
+
{ file: '__tests__/keys.test.ts', id: 'TEST-CLI-KEYS-LEGACY-FILENAMES' },
|
|
19
|
+
{ file: '__tests__/algorithm.test.ts', id: 'TEST-CLI-KEYS-INFERS-LEGACY-ALGORITHM' },
|
|
20
|
+
],
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
export const CLI_KEYS_PAIR_ADMITTED = defineLaw({
|
|
24
|
+
id: 'CLI-KEYS-PAIR-ADMITTED',
|
|
25
|
+
statement:
|
|
26
|
+
'A subject path remains inside the selected key directory, and imported or persisted material is returned only after supported-algorithm and private/public correspondence proof.',
|
|
27
|
+
tests: [
|
|
28
|
+
{ file: '__tests__/keys.test.ts', id: 'TEST-CLI-KEYS-PAIR-ADMISSION' },
|
|
29
|
+
{ file: '__tests__/keys.test.ts', id: 'TEST-CLI-KEYS-PATH-CONFINED' },
|
|
30
|
+
],
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export const CLI_KEYS_CREDENTIAL_GRANT_PROFILE = defineLaw({
|
|
34
|
+
id: 'CLI-KEYS-CREDENTIAL-GRANT-PROFILE',
|
|
35
|
+
statement:
|
|
36
|
+
'A self-issued Kernel credential carries the effective subject as an already-resolved identity Grant, while an externally issued primary credential carries an identity-self Grant for Runtime resolution.',
|
|
37
|
+
tests: [
|
|
38
|
+
{
|
|
39
|
+
file: '__tests__/keys.test.ts',
|
|
40
|
+
id: 'TEST-CLI-KEYS-DISTINGUISHES-KERNEL-ROOT-GRANT',
|
|
41
|
+
},
|
|
42
|
+
],
|
|
43
|
+
})
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
|
|
3
|
+
import { AstraleError } from '../../errors'
|
|
4
|
+
import { inferAlg } from '../algorithm'
|
|
5
|
+
|
|
6
|
+
describe('inferAlg', () => {
|
|
7
|
+
test('returns explicit alg when present', () => {
|
|
8
|
+
expect(inferAlg({ alg: 'ES256' })).toBe('ES256')
|
|
9
|
+
expect(inferAlg({ alg: 'EdDSA' })).toBe('EdDSA')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
/** @evidence TEST-CLI-KEYS-INFERS-LEGACY-ALGORITHM */
|
|
13
|
+
test('infers supported algorithms for legacy unstamped key files', () => {
|
|
14
|
+
expect(inferAlg({ kty: 'EC', crv: 'P-256' })).toBe('ES256')
|
|
15
|
+
expect(inferAlg({ kty: 'OKP', crv: 'Ed25519' })).toBe('EdDSA')
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test('explicit alg wins over inferable shape (no second-guessing)', () => {
|
|
19
|
+
expect(inferAlg({ alg: 'ES256', kty: 'OKP', crv: 'Ed25519' })).toBe('ES256')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
test('throws AstraleError with hint when no alg/crv/kty resolves', () => {
|
|
23
|
+
expect(() => inferAlg({})).toThrow(AstraleError)
|
|
24
|
+
expect(() => inferAlg({ kty: 'EC' })).toThrow(AstraleError)
|
|
25
|
+
expect(() => inferAlg({ kty: 'EC', crv: 'P-384' })).toThrow(AstraleError)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('error message includes keyPath when provided', () => {
|
|
29
|
+
expect(() => inferAlg({}, '/path/to/broken.jwk')).toThrow(/\/path\/to\/broken\.jwk/)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
test('treats empty-string alg as missing', () => {
|
|
33
|
+
expect(inferAlg({ alg: '', kty: 'EC', crv: 'P-256' })).toBe('ES256')
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('rejects unsupported explicit algorithms', () => {
|
|
37
|
+
expect(() => inferAlg({ alg: 'HS256', kty: 'oct' })).toThrow(/unsupported signing algorithm/)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { decodeJwt, decodeProtectedHeader } from 'jose'
|
|
3
|
+
import { access, mkdtemp, rm, stat, writeFile } from 'node:fs/promises'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
|
|
7
|
+
import { IdentityKeyMissingError } from '../../errors'
|
|
8
|
+
import {
|
|
9
|
+
generateEd25519Jwk,
|
|
10
|
+
importKeypair,
|
|
11
|
+
keypairPaths,
|
|
12
|
+
listIdentityKeys,
|
|
13
|
+
persistKeypair,
|
|
14
|
+
readKeypair,
|
|
15
|
+
removeKeypair,
|
|
16
|
+
signAs,
|
|
17
|
+
} from '../index'
|
|
18
|
+
|
|
19
|
+
describe('DESIGN — per-identity keys', () => {
|
|
20
|
+
let tmp = ''
|
|
21
|
+
|
|
22
|
+
beforeEach(async () => {
|
|
23
|
+
tmp = await mkdtemp(join(tmpdir(), 'astrale-keys-test-'))
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
await rm(tmp, { recursive: true, force: true })
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
/** @evidence TEST-CLI-KEYS-LEGACY-FILENAMES */
|
|
31
|
+
test('keypairPaths routes manager to legacy filenames', () => {
|
|
32
|
+
const p = keypairPaths('manager', tmp)
|
|
33
|
+
expect(p.privatePath.endsWith('manager.private.jwk')).toBe(true)
|
|
34
|
+
expect(p.publicPath.endsWith('manager.public.jwk')).toBe(true)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('keypairPaths routes others to <name>.*.jwk', () => {
|
|
38
|
+
const p = keypairPaths('alice', tmp)
|
|
39
|
+
expect(p.privatePath.endsWith('alice.private.jwk')).toBe(true)
|
|
40
|
+
expect(p.publicPath.endsWith('alice.public.jwk')).toBe(true)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
/** @evidence TEST-CLI-KEYS-PATH-CONFINED */
|
|
44
|
+
test('keypairPaths rejects subjects that escape the selected key directory', () => {
|
|
45
|
+
expect(() => keypairPaths('../alice', tmp)).toThrow(/cannot name a key file/)
|
|
46
|
+
expect(() => keypairPaths('/alice', tmp)).toThrow(/cannot name a key file/)
|
|
47
|
+
expect(() => keypairPaths('', tmp)).toThrow(/cannot name a key file/)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
/** @evidence TEST-CLI-KEYS-PRIVATE-MODE */
|
|
51
|
+
test('persistKeypair writes both files with kid and private mode', async () => {
|
|
52
|
+
const result = await persistKeypair('alice', { keysDir: tmp })
|
|
53
|
+
expect(result.kid).toMatch(/^alice-key-/)
|
|
54
|
+
const { privatePath, publicPath } = keypairPaths('alice', tmp)
|
|
55
|
+
await access(privatePath)
|
|
56
|
+
await access(publicPath)
|
|
57
|
+
expect((await stat(privatePath)).mode & 0o777).toBe(0o600)
|
|
58
|
+
expect((await stat(publicPath)).mode & 0o777).toBe(0o600)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('signAs(alice) works when alice has her own key', async () => {
|
|
62
|
+
await persistKeypair('alice', { keysDir: tmp })
|
|
63
|
+
const jwt = await signAs('alice', tmp)
|
|
64
|
+
expect(jwt.split('.').length).toBe(3)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
/** @evidence TEST-CLI-KEYS-DISTINGUISHES-KERNEL-ROOT-GRANT */
|
|
68
|
+
test('signAs resolves self-issued Kernel root grants and preserves external self grants', async () => {
|
|
69
|
+
await persistKeypair('manager-principal', { keysDir: tmp })
|
|
70
|
+
await persistKeypair('alice', { keysDir: tmp })
|
|
71
|
+
const kernel = 'https://kernel.example/kernel/host'
|
|
72
|
+
|
|
73
|
+
const root = decodeJwt(
|
|
74
|
+
await signAs('manager-principal', tmp, {
|
|
75
|
+
issuer: kernel,
|
|
76
|
+
audience: kernel,
|
|
77
|
+
}),
|
|
78
|
+
)
|
|
79
|
+
expect(root).toMatchObject({
|
|
80
|
+
iss: kernel,
|
|
81
|
+
sub: 'manager-principal',
|
|
82
|
+
aud: kernel,
|
|
83
|
+
grant: { v: 1, expr: { kind: 'identity', id: 'manager-principal' } },
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
const external = decodeJwt(
|
|
87
|
+
await signAs('alice', tmp, {
|
|
88
|
+
issuer: 'https://identity.example',
|
|
89
|
+
audience: kernel,
|
|
90
|
+
}),
|
|
91
|
+
)
|
|
92
|
+
expect(external).toMatchObject({
|
|
93
|
+
iss: 'https://identity.example',
|
|
94
|
+
sub: 'alice',
|
|
95
|
+
aud: kernel,
|
|
96
|
+
grant: { v: 1, expr: { kind: 'identity', self: true } },
|
|
97
|
+
})
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('signAs(alice) uses the key file algorithm for EdDSA identities', async () => {
|
|
101
|
+
const { privateJwk, publicJwk } = await generateEd25519Jwk('alice-ed25519')
|
|
102
|
+
const { privatePath, publicPath } = keypairPaths('alice', tmp)
|
|
103
|
+
await writeFile(privatePath, JSON.stringify(privateJwk, null, 2))
|
|
104
|
+
await writeFile(publicPath, JSON.stringify(publicJwk, null, 2))
|
|
105
|
+
|
|
106
|
+
const jwt = await signAs('alice', tmp)
|
|
107
|
+
|
|
108
|
+
expect(decodeProtectedHeader(jwt).alg).toBe('EdDSA')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
/** @evidence TEST-CLI-KEYS-PAIR-ADMISSION */
|
|
112
|
+
test('import and read prove one matching supported keypair before publication', async () => {
|
|
113
|
+
const alice = await persistKeypair('alice', { keysDir: join(tmp, 'alice-source') })
|
|
114
|
+
const bob = await persistKeypair('bob', { keysDir: join(tmp, 'bob-source') })
|
|
115
|
+
const importedDir = join(tmp, 'imported')
|
|
116
|
+
|
|
117
|
+
await expect(
|
|
118
|
+
importKeypair(
|
|
119
|
+
'alice',
|
|
120
|
+
{ privateJwk: alice.privateJwk, publicJwk: { ...bob.publicJwk, kid: alice.kid } },
|
|
121
|
+
importedDir,
|
|
122
|
+
),
|
|
123
|
+
).rejects.toThrow(/do not form a valid matching pair/)
|
|
124
|
+
await expect(access(keypairPaths('alice', importedDir).privatePath)).rejects.toThrow()
|
|
125
|
+
|
|
126
|
+
const imported = await importKeypair(
|
|
127
|
+
'alice',
|
|
128
|
+
{ privateJwk: alice.privateJwk, publicJwk: alice.publicJwk },
|
|
129
|
+
importedDir,
|
|
130
|
+
)
|
|
131
|
+
expect(imported.kid).toBe(alice.kid)
|
|
132
|
+
expect((await readKeypair('alice', importedDir)).publicJwk).toEqual(alice.publicJwk)
|
|
133
|
+
|
|
134
|
+
await expect(
|
|
135
|
+
importKeypair(
|
|
136
|
+
'unsafe-public',
|
|
137
|
+
{ privateJwk: alice.privateJwk, publicJwk: alice.privateJwk },
|
|
138
|
+
importedDir,
|
|
139
|
+
),
|
|
140
|
+
).rejects.toThrow(/must not contain private key material/)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
/** @evidence TEST-CLI-KEYS-NO-MANAGER-FALLBACK */
|
|
144
|
+
test('signAs(alice) never falls back to the manager key', async () => {
|
|
145
|
+
await persistKeypair('manager', { keysDir: tmp })
|
|
146
|
+
await expect(signAs('alice', tmp)).rejects.toThrow(IdentityKeyMissingError)
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test('signAs errors when no keys at all', async () => {
|
|
150
|
+
await expect(signAs('manager', tmp)).rejects.toThrow(IdentityKeyMissingError)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
test('removeKeypair is idempotent', async () => {
|
|
154
|
+
await persistKeypair('alice', { keysDir: tmp })
|
|
155
|
+
await removeKeypair('alice', tmp)
|
|
156
|
+
await removeKeypair('alice', tmp)
|
|
157
|
+
const { privatePath } = keypairPaths('alice', tmp)
|
|
158
|
+
await expect(access(privatePath)).rejects.toThrow()
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
test('listIdentityKeys returns names with private keys', async () => {
|
|
162
|
+
await persistKeypair('manager', { keysDir: tmp })
|
|
163
|
+
await persistKeypair('alice', { keysDir: tmp })
|
|
164
|
+
await persistKeypair('bob', { keysDir: tmp })
|
|
165
|
+
const names = await listIdentityKeys(tmp)
|
|
166
|
+
expect(names.sort()).toEqual(['alice', 'bob', 'manager'])
|
|
167
|
+
})
|
|
168
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { AstraleError } from '../errors'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolve the JOSE algorithm to use with this key. Prefers `privateJwk.alg`
|
|
5
|
+
* when present, falls back to inferring from `crv`/`kty` so keys generated
|
|
6
|
+
* by older CLIs that did not stamp `alg`. Throws when neither form resolves.
|
|
7
|
+
*/
|
|
8
|
+
export function inferAlg(privateJwk: Record<string, unknown>, keyPath?: string): string {
|
|
9
|
+
const explicit = privateJwk.alg
|
|
10
|
+
if (typeof explicit === 'string' && explicit.length > 0) {
|
|
11
|
+
if (explicit === 'ES256' || explicit === 'EdDSA') return explicit
|
|
12
|
+
throw unsupportedAlgorithm(explicit, keyPath)
|
|
13
|
+
}
|
|
14
|
+
const crv = privateJwk.crv
|
|
15
|
+
const kty = privateJwk.kty
|
|
16
|
+
if (kty === 'EC' && crv === 'P-256') return 'ES256'
|
|
17
|
+
if (kty === 'OKP' && crv === 'Ed25519') return 'EdDSA'
|
|
18
|
+
const where = keyPath ? ` at ${keyPath}` : ''
|
|
19
|
+
throw new AstraleError(
|
|
20
|
+
'INVALID_KEY_FILE',
|
|
21
|
+
`JWK${where} is missing both \`alg\` and a recognizable \`(kty, crv)\` pair — cannot pick a signing algorithm.`,
|
|
22
|
+
'Re-stamp the file with `"alg": "ES256"` (P-256 EC keys) or `"alg": "EdDSA"` (Ed25519 OKP keys), or regenerate via `astrale domain init` / `astrale identity create`.',
|
|
23
|
+
)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function unsupportedAlgorithm(algorithm: string, keyPath?: string): AstraleError {
|
|
27
|
+
const where = keyPath ? ` at ${keyPath}` : ''
|
|
28
|
+
return new AstraleError(
|
|
29
|
+
'INVALID_KEY_FILE',
|
|
30
|
+
`JWK${where} selects unsupported signing algorithm ${JSON.stringify(algorithm)}.`,
|
|
31
|
+
'Use an ES256 P-256 key or an EdDSA Ed25519 key.',
|
|
32
|
+
)
|
|
33
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { importJWK, SignJWT, type JWK } from 'jose'
|
|
2
|
+
|
|
3
|
+
import { IdentityKeyMissingError } from '../errors'
|
|
4
|
+
import { KEYS_DIR } from '../state/index'
|
|
5
|
+
import { inferAlg } from './algorithm'
|
|
6
|
+
import { fileExists, keypairPaths, persistKeypair, readKeypair } from './pair'
|
|
7
|
+
|
|
8
|
+
const DEFAULT_MANAGER_ISSUER = 'http://localhost:4400/host'
|
|
9
|
+
|
|
10
|
+
type AuthOptions = {
|
|
11
|
+
readonly issuer?: string
|
|
12
|
+
readonly subject?: string
|
|
13
|
+
readonly kid?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type AuthBinding = {
|
|
17
|
+
readonly credential: string
|
|
18
|
+
readonly publicKey: { readonly jwk: JWK }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function loadSigningMaterial(
|
|
22
|
+
subject: string,
|
|
23
|
+
keysDir: string,
|
|
24
|
+
): Promise<{ privateJwk: JWK; publicJwk: JWK; kid: string }> {
|
|
25
|
+
const { privatePath } = keypairPaths(subject, keysDir)
|
|
26
|
+
if (!(await fileExists(privatePath))) throw new IdentityKeyMissingError(subject)
|
|
27
|
+
|
|
28
|
+
const pair = await readKeypair(subject, keysDir)
|
|
29
|
+
return { ...pair, kid: pair.kid ?? `${subject}-key` }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function signIdentityCredential(options: {
|
|
33
|
+
readonly privateJwk: JWK
|
|
34
|
+
readonly kid: string
|
|
35
|
+
readonly issuer: string
|
|
36
|
+
readonly subject: string
|
|
37
|
+
readonly audience: string
|
|
38
|
+
}): Promise<string> {
|
|
39
|
+
const alg = inferAlg(options.privateJwk as Record<string, unknown>)
|
|
40
|
+
const privateKey = await importJWK(options.privateJwk, alg)
|
|
41
|
+
const expr =
|
|
42
|
+
options.issuer === options.audience
|
|
43
|
+
? { kind: 'identity' as const, id: options.subject }
|
|
44
|
+
: { kind: 'identity' as const, self: true as const }
|
|
45
|
+
return new SignJWT({ grant: { v: 1, expr } })
|
|
46
|
+
.setProtectedHeader({ alg, kid: options.kid })
|
|
47
|
+
.setIssuer(options.issuer)
|
|
48
|
+
.setSubject(options.subject)
|
|
49
|
+
.setAudience(options.audience)
|
|
50
|
+
.sign(privateKey)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function persistAuth(
|
|
54
|
+
keysDir: string = KEYS_DIR,
|
|
55
|
+
opts?: AuthOptions,
|
|
56
|
+
): Promise<AuthBinding> {
|
|
57
|
+
const issuer = opts?.issuer ?? DEFAULT_MANAGER_ISSUER
|
|
58
|
+
const subject = opts?.subject ?? 'manager'
|
|
59
|
+
const kid = opts?.kid ?? `${subject}-key`
|
|
60
|
+
const { privateJwk, publicJwk } = await persistKeypair(subject, { keysDir, kid })
|
|
61
|
+
const credential = await signIdentityCredential({
|
|
62
|
+
privateJwk,
|
|
63
|
+
kid,
|
|
64
|
+
issuer,
|
|
65
|
+
subject,
|
|
66
|
+
audience: issuer,
|
|
67
|
+
})
|
|
68
|
+
return { credential, publicKey: { jwk: publicJwk } }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function loadAuth(
|
|
72
|
+
keysDir: string = KEYS_DIR,
|
|
73
|
+
opts?: AuthOptions,
|
|
74
|
+
): Promise<AuthBinding> {
|
|
75
|
+
const issuer = opts?.issuer ?? DEFAULT_MANAGER_ISSUER
|
|
76
|
+
const subject = opts?.subject ?? 'manager'
|
|
77
|
+
const { privateJwk, publicJwk, kid } = await loadSigningMaterial(subject, keysDir)
|
|
78
|
+
const credential = await signIdentityCredential({
|
|
79
|
+
privateJwk,
|
|
80
|
+
kid,
|
|
81
|
+
issuer,
|
|
82
|
+
subject,
|
|
83
|
+
audience: issuer,
|
|
84
|
+
})
|
|
85
|
+
return { credential, publicKey: { jwk: publicJwk } }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function resolveAuth(
|
|
89
|
+
keysDir: string = KEYS_DIR,
|
|
90
|
+
opts?: AuthOptions,
|
|
91
|
+
): Promise<AuthBinding> {
|
|
92
|
+
const subject = opts?.subject ?? 'manager'
|
|
93
|
+
const { privatePath } = keypairPaths(subject, keysDir)
|
|
94
|
+
return (await fileExists(privatePath)) ? loadAuth(keysDir, opts) : persistAuth(keysDir, opts)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function signAs(
|
|
98
|
+
subject: string,
|
|
99
|
+
keysDir: string = KEYS_DIR,
|
|
100
|
+
opts?: { readonly issuer?: string; readonly audience?: string; readonly subject?: string },
|
|
101
|
+
): Promise<string> {
|
|
102
|
+
const issuer = opts?.issuer ?? DEFAULT_MANAGER_ISSUER
|
|
103
|
+
const audience = opts?.audience ?? issuer
|
|
104
|
+
const { privateJwk, kid } = await loadSigningMaterial(subject, keysDir)
|
|
105
|
+
return signIdentityCredential({
|
|
106
|
+
privateJwk,
|
|
107
|
+
kid,
|
|
108
|
+
issuer,
|
|
109
|
+
subject: opts?.subject ?? subject,
|
|
110
|
+
audience,
|
|
111
|
+
})
|
|
112
|
+
}
|