Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/generate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CAT, CommonAccessTokenRenewal } from '../src';
import { CAT } from '../src';

async function main() {
const generator = new CAT({
Expand Down
3 changes: 2 additions & 1 deletion interop/__tests__/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
}
);
console.log(token);
const result = await validateCommonAccessToken(ctx, token!, {

Check warning on line 43 in interop/__tests__/generate.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
signingKey:
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388'
});
expect(result.payload.iss).toBe('eyevinn');
}
},
10000
);
});
63 changes: 34 additions & 29 deletions interop/__tests__/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,38 @@
process.env.OSC_ACCESS_TOKEN !== undefined;

describe('CAT library can validate a token that someone else has generated', () => {
testIf(runInteropTests, 'issuer', async () => {
const ctx = new Context();
const validator = new CAT({
keys: {
Symmetric256: Buffer.from(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
)
},
expectCwtTag: true
});
const token = await generateCommonAccessToken(
ctx,
{
iss: 'eyevinn',
sub: 'jonas'
},
{
signingKey:
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388'
}
);
const result = await validator.validate(token, 'mac', {
issuer: 'eyevinn'
});
expect(result.error).not.toBeDefined();
expect(result.cat).toBeDefined();
expect(result.cat!.claims.iss).toBe('eyevinn');
});
testIf(
runInteropTests,
'issuer',
async () => {
const ctx = new Context();
const validator = new CAT({
keys: {
Symmetric256: Buffer.from(
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388',
'hex'
)
},
expectCwtTag: true
});
const token = await generateCommonAccessToken(
ctx,
{
iss: 'eyevinn',
sub: 'jonas'
},
{
signingKey:
'403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388'
}
);
const result = await validator.validate(token, 'mac', {
issuer: 'eyevinn'
});
expect(result.error).not.toBeDefined();
expect(result.cat).toBeDefined();
expect(result.cat!.claims.iss).toBe('eyevinn');

Check warning on line 41 in interop/__tests__/validate.ts

View workflow job for this annotation

GitHub Actions / lint

Forbidden non-null assertion
},
10000
);
});
1 change: 1 addition & 0 deletions interop/browser/empty-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
14 changes: 14 additions & 0 deletions interop/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>CAT Browser Test</title>
</head>
<body>
<div>
<h2>CAT Token Test</h2>
<button id="generateToken">Generate Token</button>
<pre id="output"></pre>
</div>
<script type="module" src="./main.js"></script>
</body>
</html>
60 changes: 60 additions & 0 deletions interop/browser/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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) =>

Check warning on line 9 in interop/browser/main.js

View workflow job for this annotation

GitHub Actions / lint

'toHexString' is assigned a value but never used
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);
23 changes: 23 additions & 0 deletions interop/browser/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import path from 'path';

export default defineConfig({
plugins: [nodePolyfills()],
optimizeDeps: {
include: ['@eyevinn/cat'],
exclude: ['ioredis']
},
build: {
commonjsOptions: {
transformMixedEsModules: true,
include: [/node_modules/, /..\/..\/lib/]
}
},
resolve: {
alias: {
'@eyevinn/cat': '../../lib/index.js',
ioredis: path.resolve(__dirname, './empty-module.js')
}
}
});
Loading
Loading