-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest-git-nostr-auth.js
More file actions
285 lines (243 loc) · 9.39 KB
/
test-git-nostr-auth.js
File metadata and controls
285 lines (243 loc) · 9.39 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env node
/**
* Test git push/pull with Nostr NIP-98 authentication
*
* Usage: node test-git-nostr-auth.js
*
* Prerequisites:
* - JSS server running on localhost:4000 with --git flag
* - git-credential-nostr installed globally
* - git config --global credential.helper nostr
*
* This script:
* 1. Creates a test git repo on the server
* 2. Sets up ACL with authorized Nostr DID
* 3. Tests clone (public read)
* 4. Tests push with authorized key (should succeed)
* 5. Tests push with unauthorized key (should fail with 403)
* 6. Cleans up
*/
import { generateSecretKey, getPublicKey } from 'nostr-tools/pure';
import { bytesToHex } from '@noble/hashes/utils';
import { execSync, spawn } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import os from 'os';
const BASE_URL = process.env.TEST_URL || 'http://localhost:4000';
const DATA_ROOT = process.env.DATA_ROOT || '/home/melvin/jss/data';
const TEST_POD = 'test-git-nostr';
const TEST_REPO = 'test-repo';
// Generate keypairs for testing
const authorizedSk = generateSecretKey();
const authorizedPk = getPublicKey(authorizedSk);
const authorizedPrivHex = bytesToHex(authorizedSk);
const unauthorizedSk = generateSecretKey();
const unauthorizedPk = getPublicKey(unauthorizedSk);
const unauthorizedPrivHex = bytesToHex(unauthorizedSk);
let tempDir;
let passed = 0;
let failed = 0;
function log(msg) {
console.log(` ${msg}`);
}
function pass(test) {
console.log(`✓ ${test}`);
passed++;
}
function fail(test, error) {
console.log(`✗ ${test}`);
console.log(` Error: ${error}`);
failed++;
}
function exec(cmd, options = {}) {
try {
return execSync(cmd, { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'], ...options });
} catch (e) {
if (options.allowFail) {
return { error: true, stderr: e.stderr, stdout: e.stdout, status: e.status };
}
throw e;
}
}
async function setup() {
console.log('\n=== Setup ===\n');
// Create temp directory for client repos
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'git-nostr-test-'));
log(`Temp directory: ${tempDir}`);
// Create test pod directory
const podPath = path.join(DATA_ROOT, TEST_POD);
fs.ensureDirSync(podPath);
log(`Created pod: ${podPath}`);
// Create bare git repo
const repoPath = path.join(podPath, TEST_REPO);
fs.ensureDirSync(repoPath);
exec(`git init --bare`, { cwd: repoPath });
exec(`git config --local receive.denyCurrentBranch ignore`, { cwd: repoPath });
exec(`git config --local http.receivepack true`, { cwd: repoPath });
exec(`git symbolic-ref HEAD refs/heads/main`, { cwd: repoPath });
log(`Created bare repo: ${repoPath}`);
// Create ACL with authorized Nostr DID
const acl = {
"@context": {
"acl": "http://www.w3.org/ns/auth/acl#",
"foaf": "http://xmlns.com/foaf/0.1/"
},
"@graph": [
{
"@id": "#nostr",
"@type": "acl:Authorization",
"acl:agent": { "@id": `did:nostr:${authorizedPk}` },
"acl:accessTo": { "@id": `${BASE_URL}/${TEST_POD}/` },
"acl:mode": [
{ "@id": "acl:Read" },
{ "@id": "acl:Write" }
],
"acl:default": { "@id": `${BASE_URL}/${TEST_POD}/` }
},
{
"@id": "#public",
"@type": "acl:Authorization",
"acl:agentClass": { "@id": "foaf:Agent" },
"acl:accessTo": { "@id": `${BASE_URL}/${TEST_POD}/` },
"acl:mode": [{ "@id": "acl:Read" }],
"acl:default": { "@id": `${BASE_URL}/${TEST_POD}/` }
}
]
};
fs.writeJsonSync(path.join(podPath, '.acl'), acl, { spaces: 2 });
log(`Created ACL with authorized DID: did:nostr:${authorizedPk.slice(0, 16)}...`);
// Create initial commit in a temp client repo
const initRepo = path.join(tempDir, 'init');
fs.ensureDirSync(initRepo);
exec('git init', { cwd: initRepo });
exec('git config user.email "[email protected]"', { cwd: initRepo });
exec('git config user.name "Test User"', { cwd: initRepo });
fs.writeFileSync(path.join(initRepo, 'README.md'), '# Test Repo\n');
exec('git add .', { cwd: initRepo });
exec('git commit -m "Initial commit"', { cwd: initRepo });
exec('git branch -m main', { cwd: initRepo });
exec(`git remote add origin ${BASE_URL}/${TEST_POD}/${TEST_REPO}/`, { cwd: initRepo });
// Configure authorized key for initial push
exec(`git config nostr.privkey ${authorizedPrivHex}`, { cwd: initRepo });
log('Created initial commit');
}
async function testClone() {
console.log('\n=== Test: Clone (public read) ===\n');
const cloneDir = path.join(tempDir, 'clone-test');
try {
exec(`git clone ${BASE_URL}/${TEST_POD}/${TEST_REPO}/ ${cloneDir}`);
if (fs.existsSync(path.join(cloneDir, '.git'))) {
pass('Clone succeeded (public read works)');
} else {
fail('Clone', 'No .git directory created');
}
} catch (e) {
// Clone might fail if repo is empty, that's ok
if (e.message.includes('empty repository')) {
pass('Clone of empty repo handled correctly');
} else {
fail('Clone', e.message);
}
}
}
async function testPushAuthorized() {
console.log('\n=== Test: Push with authorized key ===\n');
const initRepo = path.join(tempDir, 'init');
try {
// Push should work with authorized key
exec('git push -u origin main', { cwd: initRepo });
pass('Push with authorized key succeeded');
} catch (e) {
fail('Push with authorized key', e.stderr || e.message);
}
}
async function testPushUnauthorized() {
console.log('\n=== Test: Push with unauthorized key ===\n');
const cloneDir = path.join(tempDir, 'unauthorized-test');
try {
// Clone first (should work - public read)
exec(`git clone ${BASE_URL}/${TEST_POD}/${TEST_REPO}/ ${cloneDir}`);
exec('git config user.email "[email protected]"', { cwd: cloneDir });
exec('git config user.name "Test User"', { cwd: cloneDir });
// Configure unauthorized key
exec(`git config nostr.privkey ${unauthorizedPrivHex}`, { cwd: cloneDir });
// Make a change
fs.appendFileSync(path.join(cloneDir, 'README.md'), '\nUnauthorized change\n');
exec('git add .', { cwd: cloneDir });
exec('git commit -m "Unauthorized change"', { cwd: cloneDir });
// Push should fail with 403
const result = exec('git push 2>&1', { cwd: cloneDir, allowFail: true });
if (result.error && (result.stderr?.includes('403') || result.stdout?.includes('403'))) {
pass('Push with unauthorized key correctly rejected (403)');
} else if (result.error) {
// Check if it failed for the right reason
if (result.stderr?.includes('Authentication failed') || result.stderr?.includes('403')) {
pass('Push with unauthorized key correctly rejected');
} else {
fail('Push with unauthorized key', `Unexpected error: ${result.stderr}`);
}
} else {
fail('Push with unauthorized key', 'Push should have failed but succeeded');
}
} catch (e) {
if (e.stderr?.includes('403') || e.message?.includes('403')) {
pass('Push with unauthorized key correctly rejected (403)');
} else {
fail('Push with unauthorized key', e.stderr || e.message);
}
}
}
async function testPullAfterPush() {
console.log('\n=== Test: Pull after push ===\n');
const pullDir = path.join(tempDir, 'pull-test');
try {
exec(`git clone ${BASE_URL}/${TEST_POD}/${TEST_REPO}/ ${pullDir}`);
const readme = fs.readFileSync(path.join(pullDir, 'README.md'), 'utf8');
if (readme.includes('Test Repo')) {
pass('Pull retrieved pushed content');
} else {
fail('Pull after push', 'Content mismatch');
}
} catch (e) {
fail('Pull after push', e.stderr || e.message);
}
}
async function cleanup() {
console.log('\n=== Cleanup ===\n');
// Remove temp directory
if (tempDir) {
fs.removeSync(tempDir);
log(`Removed temp directory: ${tempDir}`);
}
// Remove test pod
const podPath = path.join(DATA_ROOT, TEST_POD);
if (fs.existsSync(podPath)) {
fs.removeSync(podPath);
log(`Removed test pod: ${podPath}`);
}
}
async function main() {
console.log('╔════════════════════════════════════════════════════╗');
console.log('║ Git Push/Pull with Nostr Authentication Test ║');
console.log('╚════════════════════════════════════════════════════╝');
console.log(`\nServer: ${BASE_URL}`);
console.log(`Data root: ${DATA_ROOT}`);
console.log(`Authorized pubkey: ${authorizedPk.slice(0, 16)}...`);
console.log(`Unauthorized pubkey: ${unauthorizedPk.slice(0, 16)}...`);
try {
await setup();
await testPushAuthorized();
await testClone();
await testPullAfterPush();
await testPushUnauthorized();
} catch (e) {
console.error('\nFatal error:', e.message);
} finally {
await cleanup();
}
console.log('\n╔════════════════════════════════════════════════════╗');
console.log(`║ Results: ${passed} passed, ${failed} failed${' '.repeat(27 - String(passed).length - String(failed).length)}║`);
console.log('╚════════════════════════════════════════════════════╝\n');
process.exit(failed > 0 ? 1 : 0);
}
main().catch(console.error);