Skip to content

Commit

Permalink
fixes #1
Browse files Browse the repository at this point in the history
Basic access token handler
  • Loading branch information
Idrinth committed Aug 13, 2020
1 parent cece3c8 commit d785a69
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 5 deletions.
4 changes: 2 additions & 2 deletions examples/get-idrinth-de.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,);
65 changes: 65 additions & 0 deletions src/middlewares/access-token.ts
Original file line number Diff line number Diff line change
@@ -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>
): string => {
for (const key of keys) {
if (typeof body[key] === 'string') {
return body[key];
}
}
return fallback;
};

@staticImplements<Middleware>()
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;
77 changes: 77 additions & 0 deletions test/middlewares/access-token.ts
Original file line number Diff line number Diff line change
@@ -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(<Request>{},),).to.deep.equal({
headers: {},
},);
},);
it('should get token by default', () => {
expect(() => Access.process(<Result><unknown>{
response: {
headers: {
'content-type': 'application/json',
},
body: '{"access":"11"}',
},
},),).to.not.throw();
},);
it('should set token it has', () => {
expect(Access.prepare(<Request>{},),).to.deep.equal({
headers: {
'authorization': 'Bearer 11',
},
},);
},);
it('should change no token if the response is incomplete', () => {
expect(() => Access.process(<Result><unknown>{
response: {},
},),).to.not.throw();
},);
it('should set token it has', () => {
expect(Access.prepare(<Request>{},),).to.deep.equal({
headers: {
'authorization': 'Bearer 11',
},
},);
},);
it('should get refresh-token by default', () => {
expect(() => Access.process(<Result><unknown>{
response: {
headers: {
'content-type': 'application/json',
},
body: '{"refresh-token":"1k"}',
},
},),).to.not.throw();
},);
it('should set all tokens it has', () => {
expect(Access.prepare(<Request>{
body: '%refresh-token-middleware%%access-token-middleware%',
},),).to.deep.equal({
headers: {
'authorization': 'Bearer 11',
},
body: '1k11',
},);
},);
},);
2 changes: 1 addition & 1 deletion test/worker/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('runner', () => {
headers: {},
body: '',
},
pre: ['#cookie']
pre: [ '#cookie', ],
},
(result,) => {
expect(result,).to.be.an('object',);
Expand Down
4 changes: 2 additions & 2 deletions test/worker/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,);
Expand Down

0 comments on commit d785a69

Please sign in to comment.