-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsolid-oidc.test.js
More file actions
211 lines (178 loc) · 5.95 KB
/
solid-oidc.test.js
File metadata and controls
211 lines (178 loc) · 5.95 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
/**
* Solid-OIDC tests
* Tests for DPoP token verification
*/
import { describe, it, before, after } from 'node:test';
import assert from 'node:assert';
import * as jose from 'jose';
import {
startTestServer,
stopTestServer,
request,
createTestPod,
getBaseUrl,
assertStatus
} from './helpers.js';
describe('Solid-OIDC', () => {
let keyPair;
let publicJwk;
before(async () => {
await startTestServer();
await createTestPod('oidctest');
// Generate a key pair for testing
keyPair = await jose.generateKeyPair('ES256');
publicJwk = await jose.exportJWK(keyPair.publicKey);
publicJwk.alg = 'ES256';
});
after(async () => {
await stopTestServer();
});
describe('DPoP Header Parsing', () => {
// Use private folder - requires authentication
const privatePath = '/oidctest/private/';
it('should reject requests with DPoP auth but no DPoP proof', async () => {
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token'
}
});
assertStatus(res, 401);
const body = await res.json();
assert.ok(body.message.includes('DPoP proof'), 'Should mention DPoP proof');
});
it('should reject invalid DPoP proof JWT', async () => {
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token',
'DPoP': 'not-a-valid-jwt'
}
});
assertStatus(res, 401);
});
it('should reject DPoP proof with wrong type', async () => {
// Create a JWT that's not a DPoP proof (wrong typ)
const wrongTypeJwt = await new jose.SignJWT({
htm: 'GET',
htu: `${getBaseUrl()}${privatePath}`,
iat: Math.floor(Date.now() / 1000),
jti: crypto.randomUUID()
})
.setProtectedHeader({ alg: 'ES256', typ: 'JWT', jwk: publicJwk })
.sign(keyPair.privateKey);
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token',
'DPoP': wrongTypeJwt
}
});
assertStatus(res, 401);
});
it('should reject DPoP proof with wrong HTTP method', async () => {
const dpopProof = await createDpopProof('POST', `${getBaseUrl()}${privatePath}`);
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
method: 'GET',
headers: {
'Authorization': 'DPoP some-token',
'DPoP': dpopProof
}
});
assertStatus(res, 401);
});
it('should reject DPoP proof with wrong URL', async () => {
const dpopProof = await createDpopProof('GET', 'https://other-server.example/');
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token',
'DPoP': dpopProof
}
});
assertStatus(res, 401);
});
it('should reject expired DPoP proof', async () => {
// Create a DPoP proof with old iat
const dpopProof = await new jose.SignJWT({
htm: 'GET',
htu: `${getBaseUrl()}${privatePath}`,
iat: Math.floor(Date.now() / 1000) - 600, // 10 minutes ago
jti: crypto.randomUUID()
})
.setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk: publicJwk })
.sign(keyPair.privateKey);
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token',
'DPoP': dpopProof
}
});
assertStatus(res, 401);
});
it('should reject DPoP proof missing jti', async () => {
const dpopProof = await new jose.SignJWT({
htm: 'GET',
htu: `${getBaseUrl()}${privatePath}`,
iat: Math.floor(Date.now() / 1000)
// missing jti
})
.setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk: publicJwk })
.sign(keyPair.privateKey);
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': 'DPoP some-token',
'DPoP': dpopProof
}
});
assertStatus(res, 401);
});
});
describe('Access Token Verification', () => {
const privatePath = '/oidctest/private/';
it('should reject token with invalid issuer (unreachable)', async () => {
// Create a valid DPoP proof
const dpopProof = await createDpopProof('GET', `${getBaseUrl()}${privatePath}`);
// Create a fake access token with unreachable issuer
const fakeToken = await new jose.SignJWT({
webid: 'https://example.com/user#me',
sub: 'https://example.com/user#me',
iss: 'https://nonexistent-idp.example.com',
aud: 'solid',
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + 3600
})
.setProtectedHeader({ alg: 'ES256' })
.sign(keyPair.privateKey);
const res = await fetch(`${getBaseUrl()}${privatePath}`, {
headers: {
'Authorization': `DPoP ${fakeToken}`,
'DPoP': dpopProof
}
});
assertStatus(res, 401);
});
});
describe('Bearer Token Fallback', () => {
it('should still accept simple Bearer tokens', async () => {
// This should work with our simple token system
const res = await request('/oidctest/public/', { auth: 'oidctest' });
assertStatus(res, 200);
});
it('should still accept simple Bearer tokens for writes', async () => {
const res = await request('/oidctest/public/solid-oidc-test.txt', {
method: 'PUT',
body: 'test content',
auth: 'oidctest'
});
assertStatus(res, 201);
});
});
// Helper to create DPoP proofs
async function createDpopProof(method, uri) {
return new jose.SignJWT({
htm: method,
htu: uri,
iat: Math.floor(Date.now() / 1000),
jti: crypto.randomUUID()
})
.setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk: publicJwk })
.sign(keyPair.privateKey);
}
});