Total Complexity | 4 |
Complexity/F | 0 |
Lines of Code | 27 |
Function Count | 0 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // tslint:disable-next-line: no-implicit-dependencies |
||
2 | import { APIGatewayProxyEvent } from 'aws-lambda' // @types |
||
3 | import UnauthorisedResponse from './UnauthorisedResponse' |
||
4 | |||
5 | type Token = string |
||
6 | |||
7 | const getToken = (event: APIGatewayProxyEvent): Token | UnauthorisedResponse => { |
||
8 | const { headers } = event |
||
9 | if (!headers) { |
||
10 | return new UnauthorisedResponse('no headers') |
||
11 | } |
||
12 | const authHeader = headers.Authorization |
||
13 | if (!authHeader) { |
||
14 | return new UnauthorisedResponse('no authorization header') |
||
15 | } |
||
16 | const [scheme, token] = authHeader.trim().split(' ') |
||
17 | if (!scheme || (scheme.toLowerCase() !== 'bearer')) { |
||
18 | return new UnauthorisedResponse('authorization scheme must be bearer') |
||
19 | } |
||
20 | if (!token) { |
||
21 | return new UnauthorisedResponse('no authorization token') |
||
22 | } |
||
23 | return token |
||
24 | } |
||
25 | |||
26 | export default getToken |
||
27 |