forked from JavaScriptSolidServer/JavaScriptSolidServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnss-local.js
More file actions
53 lines (43 loc) · 1.8 KB
/
nss-local.js
File metadata and controls
53 lines (43 loc) · 1.8 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
const fetch = require('node-fetch');
const jose = require('jose');
const crypto = require('crypto');
const https = require('https');
const ISSUER = 'https://melvincarvalho.com/';
const NSS_URL = 'https://localhost:8443/';
// Allow self-signed cert
const agent = new https.Agent({ rejectUnauthorized: false });
async function test() {
console.log('=== Testing DPoP against local NSS ===\n');
// Get DPoP token from our IdP
const { publicKey, privateKey } = await jose.generateKeyPair('ES256');
const publicJwk = await jose.exportJWK(publicKey);
const credProof = await new jose.SignJWT({
htm: 'POST', htu: ISSUER + 'idp/credentials',
iat: Math.floor(Date.now() / 1000), jti: crypto.randomUUID(),
}).setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk: publicJwk }).sign(privateKey);
const tokenResp = await fetch(ISSUER + 'idp/credentials', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'DPoP': credProof },
body: JSON.stringify({ email: 'melvin', password: 'melvintest123' }),
});
const { access_token } = await tokenResp.json();
console.log('Got token from melvincarvalho.com');
// Create DPoP proof for local NSS
const dpopProof = await new jose.SignJWT({
htm: 'GET', htu: NSS_URL,
iat: Math.floor(Date.now() / 1000), jti: crypto.randomUUID(),
}).setProtectedHeader({ alg: 'ES256', typ: 'dpop+jwt', jwk: publicJwk }).sign(privateKey);
console.log('Testing against local NSS:', NSS_URL);
const resp = await fetch(NSS_URL, {
agent,
headers: {
'Authorization': 'DPoP ' + access_token,
'DPoP': dpopProof,
'Accept': 'text/turtle',
},
});
console.log('Status:', resp.status);
const wwwAuth = resp.headers.get('www-authenticate');
if (wwwAuth) console.log('WWW-Authenticate:', wwwAuth);
}
test().catch(console.error);