Skip to content

Commit 04087ef

Browse files
authored
chore: interop browser testing (#42)
* chore: add longer timeout for interop tests * chore: interop browser testing * chore: fix prettier * fix: add cwt tag on browser test
1 parent 45a62bd commit 04087ef

File tree

7 files changed

+2145
-67
lines changed

7 files changed

+2145
-67
lines changed

examples/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CAT, CommonAccessTokenRenewal } from '../src';
1+
import { CAT } from '../src';
22

33
async function main() {
44
const generator = new CAT({

interop/browser/empty-module.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default {};

interop/browser/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CAT Browser Test</title>
5+
</head>
6+
<body>
7+
<div>
8+
<h2>CAT Token Test</h2>
9+
<button id="generateToken">Generate Token</button>
10+
<pre id="output"></pre>
11+
</div>
12+
<script type="module" src="./main.js"></script>
13+
</body>
14+
</html>

interop/browser/main.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { CAT } from '@eyevinn/cat';
2+
3+
const output = document.getElementById('output');
4+
const generateBtn = document.getElementById('generateToken');
5+
6+
const fromHexString = (hexString) =>
7+
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
8+
9+
const toHexString = (bytes) =>
10+
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
11+
12+
async function testCAT() {
13+
try {
14+
// Generate random key for testing
15+
const key = fromHexString(
16+
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388'
17+
);
18+
19+
const cat = new CAT({
20+
keys: {
21+
Symmetric256: key
22+
},
23+
expectCwtTag: true
24+
});
25+
26+
const token = await cat.generate(
27+
{
28+
iss: 'test-issuer',
29+
exp: Math.floor(Date.now() / 1000) + 3600,
30+
aud: 'test-audience'
31+
},
32+
{
33+
type: 'mac',
34+
alg: 'HS256',
35+
kid: 'Symmetric256',
36+
generateCwtId: true
37+
}
38+
);
39+
40+
// Validate the generated token
41+
const result = await cat.validate(token, 'mac', {
42+
issuer: 'test-issuer',
43+
audience: ['test-audience']
44+
});
45+
46+
output.textContent = JSON.stringify(
47+
{
48+
token,
49+
claims: result.cat?.claims
50+
},
51+
null,
52+
2
53+
);
54+
} catch (err) {
55+
output.textContent = `Error: ${err.message}`;
56+
console.error(err);
57+
}
58+
}
59+
60+
generateBtn.addEventListener('click', testCAT);

interop/browser/vite.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { defineConfig } from 'vite';
2+
import { nodePolyfills } from 'vite-plugin-node-polyfills';
3+
import path from 'path';
4+
5+
export default defineConfig({
6+
plugins: [nodePolyfills()],
7+
optimizeDeps: {
8+
include: ['@eyevinn/cat'],
9+
exclude: ['ioredis']
10+
},
11+
build: {
12+
commonjsOptions: {
13+
transformMixedEsModules: true,
14+
include: [/node_modules/, /..\/..\/lib/]
15+
}
16+
},
17+
resolve: {
18+
alias: {
19+
'@eyevinn/cat': '../../lib/index.js',
20+
ioredis: path.resolve(__dirname, './empty-module.js')
21+
}
22+
}
23+
});

0 commit comments

Comments
 (0)