typevault 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +156 -115
- package/package.json +14 -2
- package/src/access/env.ts +77 -17
- package/src/access/keys.ts +14 -14
- package/src/access/schemas.ts +6 -12
- package/src/access/scope.ts +14 -9
- package/src/access/scoped.ts +118 -44
- package/src/access/sealed-access.ts +50 -23
- package/src/access/sealed.ts +56 -31
- package/src/access/types.ts +27 -30
- package/src/admin.ts +68 -0
- package/src/audit/log.ts +28 -12
- package/src/cli/commands/admin.ts +93 -48
- package/src/cli/commands/daemon.ts +42 -17
- package/src/cli/commands/entries.ts +195 -60
- package/src/cli/commands/grant.ts +24 -22
- package/src/cli/commands/members.ts +307 -0
- package/src/cli/commands/run.ts +191 -44
- package/src/cli/context.ts +163 -15
- package/src/cli/index.ts +33 -5
- package/src/cli/password.ts +74 -0
- package/src/cli/web-prompt.ts +79 -0
- package/src/core/admin.ts +144 -76
- package/src/core/adopt.ts +40 -9
- package/src/core/crypto.ts +6 -4
- package/src/core/doctor.ts +15 -16
- package/src/core/entries.ts +66 -52
- package/src/core/errors.ts +8 -2
- package/src/core/fsx.ts +7 -3
- package/src/core/holder.ts +87 -0
- package/src/core/identity.ts +132 -0
- package/src/core/init.ts +73 -23
- package/src/core/json-file.ts +20 -0
- package/src/core/keyring.ts +100 -56
- package/src/core/layout.ts +143 -26
- package/src/core/lock.ts +128 -16
- package/src/core/naming.ts +150 -50
- package/src/core/password.ts +111 -0
- package/src/core/payload.ts +42 -38
- package/src/core/revisions.ts +11 -6
- package/src/core/rotate.ts +59 -42
- package/src/core/state.ts +102 -54
- package/src/core/txn.ts +9 -7
- package/src/core/vault.ts +108 -46
- package/src/core/worktrees.ts +51 -0
- package/src/daemon/admin-routes.ts +33 -21
- package/src/daemon/agent-routes.ts +31 -11
- package/src/daemon/client.ts +24 -2
- package/src/daemon/daemon.ts +218 -33
- package/src/daemon/grants.ts +25 -7
- package/src/daemon/http.ts +11 -2
- package/src/daemon/live-access.ts +8 -4
- package/src/daemon/protocol.ts +17 -12
- package/src/daemon/requests.ts +75 -51
- package/src/daemon/spawn.ts +218 -9
- package/src/export/1password.ts +157 -0
- package/src/import/1password-live.ts +399 -0
- package/src/import/1password.ts +26 -5
- package/src/import/bitwarden.ts +3 -3
- package/src/import/csv.ts +1 -1
- package/src/import/dotenv.ts +4 -3
- package/src/import/index.ts +1 -0
- package/src/import/keepass.ts +2 -2
- package/src/import/lastpass.ts +2 -2
- package/src/import/shared.ts +67 -10
- package/src/import/typeclaw.ts +2 -2
- package/src/import/types.ts +1 -1
- package/src/index.ts +58 -0
- package/src/owner.ts +156 -0
- package/src/prompt/browser.ts +48 -15
- package/src/prompt/index.ts +121 -64
- package/src/prompt/page.ts +27 -11
- package/src/prompt/styles.ts +64 -34
- package/src/prompt/types.ts +7 -4
- package/src/redact/index.ts +48 -12
- package/src/redact/scrub-file.ts +15 -7
package/src/cli/context.ts
CHANGED
|
@@ -1,30 +1,145 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs'
|
|
2
|
+
|
|
1
3
|
import { z } from 'zod'
|
|
2
4
|
|
|
3
|
-
import {
|
|
5
|
+
import { machineKeys } from '../access/keys'
|
|
4
6
|
import { ScopedVault } from '../access/scoped'
|
|
5
7
|
import { readSealedGrant } from '../access/sealed'
|
|
6
8
|
import { SealedAccess } from '../access/sealed-access'
|
|
7
9
|
import type { Access } from '../access/types'
|
|
8
10
|
import { Admin } from '../core/admin'
|
|
11
|
+
import type { SecretKey } from '../core/crypto'
|
|
9
12
|
import { TypeVaultError } from '../core/errors'
|
|
10
|
-
import {
|
|
13
|
+
import { atomicWrite } from '../core/fsx'
|
|
14
|
+
import { type Identity, readIdentity, sealedCopyPath, unlockIdentity } from '../core/identity'
|
|
15
|
+
import { findLocalVault, globalLayout, type Layout, localLayout } from '../core/layout'
|
|
16
|
+
import { loadState } from '../core/state'
|
|
11
17
|
import { Vault } from '../core/vault'
|
|
12
|
-
import {
|
|
18
|
+
import { worktreeVaults } from '../core/worktrees'
|
|
19
|
+
import { callDaemon, isDaemonAlive } from '../daemon/client'
|
|
13
20
|
import { LiveAccess } from '../daemon/live-access'
|
|
14
|
-
import { ensureDaemon } from '../daemon/spawn'
|
|
21
|
+
import { ensureDaemon, LOCKED_MESSAGE, startSession, stopDaemon } from '../daemon/spawn'
|
|
22
|
+
import { note } from './output'
|
|
23
|
+
import { type Passwords, passwords, passwordsFromStdin } from './password'
|
|
15
24
|
|
|
16
25
|
export type Env = Readonly<Record<string, string | undefined>>
|
|
17
26
|
|
|
18
|
-
|
|
27
|
+
const isSet = (value: string | undefined): boolean => value !== undefined && value !== ''
|
|
28
|
+
|
|
29
|
+
/** `-g` (`TYPEVAULT_GLOBAL`) or an explicit `TYPEVAULT_DIR` selects the global vault; otherwise the project's. */
|
|
30
|
+
export const wantsGlobal = (env: Env): boolean => {
|
|
31
|
+
const global = env['TYPEVAULT_GLOBAL']?.toLowerCase()
|
|
32
|
+
return (isSet(global) && global !== '0' && global !== 'false') || isSet(env['TYPEVAULT_DIR'])
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The selected vault, or undefined when no project vault is found from `cwd`. Never falls back to the global one. */
|
|
36
|
+
export function findLayout(env: Env, cwd: string = process.cwd()): Layout | undefined {
|
|
37
|
+
if (wantsGlobal(env)) return globalLayout(env)
|
|
38
|
+
const dir = findLocalVault(cwd, env)
|
|
39
|
+
return dir === undefined ? undefined : localLayout(dir, env)
|
|
40
|
+
}
|
|
19
41
|
|
|
20
|
-
|
|
42
|
+
export function layoutFrom(env: Env, cwd: string = process.cwd()): Layout {
|
|
43
|
+
const layout = findLayout(env, cwd)
|
|
44
|
+
if (layout === undefined) {
|
|
45
|
+
throw new TypeVaultError(
|
|
46
|
+
'not_found',
|
|
47
|
+
'no vault here (run `typevault init` to create one, or pass -g for the global vault)',
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
return layout
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The vault opened with this member's own key: direct mode, for owners and issuers. A machine member's key is at
|
|
55
|
+
* hand; a human types their password, since only the daemon keeps an unlocked key around.
|
|
56
|
+
*/
|
|
21
57
|
export async function openDirect(env: Env): Promise<Vault> {
|
|
22
58
|
const layout = layoutFrom(env)
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
59
|
+
const machine = machineKeys(env)
|
|
60
|
+
const identity = readIdentity(env)
|
|
61
|
+
if (machine.length === 0 && identity === undefined) {
|
|
62
|
+
throw new TypeVaultError('denied', 'no grant and no identity here (set TYPEVAULT_GRANT, or run typevault join)')
|
|
63
|
+
}
|
|
64
|
+
const keys = machine.length > 0 ? machine : [await unlockHuman(identity, passwords())]
|
|
65
|
+
return Vault.open(layout, keys, env)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function unlockHuman(identity: Identity | undefined, passwords: Passwords): Promise<SecretKey> {
|
|
69
|
+
if (identity === undefined) throw new TypeVaultError('not_found', 'no identity here (run typevault init or join)')
|
|
70
|
+
if (identity.kind === 'machine') return unlockIdentity(identity)
|
|
71
|
+
return unlockIdentity(identity, await passwords.ask(`Password for ${identity.name}: `))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Unlocks a human member's session: their key goes to a daemon that holds it for `ttlMs`, and this user's CLI
|
|
76
|
+
* reads through the session grant it writes. Returns false when a session already runs.
|
|
77
|
+
*/
|
|
78
|
+
export async function unlockSession(env: Env, passwords: Passwords, ttlMs: number): Promise<boolean> {
|
|
79
|
+
const layout = layoutFrom(env)
|
|
80
|
+
if (await hasSession(layout)) return false
|
|
81
|
+
const identity = readIdentity(env)
|
|
82
|
+
const key = await unlockHuman(identity, passwords)
|
|
83
|
+
const vault = await Vault.open(layout, [key], env)
|
|
84
|
+
if (vault.member === undefined) {
|
|
85
|
+
throw new TypeVaultError(
|
|
86
|
+
'denied',
|
|
87
|
+
`${identity?.name ?? 'you'} is not a member of this vault (share \`typevault identity show\` with an owner)`,
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
if (identity?.kind === 'human') refreshSealedCopy(vault, identity)
|
|
91
|
+
// A daemon without a session (a machine member's, or one left from before the vault was re-created) holds no key
|
|
92
|
+
// of this person's, so it can't serve them: the session replaces it.
|
|
93
|
+
if (await isDaemonAlive(layout.agentSocket)) {
|
|
94
|
+
await stopDaemon(layout)
|
|
95
|
+
note('replaced a running daemon that held no unlocked session')
|
|
96
|
+
}
|
|
97
|
+
await startSession(layout, env, key, ttlMs)
|
|
98
|
+
return true
|
|
26
99
|
}
|
|
27
100
|
|
|
101
|
+
const hasSession = async (layout: Layout): Promise<boolean> =>
|
|
102
|
+
existsSync(layout.sessionFile) && (await isDaemonAlive(layout.agentSocket))
|
|
103
|
+
|
|
104
|
+
/** This vault in the repository's other worktrees, where a session is unlocked: one unlock serves them all. */
|
|
105
|
+
export async function worktreeSessions(layout: Layout, env: Env): Promise<readonly Layout[]> {
|
|
106
|
+
if (layout.scope !== 'local') return []
|
|
107
|
+
const sessions: Layout[] = []
|
|
108
|
+
for (const vault of await worktreeVaults(layout.vault)) {
|
|
109
|
+
const worktree = localLayout(vault, env)
|
|
110
|
+
if (await hasSession(worktree)) sessions.push(worktree)
|
|
111
|
+
}
|
|
112
|
+
return sessions
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Starts here the session unlocked in another worktree, if any; that worktree's daemon checks this is one. */
|
|
116
|
+
async function shareWorktreeSession(layout: Layout, env: Env): Promise<boolean> {
|
|
117
|
+
const [unlocked] = await worktreeSessions(layout, env)
|
|
118
|
+
if (unlocked === undefined) return false
|
|
119
|
+
const token = readFileSync(unlocked.sessionFile, 'utf8').trim()
|
|
120
|
+
try {
|
|
121
|
+
await callDaemon(unlocked.agentSocket, '/v1/attach', { vault: layout.vault }, z.object({ ok: z.literal(true) }), {
|
|
122
|
+
token,
|
|
123
|
+
})
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof TypeVaultError) return false
|
|
126
|
+
throw error
|
|
127
|
+
}
|
|
128
|
+
return true
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** After `passwd`, the copy committed in this vault still opens with the old password until it is replaced. */
|
|
132
|
+
function refreshSealedCopy(vault: Vault, identity: Identity & { readonly kind: 'human' }): void {
|
|
133
|
+
const copy = sealedCopyPath(vault.layout, identity.name)
|
|
134
|
+
if (existsSync(copy) && readFileSync(copy, 'utf8') !== identity.sealedKey) {
|
|
135
|
+
atomicWrite(copy, identity.sealedKey)
|
|
136
|
+
note(`updated ${copy} to your current password; commit it`)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The vault's public, owner-signed state, readable without any key. */
|
|
141
|
+
export const openPublic = (env: Env): Promise<Vault> => Vault.open(layoutFrom(env), [], env)
|
|
142
|
+
|
|
28
143
|
export const openAdmin = async (env: Env): Promise<Admin> => new Admin(await openDirect(env))
|
|
29
144
|
|
|
30
145
|
export type ResolvedAccess =
|
|
@@ -34,25 +149,58 @@ export type ResolvedAccess =
|
|
|
34
149
|
|
|
35
150
|
/** `TYPEVAULT_GRANT` decides: `tvl1.` live via the daemon, `tvg1:`/`@path` sealed, unset = direct mode. */
|
|
36
151
|
export async function resolveAccess(env: Env): Promise<ResolvedAccess> {
|
|
37
|
-
const layout = layoutFrom(env)
|
|
38
152
|
const grant = env['TYPEVAULT_GRANT']?.trim()
|
|
39
153
|
if (grant === undefined || grant === '') {
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
const
|
|
154
|
+
const machine = machineKeys(env)
|
|
155
|
+
if (machine.length === 0) return { kind: 'live', access: await sessionAccess(env) }
|
|
156
|
+
const tier = env['TYPEVAULT_TIER']
|
|
157
|
+
const vault = await Vault.open(layoutFrom(env), machine, env)
|
|
158
|
+
const ownTiers = vault.member === undefined ? [] : (vault.state.members[vault.member]?.tiers ?? [])
|
|
159
|
+
const identity = { mode: 'direct' as const, ownTiers, ...(tier === undefined ? {} : { defaultTier: tier }) }
|
|
44
160
|
return { kind: 'direct', access: new ScopedVault(vault, [], identity) }
|
|
45
161
|
}
|
|
46
162
|
if (grant.startsWith('tvl1.')) {
|
|
163
|
+
const layout = layoutFrom(env)
|
|
164
|
+
checkOwnerPin(layout, env)
|
|
47
165
|
await ensureDaemon(layout, env)
|
|
48
166
|
return { kind: 'live', access: new LiveAccess(layout.agentSocket, grant) }
|
|
49
167
|
}
|
|
50
168
|
if (grant.startsWith('tvg1:') || grant.startsWith('@')) {
|
|
51
|
-
return { kind: 'sealed', access: new SealedAccess(await readSealedGrant(grant),
|
|
169
|
+
return { kind: 'sealed', access: new SealedAccess(await readSealedGrant(grant), findLayout(env), env) }
|
|
52
170
|
}
|
|
53
171
|
throw new TypeVaultError('invalid', 'TYPEVAULT_GRANT is not a typevault grant')
|
|
54
172
|
}
|
|
55
173
|
|
|
174
|
+
/**
|
|
175
|
+
* A human member's CLI reads through the unlocked session's grant. On a terminal a locked vault asks for the
|
|
176
|
+
* password right away; anywhere else (an agent) it stays locked until a human unlocks it.
|
|
177
|
+
*/
|
|
178
|
+
async function sessionAccess(env: Env): Promise<LiveAccess> {
|
|
179
|
+
const layout = layoutFrom(env)
|
|
180
|
+
checkOwnerPin(layout, env)
|
|
181
|
+
if (!(await hasSession(layout)) && !(await shareWorktreeSession(layout, env))) {
|
|
182
|
+
const identity = readIdentity(env)
|
|
183
|
+
if (identity === undefined) {
|
|
184
|
+
throw new TypeVaultError('denied', 'no grant and no identity here (set TYPEVAULT_GRANT, or run typevault join)')
|
|
185
|
+
}
|
|
186
|
+
const canAsk = process.stdin.isTTY || passwordsFromStdin()
|
|
187
|
+
if (identity.kind !== 'human' || !canAsk) throw new TypeVaultError('locked', LOCKED_MESSAGE)
|
|
188
|
+
await unlockSession(env, passwords(), DEFAULT_SESSION_MS)
|
|
189
|
+
}
|
|
190
|
+
return new LiveAccess(layout.agentSocket, readFileSync(layout.sessionFile, 'utf8').trim())
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* A live connection reads through a daemon that may have trusted the owner without this process's pin, so a set
|
|
195
|
+
* `TYPEVAULT_OWNER` is checked against the vault here, before anything is read through it.
|
|
196
|
+
*/
|
|
197
|
+
function checkOwnerPin(layout: Layout, env: Env): void {
|
|
198
|
+
if (isSet(env['TYPEVAULT_OWNER'])) loadState(layout, env)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** How long an unlocked session lasts unless `unlock --for` says otherwise. */
|
|
202
|
+
export const DEFAULT_SESSION_MS = 8 * 60 * 60_000
|
|
203
|
+
|
|
56
204
|
export const accessOf = async (env: Env): Promise<Access> => (await resolveAccess(env)).access
|
|
57
205
|
|
|
58
206
|
/** A call on this process's admin socket (`TYPEVAULT_CEILING`, default `default`), starting the daemon if needed. */
|
package/src/cli/index.ts
CHANGED
|
@@ -2,17 +2,32 @@
|
|
|
2
2
|
import { defineCommand, runCommand, runMain } from 'citty'
|
|
3
3
|
|
|
4
4
|
import { TypeVaultError } from '../core/errors'
|
|
5
|
-
import { ceiling, doctor,
|
|
5
|
+
import { ceiling, doctor, tier, init, key, signer } from './commands/admin'
|
|
6
6
|
import { daemon } from './commands/daemon'
|
|
7
7
|
import { get, ls, mv, otp, rm, set, whoami } from './commands/entries'
|
|
8
8
|
import { audit, grant } from './commands/grant'
|
|
9
|
+
import { identity, joinCommand, lock, member, passwd, unlock } from './commands/members'
|
|
9
10
|
import { importCommand, redact, request, run, scrub } from './commands/run'
|
|
11
|
+
import { readPasswordsFromStdin } from './password'
|
|
10
12
|
|
|
11
13
|
const main = defineCommand({
|
|
12
14
|
meta: { name: 'typevault', description: 'A CLI-first secrets vault for agents' },
|
|
15
|
+
args: {
|
|
16
|
+
global: { type: 'boolean', alias: 'g', description: 'Use the global vault instead of the project vault' },
|
|
17
|
+
'vault-password-stdin': {
|
|
18
|
+
type: 'boolean',
|
|
19
|
+
description: 'Read your TypeVault password(s) from stdin, one per line (for scripts)',
|
|
20
|
+
},
|
|
21
|
+
},
|
|
13
22
|
subCommands: {
|
|
14
23
|
init,
|
|
15
|
-
|
|
24
|
+
join: joinCommand,
|
|
25
|
+
member,
|
|
26
|
+
identity,
|
|
27
|
+
unlock,
|
|
28
|
+
lock,
|
|
29
|
+
passwd,
|
|
30
|
+
tier,
|
|
16
31
|
ceiling,
|
|
17
32
|
signer,
|
|
18
33
|
key,
|
|
@@ -35,8 +50,16 @@ const main = defineCommand({
|
|
|
35
50
|
},
|
|
36
51
|
})
|
|
37
52
|
|
|
38
|
-
const
|
|
39
|
-
const
|
|
53
|
+
const argv = process.argv.slice(2)
|
|
54
|
+
const split = argv.includes('--') ? argv.indexOf('--') : argv.length
|
|
55
|
+
const isGlobalFlag = (arg: string): boolean => arg === '-g' || arg === '--global'
|
|
56
|
+
const PASSWORD_STDIN = '--vault-password-stdin'
|
|
57
|
+
// -g and --vault-password-stdin apply to every command, so they are taken out here; anything after `--` belongs to
|
|
58
|
+
// the child of `run`.
|
|
59
|
+
if (argv.slice(0, split).some(isGlobalFlag)) process.env['TYPEVAULT_GLOBAL'] = '1'
|
|
60
|
+
if (argv.slice(0, split).includes(PASSWORD_STDIN)) readPasswordsFromStdin()
|
|
61
|
+
const ownArgs = argv.slice(0, split).filter((arg) => !isGlobalFlag(arg) && arg !== PASSWORD_STDIN)
|
|
62
|
+
const rawArgs = [...ownArgs, ...argv.slice(split)]
|
|
40
63
|
const wantsUsage = ownArgs.length === 0 || ownArgs.some((arg) => arg === '--help' || arg === '-h')
|
|
41
64
|
|
|
42
65
|
if (wantsUsage) {
|
|
@@ -52,6 +75,11 @@ if (wantsUsage) {
|
|
|
52
75
|
}
|
|
53
76
|
// citty's usage errors (unknown command, missing argument): its own runner prints usage for them.
|
|
54
77
|
if (error instanceof Error && error.name === 'CLIError') await runMain(main)
|
|
55
|
-
else
|
|
78
|
+
else {
|
|
79
|
+
const message = error instanceof Error ? error.message : 'unexpected error'
|
|
80
|
+
process.stderr.write(`typevault: ${message.replaceAll(/[\r\n]+/g, ' ')}\n`)
|
|
81
|
+
if (process.env['TYPEVAULT_DEBUG'] === '1') console.error(error)
|
|
82
|
+
process.exit(1)
|
|
83
|
+
}
|
|
56
84
|
}
|
|
57
85
|
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { TypeVaultError } from '../core/errors'
|
|
2
|
+
|
|
3
|
+
export type Passwords = { readonly ask: (question: string) => Promise<string> }
|
|
4
|
+
|
|
5
|
+
let fromStdin = false
|
|
6
|
+
let stdinLines: Promise<string[]> | undefined
|
|
7
|
+
let nextLine = 0
|
|
8
|
+
|
|
9
|
+
/** Set by the global `--vault-password-stdin` flag: passwords are read from stdin, one per line, instead of a terminal. */
|
|
10
|
+
export function readPasswordsFromStdin(): void {
|
|
11
|
+
fromStdin = true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const passwordsFromStdin = (): boolean => fromStdin
|
|
15
|
+
|
|
16
|
+
export function passwords(): Passwords {
|
|
17
|
+
if (!fromStdin) return { ask: promptHidden }
|
|
18
|
+
return {
|
|
19
|
+
ask: async () => {
|
|
20
|
+
stdinLines ??= Bun.stdin.text().then((text) => text.split(/\r?\n/))
|
|
21
|
+
const line = (await stdinLines)[nextLine++]
|
|
22
|
+
if (line === undefined || line === '')
|
|
23
|
+
throw new TypeVaultError('invalid', '--vault-password-stdin needs one password per line')
|
|
24
|
+
return line
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A new password: asked twice on a terminal so a typo can't lock the owner out. */
|
|
30
|
+
export async function newPassword(source: Passwords): Promise<string> {
|
|
31
|
+
const password = await source.ask('New password: ')
|
|
32
|
+
if (!fromStdin && (await source.ask('Repeat it: ')) !== password) {
|
|
33
|
+
throw new TypeVaultError('invalid', 'the passwords do not match')
|
|
34
|
+
}
|
|
35
|
+
return password
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function promptHidden(question: string): Promise<string> {
|
|
39
|
+
const { stdin, stderr } = process
|
|
40
|
+
if (!stdin.isTTY) {
|
|
41
|
+
throw new TypeVaultError(
|
|
42
|
+
'locked',
|
|
43
|
+
'no terminal to ask for your TypeVault password (pass --vault-password-stdin to pipe it)',
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
stderr.write(question)
|
|
47
|
+
stdin.setRawMode(true)
|
|
48
|
+
stdin.resume()
|
|
49
|
+
const { promise, resolve, reject } = Promise.withResolvers<string>()
|
|
50
|
+
let typed = ''
|
|
51
|
+
const finish = (): void => {
|
|
52
|
+
stdin.off('data', onData)
|
|
53
|
+
stdin.setRawMode(false)
|
|
54
|
+
stdin.pause()
|
|
55
|
+
stderr.write('\n')
|
|
56
|
+
}
|
|
57
|
+
const onData = (chunk: Buffer): void => {
|
|
58
|
+
for (const char of chunk.toString('utf8')) {
|
|
59
|
+
if (char === '\r' || char === '\n') {
|
|
60
|
+
finish()
|
|
61
|
+
resolve(typed)
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
if (char === '\u0003') {
|
|
65
|
+
finish()
|
|
66
|
+
reject(new TypeVaultError('invalid', 'cancelled'))
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
typed = char === '\u007f' || char === '\b' ? [...typed].slice(0, -1).join('') : typed + char
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
stdin.on('data', onData)
|
|
73
|
+
return promise
|
|
74
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { generateX25519Identity, identityToRecipient } from 'age-encryption'
|
|
2
|
+
|
|
3
|
+
import { unseal } from '../core/crypto'
|
|
4
|
+
import { TypeVaultError } from '../core/errors'
|
|
5
|
+
import type { EntryName, Field, Preset } from '../core/naming'
|
|
6
|
+
import { decodePayload } from '../core/payload'
|
|
7
|
+
import { matchCode } from '../daemon/requests'
|
|
8
|
+
import { startPromptServer } from '../prompt'
|
|
9
|
+
|
|
10
|
+
const TTL_MS = 10 * 60_000
|
|
11
|
+
|
|
12
|
+
export type WebEntry = {
|
|
13
|
+
readonly name: EntryName
|
|
14
|
+
readonly preset?: Preset | undefined
|
|
15
|
+
readonly tier: string | undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Collects an entry's fields on the local one-time page. The page encrypts to a throwaway key held only by this
|
|
20
|
+
* process, which then stores the fields like `--stdin` would, so it works whatever access the caller has.
|
|
21
|
+
*/
|
|
22
|
+
export async function promptFields(
|
|
23
|
+
entry: WebEntry,
|
|
24
|
+
show: (url: string, code: string) => void,
|
|
25
|
+
options: { readonly ttlMs?: number } = {},
|
|
26
|
+
): Promise<Record<string, Field>> {
|
|
27
|
+
const identity = await generateX25519Identity()
|
|
28
|
+
const server = await startPromptServer()
|
|
29
|
+
try {
|
|
30
|
+
const code = matchCode()
|
|
31
|
+
const handle = server.open(
|
|
32
|
+
{
|
|
33
|
+
name: entry.name,
|
|
34
|
+
...(entry.preset === undefined ? {} : { preset: entry.preset }),
|
|
35
|
+
tier: entry.tier ?? 'your default tier',
|
|
36
|
+
reason: `Store ${entry.name} from your terminal`,
|
|
37
|
+
requester: 'typevault set',
|
|
38
|
+
code,
|
|
39
|
+
recipients: [await identityToRecipient(identity)],
|
|
40
|
+
expiresAt: new Date(Date.now() + (options.ttlMs ?? TTL_MS)),
|
|
41
|
+
},
|
|
42
|
+
{ validate: true },
|
|
43
|
+
)
|
|
44
|
+
show(handle.url, code)
|
|
45
|
+
try {
|
|
46
|
+
const payload = decodePayload(await unseal([identity], await handle.done))
|
|
47
|
+
if (payload.name !== entry.name) {
|
|
48
|
+
throw new TypeVaultError('tampered', 'the submitted secret does not match this entry')
|
|
49
|
+
}
|
|
50
|
+
await handle.accept()
|
|
51
|
+
return { ...payload.fields }
|
|
52
|
+
} catch (error) {
|
|
53
|
+
await handle.reject(
|
|
54
|
+
error instanceof TypeVaultError
|
|
55
|
+
? error
|
|
56
|
+
: new TypeVaultError('unavailable', 'the submitted secret could not be read'),
|
|
57
|
+
)
|
|
58
|
+
throw error
|
|
59
|
+
}
|
|
60
|
+
} finally {
|
|
61
|
+
await server.stop()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function openBrowser(url: string): boolean {
|
|
66
|
+
const command =
|
|
67
|
+
process.platform === 'darwin'
|
|
68
|
+
? ['open', url]
|
|
69
|
+
: process.platform === 'win32'
|
|
70
|
+
? ['cmd', '/c', 'start', '', url]
|
|
71
|
+
: ['xdg-open', url]
|
|
72
|
+
try {
|
|
73
|
+
Bun.spawn(command, { stdio: ['ignore', 'ignore', 'ignore'] }).unref()
|
|
74
|
+
return true
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (error instanceof Error) return false
|
|
77
|
+
throw error
|
|
78
|
+
}
|
|
79
|
+
}
|