UNPKG

vhook-js

Version:

Verifyable Webhooks

188 lines (152 loc) 6.37 kB
// test/vhooks_keypair.test.js const assert = require('assert'); const vhooks = require('../vhook'); // Adjust the path as necessary describe('create_vhook_keypair', function () { this.timeout(10000); // Increase timeout for key generation if needed it('should generate default RSA key pair with default parameters', async function () { const keys = await vhooks.create_vhook_keypair(); // Verify that keys are generated assert.ok(keys.privateKey); assert.ok(keys.publicKey); // Check that keys are in PEM format assert.ok(keys.privateKey.pem.includes('-----BEGIN RSA PRIVATE KEY-----')); assert.ok(keys.publicKey.pem.includes('-----BEGIN PUBLIC KEY-----')); // Check that keys are in JWK format assert.strictEqual(keys.privateKey.jwk.kty, 'RSA'); assert.strictEqual(keys.publicKey.jwk.kty, 'RSA'); // Verify algorithm and use assert.strictEqual(keys.privateKey.jwk.alg, 'RS256'); assert.strictEqual(keys.privateKey.jwk.use, 'sig'); // Check modulus length (n length in bits) const modulusLength = Buffer.from(keys.publicKey.jwk.n, 'base64').length * 8; assert.ok(modulusLength >= 2048, `Expected modulus length >= 2048 bits, got ${modulusLength} bits`); }); it('should generate RSA key pair with custom modulus length and algorithm', async function () { const options = { key_size: 3072, algorithm: 'RS512', usage: 'sig', key_id: 'test-key-id', }; const keys = await vhooks.create_vhook_keypair(options); // Verify that keys are generated assert.ok(keys.privateKey); assert.ok(keys.publicKey); // Check that keys are in JWK format assert.strictEqual(keys.privateKey.jwk.kty, 'RSA'); assert.strictEqual(keys.publicKey.jwk.kty, 'RSA'); // Verify algorithm and use assert.strictEqual(keys.privateKey.jwk.alg, 'RS512'); assert.strictEqual(keys.privateKey.jwk.use, 'sig'); // Verify key ID assert.strictEqual(keys.privateKey.jwk.kid, 'test-key-id'); assert.strictEqual(keys.publicKey.jwk.kid, 'test-key-id'); // Check modulus length (n length in bits) const modulusLength = Buffer.from(keys.publicKey.jwk.n, 'base64').length * 8; assert.ok(modulusLength >= 3072, `Expected modulus length >= 3072 bits, got ${modulusLength} bits`); }); it('should generate EC key pair with curve P-256 and algorithm ES256', async function () { const options = { key_type: 'EC', key_size: 'P-256', algorithm: 'ES256', usage: 'sig', key_id: 'ec-key-1', }; const keys = await vhooks.create_vhook_keypair(options); // Verify that keys are generated assert.ok(keys.privateKey); assert.ok(keys.publicKey); // Check that keys are in JWK format assert.strictEqual(keys.privateKey.jwk.kty, 'EC'); assert.strictEqual(keys.publicKey.jwk.kty, 'EC'); // Verify algorithm, use, and curve assert.strictEqual(keys.privateKey.jwk.alg, 'ES256'); assert.strictEqual(keys.privateKey.jwk.use, 'sig'); assert.strictEqual(keys.privateKey.jwk.crv, 'P-256'); // Verify key ID assert.strictEqual(keys.privateKey.jwk.kid, 'ec-key-1'); assert.strictEqual(keys.publicKey.jwk.kid, 'ec-key-1'); }); it('should generate EC key pair with curve P-384 and algorithm ES384', async function () { const options = { key_type: 'EC', curve: 'P-384', algorithm: 'ES384', usage: 'sig', }; const keys = await vhooks.create_vhook_keypair(options); // Verify that keys are generated assert.ok(keys.privateKey); assert.ok(keys.publicKey); // Check that keys are in JWK format assert.strictEqual(keys.privateKey.jwk.kty, 'EC'); assert.strictEqual(keys.publicKey.jwk.kty, 'EC'); // Verify algorithm, use, and curve assert.strictEqual(keys.privateKey.jwk.alg, 'ES384'); assert.strictEqual(keys.privateKey.jwk.use, 'sig'); assert.strictEqual(keys.privateKey.jwk.crv, 'P-384'); }); it('should generate EC key pair with curve P-521 and algorithm ES512', async function () { const options = { key_type: 'EC', curve: 'P-521', algorithm: 'ES512', usage: 'sig', }; const keys = await vhooks.create_vhook_keypair(options); // Verify that keys are generated assert.ok(keys.privateKey); assert.ok(keys.publicKey); // Check that keys are in JWK format assert.strictEqual(keys.privateKey.jwk.kty, 'EC'); assert.strictEqual(keys.publicKey.jwk.kty, 'EC'); // Verify algorithm, use, and curve assert.strictEqual(keys.privateKey.jwk.alg, 'ES512'); assert.strictEqual(keys.privateKey.jwk.use, 'sig'); assert.strictEqual(keys.privateKey.jwk.crv, 'P-521'); }); it('should generate EC key pair with curve P-256 if no crv provided', async function () { const options = { key_type: 'EC', algorithm: 'ES256', usage: 'sig', }; const keys = await vhooks.create_vhook_keypair(options); assert.strictEqual(keys.privateKey.jwk.crv, 'P-256'); }); it('should throw an error for unsupported key type', async function () { const options = { key_type: 'unsupported', algorithm: 'RS256', usage: 'sig', }; try { await vhooks.create_vhook_keypair(options); assert.fail('Expected error not thrown'); } catch (err) { assert.ok(err instanceof Error); assert.strictEqual(err.message, 'Unsupported key type: unsupported'); } }); it('should generate oct (symmetric) key with algorithm HS256', async function () { const options = { key_type: 'oct', key_size: 256, // Key size in bits algorithm: 'HS256', usage: 'sig', }; const keys = await vhooks.create_vhook_keypair(options); // Verify that keys are generated (only private key for symmetric keys) assert.ok(keys.key); assert.ok(!keys.publicKey); // No public key for symmetric keys // Check that key is in JWK format assert.strictEqual(keys.key.jwk.kty, 'oct'); // Verify algorithm and use assert.strictEqual(keys.key.jwk.alg, 'HS256'); assert.strictEqual(keys.key.jwk.use, 'sig'); // Check key size (k length in bits) const keyLength = Buffer.from(keys.key.jwk.k, 'base64').length * 8; assert.ok(keyLength >= 256, `Expected key length >= 256 bits, got ${keyLength} bits`); }); });