2021-01-03 14:51:07 +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>, January 2021
|
|
|
|
*/
|
|
|
|
|
2020-12-28 16:53:08 +01:00
|
|
|
import http from 'http';
|
|
|
|
import gateway from '../../lib/Gateway';
|
|
|
|
import authority from '../../lib/Authority';
|
|
|
|
import blacklist from '../../lib/Blacklist';
|
2020-12-30 17:21:56 +01:00
|
|
|
import { get } from '../Helper';
|
2020-12-28 16:53:08 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line max-lines-per-function
|
|
|
|
describe ('gateway', () => {
|
|
|
|
let server: http.Server|null = null;
|
|
|
|
|
|
|
|
beforeAll (() => {
|
|
|
|
jasmine.clock ()
|
|
|
|
.install ();
|
|
|
|
jasmine.clock ()
|
|
|
|
.mockDate (new Date);
|
|
|
|
|
|
|
|
const g = gateway ({
|
|
|
|
redirect_url: 'http://localhost/auth',
|
|
|
|
cookie_name: 'cookie_jar'
|
|
|
|
});
|
|
|
|
|
|
|
|
server = http.createServer ((req, res) => {
|
|
|
|
const passed_handler = () => {
|
|
|
|
res.writeHead (200);
|
|
|
|
res.end ('passed');
|
|
|
|
};
|
|
|
|
g (req, res, passed_handler);
|
|
|
|
});
|
|
|
|
server.listen (3000);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll (() => {
|
|
|
|
if (server === null)
|
|
|
|
throw new Error ('server is null');
|
|
|
|
server.close ();
|
|
|
|
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (24 * 60 * 60 * 1000);
|
|
|
|
jasmine.clock ()
|
|
|
|
.uninstall ();
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should redirect any unauthorized request', async () => {
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ();
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should allow a valid access token', async () => {
|
|
|
|
const token = authority.sign ('access_token', 60);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (200);
|
|
|
|
expect (resp.body)
|
|
|
|
.toEqual ('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should allow a valid access token using cookies', async () => {
|
|
|
|
const token = authority.sign ('access_token', 60);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ cookie: `cookie_jar=${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (200);
|
|
|
|
expect (resp.body)
|
|
|
|
.toEqual ('passed');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject an outdated access token', async () => {
|
|
|
|
const token = authority.sign ('access_token', 60);
|
|
|
|
jasmine.clock ()
|
|
|
|
.tick (70000);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject a blacklisted access token', async () => {
|
|
|
|
const token = authority.sign ('access_token', 60);
|
|
|
|
blacklist.add_signature (token.id);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject any refresh_token', async () => {
|
|
|
|
const token = authority.sign ('refresh_token', 60);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject any part_token', async () => {
|
|
|
|
const token = authority.sign ('part_token', 60);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject any noname token', async () => {
|
|
|
|
const token = authority.sign ('none', 60);
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
|
|
|
|
it ('should reject non-bearer auth', async () => {
|
2020-12-30 17:21:56 +01:00
|
|
|
const resp = await get ({ authorization: 'Basic foo:bar' });
|
|
|
|
expect (resp.statusCode)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual (302);
|
2020-12-30 17:21:56 +01:00
|
|
|
expect (resp.headers.location)
|
2020-12-28 16:53:08 +01:00
|
|
|
.toEqual ('http://localhost/auth');
|
|
|
|
});
|
|
|
|
});
|