2020-12-13 13:37:11 +01: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>, December 2020
|
|
|
|
*/
|
|
|
|
|
2020-12-06 21:06:40 +01:00
|
|
|
import ks from '../../lib/KeyStore';
|
|
|
|
|
2021-01-06 11:15:56 +01:00
|
|
|
const frame = 60;
|
|
|
|
|
2020-12-06 21:06:40 +01:00
|
|
|
/* eslint-disable-next-line max-lines-per-function */
|
|
|
|
describe ('key store', () => {
|
|
|
|
beforeAll (() => {
|
|
|
|
jasmine.clock ()
|
|
|
|
.install ();
|
|
|
|
const base_date = (new Date);
|
|
|
|
base_date.setSeconds (2);
|
|
|
|
jasmine.clock ()
|
|
|
|
.mockDate (base_date);
|
|
|
|
});
|
|
|
|
|
|
|
|
const keys: {key:string, iat:number}[] = [];
|
|
|
|
|
|
|
|
it ('should generate a new key', () => {
|
|
|
|
const iat = (new Date)
|
|
|
|
.getTime () / 1000;
|
2021-01-06 11:15:56 +01:00
|
|
|
const duration = 10 * frame;
|
2020-12-06 21:06:40 +01:00
|
|
|
const key = ks.get_key (iat, duration);
|
|
|
|
expect (typeof key)
|
|
|
|
.toEqual ('string');
|
|
|
|
expect (key.length)
|
|
|
|
.toEqual (64);
|
|
|
|
keys.push ({ iat, key });
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should return the generated key', () => {
|
|
|
|
const key = ks.get_key (keys[0].iat);
|
|
|
|
expect (key)
|
|
|
|
.toEqual (keys[0].key);
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should return the same key on a different time', () => {
|
2021-01-06 11:15:56 +01:00
|
|
|
const key = ks.get_key (keys[0].iat + (frame / 2));
|
2020-12-06 21:06:40 +01:00
|
|
|
expect (key)
|
|
|
|
.toEqual (keys[0].key);
|
|
|
|
});
|
|
|
|
|
2021-01-06 11:15:56 +01:00
|
|
|
it ('should generate a new key after time frame is over', () => {
|
2020-12-06 21:06:40 +01:00
|
|
|
jasmine.clock ()
|
2021-01-06 11:15:56 +01:00
|
|
|
.tick (frame * 1000);
|
2020-12-06 21:06:40 +01:00
|
|
|
const iat = (new Date)
|
|
|
|
.getTime () / 1000;
|
2021-01-06 11:15:56 +01:00
|
|
|
const duration = 10 * frame;
|
2020-12-06 21:06:40 +01:00
|
|
|
const key = ks.get_key (iat, duration);
|
|
|
|
expect (typeof key)
|
|
|
|
.toEqual ('string');
|
|
|
|
expect (key.length)
|
|
|
|
.toEqual (64);
|
|
|
|
expect (key).not.toEqual (keys[0].key);
|
|
|
|
keys.push ({ iat, key });
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should return both keys', () => {
|
|
|
|
const key = ks.get_key (keys[0].iat);
|
|
|
|
expect (key)
|
|
|
|
.toEqual (keys[0].key);
|
|
|
|
const k2 = ks.get_key (keys[1].iat);
|
|
|
|
expect (k2)
|
|
|
|
.toEqual (keys[1].key);
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should throw on non existing key', () => {
|
2021-01-06 11:15:56 +01:00
|
|
|
expect (() => ks.get_key (keys[1].iat + frame))
|
2020-12-06 21:06:40 +01:00
|
|
|
.toThrowError ('key could not be found');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should delete a key after it expires', () => {
|
|
|
|
jasmine.clock ()
|
2021-01-06 11:15:56 +01:00
|
|
|
.tick (10000 * frame);
|
2020-12-06 21:06:40 +01:00
|
|
|
expect (() => ks.get_key (keys[0].iat))
|
|
|
|
.toThrowError ('key could not be found');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should still retrieve the second key', () => {
|
|
|
|
const key = ks.get_key (keys[1].iat);
|
|
|
|
expect (key)
|
|
|
|
.toEqual (keys[1].key);
|
|
|
|
});
|
|
|
|
|
2020-12-06 21:29:11 +01:00
|
|
|
it ('should reject key generation of expired keys', () => {
|
|
|
|
const iat = ((new Date)
|
2021-01-06 11:15:56 +01:00
|
|
|
.getTime () / 1000) - 2;
|
2020-12-06 21:29:11 +01:00
|
|
|
const duration = 5;
|
|
|
|
expect (() => ks.get_key (iat, duration))
|
|
|
|
.toThrowError ('cannot create already expired keys');
|
|
|
|
});
|
|
|
|
|
2021-01-06 11:15:56 +01:00
|
|
|
it ('key should live as long as the longest created token', () => {
|
|
|
|
const base = new Date;
|
|
|
|
base.setSeconds (2, 0);
|
|
|
|
jasmine.clock ()
|
|
|
|
.mockDate (base);
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (24 * 60 * 60 * 1000);
|
|
|
|
const iat = (new Date)
|
|
|
|
.getTime () / 1000;
|
|
|
|
const duration1 = frame;
|
|
|
|
const duration2 = frame * 10;
|
|
|
|
|
|
|
|
const key1 = ks.get_key (iat, duration1);
|
|
|
|
const step = 0.9 * frame;
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (step * 1000);
|
|
|
|
const key2 = ks.get_key (iat + step, duration2);
|
|
|
|
expect (key1)
|
|
|
|
.toEqual (key2);
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (5000 * frame);
|
|
|
|
const keyv = ks.get_key (iat + step);
|
|
|
|
expect (keyv)
|
|
|
|
.toEqual (key1);
|
|
|
|
});
|
|
|
|
|
|
|
|
// required use case: insert keys for verification of old tokens
|
|
|
|
|
2020-12-06 21:06:40 +01:00
|
|
|
afterAll (() => {
|
2020-12-28 14:53:14 +01:00
|
|
|
jasmine.clock ()
|
|
|
|
.tick (24 * 60 * 60 * 1000);
|
2020-12-06 21:06:40 +01:00
|
|
|
jasmine.clock ()
|
|
|
|
.uninstall ();
|
|
|
|
});
|
|
|
|
});
|