163 lines
4.2 KiB
TypeScript
Raw Normal View History

2020-12-28 15:04:52 +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-19 16:19:09 +01:00
import auth from '../../lib/Authority';
2020-12-28 14:53:14 +01:00
import bl from '../../lib/Blacklist';
2021-01-06 22:43:03 +01:00
import {
2021-01-07 15:43:54 +01:00
clock_finalize,
clock_setup,
modify_signature
2021-01-06 22:43:03 +01:00
} from '../Helper';
2020-12-28 14:53:14 +01:00
// eslint-disable-next-line max-lines-per-function
2020-12-19 16:19:09 +01:00
describe ('authority', () => {
beforeEach (() => {
2021-01-07 15:43:54 +01:00
clock_setup ();
2020-12-19 16:19:09 +01:00
});
afterEach (() => {
2021-01-07 15:43:54 +01:00
clock_finalize ();
2020-12-19 16:19:09 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should create an access token', async () => {
const token = await auth.sign ('access_token', 60);
2020-12-19 16:19:09 +01:00
jasmine.clock ()
.tick (30000);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeTrue ();
2020-12-28 14:53:14 +01:00
expect (res.valid)
.toBeTrue ();
2020-12-19 16:19:09 +01:00
expect (res.type)
.toEqual ('access_token');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual (token.id);
expect (res.error)
.toBeUndefined ();
2020-12-19 16:19:09 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should create a refresh token', async () => {
const token = await auth.sign ('refresh_token', 600);
2020-12-19 16:19:09 +01:00
jasmine.clock ()
.tick (30000);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
2020-12-28 14:53:14 +01:00
expect (res.valid)
.toBeTrue ();
2020-12-19 16:19:09 +01:00
expect (res.type)
.toEqual ('refresh_token');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual (token.id);
expect (res.error)
.toBeUndefined ();
2020-12-19 16:19:09 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should create a part token', async () => {
const token = await auth.sign ('part_token', 60, { next_module: '2fa' });
2020-12-19 16:19:09 +01:00
jasmine.clock ()
.tick (30000);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
2020-12-28 14:53:14 +01:00
expect (res.valid)
.toBeTrue ();
2020-12-19 16:19:09 +01:00
expect (res.type)
.toEqual ('part_token');
expect (res.next_module)
.toEqual ('2fa');
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual (token.id);
expect (res.error)
.toBeUndefined ();
2020-12-19 16:19:09 +01:00
});
2020-12-28 14:53:14 +01:00
2021-01-06 16:06:03 +01:00
it ('should reject an invalid access token', async () => {
const token = await auth.sign ('access_token', 60);
2020-12-28 14:53:14 +01:00
token.signature = modify_signature (token.signature);
jasmine.clock ()
.tick (30000);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
expect (res.valid)
.toBeFalse ();
expect (res.type)
.toEqual ('none');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual ('');
expect (res.error)
.toEqual ('invalid signature');
2020-12-28 14:53:14 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should reject blacklisted access token', async () => {
const token = await auth.sign ('access_token', 60);
2020-12-28 14:53:14 +01:00
jasmine.clock ()
.tick (30000);
bl.add_signature (token.id);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
expect (res.valid)
.toBeFalse ();
expect (res.type)
.toEqual ('access_token');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual (token.id);
expect (res.error)
.toEqual ('blacklisted');
2020-12-28 14:53:14 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should reject an invalid refresh token', async () => {
const token = await auth.sign ('refresh_token', 600);
2020-12-28 14:53:14 +01:00
token.signature = modify_signature (token.signature);
jasmine.clock ()
.tick (30000);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
expect (res.valid)
.toBeFalse ();
expect (res.type)
.toEqual ('none');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual ('');
expect (res.error)
.toEqual ('invalid signature');
2020-12-28 14:53:14 +01:00
});
2021-01-06 16:06:03 +01:00
it ('should reject a blacklisted refresh token', async () => {
const token = await auth.sign ('refresh_token', 600);
2020-12-28 14:53:14 +01:00
jasmine.clock ()
.tick (30000);
bl.add_signature (token.id);
const res = auth.verify (token.signature);
expect (res.authorized)
.toBeFalse ();
expect (res.valid)
.toBeFalse ();
expect (res.type)
.toEqual ('refresh_token');
expect (res.next_module)
.toBeUndefined ();
2021-01-03 15:32:29 +01:00
expect (res.id)
.toEqual (token.id);
expect (res.error)
.toEqual ('blacklisted');
2020-12-28 14:53:14 +01:00
});
2020-12-19 16:19:09 +01:00
});