@sekyuriti/attest 0.1.0 → 0.2.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.
Files changed (2) hide show
  1. package/bin/attest.js +482 -0
  2. package/package.json +7 -7
package/bin/attest.js ADDED
@@ -0,0 +1,482 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { spawn } = require("child_process");
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const https = require("https");
7
+ const http = require("http");
8
+ const crypto = require("crypto");
9
+ const os = require("os");
10
+ const readline = require("readline");
11
+
12
+ // ANSI colors
13
+ const c = {
14
+ reset: "\x1b[0m",
15
+ bold: "\x1b[1m",
16
+ dim: "\x1b[2m",
17
+ black: "\x1b[30m",
18
+ white: "\x1b[37m",
19
+ gray: "\x1b[90m",
20
+ inverse: "\x1b[7m",
21
+ };
22
+
23
+ const CONFIG_DIR = path.join(os.homedir(), ".attest");
24
+ const CONFIG_FILE = path.join(CONFIG_DIR, "config.json");
25
+ const API_BASE = "https://sekyuriti.build";
26
+
27
+ // ═══════════════════════════════════════════════════════════════════
28
+ // UTILITIES
29
+ // ═══════════════════════════════════════════════════════════════════
30
+
31
+ function log(msg) {
32
+ console.log(msg);
33
+ }
34
+
35
+ function logBold(msg) {
36
+ console.log(`${c.bold}${msg}${c.reset}`);
37
+ }
38
+
39
+ function logDim(msg) {
40
+ console.log(`${c.dim}${msg}${c.reset}`);
41
+ }
42
+
43
+ function logBox(lines) {
44
+ const maxLen = Math.max(...lines.map((l) => l.length));
45
+ const top = "┌" + "─".repeat(maxLen + 2) + "┐";
46
+ const bot = "└" + "─".repeat(maxLen + 2) + "┘";
47
+ log(top);
48
+ for (const line of lines) {
49
+ log("│ " + line.padEnd(maxLen) + " │");
50
+ }
51
+ log(bot);
52
+ }
53
+
54
+ function sleep(ms) {
55
+ return new Promise((r) => setTimeout(r, ms));
56
+ }
57
+
58
+ function fetch(url, options = {}) {
59
+ return new Promise((resolve, reject) => {
60
+ const isHttps = url.startsWith("https");
61
+ const lib = isHttps ? https : http;
62
+ const urlObj = new URL(url);
63
+
64
+ const req = lib.request(
65
+ {
66
+ hostname: urlObj.hostname,
67
+ port: urlObj.port || (isHttps ? 443 : 80),
68
+ path: urlObj.pathname + urlObj.search,
69
+ method: options.method || "GET",
70
+ headers: options.headers || {},
71
+ },
72
+ (res) => {
73
+ let data = "";
74
+ res.on("data", (chunk) => (data += chunk));
75
+ res.on("end", () => {
76
+ resolve({
77
+ ok: res.statusCode >= 200 && res.statusCode < 300,
78
+ status: res.statusCode,
79
+ json: () => Promise.resolve(JSON.parse(data)),
80
+ text: () => Promise.resolve(data),
81
+ });
82
+ });
83
+ }
84
+ );
85
+
86
+ req.on("error", reject);
87
+
88
+ if (options.body) {
89
+ req.write(options.body);
90
+ }
91
+
92
+ req.end();
93
+ });
94
+ }
95
+
96
+ function openBrowser(url) {
97
+ const platform = process.platform;
98
+ let cmd;
99
+
100
+ if (platform === "darwin") {
101
+ cmd = "open";
102
+ } else if (platform === "win32") {
103
+ cmd = "start";
104
+ } else {
105
+ cmd = "xdg-open";
106
+ }
107
+
108
+ spawn(cmd, [url], { detached: true, stdio: "ignore" }).unref();
109
+ }
110
+
111
+ function loadConfig() {
112
+ try {
113
+ if (fs.existsSync(CONFIG_FILE)) {
114
+ return JSON.parse(fs.readFileSync(CONFIG_FILE, "utf-8"));
115
+ }
116
+ } catch (e) {}
117
+ return null;
118
+ }
119
+
120
+ function saveConfig(config) {
121
+ if (!fs.existsSync(CONFIG_DIR)) {
122
+ fs.mkdirSync(CONFIG_DIR, { recursive: true });
123
+ }
124
+ fs.writeFileSync(CONFIG_FILE, JSON.stringify(config, null, 2));
125
+ }
126
+
127
+ function clearConfig() {
128
+ if (fs.existsSync(CONFIG_FILE)) {
129
+ fs.unlinkSync(CONFIG_FILE);
130
+ }
131
+ }
132
+
133
+ // ═══════════════════════════════════════════════════════════════════
134
+ // HEADER
135
+ // ═══════════════════════════════════════════════════════════════════
136
+
137
+ function printHeader() {
138
+ log("");
139
+ logBold(" █▀▀ █▀▀ █▄▀ █▄█ █ █ █▀█ █ ▀█▀ █");
140
+ logBold(" ▄▄█ ██▄ █ █ █ █▄█ █▀▄ █ █ █");
141
+ log("");
142
+ logDim(" ATTEST CLI v0.2.0");
143
+ log("");
144
+ }
145
+
146
+ // ═══════════════════════════════════════════════════════════════════
147
+ // LOGIN COMMAND
148
+ // ═══════════════════════════════════════════════════════════════════
149
+
150
+ async function cmdLogin() {
151
+ printHeader();
152
+
153
+ // Check if already logged in
154
+ const existing = loadConfig();
155
+ if (existing && existing.token) {
156
+ log(" You are already logged in as:");
157
+ logBold(` ${existing.email}`);
158
+ log("");
159
+ log(` Project: ${existing.projectName || "Unknown"}`);
160
+ logDim(` Key: ${existing.publicKey}`);
161
+ log("");
162
+ log(" Run 'attest logout' to sign out.");
163
+ log("");
164
+ return;
165
+ }
166
+
167
+ // Generate session token
168
+ const sessionId = crypto.randomBytes(16).toString("hex");
169
+ const authUrl = `${API_BASE}/cli/auth?session=${sessionId}`;
170
+
171
+ log(" Opening browser to authenticate...");
172
+ log("");
173
+ logBox([
174
+ "Waiting for browser confirmation...",
175
+ "",
176
+ `${authUrl.slice(0, 45)}...`,
177
+ ]);
178
+ log("");
179
+
180
+ // Open browser
181
+ openBrowser(authUrl);
182
+
183
+ // Poll for completion
184
+ let attempts = 0;
185
+ const maxAttempts = 120; // 2 minutes
186
+
187
+ process.stdout.write(" Waiting");
188
+
189
+ while (attempts < maxAttempts) {
190
+ try {
191
+ const res = await fetch(
192
+ `${API_BASE}/api/v2/attest/cli/poll?session=${sessionId}`
193
+ );
194
+
195
+ if (res.ok) {
196
+ const data = await res.json();
197
+
198
+ if (data.status === "completed") {
199
+ process.stdout.write("\n\n");
200
+ log(` ${c.bold}✓${c.reset} Authenticated as ${c.bold}${data.email}${c.reset}`);
201
+ log(` ${c.bold}✓${c.reset} Project: ${c.bold}${data.projectName}${c.reset}`);
202
+ log(` ${c.bold}✓${c.reset} Credentials saved to ~/.attest/config.json`);
203
+ log("");
204
+
205
+ saveConfig({
206
+ token: data.token,
207
+ email: data.email,
208
+ projectId: data.projectId,
209
+ projectName: data.projectName,
210
+ publicKey: data.publicKey,
211
+ apiKey: data.apiKey,
212
+ });
213
+
214
+ return;
215
+ }
216
+ }
217
+ } catch (e) {}
218
+
219
+ process.stdout.write(".");
220
+ await sleep(1000);
221
+ attempts++;
222
+ }
223
+
224
+ process.stdout.write("\n\n");
225
+ log(" ✗ Authentication timed out. Please try again.");
226
+ log("");
227
+ }
228
+
229
+ // ═══════════════════════════════════════════════════════════════════
230
+ // LOGOUT COMMAND
231
+ // ═══════════════════════════════════════════════════════════════════
232
+
233
+ async function cmdLogout() {
234
+ printHeader();
235
+
236
+ const config = loadConfig();
237
+
238
+ if (!config || !config.token) {
239
+ log(" You are not logged in.");
240
+ log("");
241
+ return;
242
+ }
243
+
244
+ clearConfig();
245
+
246
+ log(` ${c.bold}✓${c.reset} Logged out successfully`);
247
+ log(` ${c.dim}Credentials removed from ~/.attest/config.json${c.reset}`);
248
+ log("");
249
+ }
250
+
251
+ // ═══════════════════════════════════════════════════════════════════
252
+ // STATUS COMMAND
253
+ // ═══════════════════════════════════════════════════════════════════
254
+
255
+ async function cmdStatus() {
256
+ printHeader();
257
+
258
+ const config = loadConfig();
259
+
260
+ if (!config || !config.token) {
261
+ log(" You are not logged in.");
262
+ log(" Run 'attest login' to authenticate.");
263
+ log("");
264
+ return;
265
+ }
266
+
267
+ log(` Account: ${c.bold}${config.email}${c.reset}`);
268
+ log(` Project: ${c.bold}${config.projectName}${c.reset}`);
269
+ log(` Key: ${c.dim}${config.publicKey}${c.reset}`);
270
+ log("");
271
+
272
+ // Fetch usage stats
273
+ process.stdout.write(" Fetching usage stats...");
274
+
275
+ try {
276
+ const res = await fetch(
277
+ `${API_BASE}/api/v2/attest/cli/status?key=${config.publicKey}`,
278
+ {
279
+ headers: {
280
+ Authorization: `Bearer ${config.token}`,
281
+ },
282
+ }
283
+ );
284
+
285
+ if (res.ok) {
286
+ const data = await res.json();
287
+ process.stdout.write("\r" + " ".repeat(30) + "\r");
288
+
289
+ log(" ┌─────────────────────────────────────────┐");
290
+ log(` │ MONTHLY USAGE │`);
291
+ log(" ├─────────────────────────────────────────┤");
292
+
293
+ const used = data.used || 0;
294
+ const limit = data.limit || 10000;
295
+ const percent = Math.round((used / limit) * 100);
296
+ const barLen = 20;
297
+ const filled = Math.round((percent / 100) * barLen);
298
+ const bar = "█".repeat(filled) + "░".repeat(barLen - filled);
299
+
300
+ log(` │ ${bar} ${percent}% │`);
301
+ log(` │ ${used.toLocaleString()} / ${limit.toLocaleString()} requests │`);
302
+ log(" └─────────────────────────────────────────┘");
303
+ log("");
304
+
305
+ if (data.tier) {
306
+ logDim(` Tier: ${data.tier.toUpperCase()}`);
307
+ log("");
308
+ }
309
+ } else {
310
+ process.stdout.write("\n");
311
+ log(" Could not fetch usage stats.");
312
+ log("");
313
+ }
314
+ } catch (e) {
315
+ process.stdout.write("\n");
316
+ log(" Could not fetch usage stats.");
317
+ log("");
318
+ }
319
+ }
320
+
321
+ // ═══════════════════════════════════════════════════════════════════
322
+ // INIT COMMAND
323
+ // ═══════════════════════════════════════════════════════════════════
324
+
325
+ async function cmdInit() {
326
+ printHeader();
327
+
328
+ const config = loadConfig();
329
+
330
+ if (!config || !config.token) {
331
+ log(" You are not logged in.");
332
+ log(" Run 'attest login' first.");
333
+ log("");
334
+ return;
335
+ }
336
+
337
+ // Check if .env exists
338
+ const envPath = path.join(process.cwd(), ".env");
339
+ const envLocalPath = path.join(process.cwd(), ".env.local");
340
+ let targetPath = envLocalPath;
341
+
342
+ if (fs.existsSync(envLocalPath)) {
343
+ targetPath = envLocalPath;
344
+ } else if (fs.existsSync(envPath)) {
345
+ targetPath = envPath;
346
+ }
347
+
348
+ const envVars = `
349
+ # ATTEST Configuration
350
+ NEXT_PUBLIC_ATTEST_KEY=${config.publicKey}
351
+ ATTEST_SECRET_KEY=${config.apiKey}
352
+ `.trim();
353
+
354
+ if (fs.existsSync(targetPath)) {
355
+ // Append to existing
356
+ let content = fs.readFileSync(targetPath, "utf-8");
357
+
358
+ if (content.includes("ATTEST_KEY")) {
359
+ log(" ATTEST is already configured in your .env file.");
360
+ log("");
361
+ return;
362
+ }
363
+
364
+ content += "\n\n" + envVars + "\n";
365
+ fs.writeFileSync(targetPath, content);
366
+
367
+ log(` ${c.bold}✓${c.reset} Added ATTEST config to ${path.basename(targetPath)}`);
368
+ } else {
369
+ // Create new .env.local
370
+ fs.writeFileSync(envLocalPath, envVars + "\n");
371
+ log(` ${c.bold}✓${c.reset} Created .env.local with ATTEST config`);
372
+ }
373
+
374
+ log("");
375
+ log(" Next steps:");
376
+ log("");
377
+ log(` ${c.dim}1.${c.reset} Add the script to your app:`);
378
+ log("");
379
+ log(` ${c.dim}<Script`);
380
+ log(` src="https://sekyuriti.build/api/v2/attest/script/${config.publicKey}"`);
381
+ log(` strategy="beforeInteractive"`);
382
+ log(` />${c.reset}`);
383
+ log("");
384
+ log(` ${c.dim}2.${c.reset} Add the middleware (optional):`);
385
+ log("");
386
+ log(` ${c.dim}import { createAttestMiddleware } from '@sekyuriti/attest/middleware';${c.reset}`);
387
+ log("");
388
+ log(" Documentation: https://sekyuriti.build/docs/attest");
389
+ log("");
390
+ }
391
+
392
+ // ═══════════════════════════════════════════════════════════════════
393
+ // WHOAMI COMMAND
394
+ // ═══════════════════════════════════════════════════════════════════
395
+
396
+ async function cmdWhoami() {
397
+ const config = loadConfig();
398
+
399
+ if (!config || !config.token) {
400
+ console.log("Not logged in");
401
+ return;
402
+ }
403
+
404
+ console.log(config.email);
405
+ }
406
+
407
+ // ═══════════════════════════════════════════════════════════════════
408
+ // HELP
409
+ // ═══════════════════════════════════════════════════════════════════
410
+
411
+ function printHelp() {
412
+ printHeader();
413
+ log(" USAGE");
414
+ log("");
415
+ log(` ${c.bold}attest${c.reset} <command>`);
416
+ log("");
417
+ log(" COMMANDS");
418
+ log("");
419
+ log(` ${c.bold}login${c.reset} Authenticate with SEKYURITI`);
420
+ log(` ${c.bold}logout${c.reset} Sign out and clear credentials`);
421
+ log(` ${c.bold}status${c.reset} Show account and usage info`);
422
+ log(` ${c.bold}init${c.reset} Setup ATTEST in current project`);
423
+ log(` ${c.bold}whoami${c.reset} Print current user email`);
424
+ log(` ${c.bold}help${c.reset} Show this help message`);
425
+ log("");
426
+ log(" EXAMPLES");
427
+ log("");
428
+ logDim(" $ attest login");
429
+ logDim(" $ attest init");
430
+ logDim(" $ attest status");
431
+ log("");
432
+ log(" DOCUMENTATION");
433
+ log("");
434
+ log(" https://sekyuriti.build/docs/attest");
435
+ log("");
436
+ }
437
+
438
+ // ═══════════════════════════════════════════════════════════════════
439
+ // MAIN
440
+ // ═══════════════════════════════════════════════════════════════════
441
+
442
+ async function main() {
443
+ const args = process.argv.slice(2);
444
+ const cmd = args[0];
445
+
446
+ switch (cmd) {
447
+ case "login":
448
+ await cmdLogin();
449
+ break;
450
+ case "logout":
451
+ await cmdLogout();
452
+ break;
453
+ case "status":
454
+ await cmdStatus();
455
+ break;
456
+ case "init":
457
+ await cmdInit();
458
+ break;
459
+ case "whoami":
460
+ await cmdWhoami();
461
+ break;
462
+ case "help":
463
+ case "--help":
464
+ case "-h":
465
+ printHelp();
466
+ break;
467
+ case undefined:
468
+ printHelp();
469
+ break;
470
+ default:
471
+ log("");
472
+ log(` Unknown command: ${cmd}`);
473
+ log(" Run 'attest help' for usage.");
474
+ log("");
475
+ process.exit(1);
476
+ }
477
+ }
478
+
479
+ main().catch((e) => {
480
+ console.error("Error:", e.message);
481
+ process.exit(1);
482
+ });
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@sekyuriti/attest",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "API protection middleware for Next.js - verify requests with ATTEST",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "attest": "./bin/attest.js"
10
+ },
8
11
  "exports": {
9
12
  ".": {
10
13
  "types": "./dist/index.d.ts",
@@ -18,7 +21,8 @@
18
21
  }
19
22
  },
20
23
  "files": [
21
- "dist"
24
+ "dist",
25
+ "bin"
22
26
  ],
23
27
  "scripts": {
24
28
  "build": "tsup",
@@ -36,11 +40,7 @@
36
40
  ],
37
41
  "author": "SEKYURITI",
38
42
  "license": "MIT",
39
- "repository": {
40
- "type": "git",
41
- "url": "https://github.com/sekyuriti/attest"
42
- },
43
- "homepage": "https://sekyuriti.build/attest",
43
+ "homepage": "https://sekyuriti.build/guard/attest",
44
44
  "peerDependencies": {
45
45
  "next": ">=13.0.0"
46
46
  },