This commit is contained in:
23
test/Helper.ts
Normal file
23
test/Helper.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import http from 'http';
|
||||
|
||||
class Response extends http.IncomingMessage {
|
||||
body?: string;
|
||||
}
|
||||
|
||||
export
|
||||
function get (
|
||||
headers: http.OutgoingHttpHeaders = {}
|
||||
): Promise<Response> {
|
||||
return new Promise ((resolve) => {
|
||||
http.get ('http://localhost:3000', { headers }, (res: Response) => {
|
||||
let body = '';
|
||||
res.on ('data', (d) => {
|
||||
body += d;
|
||||
});
|
||||
res.on ('end', () => {
|
||||
res.body = body;
|
||||
resolve (res);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -5,15 +5,13 @@
|
||||
* Created by Timo Hocker <timo@scode.ovh>, December 2020
|
||||
*/
|
||||
|
||||
import { hash_sha512 } from '@sapphirecode/crypto-helper';
|
||||
import auth from '../../lib/Authority';
|
||||
import bl from '../../lib/Blacklist';
|
||||
|
||||
function modify_signature (signature: string): string {
|
||||
const dec = decodeURIComponent (signature)
|
||||
.split ('.');
|
||||
dec[1] = hash_sha512 ('', '');
|
||||
return encodeURIComponent (dec.join ('.'));
|
||||
const dec = signature.split ('.');
|
||||
dec[1] = '';
|
||||
return dec.join ('.');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
|
@ -2,42 +2,7 @@ import http from 'http';
|
||||
import gateway from '../../lib/Gateway';
|
||||
import authority from '../../lib/Authority';
|
||||
import blacklist from '../../lib/Blacklist';
|
||||
|
||||
interface Response {
|
||||
body: string
|
||||
status?: number
|
||||
location?: string
|
||||
}
|
||||
|
||||
|
||||
function get (
|
||||
url: string,
|
||||
token?: string,
|
||||
mode = 0
|
||||
): Promise<Response> {
|
||||
const headers: http.OutgoingHttpHeaders = {};
|
||||
if (mode === 1)
|
||||
headers.cookie = `cookie_jar=${token}`;
|
||||
else if (mode === 0 && typeof token === 'string')
|
||||
headers.authorization = `Bearer ${token}`;
|
||||
else if (mode === 2)
|
||||
headers.authorization = `Basic ${token}`;
|
||||
return new Promise ((resolve) => {
|
||||
http.get (url, { headers }, (res) => {
|
||||
let body = '';
|
||||
res.on ('data', (d) => {
|
||||
body += d;
|
||||
});
|
||||
res.on ('end', () => {
|
||||
resolve ({
|
||||
body,
|
||||
status: res.statusCode,
|
||||
location: res.headers.location
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
import { get } from '../Helper';
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function
|
||||
describe ('gateway', () => {
|
||||
@ -76,17 +41,17 @@ describe ('gateway', () => {
|
||||
});
|
||||
|
||||
it ('should redirect any unauthorized request', async () => {
|
||||
const resp = await get ('http://localhost:3000');
|
||||
expect (resp.status)
|
||||
const resp = await get ();
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should allow a valid access token', async () => {
|
||||
const token = authority.sign ('access_token', 60);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (200);
|
||||
expect (resp.body)
|
||||
.toEqual ('passed');
|
||||
@ -94,8 +59,8 @@ describe ('gateway', () => {
|
||||
|
||||
it ('should allow a valid access token using cookies', async () => {
|
||||
const token = authority.sign ('access_token', 60);
|
||||
const resp = await get ('http://localhost:3000', token.signature, 1);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ cookie: `cookie_jar=${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (200);
|
||||
expect (resp.body)
|
||||
.toEqual ('passed');
|
||||
@ -105,55 +70,55 @@ describe ('gateway', () => {
|
||||
const token = authority.sign ('access_token', 60);
|
||||
jasmine.clock ()
|
||||
.tick (70000);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should reject a blacklisted access token', async () => {
|
||||
const token = authority.sign ('access_token', 60);
|
||||
blacklist.add_signature (token.id);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should reject any refresh_token', async () => {
|
||||
const token = authority.sign ('refresh_token', 60);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should reject any part_token', async () => {
|
||||
const token = authority.sign ('part_token', 60);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should reject any noname token', async () => {
|
||||
const token = authority.sign ('none', 60);
|
||||
const resp = await get ('http://localhost:3000', token.signature);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: `Bearer ${token.signature}` });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
|
||||
it ('should reject non-bearer auth', async () => {
|
||||
const resp = await get ('http://localhost:3000', 'foo:bar', 2);
|
||||
expect (resp.status)
|
||||
const resp = await get ({ authorization: 'Basic foo:bar' });
|
||||
expect (resp.statusCode)
|
||||
.toEqual (302);
|
||||
expect (resp.location)
|
||||
expect (resp.headers.location)
|
||||
.toEqual ('http://localhost/auth');
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user