From d785a69936ed9f8a30b596e81f118937bea22df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20B=C3=BCttner?= Date: Thu, 13 Aug 2020 21:43:41 +0200 Subject: [PATCH] fixes #1 Basic access token handler --- examples/get-idrinth-de.js | 4 +- src/middlewares/access-token.ts | 65 +++++++++++++++++++++++++++ test/middlewares/access-token.ts | 77 ++++++++++++++++++++++++++++++++ test/worker/runner.ts | 2 +- test/worker/validator.ts | 4 +- 5 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 src/middlewares/access-token.ts create mode 100644 test/middlewares/access-token.ts diff --git a/examples/get-idrinth-de.js b/examples/get-idrinth-de.js index a5511cac9..c8f560224 100644 --- a/examples/get-idrinth-de.js +++ b/examples/get-idrinth-de.js @@ -18,7 +18,7 @@ const tasks = [ { __dirname + '/../src/middlewares/status-2xx', ], }, ]; -const threads = 12; -const repetitions = 180; +const threads = 2; +const repetitions = 18; execute(threads, repetitions, tasks,); diff --git a/src/middlewares/access-token.ts b/src/middlewares/access-token.ts new file mode 100644 index 000000000..22cf66ae6 --- /dev/null +++ b/src/middlewares/access-token.ts @@ -0,0 +1,65 @@ +import { + Middleware, +} from '../middleware'; +import { + Request, +} from '../request'; +import { + Result, +} from '../result'; +import staticImplements from '../helper/static-implements'; +import { + HashMap, +} from '../hashmap'; + +let access = ''; +let refresh = ''; + +const get = ( + fallback: string, + body: HashMap, + ...keys: Array +): string => { + for (const key of keys) { + if (typeof body[key] === 'string') { + return body[key]; + } + } + return fallback; +}; + +@staticImplements() +class Access { + public static prepare(request: Request,): Request { + if (typeof request.body === 'string') { + request.body = request.body.replace( + /%refresh-token-middleware%/ug, + refresh, + ); + request.body = request.body.replace( + /%access-token-middleware%/ug, + access, + ); + } + if (typeof request.headers === 'undefined') { + request.headers = {}; + } + if (access) { + request.headers.authorization = `Bearer ${ access }`; + } + return request; + } + + public static process(response: Result,): void { + if (typeof response.response.headers === 'undefined') { + return; + } + if (response.response.headers['content-type'] !== 'application/json') { + return; + } + const body = JSON.parse(response.response.body,); + access = get(access, body, 'access', 'access_token', 'access-token',); + refresh = get(refresh, body, 'refresh', 'refresh_token', 'refresh-token',); + } +} +export default Access; diff --git a/test/middlewares/access-token.ts b/test/middlewares/access-token.ts new file mode 100644 index 000000000..6d263d50d --- /dev/null +++ b/test/middlewares/access-token.ts @@ -0,0 +1,77 @@ +import Access from '../../src/middlewares/access-token'; +import { + expect, +} from 'chai'; +import 'mocha'; +import { + Request, +} from '../../src/request'; +import { + Result, +} from '../../src/result'; + +describe('middlewares/csrf-header', () => { + it('should be a class', () => { + expect(Access,).to.be.a('function',); + },); + it('should have a static method prepare', () => { + expect(Access.prepare,).to.be.a('function',); + },); + it('should have a static method process', () => { + expect(Access.process,).to.be.a('function',); + },); + it('should not set token by default', () => { + expect(Access.prepare({},),).to.deep.equal({ + headers: {}, + },); + },); + it('should get token by default', () => { + expect(() => Access.process({ + response: { + headers: { + 'content-type': 'application/json', + }, + body: '{"access":"11"}', + }, + },),).to.not.throw(); + },); + it('should set token it has', () => { + expect(Access.prepare({},),).to.deep.equal({ + headers: { + 'authorization': 'Bearer 11', + }, + },); + },); + it('should change no token if the response is incomplete', () => { + expect(() => Access.process({ + response: {}, + },),).to.not.throw(); + },); + it('should set token it has', () => { + expect(Access.prepare({},),).to.deep.equal({ + headers: { + 'authorization': 'Bearer 11', + }, + },); + },); + it('should get refresh-token by default', () => { + expect(() => Access.process({ + response: { + headers: { + 'content-type': 'application/json', + }, + body: '{"refresh-token":"1k"}', + }, + },),).to.not.throw(); + },); + it('should set all tokens it has', () => { + expect(Access.prepare({ + body: '%refresh-token-middleware%%access-token-middleware%', + },),).to.deep.equal({ + headers: { + 'authorization': 'Bearer 11', + }, + body: '1k11', + },); + },); +},); diff --git a/test/worker/runner.ts b/test/worker/runner.ts index 604d7b676..d6c4ef669 100644 --- a/test/worker/runner.ts +++ b/test/worker/runner.ts @@ -49,7 +49,7 @@ describe('runner', () => { headers: {}, body: '', }, - pre: ['#cookie'] + pre: [ '#cookie', ], }, (result,) => { expect(result,).to.be.an('object',); diff --git a/test/worker/validator.ts b/test/worker/validator.ts index 46b256a4f..f3c3c0803 100644 --- a/test/worker/validator.ts +++ b/test/worker/validator.ts @@ -54,9 +54,9 @@ describe('validator', () => { id: '#', duration, response: { - status: 209 + status: 209, }, - validators: ['#status-2xx'], + validators: [ '#status-2xx', ], },); expect(result.id,).to.equal('#',); expect(result.duration,).to.equal(duration,);