-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathsep10Compliance.test.js
More file actions
338 lines (273 loc) · 11.3 KB
/
Copy pathsep10Compliance.test.js
File metadata and controls
338 lines (273 loc) · 11.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const request = require("supertest");
const app = require("./index");
const StellarSdk = require("@stellar/stellar-sdk");
describe("SEP-10 Compliance Tests", () => {
let testKeypair;
let testPublicKey;
let authToken;
let challengeXDR;
beforeAll(() => {
// Generate test keypair for testing
testKeypair = StellarSdk.Keypair.random();
testPublicKey = testKeypair.publicKey();
});
describe("Challenge Generation (/auth/challenge)", () => {
it("should generate a SEP-10 compliant challenge", async () => {
const response = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
expect(response.body.success).toBe(true);
expect(response.body.challenge).toBeDefined();
expect(response.body.nonce).toBeDefined();
expect(response.body.expiresAt).toBeDefined();
// Verify challenge is valid XDR
const transaction = StellarSdk.TransactionBuilder.fromXDR(
response.body.challenge,
process.env.STELLAR_NETWORK_PASSPHRASE || "Test SDF Network ; September 2015"
);
// Verify transaction structure
expect(transaction.operations.length).toBe(1);
expect(transaction.operations[0].type).toBe("manageData");
expect(transaction.operations[0].source).toBe(testPublicKey);
// Verify operation name matches domain auth pattern
const expectedName = `${process.env.DOMAIN || "substream-protocol.com"} auth`;
expect(transaction.operations[0].name).toBe(expectedName);
// Verify timebounds are present and reasonable
expect(transaction.timebounds).toBeDefined();
expect(transaction.timebounds.minTime).toBeGreaterThan(0);
expect(transaction.timebounds.maxTime).toBeGreaterThan(transaction.timebounds.minTime);
challengeXDR = response.body.challenge;
});
it("should reject request without public key", async () => {
const response = await request(app)
.get("/auth/challenge")
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Stellar public key required");
});
it("should reject invalid public key format", async () => {
const response = await request(app)
.get("/auth/challenge")
.query({ publicKey: "invalid-key" })
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Invalid Stellar public key format");
});
it("should generate unique nonces for each request", async () => {
const response1 = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
const response2 = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
expect(response1.body.nonce).not.toBe(response2.body.nonce);
});
});
describe("Challenge Verification (/auth/verify)", () => {
it("should reject verification without required fields", async () => {
const response = await request(app)
.post("/auth/verify")
.send({})
.expect(400);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe(
"Missing required fields: publicKey, challengeXDR"
);
});
it("should reject invalid challenge XDR", async () => {
const response = await request(app)
.post("/auth/verify")
.send({
publicKey: testPublicKey,
challengeXDR: "invalid-xdr",
})
.expect(400);
expect(response.body.success).toBe(false);
});
it("should reject challenge with wrong public key", async () => {
const differentKeypair = StellarSdk.Keypair.random();
const response = await request(app)
.post("/auth/verify")
.send({
publicKey: differentKeypair.publicKey(),
challengeXDR: challengeXDR,
})
.expect(400);
expect(response.body.success).toBe(false);
});
});
describe("JWT Token Security", () => {
it("should issue JWT with correct claims after successful authentication", async () => {
// This test would require a funded testnet account for full integration
// For now, we test the token structure expectations
expect(true).toBe(true); // Placeholder
});
it("should reject requests with invalid JWT", async () => {
const response = await request(app)
.get("/auth/stellar/session")
.set("Authorization", "Bearer invalid-token")
.expect(403);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Invalid or expired token");
});
it("should reject requests without JWT", async () => {
const response = await request(app)
.get("/auth/stellar/session")
.expect(401);
expect(response.body.success).toBe(false);
expect(response.body.error).toBe("Access token required");
});
});
describe("SEP-10 Specification Compliance", () => {
it("should follow SEP-10 challenge transaction format", async () => {
const response = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
const transaction = StellarSdk.TransactionBuilder.fromXDR(
response.body.challenge,
process.env.STELLAR_NETWORK_PASSPHRASE || "Test SDF Network ; September 2015"
);
// SEP-10 requirements:
// 1. Transaction must have exactly one operation
expect(transaction.operations.length).toBe(1);
// 2. Operation must be manageData
expect(transaction.operations[0].type).toBe("manageData");
// 3. Operation source account must be the client account
expect(transaction.operations[0].source).toBe(testPublicKey);
// 4. Operation name must be <domain> auth
const expectedName = `${process.env.DOMAIN || "substream-protocol.com"} auth`;
expect(transaction.operations[0].name).toBe(expectedName);
// 5. Operation value must be a nonce
expect(transaction.operations[0].value).toBeDefined();
expect(transaction.operations[0].value.length).toBeGreaterThan(0);
// 6. Transaction must have timebounds
expect(transaction.timebounds).toBeDefined();
expect(transaction.timebounds.minTime).toBeDefined();
expect(transaction.timebounds.maxTime).toBeDefined();
// 7. Timebounds should be reasonable (5 minutes is standard)
const timeDiff = transaction.timebounds.maxTime - transaction.timebounds.minTime;
expect(timeDiff).toBeLessThanOrEqual(300); // 5 minutes
expect(timeDiff).toBeGreaterThan(0);
});
it("should use correct network passphrase", async () => {
const expectedPassphrase = process.env.STELLAR_NETWORK_PASSPHRASE || "Test SDF Network ; September 2015";
// This is verified implicitly by the fromXDR parsing succeeding
// If the network passphrase was wrong, parsing would fail
expect(expectedPassphrase).toBeDefined();
});
});
describe("Security Requirements", () => {
it("should have short-lived challenges (5 minutes)", async () => {
const response = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
const expiresAt = new Date(response.body.expiresAt);
const now = new Date();
const timeToExpiry = expiresAt - now;
// Should expire in approximately 5 minutes (with some tolerance)
expect(timeToExpiry).toBeGreaterThan(4 * 60 * 1000); // More than 4 minutes
expect(timeToExpiry).toBeLessThan(6 * 60 * 1000); // Less than 6 minutes
});
it("should use secure nonce generation", async () => {
const response = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
// Nonce should be base64-encoded and reasonable length
expect(response.body.nonce).toBeDefined();
expect(response.body.nonce.length).toBeGreaterThan(10);
// Should be different each time
const response2 = await request(app)
.get("/auth/challenge")
.query({ publicKey: testPublicKey })
.expect(200);
expect(response.body.nonce).not.toBe(response2.body.nonce);
});
});
});
// Full Integration Test (requires testnet account)
describe("SEP-10 Full Integration Test", () => {
let fundedKeypair;
let fundedPublicKey;
beforeAll(async () => {
// These tests require a funded testnet account
if (
!process.env.STELLAR_TEST_PUBLIC_KEY ||
!process.env.STELLAR_TEST_SECRET
) {
console.log("Skipping integration tests - no test credentials provided");
return;
}
fundedPublicKey = process.env.STELLAR_TEST_PUBLIC_KEY;
fundedKeypair = StellarSdk.Keypair.fromSecret(
process.env.STELLAR_TEST_SECRET,
);
});
it("should complete full SEP-10 authentication flow", async () => {
if (!fundedKeypair) {
console.log("Skipping integration test");
return;
}
// 1. Generate challenge
const challengeResponse = await request(app)
.get("/auth/challenge")
.query({ publicKey: fundedPublicKey })
.expect(200);
expect(challengeResponse.body.success).toBe(true);
// 2. Parse and sign the challenge
const transaction = StellarSdk.TransactionBuilder.fromXDR(
challengeResponse.body.challenge,
process.env.STELLAR_NETWORK_PASSPHRASE || "Test SDF Network ; September 2015",
);
transaction.sign(fundedKeypair);
const signedChallengeXDR = transaction.toXDR();
// 3. Verify and authenticate
const verifyResponse = await request(app)
.post("/auth/verify")
.send({
publicKey: fundedPublicKey,
challengeXDR: signedChallengeXDR,
})
.expect(200);
expect(verifyResponse.body.success).toBe(true);
expect(verifyResponse.body.token).toBeDefined();
expect(verifyResponse.body.user.publicKey).toBe(
fundedPublicKey.toLowerCase(),
);
expect(verifyResponse.body.user.type).toBe('stellar');
const token = verifyResponse.body.token;
// 4. Test protected endpoint access
const sessionResponse = await request(app)
.get("/auth/stellar/session")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(sessionResponse.body.success).toBe(true);
expect(sessionResponse.body.session.publicKey).toBe(
fundedPublicKey.toLowerCase(),
);
// 5. Verify JWT contains required claims
const jwt = require('jsonwebtoken');
const decoded = jwt.decode(token);
expect(decoded.publicKey).toBe(fundedPublicKey.toLowerCase());
expect(decoded.type).toBe('stellar');
expect(decoded.iat).toBeDefined();
expect(decoded.sessionId).toBeDefined();
// 6. Test logout
const logoutResponse = await request(app)
.post("/auth/stellar/logout")
.set("Authorization", `Bearer ${token}`)
.expect(200);
expect(logoutResponse.body.success).toBe(true);
// 7. Verify token is invalidated after logout
const sessionAfterLogout = await request(app)
.get("/auth/stellar/session")
.set("Authorization", `Bearer ${token}`)
.expect(403);
expect(sessionAfterLogout.body.success).toBe(false);
}, 30000); // Longer timeout for network operations
});