2020-10-04 12:18:39 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const crypto = require ('../../index');
|
2021-01-06 15:13:04 +01:00
|
|
|
const rsa = require ('../../lib/rsa');
|
2020-10-04 12:18:39 +02:00
|
|
|
|
2021-01-06 15:13:04 +01:00
|
|
|
// eslint-disable-next-line max-lines-per-function
|
|
|
|
describe ('signatures', () => {
|
2020-12-30 17:16:29 +01:00
|
|
|
beforeEach (() => {
|
|
|
|
jasmine.clock ()
|
|
|
|
.install ();
|
|
|
|
const base_time = (new Date);
|
|
|
|
jasmine.clock ()
|
|
|
|
.mockDate (base_time);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach (() => {
|
|
|
|
jasmine.clock ()
|
|
|
|
.uninstall ();
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('sign_object', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
2022-08-08 13:07:06 +02:00
|
|
|
await expectAsync ((async () => {
|
|
|
|
const str = await crypto.sign_object (obj, 'baz');
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (typeof str)
|
|
|
|
.toEqual ('string');
|
2022-08-08 13:07:06 +02:00
|
|
|
}) ())
|
|
|
|
.toBeResolved ();
|
2020-10-04 12:18:39 +02:00
|
|
|
});
|
|
|
|
|
2021-01-06 15:13:04 +01:00
|
|
|
it ('sign_object with rsa key', async () => {
|
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const k = await rsa.generate_keypair ();
|
2022-08-08 13:07:06 +02:00
|
|
|
await expectAsync ((async () => {
|
|
|
|
const str = await crypto.sign_object (obj, k.private_key);
|
2021-01-06 15:13:04 +01:00
|
|
|
expect (typeof str)
|
|
|
|
.toEqual ('string');
|
2022-08-08 13:07:06 +02:00
|
|
|
}) ())
|
|
|
|
.toBeResolved ();
|
2021-01-06 15:13:04 +01:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('verify_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AQAB\n'
|
|
|
|
+ '-----END RSA PUBLIC KEY-----\n';
|
2022-08-08 13:07:06 +02:00
|
|
|
const ver = await crypto.verify_signature (str, key);
|
2021-01-06 15:13:04 +01:00
|
|
|
expect (ver)
|
|
|
|
.toEqual (obj);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('verify_signature reject with rsa key', async () => {
|
2021-01-06 15:13:04 +01:00
|
|
|
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';
|
2022-08-08 13:07:06 +02:00
|
|
|
const ver = await crypto.verify_signature (str, key);
|
2021-01-06 15:13:04 +01:00
|
|
|
expect (ver)
|
|
|
|
.toBeNull ();
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should sign object with key info', async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const obj = { foo: 'bar' };
|
2022-08-08 13:07:06 +02:00
|
|
|
await expectAsync ((async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const str = crypto.sign_object (obj, 'baz', 'baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
const res = await crypto.get_signature_info (str);
|
2020-12-13 13:09:34 +01:00
|
|
|
expect (res.key_info)
|
|
|
|
.toEqual ('baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
}) ())
|
|
|
|
.toBeResolved ();
|
2020-12-13 13:09:34 +01:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should sign object with custom properties', async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const obj = { foo: 'bar' };
|
2022-08-08 13:07:06 +02:00
|
|
|
await expectAsync ((async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const str = crypto.sign_object (obj, 'baz', { bar: 'baz' });
|
2022-08-08 13:07:06 +02:00
|
|
|
const res = await crypto.get_signature_info (str);
|
2020-12-13 13:09:34 +01:00
|
|
|
expect (res.bar)
|
|
|
|
.toEqual ('baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
}) ())
|
|
|
|
.toBeResolved ();
|
2020-12-13 13:09:34 +01:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should sign object with custom override properties', async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const obj = { foo: 'bar' };
|
2022-08-08 13:07:06 +02:00
|
|
|
await expectAsync ((async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
|
2022-08-08 13:07:06 +02:00
|
|
|
const res = await crypto.get_signature_info (str);
|
2020-12-13 13:09:34 +01:00
|
|
|
expect (res.iat)
|
|
|
|
.toEqual ('baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
}) ())
|
|
|
|
.toBeResolved ();
|
2020-12-13 13:09:34 +01:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('decode_signed', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.decode_signed (str);
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('verify_signature', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz');
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should verify and return all info', async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz', { iat: 'baz' });
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature_get_info (str, 'baz');
|
2020-12-13 13:09:34 +01:00
|
|
|
expect (dec.obj)
|
|
|
|
.toEqual (obj);
|
|
|
|
expect (dec.iat)
|
|
|
|
.toEqual ('baz');
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should verify signature using function retrieved key', async () => {
|
2020-12-13 13:09:34 +01:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, () => 'baz');
|
2020-12-13 13:09:34 +01:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
2020-12-13 13:31:48 +01:00
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should reject tampered signatures', async () => {
|
2020-11-29 12:01:43 +01:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'foo');
|
2020-11-29 12:01:43 +01:00
|
|
|
expect (dec)
|
|
|
|
.toEqual (null);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should return null on invalid input', async () => {
|
|
|
|
const ver = await crypto.verify_signature (null, 'foo');
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (ver)
|
|
|
|
.toEqual (null);
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.decode_signed (null);
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (dec)
|
|
|
|
.toEqual (null);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should not fail verification if timeout unspecified', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2020-11-29 12:01:43 +01:00
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (36e5);
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz');
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should reject old signatures', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2020-11-29 12:01:43 +01:00
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (50);
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz', 1);
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (dec)
|
|
|
|
.toEqual (null);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should not reject valid signatures', async () => {
|
2020-10-04 12:18:39 +02:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
2020-11-29 12:01:43 +01:00
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (50);
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz', 100);
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
2020-11-29 12:01:43 +01:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should verify signature using function retrieved timeout', async () => {
|
2020-12-13 13:31:48 +01:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz', { to: 100 });
|
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (50);
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz', (info) => info.to);
|
2020-12-13 13:31:48 +01:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('verify_signature on almost timed out packet', async () => {
|
2020-11-29 12:01:43 +01:00
|
|
|
const obj = { foo: 'bar' };
|
|
|
|
const str = crypto.sign_object (obj, 'baz');
|
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (10);
|
2022-08-08 13:07:06 +02:00
|
|
|
const dec = await crypto.verify_signature (str, 'baz', 10);
|
2020-11-29 12:01:43 +01:00
|
|
|
expect (obj)
|
|
|
|
.toEqual (dec);
|
2020-10-04 12:18:39 +02:00
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should decode problematic token', async () => {
|
2020-12-30 17:16:29 +01:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
const str = 'wEJbzvUywiaiGWZUG6CtCXNkNmRGyVoi9icytpTe4gZhsb8Gk.5PZbhGL525mdV7EmYomTwUei6qULpLaZwSXy92eaUDNgbyXPHsr9dfUCeEBpTqmzuq3VtmmV43epUyWRoHocAsV3.2';
|
2022-08-08 13:07:06 +02:00
|
|
|
const obj = await crypto.decode_signed (str);
|
2020-12-30 17:16:29 +01:00
|
|
|
expect (obj)
|
|
|
|
.toEqual ({ id: 1 });
|
|
|
|
});
|
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('should automatically reencode b64 tokens', async () => {
|
2020-12-30 17:16:29 +01:00
|
|
|
// eslint-disable-next-line max-len
|
2020-10-04 12:18:39 +02:00
|
|
|
const str = 'eyJpYXQiOjE1ODE0NDAwMTIyODgsIm9iaiI6eyJpZCI6MX19.24ZOsWrnfkNe%2FbM0r7DaVJMqE2bfn2aAM%2BZSzWeSf31OCTlXXNWD34RBL2X5v3UliYQ4IIsLNBFbaW9texPHug%3D%3D';
|
2022-08-08 13:07:06 +02:00
|
|
|
const obj = await crypto.decode_signed (str);
|
2020-10-04 12:18:39 +02:00
|
|
|
expect (obj)
|
|
|
|
.toEqual ({ id: 1 });
|
|
|
|
});
|
2020-12-30 17:16:29 +01:00
|
|
|
|
2022-08-08 13:07:06 +02:00
|
|
|
it ('verify_signature on b64 string', async () => {
|
2020-12-30 17:16:29 +01:00
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
const str = 'eyJpYXQiOjE2MDkzNDQ4MDMyMjcsIm9iaiI6eyJpZCI6MX19.N762xuMaNbT%2Fqb0uTKST68BZgSnmNxXaHl4GY7iAKqaDDEwZn3biYfg5DgJ45QgPZrndchczDjUqLkyXoqw4KQ%3D%3D';
|
2022-08-08 13:07:06 +02:00
|
|
|
const obj = await crypto.verify_signature (str, 'baz');
|
2020-12-30 17:16:29 +01:00
|
|
|
expect (obj)
|
|
|
|
.toEqual ({ id: 1 });
|
|
|
|
});
|
2020-10-04 12:18:39 +02:00
|
|
|
});
|