-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
60 lines (51 loc) · 1.37 KB
/
main.js
File metadata and controls
60 lines (51 loc) · 1.37 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
import { CAT } from '@eyevinn/cat';
const output = document.getElementById('output');
const generateBtn = document.getElementById('generateToken');
const fromHexString = (hexString) =>
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
const toHexString = (bytes) =>
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
async function testCAT() {
try {
// Generate random key for testing
const key = fromHexString(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388'
);
const cat = new CAT({
keys: {
Symmetric256: key
},
expectCwtTag: true
});
const token = await cat.generate(
{
iss: 'test-issuer',
exp: Math.floor(Date.now() / 1000) + 3600,
aud: 'test-audience'
},
{
type: 'mac',
alg: 'HS256',
kid: 'Symmetric256',
generateCwtId: true
}
);
// Validate the generated token
const result = await cat.validate(token, 'mac', {
issuer: 'test-issuer',
audience: ['test-audience']
});
output.textContent = JSON.stringify(
{
token,
claims: result.cat?.claims
},
null,
2
);
} catch (err) {
output.textContent = `Error: ${err.message}`;
console.error(err);
}
}
generateBtn.addEventListener('click', testCAT);