@lifeaitools/clauth 1.5.55 → 1.5.58
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/.clauth-skill/SKILL.md +216 -216
- package/.clauth-skill/references/keys-guide.md +270 -270
- package/.clauth-skill/references/operator-guide.md +148 -148
- package/README.md +211 -211
- package/cli/api.js +121 -121
- package/cli/commands/install.js +404 -404
- package/cli/commands/scrub.js +231 -231
- package/cli/commands/serve.js +7409 -7266
- package/cli/commands/uninstall.js +164 -164
- package/cli/fingerprint.js +133 -133
- package/install.ps1 +109 -51
- package/install.sh +49 -49
- package/package.json +61 -61
- package/scripts/bin/bootstrap-linux +0 -0
- package/scripts/bin/bootstrap-macos +0 -0
- package/scripts/bootstrap.cjs +121 -121
- package/scripts/build.sh +45 -45
- package/supabase/functions/auth-vault/index.ts +255 -255
- package/supabase/migrations/001_clauth_schema.sql +103 -103
- package/supabase/migrations/002_vault_helpers.sql +90 -90
- package/supabase/migrations/20260317_lockout.sql +26 -26
|
@@ -1,164 +1,164 @@
|
|
|
1
|
-
// cli/commands/uninstall.js
|
|
2
|
-
// clauth uninstall — full teardown: DB objects, Edge Function, secrets, skill, local config
|
|
3
|
-
//
|
|
4
|
-
// Reverses everything `clauth install` does:
|
|
5
|
-
// 1. Drops clauth tables, policies, triggers, functions from Supabase
|
|
6
|
-
// 2. Deletes auth-vault Edge Function
|
|
7
|
-
// 3. Removes CLAUTH_* secrets
|
|
8
|
-
// 4. Removes Claude skill directory
|
|
9
|
-
// 5. Clears local config (Conf store)
|
|
10
|
-
|
|
11
|
-
import { existsSync, rmSync } from 'fs';
|
|
12
|
-
import { join } from 'path';
|
|
13
|
-
import Conf from 'conf';
|
|
14
|
-
import chalk from 'chalk';
|
|
15
|
-
import ora from 'ora';
|
|
16
|
-
|
|
17
|
-
const MGMT = 'https://api.supabase.com/v1';
|
|
18
|
-
const SKILLS_DIR = process.env.CLAUTH_SKILLS_DIR ||
|
|
19
|
-
(process.platform === 'win32'
|
|
20
|
-
? join(process.env.USERPROFILE || '', '.claude', 'skills')
|
|
21
|
-
: join(process.env.HOME || '', '.claude', 'skills'));
|
|
22
|
-
|
|
23
|
-
// ─────────────────────────────────────────────
|
|
24
|
-
// Supabase Management API helper
|
|
25
|
-
// ─────────────────────────────────────────────
|
|
26
|
-
async function mgmt(pat, method, path, body) {
|
|
27
|
-
const res = await fetch(`${MGMT}${path}`, {
|
|
28
|
-
method,
|
|
29
|
-
headers: { 'Authorization': `Bearer ${pat}`, 'Content-Type': 'application/json' },
|
|
30
|
-
body: body ? JSON.stringify(body) : undefined,
|
|
31
|
-
});
|
|
32
|
-
if (!res.ok) {
|
|
33
|
-
const text = await res.text().catch(() => res.statusText);
|
|
34
|
-
throw new Error(`${method} ${path} → HTTP ${res.status}: ${text}`);
|
|
35
|
-
}
|
|
36
|
-
if (res.status === 204) return {};
|
|
37
|
-
const text = await res.text();
|
|
38
|
-
if (!text) return {};
|
|
39
|
-
return JSON.parse(text);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// ─────────────────────────────────────────────
|
|
43
|
-
// Main uninstall command
|
|
44
|
-
// ─────────────────────────────────────────────
|
|
45
|
-
export async function runUninstall(opts = {}) {
|
|
46
|
-
console.log(chalk.red('\n🗑️ clauth uninstall\n'));
|
|
47
|
-
|
|
48
|
-
const config = new Conf({ projectName: 'clauth' });
|
|
49
|
-
|
|
50
|
-
// ── Collect credentials ────────────────────
|
|
51
|
-
const ref = opts.ref || config.get('supabase_url')?.match(/https:\/\/(.+)\.supabase\.co/)?.[1];
|
|
52
|
-
const pat = opts.pat;
|
|
53
|
-
|
|
54
|
-
if (!ref) {
|
|
55
|
-
console.log(chalk.red(' Cannot determine Supabase project ref.'));
|
|
56
|
-
console.log(chalk.gray(' Use: clauth uninstall --ref <project-ref> --pat <personal-access-token>'));
|
|
57
|
-
process.exit(1);
|
|
58
|
-
}
|
|
59
|
-
if (!pat) {
|
|
60
|
-
console.log(chalk.red(' Supabase PAT required for teardown.'));
|
|
61
|
-
console.log(chalk.gray(' Use: clauth uninstall --ref <project-ref> --pat <personal-access-token>'));
|
|
62
|
-
process.exit(1);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
console.log(chalk.gray(` Project: ${ref}\n`));
|
|
66
|
-
|
|
67
|
-
// ── Step 1: Drop database objects ──────────
|
|
68
|
-
const s1 = ora('Dropping clauth database objects...').start();
|
|
69
|
-
const teardownSQL = `
|
|
70
|
-
-- Drop triggers
|
|
71
|
-
DROP TRIGGER IF EXISTS clauth_services_updated ON public.clauth_services;
|
|
72
|
-
|
|
73
|
-
-- Drop tables (CASCADE drops policies automatically)
|
|
74
|
-
DROP TABLE IF EXISTS public.clauth_audit CASCADE;
|
|
75
|
-
DROP TABLE IF EXISTS public.clauth_machines CASCADE;
|
|
76
|
-
DROP TABLE IF EXISTS public.clauth_services CASCADE;
|
|
77
|
-
|
|
78
|
-
-- Drop functions
|
|
79
|
-
DROP FUNCTION IF EXISTS public.clauth_touch_updated() CASCADE;
|
|
80
|
-
DROP FUNCTION IF EXISTS public.clauth_upsert_vault_secret(text, text) CASCADE;
|
|
81
|
-
DROP FUNCTION IF EXISTS public.clauth_get_vault_secret(text) CASCADE;
|
|
82
|
-
DROP FUNCTION IF EXISTS public.clauth_delete_vault_secret(text) CASCADE;
|
|
83
|
-
`;
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
await mgmt(pat, 'POST', `/projects/${ref}/database/query`, { query: teardownSQL });
|
|
87
|
-
s1.succeed('Database objects dropped (tables, triggers, functions, policies)');
|
|
88
|
-
} catch (e) {
|
|
89
|
-
s1.fail(`Database teardown failed: ${e.message}`);
|
|
90
|
-
console.log(chalk.yellow(' You may need to drop objects manually via SQL editor.'));
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// ── Step 2: Delete Edge Function ───────────
|
|
94
|
-
const s2 = ora('Deleting auth-vault Edge Function...').start();
|
|
95
|
-
try {
|
|
96
|
-
const res = await fetch(`${MGMT}/projects/${ref}/functions/auth-vault`, {
|
|
97
|
-
method: 'DELETE',
|
|
98
|
-
headers: { 'Authorization': `Bearer ${pat}` },
|
|
99
|
-
});
|
|
100
|
-
if (res.ok || res.status === 404) {
|
|
101
|
-
s2.succeed(res.status === 404
|
|
102
|
-
? 'Edge Function not found (already deleted)'
|
|
103
|
-
: 'Edge Function deleted');
|
|
104
|
-
} else {
|
|
105
|
-
const text = await res.text().catch(() => res.statusText);
|
|
106
|
-
throw new Error(`HTTP ${res.status}: ${text}`);
|
|
107
|
-
}
|
|
108
|
-
} catch (e) {
|
|
109
|
-
s2.fail(`Edge Function delete failed: ${e.message}`);
|
|
110
|
-
console.log(chalk.yellow(' Delete manually: Supabase Dashboard → Edge Functions → auth-vault → Delete'));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// ── Step 3: Remove secrets ─────────────────
|
|
114
|
-
const s3 = ora('Removing clauth secrets...').start();
|
|
115
|
-
try {
|
|
116
|
-
// Supabase Management API: DELETE /projects/{ref}/secrets with body listing secret names
|
|
117
|
-
const res = await fetch(`${MGMT}/projects/${ref}/secrets`, {
|
|
118
|
-
method: 'DELETE',
|
|
119
|
-
headers: { 'Authorization': `Bearer ${pat}`, 'Content-Type': 'application/json' },
|
|
120
|
-
body: JSON.stringify(['CLAUTH_HMAC_SALT', 'CLAUTH_ADMIN_BOOTSTRAP_TOKEN']),
|
|
121
|
-
});
|
|
122
|
-
if (res.ok) {
|
|
123
|
-
s3.succeed('Secrets removed (CLAUTH_HMAC_SALT, CLAUTH_ADMIN_BOOTSTRAP_TOKEN)');
|
|
124
|
-
} else {
|
|
125
|
-
const text = await res.text().catch(() => res.statusText);
|
|
126
|
-
throw new Error(`HTTP ${res.status}: ${text}`);
|
|
127
|
-
}
|
|
128
|
-
} catch (e) {
|
|
129
|
-
s3.warn(`Secret removal failed: ${e.message}`);
|
|
130
|
-
console.log(chalk.yellow(' Remove manually: Supabase → Settings → Edge Functions → Secrets'));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// ── Step 4: Remove Claude skill ────────────
|
|
134
|
-
const s4 = ora('Removing Claude skill...').start();
|
|
135
|
-
const skillDir = join(SKILLS_DIR, 'clauth');
|
|
136
|
-
if (existsSync(skillDir)) {
|
|
137
|
-
try {
|
|
138
|
-
rmSync(skillDir, { recursive: true, force: true });
|
|
139
|
-
s4.succeed(`Skill removed: ${skillDir}`);
|
|
140
|
-
} catch (e) {
|
|
141
|
-
s4.warn(`Could not remove skill: ${e.message}`);
|
|
142
|
-
}
|
|
143
|
-
} else {
|
|
144
|
-
s4.succeed('Skill directory not found (already removed)');
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// ── Step 5: Clear local config ─────────────
|
|
148
|
-
const s5 = ora('Clearing local config...').start();
|
|
149
|
-
try {
|
|
150
|
-
config.clear();
|
|
151
|
-
s5.succeed('Local config cleared');
|
|
152
|
-
} catch (e) {
|
|
153
|
-
s5.warn(`Could not clear config: ${e.message}`);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// ── Done ───────────────────────────────────
|
|
157
|
-
console.log('');
|
|
158
|
-
console.log(chalk.red('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'));
|
|
159
|
-
console.log(chalk.yellow(' ✓ clauth fully uninstalled'));
|
|
160
|
-
console.log(chalk.red('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'));
|
|
161
|
-
console.log('');
|
|
162
|
-
console.log(chalk.gray(' To reinstall: npx @lifeaitools/clauth install'));
|
|
163
|
-
console.log('');
|
|
164
|
-
}
|
|
1
|
+
// cli/commands/uninstall.js
|
|
2
|
+
// clauth uninstall — full teardown: DB objects, Edge Function, secrets, skill, local config
|
|
3
|
+
//
|
|
4
|
+
// Reverses everything `clauth install` does:
|
|
5
|
+
// 1. Drops clauth tables, policies, triggers, functions from Supabase
|
|
6
|
+
// 2. Deletes auth-vault Edge Function
|
|
7
|
+
// 3. Removes CLAUTH_* secrets
|
|
8
|
+
// 4. Removes Claude skill directory
|
|
9
|
+
// 5. Clears local config (Conf store)
|
|
10
|
+
|
|
11
|
+
import { existsSync, rmSync } from 'fs';
|
|
12
|
+
import { join } from 'path';
|
|
13
|
+
import Conf from 'conf';
|
|
14
|
+
import chalk from 'chalk';
|
|
15
|
+
import ora from 'ora';
|
|
16
|
+
|
|
17
|
+
const MGMT = 'https://api.supabase.com/v1';
|
|
18
|
+
const SKILLS_DIR = process.env.CLAUTH_SKILLS_DIR ||
|
|
19
|
+
(process.platform === 'win32'
|
|
20
|
+
? join(process.env.USERPROFILE || '', '.claude', 'skills')
|
|
21
|
+
: join(process.env.HOME || '', '.claude', 'skills'));
|
|
22
|
+
|
|
23
|
+
// ─────────────────────────────────────────────
|
|
24
|
+
// Supabase Management API helper
|
|
25
|
+
// ─────────────────────────────────────────────
|
|
26
|
+
async function mgmt(pat, method, path, body) {
|
|
27
|
+
const res = await fetch(`${MGMT}${path}`, {
|
|
28
|
+
method,
|
|
29
|
+
headers: { 'Authorization': `Bearer ${pat}`, 'Content-Type': 'application/json' },
|
|
30
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
31
|
+
});
|
|
32
|
+
if (!res.ok) {
|
|
33
|
+
const text = await res.text().catch(() => res.statusText);
|
|
34
|
+
throw new Error(`${method} ${path} → HTTP ${res.status}: ${text}`);
|
|
35
|
+
}
|
|
36
|
+
if (res.status === 204) return {};
|
|
37
|
+
const text = await res.text();
|
|
38
|
+
if (!text) return {};
|
|
39
|
+
return JSON.parse(text);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// ─────────────────────────────────────────────
|
|
43
|
+
// Main uninstall command
|
|
44
|
+
// ─────────────────────────────────────────────
|
|
45
|
+
export async function runUninstall(opts = {}) {
|
|
46
|
+
console.log(chalk.red('\n🗑️ clauth uninstall\n'));
|
|
47
|
+
|
|
48
|
+
const config = new Conf({ projectName: 'clauth' });
|
|
49
|
+
|
|
50
|
+
// ── Collect credentials ────────────────────
|
|
51
|
+
const ref = opts.ref || config.get('supabase_url')?.match(/https:\/\/(.+)\.supabase\.co/)?.[1];
|
|
52
|
+
const pat = opts.pat;
|
|
53
|
+
|
|
54
|
+
if (!ref) {
|
|
55
|
+
console.log(chalk.red(' Cannot determine Supabase project ref.'));
|
|
56
|
+
console.log(chalk.gray(' Use: clauth uninstall --ref <project-ref> --pat <personal-access-token>'));
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
if (!pat) {
|
|
60
|
+
console.log(chalk.red(' Supabase PAT required for teardown.'));
|
|
61
|
+
console.log(chalk.gray(' Use: clauth uninstall --ref <project-ref> --pat <personal-access-token>'));
|
|
62
|
+
process.exit(1);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
console.log(chalk.gray(` Project: ${ref}\n`));
|
|
66
|
+
|
|
67
|
+
// ── Step 1: Drop database objects ──────────
|
|
68
|
+
const s1 = ora('Dropping clauth database objects...').start();
|
|
69
|
+
const teardownSQL = `
|
|
70
|
+
-- Drop triggers
|
|
71
|
+
DROP TRIGGER IF EXISTS clauth_services_updated ON public.clauth_services;
|
|
72
|
+
|
|
73
|
+
-- Drop tables (CASCADE drops policies automatically)
|
|
74
|
+
DROP TABLE IF EXISTS public.clauth_audit CASCADE;
|
|
75
|
+
DROP TABLE IF EXISTS public.clauth_machines CASCADE;
|
|
76
|
+
DROP TABLE IF EXISTS public.clauth_services CASCADE;
|
|
77
|
+
|
|
78
|
+
-- Drop functions
|
|
79
|
+
DROP FUNCTION IF EXISTS public.clauth_touch_updated() CASCADE;
|
|
80
|
+
DROP FUNCTION IF EXISTS public.clauth_upsert_vault_secret(text, text) CASCADE;
|
|
81
|
+
DROP FUNCTION IF EXISTS public.clauth_get_vault_secret(text) CASCADE;
|
|
82
|
+
DROP FUNCTION IF EXISTS public.clauth_delete_vault_secret(text) CASCADE;
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await mgmt(pat, 'POST', `/projects/${ref}/database/query`, { query: teardownSQL });
|
|
87
|
+
s1.succeed('Database objects dropped (tables, triggers, functions, policies)');
|
|
88
|
+
} catch (e) {
|
|
89
|
+
s1.fail(`Database teardown failed: ${e.message}`);
|
|
90
|
+
console.log(chalk.yellow(' You may need to drop objects manually via SQL editor.'));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ── Step 2: Delete Edge Function ───────────
|
|
94
|
+
const s2 = ora('Deleting auth-vault Edge Function...').start();
|
|
95
|
+
try {
|
|
96
|
+
const res = await fetch(`${MGMT}/projects/${ref}/functions/auth-vault`, {
|
|
97
|
+
method: 'DELETE',
|
|
98
|
+
headers: { 'Authorization': `Bearer ${pat}` },
|
|
99
|
+
});
|
|
100
|
+
if (res.ok || res.status === 404) {
|
|
101
|
+
s2.succeed(res.status === 404
|
|
102
|
+
? 'Edge Function not found (already deleted)'
|
|
103
|
+
: 'Edge Function deleted');
|
|
104
|
+
} else {
|
|
105
|
+
const text = await res.text().catch(() => res.statusText);
|
|
106
|
+
throw new Error(`HTTP ${res.status}: ${text}`);
|
|
107
|
+
}
|
|
108
|
+
} catch (e) {
|
|
109
|
+
s2.fail(`Edge Function delete failed: ${e.message}`);
|
|
110
|
+
console.log(chalk.yellow(' Delete manually: Supabase Dashboard → Edge Functions → auth-vault → Delete'));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── Step 3: Remove secrets ─────────────────
|
|
114
|
+
const s3 = ora('Removing clauth secrets...').start();
|
|
115
|
+
try {
|
|
116
|
+
// Supabase Management API: DELETE /projects/{ref}/secrets with body listing secret names
|
|
117
|
+
const res = await fetch(`${MGMT}/projects/${ref}/secrets`, {
|
|
118
|
+
method: 'DELETE',
|
|
119
|
+
headers: { 'Authorization': `Bearer ${pat}`, 'Content-Type': 'application/json' },
|
|
120
|
+
body: JSON.stringify(['CLAUTH_HMAC_SALT', 'CLAUTH_ADMIN_BOOTSTRAP_TOKEN']),
|
|
121
|
+
});
|
|
122
|
+
if (res.ok) {
|
|
123
|
+
s3.succeed('Secrets removed (CLAUTH_HMAC_SALT, CLAUTH_ADMIN_BOOTSTRAP_TOKEN)');
|
|
124
|
+
} else {
|
|
125
|
+
const text = await res.text().catch(() => res.statusText);
|
|
126
|
+
throw new Error(`HTTP ${res.status}: ${text}`);
|
|
127
|
+
}
|
|
128
|
+
} catch (e) {
|
|
129
|
+
s3.warn(`Secret removal failed: ${e.message}`);
|
|
130
|
+
console.log(chalk.yellow(' Remove manually: Supabase → Settings → Edge Functions → Secrets'));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ── Step 4: Remove Claude skill ────────────
|
|
134
|
+
const s4 = ora('Removing Claude skill...').start();
|
|
135
|
+
const skillDir = join(SKILLS_DIR, 'clauth');
|
|
136
|
+
if (existsSync(skillDir)) {
|
|
137
|
+
try {
|
|
138
|
+
rmSync(skillDir, { recursive: true, force: true });
|
|
139
|
+
s4.succeed(`Skill removed: ${skillDir}`);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
s4.warn(`Could not remove skill: ${e.message}`);
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
s4.succeed('Skill directory not found (already removed)');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// ── Step 5: Clear local config ─────────────
|
|
148
|
+
const s5 = ora('Clearing local config...').start();
|
|
149
|
+
try {
|
|
150
|
+
config.clear();
|
|
151
|
+
s5.succeed('Local config cleared');
|
|
152
|
+
} catch (e) {
|
|
153
|
+
s5.warn(`Could not clear config: ${e.message}`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ── Done ───────────────────────────────────
|
|
157
|
+
console.log('');
|
|
158
|
+
console.log(chalk.red('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'));
|
|
159
|
+
console.log(chalk.yellow(' ✓ clauth fully uninstalled'));
|
|
160
|
+
console.log(chalk.red('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'));
|
|
161
|
+
console.log('');
|
|
162
|
+
console.log(chalk.gray(' To reinstall: npx @lifeaitools/clauth install'));
|
|
163
|
+
console.log('');
|
|
164
|
+
}
|
package/cli/fingerprint.js
CHANGED
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
// cli/fingerprint.js
|
|
2
|
-
// Collects stable hardware identifiers and derives HMAC tokens
|
|
3
|
-
// Works on Windows (primary) + Linux/macOS (fallback)
|
|
4
|
-
|
|
5
|
-
import { execSync } from "child_process";
|
|
6
|
-
import { createHmac, createHash } from "crypto";
|
|
7
|
-
import os from "os";
|
|
8
|
-
import fs from "fs";
|
|
9
|
-
import path from "path";
|
|
10
|
-
|
|
11
|
-
// ============================================================
|
|
12
|
-
// Machine ID collection
|
|
13
|
-
// ============================================================
|
|
14
|
-
|
|
15
|
-
// Cache path — avoids re-querying WMI/CIM on every daemon start.
|
|
16
|
-
// This eliminates the spawnSync cmd.exe ETIMEDOUT crash that occurs
|
|
17
|
-
// when PowerShell/WMI is slow on first call after boot.
|
|
18
|
-
const CACHE_FILE = path.join(os.tmpdir(), "clauth-machine.cache");
|
|
19
|
-
|
|
20
|
-
function readCache() {
|
|
21
|
-
try {
|
|
22
|
-
const raw = fs.readFileSync(CACHE_FILE, "utf8").trim();
|
|
23
|
-
// Validate: must be two non-empty lines (primary:secondary)
|
|
24
|
-
const [primary, secondary] = raw.split("\n");
|
|
25
|
-
if (primary && secondary) return { primary: primary.trim(), secondary: secondary.trim() };
|
|
26
|
-
} catch { /* cache miss */ }
|
|
27
|
-
return null;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function writeCache(primary, secondary) {
|
|
31
|
-
try { fs.writeFileSync(CACHE_FILE, `${primary}\n${secondary}`, "utf8"); } catch { /* best effort */ }
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function getMachineId() {
|
|
35
|
-
// Fast path: use cached IDs if available (avoids WMI/PowerShell on every restart)
|
|
36
|
-
const cached = readCache();
|
|
37
|
-
if (cached) return { ...cached, platform: os.platform() };
|
|
38
|
-
|
|
39
|
-
const platform = os.platform();
|
|
40
|
-
|
|
41
|
-
try {
|
|
42
|
-
if (platform === "win32") {
|
|
43
|
-
const psPath = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
|
|
44
|
-
// Increased timeout to 15s — CimInstance/WMI can be slow after boot or under load.
|
|
45
|
-
// Each call is wrapped independently so a partial failure still yields a result.
|
|
46
|
-
let uuid = "";
|
|
47
|
-
try {
|
|
48
|
-
uuid = execSync(
|
|
49
|
-
`${psPath} -NoProfile -Command "(Get-CimInstance Win32_ComputerSystemProduct).UUID"`,
|
|
50
|
-
{ encoding: "utf8", timeout: 15000, stdio: ["pipe", "pipe", "pipe"] }
|
|
51
|
-
).trim();
|
|
52
|
-
} catch { /* fall through to registry-only path */ }
|
|
53
|
-
|
|
54
|
-
let machineGuid = "";
|
|
55
|
-
try {
|
|
56
|
-
machineGuid = execSync(
|
|
57
|
-
`${psPath} -NoProfile -Command "(Get-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Cryptography').MachineGuid"`,
|
|
58
|
-
{ encoding: "utf8", timeout: 15000, stdio: ["pipe", "pipe", "pipe"] }
|
|
59
|
-
).trim();
|
|
60
|
-
} catch { /* fall through */ }
|
|
61
|
-
|
|
62
|
-
// Require at least one identifier
|
|
63
|
-
if (!uuid && !machineGuid) throw new Error("Could not read any Windows machine ID");
|
|
64
|
-
|
|
65
|
-
// Use hostname as fallback secondary if registry query failed
|
|
66
|
-
const primary = uuid || machineGuid;
|
|
67
|
-
const secondary = machineGuid || os.hostname();
|
|
68
|
-
|
|
69
|
-
writeCache(primary, secondary);
|
|
70
|
-
return { primary, secondary, platform: "win32" };
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (platform === "darwin") {
|
|
74
|
-
const uuid = execSync(
|
|
75
|
-
"ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3 }'",
|
|
76
|
-
{ encoding: "utf8", timeout: 5000, stdio: ["pipe", "pipe", "pipe"] }
|
|
77
|
-
).replace(/['"]/g, "").trim();
|
|
78
|
-
writeCache(uuid, os.hostname());
|
|
79
|
-
return { primary: uuid, secondary: os.hostname(), platform: "darwin" };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Linux
|
|
83
|
-
let uuid = "";
|
|
84
|
-
try { uuid = execSync("cat /etc/machine-id", { encoding: "utf8", timeout: 2000 }).trim(); }
|
|
85
|
-
catch { uuid = execSync("cat /var/lib/dbus/machine-id", { encoding: "utf8", timeout: 2000 }).trim(); }
|
|
86
|
-
writeCache(uuid, os.hostname());
|
|
87
|
-
return { primary: uuid, secondary: os.hostname(), platform: "linux" };
|
|
88
|
-
|
|
89
|
-
} catch (err) {
|
|
90
|
-
throw new Error(`Machine ID collection failed: ${err.message}`);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// ============================================================
|
|
95
|
-
// Derive stable machine hash (what gets stored in Supabase)
|
|
96
|
-
// ============================================================
|
|
97
|
-
|
|
98
|
-
export function getMachineHash() {
|
|
99
|
-
const { primary, secondary } = getMachineId();
|
|
100
|
-
return createHash("sha256")
|
|
101
|
-
.update(`${primary}:${secondary}`)
|
|
102
|
-
.digest("hex");
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// ============================================================
|
|
106
|
-
// Derive HMAC token for a given password + timestamp
|
|
107
|
-
// ============================================================
|
|
108
|
-
|
|
109
|
-
export function deriveToken(password, machineHash) {
|
|
110
|
-
const windowMs = 5 * 60 * 1000;
|
|
111
|
-
const window = Math.floor(Date.now() / windowMs);
|
|
112
|
-
const message = `${machineHash}:${window}`;
|
|
113
|
-
|
|
114
|
-
// Server reconstructs this — password + CLAUTH_HMAC_SALT
|
|
115
|
-
// Client sends token; server adds its SALT to the password before verifying
|
|
116
|
-
const token = createHmac("sha256", password)
|
|
117
|
-
.update(message)
|
|
118
|
-
.digest("hex");
|
|
119
|
-
|
|
120
|
-
return { token, timestamp: window * windowMs, machineHash };
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// ============================================================
|
|
124
|
-
// Derive HMAC seed hash (stored during machine registration)
|
|
125
|
-
// ============================================================
|
|
126
|
-
|
|
127
|
-
export function deriveSeedHash(machineHash, password) {
|
|
128
|
-
return createHash("sha256")
|
|
129
|
-
.update(`seed:${machineHash}:${password}`)
|
|
130
|
-
.digest("hex");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export default { getMachineHash, deriveToken, deriveSeedHash };
|
|
1
|
+
// cli/fingerprint.js
|
|
2
|
+
// Collects stable hardware identifiers and derives HMAC tokens
|
|
3
|
+
// Works on Windows (primary) + Linux/macOS (fallback)
|
|
4
|
+
|
|
5
|
+
import { execSync } from "child_process";
|
|
6
|
+
import { createHmac, createHash } from "crypto";
|
|
7
|
+
import os from "os";
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
import path from "path";
|
|
10
|
+
|
|
11
|
+
// ============================================================
|
|
12
|
+
// Machine ID collection
|
|
13
|
+
// ============================================================
|
|
14
|
+
|
|
15
|
+
// Cache path — avoids re-querying WMI/CIM on every daemon start.
|
|
16
|
+
// This eliminates the spawnSync cmd.exe ETIMEDOUT crash that occurs
|
|
17
|
+
// when PowerShell/WMI is slow on first call after boot.
|
|
18
|
+
const CACHE_FILE = path.join(os.tmpdir(), "clauth-machine.cache");
|
|
19
|
+
|
|
20
|
+
function readCache() {
|
|
21
|
+
try {
|
|
22
|
+
const raw = fs.readFileSync(CACHE_FILE, "utf8").trim();
|
|
23
|
+
// Validate: must be two non-empty lines (primary:secondary)
|
|
24
|
+
const [primary, secondary] = raw.split("\n");
|
|
25
|
+
if (primary && secondary) return { primary: primary.trim(), secondary: secondary.trim() };
|
|
26
|
+
} catch { /* cache miss */ }
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeCache(primary, secondary) {
|
|
31
|
+
try { fs.writeFileSync(CACHE_FILE, `${primary}\n${secondary}`, "utf8"); } catch { /* best effort */ }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function getMachineId() {
|
|
35
|
+
// Fast path: use cached IDs if available (avoids WMI/PowerShell on every restart)
|
|
36
|
+
const cached = readCache();
|
|
37
|
+
if (cached) return { ...cached, platform: os.platform() };
|
|
38
|
+
|
|
39
|
+
const platform = os.platform();
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
if (platform === "win32") {
|
|
43
|
+
const psPath = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
|
|
44
|
+
// Increased timeout to 15s — CimInstance/WMI can be slow after boot or under load.
|
|
45
|
+
// Each call is wrapped independently so a partial failure still yields a result.
|
|
46
|
+
let uuid = "";
|
|
47
|
+
try {
|
|
48
|
+
uuid = execSync(
|
|
49
|
+
`${psPath} -NoProfile -Command "(Get-CimInstance Win32_ComputerSystemProduct).UUID"`,
|
|
50
|
+
{ encoding: "utf8", timeout: 15000, stdio: ["pipe", "pipe", "pipe"] }
|
|
51
|
+
).trim();
|
|
52
|
+
} catch { /* fall through to registry-only path */ }
|
|
53
|
+
|
|
54
|
+
let machineGuid = "";
|
|
55
|
+
try {
|
|
56
|
+
machineGuid = execSync(
|
|
57
|
+
`${psPath} -NoProfile -Command "(Get-ItemProperty 'HKLM:\\SOFTWARE\\Microsoft\\Cryptography').MachineGuid"`,
|
|
58
|
+
{ encoding: "utf8", timeout: 15000, stdio: ["pipe", "pipe", "pipe"] }
|
|
59
|
+
).trim();
|
|
60
|
+
} catch { /* fall through */ }
|
|
61
|
+
|
|
62
|
+
// Require at least one identifier
|
|
63
|
+
if (!uuid && !machineGuid) throw new Error("Could not read any Windows machine ID");
|
|
64
|
+
|
|
65
|
+
// Use hostname as fallback secondary if registry query failed
|
|
66
|
+
const primary = uuid || machineGuid;
|
|
67
|
+
const secondary = machineGuid || os.hostname();
|
|
68
|
+
|
|
69
|
+
writeCache(primary, secondary);
|
|
70
|
+
return { primary, secondary, platform: "win32" };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (platform === "darwin") {
|
|
74
|
+
const uuid = execSync(
|
|
75
|
+
"ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3 }'",
|
|
76
|
+
{ encoding: "utf8", timeout: 5000, stdio: ["pipe", "pipe", "pipe"] }
|
|
77
|
+
).replace(/['"]/g, "").trim();
|
|
78
|
+
writeCache(uuid, os.hostname());
|
|
79
|
+
return { primary: uuid, secondary: os.hostname(), platform: "darwin" };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Linux
|
|
83
|
+
let uuid = "";
|
|
84
|
+
try { uuid = execSync("cat /etc/machine-id", { encoding: "utf8", timeout: 2000 }).trim(); }
|
|
85
|
+
catch { uuid = execSync("cat /var/lib/dbus/machine-id", { encoding: "utf8", timeout: 2000 }).trim(); }
|
|
86
|
+
writeCache(uuid, os.hostname());
|
|
87
|
+
return { primary: uuid, secondary: os.hostname(), platform: "linux" };
|
|
88
|
+
|
|
89
|
+
} catch (err) {
|
|
90
|
+
throw new Error(`Machine ID collection failed: ${err.message}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ============================================================
|
|
95
|
+
// Derive stable machine hash (what gets stored in Supabase)
|
|
96
|
+
// ============================================================
|
|
97
|
+
|
|
98
|
+
export function getMachineHash() {
|
|
99
|
+
const { primary, secondary } = getMachineId();
|
|
100
|
+
return createHash("sha256")
|
|
101
|
+
.update(`${primary}:${secondary}`)
|
|
102
|
+
.digest("hex");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// ============================================================
|
|
106
|
+
// Derive HMAC token for a given password + timestamp
|
|
107
|
+
// ============================================================
|
|
108
|
+
|
|
109
|
+
export function deriveToken(password, machineHash) {
|
|
110
|
+
const windowMs = 5 * 60 * 1000;
|
|
111
|
+
const window = Math.floor(Date.now() / windowMs);
|
|
112
|
+
const message = `${machineHash}:${window}`;
|
|
113
|
+
|
|
114
|
+
// Server reconstructs this — password + CLAUTH_HMAC_SALT
|
|
115
|
+
// Client sends token; server adds its SALT to the password before verifying
|
|
116
|
+
const token = createHmac("sha256", password)
|
|
117
|
+
.update(message)
|
|
118
|
+
.digest("hex");
|
|
119
|
+
|
|
120
|
+
return { token, timestamp: window * windowMs, machineHash };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ============================================================
|
|
124
|
+
// Derive HMAC seed hash (stored during machine registration)
|
|
125
|
+
// ============================================================
|
|
126
|
+
|
|
127
|
+
export function deriveSeedHash(machineHash, password) {
|
|
128
|
+
return createHash("sha256")
|
|
129
|
+
.update(`seed:${machineHash}:${password}`)
|
|
130
|
+
.digest("hex");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export default { getMachineHash, deriveToken, deriveSeedHash };
|