-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic access token handler
- Loading branch information
Showing
5 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
},); | ||
},); | ||
},); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters