2022-08-08 15:52:56 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Auth-Server-Helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, August 2022
|
|
|
|
*/
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
/* eslint-disable dot-notation */
|
|
|
|
|
|
|
|
import { blacklist } from '../../lib';
|
2022-08-08 15:52:56 +02:00
|
|
|
import ks from '../../lib/KeyStore';
|
2022-08-27 16:39:07 +02:00
|
|
|
import { Redis } from '../../lib/Redis';
|
2022-09-09 15:53:38 +02:00
|
|
|
import { generate_token_id } from '../../lib/token_id';
|
2022-08-08 15:52:56 +02:00
|
|
|
import { clock_finalize, clock_setup } from '../Helper';
|
|
|
|
|
|
|
|
const frame = 3600;
|
|
|
|
const redis_url = process.env.TEST_REDIS_URL || 'redis://localhost';
|
2022-08-27 16:39:07 +02:00
|
|
|
const redis = new Redis;
|
|
|
|
redis.connect (redis_url);
|
2022-08-08 15:52:56 +02:00
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
// eslint-disable-next-line max-lines-per-function
|
2022-08-08 15:52:56 +02:00
|
|
|
describe ('redis', () => {
|
2022-09-09 15:53:38 +02:00
|
|
|
const token1 = generate_token_id (new Date (Date.now () + 3600000));
|
|
|
|
const token2 = generate_token_id (new Date (Date.now () + 3600000));
|
|
|
|
const token3 = generate_token_id (new Date (Date.now () + 3600000));
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
beforeAll (async () => {
|
2022-08-08 15:52:56 +02:00
|
|
|
ks.reset_instance ();
|
|
|
|
ks.sync_redis (redis_url);
|
2022-08-27 16:39:07 +02:00
|
|
|
await blacklist.clear ();
|
|
|
|
blacklist.sync_redis (redis_url);
|
2022-08-08 15:52:56 +02:00
|
|
|
clock_setup ();
|
|
|
|
});
|
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
let iat1 = 0;
|
|
|
|
let iat2 = 0;
|
|
|
|
let k1 = '';
|
|
|
|
let k2 = '';
|
|
|
|
let i1 = '';
|
|
|
|
let i2 = '';
|
|
|
|
|
2022-08-08 15:52:56 +02:00
|
|
|
afterAll (() => clock_finalize ());
|
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
it ('should generate two keys', async () => {
|
|
|
|
iat1 = (new Date)
|
2022-08-08 15:52:56 +02:00
|
|
|
.getTime () / 1000;
|
|
|
|
await ks.get_sign_key (iat1, frame);
|
2022-08-15 17:33:25 +02:00
|
|
|
k1 = await ks.get_key (iat1);
|
2022-08-08 15:52:56 +02:00
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (frame * 1000);
|
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
iat2 = (new Date)
|
2022-08-08 15:52:56 +02:00
|
|
|
.getTime () / 1000;
|
|
|
|
await ks.get_sign_key (iat2, frame);
|
2022-08-15 17:33:25 +02:00
|
|
|
k2 = await ks.get_key (iat2);
|
2022-08-08 15:52:56 +02:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-08-15 17:33:25 +02:00
|
|
|
i1 = ks['get_index'] (iat1);
|
2022-08-08 15:52:56 +02:00
|
|
|
// eslint-disable-next-line dot-notation
|
2022-08-15 17:33:25 +02:00
|
|
|
i2 = ks['get_index'] (iat2);
|
|
|
|
});
|
2022-08-08 15:52:56 +02:00
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
it ('should have two keys in redis', async () => {
|
|
|
|
expect (JSON.parse (await redis['_redis']
|
|
|
|
?.get (`keystore_${i1}`) as string).key)
|
2022-08-08 15:52:56 +02:00
|
|
|
.toEqual (k1);
|
2022-08-15 17:33:25 +02:00
|
|
|
expect (JSON.parse (await redis['_redis']
|
|
|
|
?.get (`keystore_${i2}`) as string).key)
|
2022-08-08 15:52:56 +02:00
|
|
|
.toEqual (k2);
|
2022-08-15 17:33:25 +02:00
|
|
|
});
|
2022-08-08 15:52:56 +02:00
|
|
|
|
2022-08-15 17:33:25 +02:00
|
|
|
it ('should read two keys with a new instance', async () => {
|
2022-08-08 15:52:56 +02:00
|
|
|
const old_instance = ks.instance_id;
|
|
|
|
ks.reset_instance ();
|
|
|
|
expectAsync (ks.get_key (iat1, old_instance))
|
|
|
|
.toBeRejectedWithError ('key could not be found');
|
|
|
|
expectAsync (ks.get_key (iat1, old_instance))
|
|
|
|
.toBeRejectedWithError ('key could not be found');
|
|
|
|
|
|
|
|
ks.sync_redis (redis_url);
|
|
|
|
expect (await ks.get_key (iat1, old_instance))
|
|
|
|
.toEqual (k1);
|
|
|
|
expect (await ks.get_key (iat2, old_instance))
|
|
|
|
.toEqual (k2);
|
|
|
|
});
|
2022-08-27 16:39:07 +02:00
|
|
|
|
|
|
|
it ('should add two keys to the blacklist', async () => {
|
2022-09-09 15:53:38 +02:00
|
|
|
await blacklist.add_signature (token1);
|
|
|
|
await blacklist.add_signature (token2);
|
2022-08-27 16:39:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it ('should have two keys in redis blacklist', async () => {
|
2022-09-09 15:53:38 +02:00
|
|
|
expect ((await redis['_redis']?.exists (`blacklist_${token1}`)) === 1)
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:53:38 +02:00
|
|
|
expect ((await redis['_redis']?.exists (`blacklist_${token2}`)) === 1)
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:53:38 +02:00
|
|
|
expect ((await redis['_redis']?.exists (`blacklist_${token3}`)) === 1)
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeFalse ();
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should read keys from redis', async () => {
|
|
|
|
blacklist['_signatures'].splice (0, blacklist['_signatures'].length);
|
2022-09-09 15:53:38 +02:00
|
|
|
expect (await blacklist.is_valid (token1))
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:53:38 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:53:38 +02:00
|
|
|
expect (await blacklist.is_valid (token3))
|
2022-08-27 16:39:07 +02:00
|
|
|
.toBeTrue ();
|
|
|
|
});
|
2022-08-08 15:52:56 +02:00
|
|
|
});
|