'use strict'; const crypto = require ('../../index'); const rsa = require ('../../lib/rsa'); // eslint-disable-next-line max-lines-per-function describe ('signatures', () => { beforeEach (() => { jasmine.clock () .install (); const base_time = (new Date); jasmine.clock () .mockDate (base_time); }); afterEach (() => { jasmine.clock () .uninstall (); }); it ('sign_object', async () => { const obj = { foo: 'bar' }; await expectAsync ((async () => { const str = await crypto.sign_object (obj, 'baz'); expect (typeof str) .toEqual ('string'); }) ()) .toBeResolved (); }); it ('sign_object with rsa key', async () => { const obj = { foo: 'bar' }; const k = await rsa.generate_keypair (); await expectAsync ((async () => { const str = await crypto.sign_object (obj, k.private_key); expect (typeof str) .toEqual ('string'); }) ()) .toBeResolved (); }); it ('verify_signature with rsa key', async () => { const obj = { foo: 'bar' }; const str = 'U1GcsN3yZzSKxPH8jhCGTKiswfazB9rMfUtE5351LT11t6EmS7xfPjnt' + '.5ytniC6q2ovoF7ZqbD8qk9r2kjjAcA9EYhLwC3wwJKPPsKdHSTFd7d9TzBP1skQ98X' + 'LjRUkc2M8M84LmWLg76EvcY2pw6HwsFvCUoZYcKAJp3vkp9MQVrVYdHKMPkBjQKyy2V' + 'KtEZiBsomBVJd6Hudd1YLMQ4J4s52iHsegDswKE9djYVEmgKkJUAiZJ2viFHw3fBbp2' + 'Abo2Dm5oqYtw7Nn9RFstW3CcNQHV1PzHDKD56Uw3opuYwVZhQth8ux2CdkC2yMvgVsT' + 'dUyCuu78ugaGvzsMXCbe2BzaPFDTE9JYtMcDFFP43nUGHNd6cWwzoKTZBX852Exz6Rb' + 'VjcWUvL81dLPBLJV.2'; const key = '-----BEGIN RSA PUBLIC KEY-----\n' + 'MIIBCgKCAQEA4LCEoJYNwwksuzPESpmPziHp98WhY5Qml6RiN9uxrKGPV6QwwmDQ\n' + 'ks6C+ZfYbFG9NCx1MEuWL0Tvp/6ZBhMyaJrI5iwo0CmSX3WdFcbXmdl0l6N1+5r7\n' + 'l3SkKsr/AX4gwcDor4dYuLEv5KawGdfcP0IxsoAcIN1UJ5HJ+eheB3fVcSh/IIBf\n' + 'O+cL/4Chw8eAaDBG5mZ1Xgd4gIjJGYAxgUNvaShGzs8k1y+jqjD5IkZ1h9dgoGJG\n' + 'dUmjCLWrOzx8SqdqJYmQJX+6GNswnvVF30bkW+/MJZF/P2jLFtSa24Monh7axIqx\n' + '8HG0xDw1Z98WV9oQh/vDP/KAs1cPp0AJlwIDAQAB\n' + '-----END RSA PUBLIC KEY-----\n'; const ver = await crypto.verify_signature (str, key); expect (ver) .toEqual (obj); }); it ('verify_signature reject with rsa key', async () => { const str = 'U1GcsN3yZzSKxPH8jhCGTKiswfazB9rMfUtE5351LT11t6EmS7xfPjnt' + '.5ytniC6q2ovoF7ZqbD8qk9r2kjjAcA9EYhLwC3wwJKPPsKdHSTFd7d9TzBP1skQ98X' + 'LjRUkc2M8M84LmWLg76EvcY2pw6HwsFvCUoZYcKAJp3vkp9MQVrVYdHKMPkBjQKyy2V' + 'KtEZiBsomBVJd6Hudd1YLMQ4J4s52iHsegDswKE9djYVEmgKkJUAiZJ2viFHw3fBbp2' + 'Abo2Dm5oqYtw7Nn9RFstW3CcNQHV1PzHDKD56Uw3opuYwVZhQth8ux2CdkC2yMvgVsT' + 'dUyCuu78ugaGvzsMXCbe2BzaPFDTE9JYtMcDFFP43nUGHNd6cWwzoKTZBX852Exz6Rb' + 'VjcWUvL81dLPBLJA.2'; const key = '-----BEGIN RSA PUBLIC KEY-----\n' + 'MIIBCgKCAQEA4LCEoJYNwwksuzPESpmPziHp98WhY5Qml6RiN9uxrKGPV6QwwmDQ\n' + 'ks6C+ZfYbFG9NCx1MEuWL0Tvp/6ZBhMyaJrI5iwo0CmSX3WdFcbXmdl0l6N1+5r7\n' + 'l3SkKsr/AX4gwcDor4dYuLEv5KawGdfcP0IxsoAcIN1UJ5HJ+eheB3fVcSh/IIBf\n' + 'O+cL/4Chw8eAaDBG5mZ1Xgd4gIjJGYAxgUNvaShGzs8k1y+jqjD5IkZ1h9dgoGJG\n' + 'dUmjCLWrOzx8SqdqJYmQJX+6GNswnvVF30bkW+/MJZF/P2jLFtSa24Monh7axIqx\n' + '8HG0xDw1Z98WV9oQh/vDP/KAs1cPp0AJlwIDAQAB\n' + '-----END RSA PUBLIC KEY-----\n'; const ver = await crypto.verify_signature (str, key); expect (ver) .toBeNull (); }); it ('should sign object with key info', async () => { const obj = { foo: 'bar' }; await expectAsync ((async () => { const str = crypto.sign_object (obj, 'baz', 'baz'); const res = await crypto.get_signature_info (str); expect (res.key_info) .toEqual ('baz'); }) ()) .toBeResolved (); }); it ('should sign object with custom properties', async () => { const obj = { foo: 'bar' }; await expectAsync ((async () => { const str = crypto.sign_object (obj, 'baz', { bar: 'baz' }); const res = await crypto.get_signature_info (str); expect (res.bar) .toEqual ('baz'); }) ()) .toBeResolved (); }); it ('should sign object with custom override properties', async () => { const obj = { foo: 'bar' }; await expectAsync ((async () => { const str = crypto.sign_object (obj, 'baz', { iat: 'baz' }); const res = await crypto.get_signature_info (str); expect (res.iat) .toEqual ('baz'); }) ()) .toBeResolved (); }); it ('decode_signed', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); const dec = await crypto.decode_signed (str); expect (obj) .toEqual (dec); }); it ('verify_signature', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); const dec = await crypto.verify_signature (str, 'baz'); expect (obj) .toEqual (dec); }); it ('should verify and return all info', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz', { iat: 'baz' }); const dec = await crypto.verify_signature_get_info (str, 'baz'); expect (dec.obj) .toEqual (obj); expect (dec.iat) .toEqual ('baz'); }); it ('should verify signature using function retrieved key', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); const dec = await crypto.verify_signature (str, () => 'baz'); expect (obj) .toEqual (dec); }); it ( 'should verify signature using function retrieved timeout 0', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); const dec = await crypto.verify_signature (str, 'baz', () => 0); expect (obj) .toEqual (dec); } ); it ('should reject tampered signatures', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); const dec = await crypto.verify_signature (str, 'foo'); expect (dec) .toEqual (null); }); it ('should return null on invalid input', async () => { const ver = await crypto.verify_signature (null, 'foo'); expect (ver) .toEqual (null); const dec = await crypto.decode_signed (null); expect (dec) .toEqual (null); }); it ('should not fail verification if timeout unspecified', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); jasmine.clock () .tick (36e5); const dec = await crypto.verify_signature (str, 'baz'); expect (obj) .toEqual (dec); }); it ('should reject old signatures', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); jasmine.clock () .tick (50); const dec = await crypto.verify_signature (str, 'baz', 1); expect (dec) .toEqual (null); }); it ('should not reject valid signatures', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); jasmine.clock () .tick (50); const dec = await crypto.verify_signature (str, 'baz', 100); expect (obj) .toEqual (dec); }); it ('should verify signature using function retrieved timeout', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz', { to: 100 }); jasmine.clock () .tick (50); const dec = await crypto.verify_signature (str, 'baz', (info) => info.to); expect (obj) .toEqual (dec); }); it ('verify_signature on almost timed out packet', async () => { const obj = { foo: 'bar' }; const str = crypto.sign_object (obj, 'baz'); jasmine.clock () .tick (10); const dec = await crypto.verify_signature (str, 'baz', 10); expect (obj) .toEqual (dec); }); it ('should decode problematic token', async () => { // eslint-disable-next-line max-len const str = 'wEJbzvUywiaiGWZUG6CtCXNkNmRGyVoi9icytpTe4gZhsb8Gk.5PZbhGL525mdV7EmYomTwUei6qULpLaZwSXy92eaUDNgbyXPHsr9dfUCeEBpTqmzuq3VtmmV43epUyWRoHocAsV3.2'; const obj = await crypto.decode_signed (str); expect (obj) .toEqual ({ id: 1 }); }); it ('should automatically reencode b64 tokens', async () => { // eslint-disable-next-line max-len const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D'; const obj = await crypto.decode_signed (str); expect (obj) .toEqual ({ id: 1 }); }); it ('verify_signature on b64 string', async () => { // eslint-disable-next-line max-len const str = 'eyJpYXQiOjE2MDkzNDQ4MDMyMjcsIm9iaiI6eyJpZCI6MX19.N762xuMaNbT%2Fqb0uTKST68BZgSnmNxXaHl4GY7iAKqaDDEwZn3biYfg5DgJ45QgPZrndchczDjUqLkyXoqw4KQ%3D%3D'; const obj = await crypto.verify_signature (str, 'baz'); expect (obj) .toEqual ({ id: 1 }); }); });