-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage.ts
More file actions
231 lines (201 loc) · 7.53 KB
/
storage.ts
File metadata and controls
231 lines (201 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import * as fs from "node:fs";
import * as path from "node:path";
import * as os from "node:os";
import * as crypto from "node:crypto";
import type { AgentConnection, HostIdentity, ProviderConfig, Storage } from "@auth/agent";
const DEFAULT_DIR = path.join(os.homedir(), ".agent-auth");
const ALGORITHM = "aes-256-gcm";
const IV_LENGTH = 12;
const AUTH_TAG_LENGTH = 16;
function deriveKey(secret: string): Buffer {
return crypto.createHash("sha256").update(secret).digest();
}
function encrypt(data: string, secret: string): string {
const key = deriveKey(secret);
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv(ALGORITHM, key, iv, {
authTagLength: AUTH_TAG_LENGTH,
});
const encrypted = Buffer.concat([cipher.update(data, "utf-8"), cipher.final()]);
const tag = cipher.getAuthTag();
return Buffer.concat([iv, tag, encrypted]).toString("base64url");
}
function decrypt(encoded: string, secret: string): string {
const key = deriveKey(secret);
const buf = Buffer.from(encoded, "base64url");
const iv = buf.subarray(0, IV_LENGTH);
const tag = buf.subarray(IV_LENGTH, IV_LENGTH + AUTH_TAG_LENGTH);
const ciphertext = buf.subarray(IV_LENGTH + AUTH_TAG_LENGTH);
const decipher = crypto.createDecipheriv(ALGORITHM, key, iv, {
authTagLength: AUTH_TAG_LENGTH,
});
decipher.setAuthTag(tag);
return decipher.update(ciphertext) + decipher.final("utf-8");
}
export interface FileStorageOptions {
/**
* Custom directory for storing agent data.
* @default ~/.agent-auth
*/
directory?: string;
/**
* Encryption key for private keys at rest.
*
* When set, private keys in host identity and agent connection files
* are encrypted with AES-256-GCM before writing to disk and decrypted
* transparently on read.
*
* Set via env: `AGENT_AUTH_ENCRYPTION_KEY`.
* Existing unencrypted files are read normally and re-encrypted on
* next write.
*/
encryptionKey?: string;
}
/**
* File-based storage that persists data to disk as JSON files.
* Data is stored in ~/.agent-auth/ by default, organized as:
* host.json — single host identity (shared across providers)
* agents/<agent-id>.json
* providers/<encoded-issuer>.json
*
* Private keys are encrypted at rest when an encryption key is provided.
* Files containing secrets are written with mode 0o600.
*/
export class FileStorage implements Storage {
private readonly dir: string;
private readonly encKey: string | null;
constructor(dir?: string, encryptionKey?: string) {
this.dir = dir ?? DEFAULT_DIR;
this.encKey = encryptionKey ?? process.env.AGENT_AUTH_ENCRYPTION_KEY ?? null;
for (const sub of ["agents", "providers"]) {
fs.mkdirSync(path.join(this.dir, sub), { recursive: true });
}
fs.mkdirSync(this.dir, { recursive: true });
if (!this.encKey) {
const hint = "Set AGENT_AUTH_ENCRYPTION_KEY or pass encryptionKey to FileStorage.";
process.stderr.write(
`[agent-auth] WARNING: Private keys will be stored unencrypted. ${hint}\n`,
);
}
}
private encode(key: string): string {
return encodeURIComponent(key).replace(/%/g, "_");
}
private readJSON<T>(filePath: string): T | null {
try {
return JSON.parse(fs.readFileSync(filePath, "utf-8")) as T;
} catch {
return null;
}
}
private writeJSON(filePath: string, data: unknown, secret = false): void {
const tmpPath = `${filePath}.${Date.now()}.tmp`;
fs.writeFileSync(tmpPath, JSON.stringify(data, null, 2), {
encoding: "utf-8",
mode: secret ? 0o600 : undefined,
});
fs.renameSync(tmpPath, filePath);
}
private deleteFile(filePath: string): void {
try {
fs.unlinkSync(filePath);
} catch {
/* noop */
}
}
private listDir(dir: string): string[] {
try {
return fs.readdirSync(dir).filter((f) => f.endsWith(".json"));
} catch {
return [];
}
}
private encryptKeypair(keypair: unknown): unknown {
if (!this.encKey) return keypair;
return { __encrypted: encrypt(JSON.stringify(keypair), this.encKey) };
}
private decryptKeypair<T>(stored: unknown): T {
if (stored && typeof stored === "object" && "__encrypted" in stored) {
if (!this.encKey) {
throw new Error("Private key is encrypted but no AGENT_AUTH_ENCRYPTION_KEY is set.");
}
const raw = (stored as { __encrypted: string }).__encrypted;
return JSON.parse(decrypt(raw, this.encKey)) as T;
}
return stored as T;
}
// ─── Host Identity ──────────────────────────────────────────
private get hostPath(): string {
return path.join(this.dir, "host.json");
}
async getHostIdentity(): Promise<HostIdentity | null> {
const stored = this.readJSON<{
keypair: unknown;
createdAt: number;
}>(this.hostPath);
if (!stored) return null;
return {
...stored,
keypair: this.decryptKeypair(stored.keypair),
} as HostIdentity;
}
async setHostIdentity(host: HostIdentity): Promise<void> {
this.writeJSON(this.hostPath, { ...host, keypair: this.encryptKeypair(host.keypair) }, true);
}
async deleteHostIdentity(): Promise<void> {
this.deleteFile(this.hostPath);
}
// ─── Agent Connection ───────────────────────────────────────
async getAgentConnection(agentId: string): Promise<AgentConnection | null> {
const stored = this.readJSON<AgentConnection & { agentKeypair: unknown }>(
path.join(this.dir, "agents", `${this.encode(agentId)}.json`),
);
if (!stored) return null;
return {
...stored,
agentKeypair: this.decryptKeypair(stored.agentKeypair),
} as AgentConnection;
}
async setAgentConnection(agentId: string, conn: AgentConnection): Promise<void> {
this.writeJSON(
path.join(this.dir, "agents", `${this.encode(agentId)}.json`),
{ ...conn, agentKeypair: this.encryptKeypair(conn.agentKeypair) },
true,
);
}
async deleteAgentConnection(agentId: string): Promise<void> {
this.deleteFile(path.join(this.dir, "agents", `${this.encode(agentId)}.json`));
}
async listAgentConnections(): Promise<AgentConnection[]> {
const files = this.listDir(path.join(this.dir, "agents"));
const result: AgentConnection[] = [];
for (const file of files) {
const stored = this.readJSON<AgentConnection & { agentKeypair: unknown }>(
path.join(this.dir, "agents", file),
);
if (stored) {
result.push({
...stored,
agentKeypair: this.decryptKeypair(stored.agentKeypair),
} as AgentConnection);
}
}
return result;
}
// ─── Provider Config ────────────────────────────────────────
async getProviderConfig(issuer: string): Promise<ProviderConfig | null> {
return this.readJSON(path.join(this.dir, "providers", `${this.encode(issuer)}.json`));
}
async setProviderConfig(issuer: string, config: ProviderConfig): Promise<void> {
this.writeJSON(path.join(this.dir, "providers", `${this.encode(issuer)}.json`), config);
}
async listProviderConfigs(): Promise<ProviderConfig[]> {
const files = this.listDir(path.join(this.dir, "providers"));
const result: ProviderConfig[] = [];
for (const file of files) {
const config = this.readJSON<ProviderConfig>(path.join(this.dir, "providers", file));
if (config) result.push(config);
}
return result;
}
}