Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Apr 26, 2024
1 parent 3646379 commit f1c14fd
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions frontend/test/common/jwe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('JWE', () => {
expect(decrypted).to.deep.eq(orig);
});

it('decrypt ECDH', async () => {
it('decrypt ECDH-ES', async () => {
// JWE generated by https://dinochiesa.github.io/jwt/
const jwe = 'eyJhbGciOiJFQ0RILUVTIiwiZW5jIjoiQTI1NkdDTSIsImVwayI6eyJrdHkiOiJFQyIsImNydiI6IlAtMjU2IiwieCI6InpMYlRUN3M5d3lZeGpHdGZxeHVDQk81TTNTMTlNRzJHUGJzTlJhMFEwT2MiLCJ5IjoiTGpmVG9jLXpIcWRqcHA4VURPSl9aS1JLQ0FoSGpSSmFGS0FJbjdTR1RXdyJ9fQ..30WmoR8Qp1VA5NMr.TBRC30hVRwi_W_HIe03JWNY.zvfl5wQuG8vhUjhr1-f8jQ';
const rawRecipientPrivateKey = base64.parse('MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgzMoA2cm0rNRLA3zZa2VzYxd1QLFTdOxsMQ+6V6faoLmhRANCAAR4M7kZS/VdkdOQG56ELvL2/3L8ti+yQeQBv0BuyUJqWHMOv13VLZOKTnvtFrAZbjM6lONayft9qSr43thfj1Pb', { loose: true });
Expand All @@ -108,7 +108,31 @@ describe('JWE', () => {
expect(decrypted).to.deep.eq(orig);
});

// TODO: add some more decrypt-only tests with JWE from 3rd party
it('decrypt ECDH-ES+A256KW 1', async () => {
// JWE generated by https://dinochiesa.github.io/jwt/
const jwe = 'eyJlcGsiOnsia3R5IjoiRUMiLCJjcnYiOiJQLTI1NiIsIngiOiJGX3lIQWlEQkNMajdVdW43NXZtTGpsSkRnSFRmVnFPUW51X0g0cGhNbjVZIiwieSI6IlFMRThtd1V2M0tDU3pjNmtqT0R2QnhEQjFqRFdUVnE1N052UlpUcFZNZFUifSwiZW5jIjoiQTI1NkdDTSIsImFsZyI6IkVDREgtRVMrQTI1NktXIn0.ziLX2llBDAO0Ha_EV2QyLVGo47qN0c2QpSt-tu35yn8t6Q-elpVG3A.QXT0c27wNHVbvcQN.z82EXvOQ_IWaCGcSmlizzFY.JkrlVlJin8E4uH4WEjPs0w';
const rawRecipientPrivateKey = base64.parse('MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8w/B3QWfLRwtt+pCYScLE8CzqWOYGYl/t2gwmb89AA2hRANCAAR/S7NvYpmAinsrGaDaFNmN8eQFRpwc5alP4Clu8hV//hcrN9DMHGbO2lsKk//ktpUNXt4Fj4UIsBw3ABUchbxI', { loose: true });
const recipientPrivateKey = await crypto.subtle.importKey('pkcs8', rawRecipientPrivateKey, { name: 'ECDH', namedCurve: 'P-256' }, false, ['deriveBits']);
const orig = { hello: 'world' };
const recipient = Recipient.ecdhEs('alice', recipientPrivateKey);

const decrypted = await JWE.parseCompact(jwe).decrypt(recipient);
expect(decrypted).to.deep.eq(orig);
});

//

it('decrypt ECDH-ES+A256KW 2', async () => {
// JWE generated by nimbus-jose-jwt
const jwe = 'eyJlcGsiOnsia3R5IjoiRUMiLCJjcnYiOiJQLTM4NCIsIngiOiJDMndad2lrUUc2NFVjNWdHRHV4VWhVb0dHbEpDVnI4ZHFjMFZkelZIUDl3WUtmSTNCYl8tVnQ3VGtfRER5N1RoIiwieSI6IlNTMTNRS18wLWJYQ1J4OHkyZXFETUNPU0I5OUhqOGtHQWh3b2hzWmwwYi1vamdiTzBsNUpKQW4wYmFCTWE4d1YifSwiZW5jIjoiQTI1NkdDTSIsImFsZyI6IkVDREgtRVMrQTI1NktXIn0.VZPzMmLm31EfxoOhVmLRIu0ILMs473FKeLd8jhaCrzHlLhXS0S2VSA.7W9iNEn-1B1PnfuQ.6y_7YaOXUCCXvuaxwbHLtpc.yHRzBceUHw6huMv4jFApxg';
const privateKeyJwk: JsonWebKey = { "kty": "EC", "crv": "P-384", "x": "I47LcQjeZZ5_HxIbw3e5nwaA2S7BFr0CFfiGaay9q3OTNNxFm-O4pjQ2wntSS3hu", "y": "mDFWsM-fkpHpM6JmJczl31PkjrfCbz6B_IhYcMKsJc3NBIO1ALRvmsjrX0aTRH_e", "d": "tgit5ED1PpM6rwIDqMovYkKSZ8n5V05wc_sHi-AiqwM9atiTsO3w2E2wDa-L1lFN" };
const privateKey = await crypto.subtle.importKey('jwk', privateKeyJwk, { name: 'ECDH', namedCurve: 'P-384' }, false, ['deriveBits']);
const orig = { hello: 'world' };
const recipient = Recipient.ecdhEs('alice', privateKey);

const decrypted = await JWE.parseCompact(jwe).decrypt(recipient);
expect(decrypted).to.deep.eq(orig);
});
});

describe('JWE using alg: PBES2-HS512+A256KW', () => {
Expand All @@ -132,7 +156,16 @@ describe('JWE', () => {
expect(token).not.to.be.null;
});

// TODO: add some more decrypt-only tests with JWE from 3rd party
it('decrypt PBES2-HS512+A256KW', async () => {
// JWE generated by nimbus-jose-jwt
const jwe = 'eyJwMnMiOiI1REp2cFJVTzJkOWlUdjdHc24wTlp3IiwicDJjIjoxMDAwLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiUEJFUzItSFM1MTIrQTI1NktXIn0.uOPjfcgWobRPtoO124qLyv9XSRWEw1rmdzDqroH6iTBQyyNMKIjdPw.dIH9LVbMH5aAmCMj.hsIdH2JSliAV2ueFErovkHY.OA2nayTnJYTQO04HGvlaMA';
const orig = { hello: 'world' };

const recipient = Recipient.pbes2('password', 'topsecret');

const decrypted = await JWE.parseCompact(jwe).decrypt(recipient);
expect(decrypted).to.deep.eq(orig);
});
});

describe('JWE using alg: A256KW', () => {
Expand Down

0 comments on commit f1c14fd

Please sign in to comment.