Completed
Pull Request — master (#4)
by Aaron
02:20
created

main/src/getToken.ts   A

Complexity

Total Complexity 4
Complexity/F 0

Size

Lines of Code 27
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 21
mnd 4
bc 4
fnc 0
dl 0
loc 27
rs 10
bpm 0
cpm 0
noi 0
c 0
b 0
f 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