-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (128 loc) · 5.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
'use strict'
const crypto = require('crypto')
const util = require('util')
const steed = require('steed')
const fp = require('fastify-plugin')
const { BadRequest, Unauthorized } = require('http-errors')
const requiredParameters = ['keyid', 'algorithm', 'signature']
const availableAlgorithm = ['hmac-sha256', 'hmac-sha1', 'hmac-sha512']
const messages = {
missingRequiredHeadersErrorMessage: (headers) => {
return `Missing required HTTP headers : ${headers.join(', ')}`
},
missingRequiredSignatureParamsErrorMessage: (parameters) => {
return `Missing required signature parameters : ${parameters.join(', ')}`
},
badHeaderFormatError: (header, expectedFormat) => {
return `Bad value format for the HTTP header ${header}. Expected format : ${expectedFormat}`
},
unsupportedAlgorithmErrorMessage: 'Unsupported algorithm',
expiredRequestErrorMessage: 'Request has expired',
invalidSignatureErrorMessage: 'Authorization signature is invalid'
}
function fastifyApiKey (fastify, options, next) {
if (!options.getSecret) {
return next(new Error('missing getSecret'))
}
const {
getSecret: secretCallback,
requestLifetime = 300
} = options
fastify.decorateRequest('apiKeyVerify', requestVerify)
next()
function requestVerify (next) {
const request = this
if (next === undefined) {
return new Promise((resolve, reject) => {
request.apiKeyVerify((err, value) => {
err ? reject(err) : resolve(value)
})
})
}
if (!request.headers || !request.headers.authorization) {
return next(new Unauthorized(messages.missingRequiredHeadersErrorMessage(['authorization'])))
}
let { authorization } = request.headers
const scheme = 'signature'
const prefix = authorization.substring(0, scheme.length).toLowerCase()
if (prefix !== scheme) {
return next(new BadRequest(messages.badHeaderFormatError('Authorization', 'Signature [params]')))
}
authorization = authorization.substring(scheme.length).trim()
const parts = authorization.split(',')
const signatureParams = {}
for (const part of parts) {
const index = part.indexOf('="')
const key = part.substring(0, index).toLowerCase()
signatureParams[key] = part.substring(index + 2, part.length - 1)
}
const missingSignatureParams = []
for (const param of requiredParameters) {
if (!signatureParams[param]) {
missingSignatureParams.push(param)
}
}
if (missingSignatureParams.length > 0) {
return next(new BadRequest(messages.missingRequiredSignatureParamsErrorMessage(missingSignatureParams)))
}
signatureParams.headers = signatureParams.headers ? signatureParams.headers.toLowerCase().split(' ') : ['date']
if (!availableAlgorithm.includes(signatureParams.algorithm)) {
return next(new BadRequest(messages.unsupportedAlgorithmErrorMessage))
}
const missingRequiredHeaders = []
signatureParams.signingString = ''
signatureParams.headers.forEach((header, index, arr) => {
if (header === '(request-target)') {
signatureParams.signingString += `(request-target): ${request.method.toLowerCase()} ${request.url}`
} else if (request.headers[header]) {
signatureParams.signingString += `${header}: ${request.headers[header]}`
} else {
missingRequiredHeaders.push(header)
}
if (index < arr.length - 1) {
signatureParams.signingString += '\n'
}
})
if (missingRequiredHeaders.length > 0) {
return next(new BadRequest(messages.missingRequiredHeadersErrorMessage(missingRequiredHeaders)))
}
if (signatureParams.headers.includes('date') &&
request.headers.date &&
requestLifetime) {
const currentDate = new Date().getTime()
const requestDate = Date.parse(request.headers.date)
if (Number.isNaN(requestDate)) {
return next(new BadRequest(
messages.badHeaderFormatError('date', '<day-name>, <day> <month> <year> <hour>:<minute>:<second> GMT')))
}
if (Math.abs(currentDate - requestDate) >= requestLifetime * 1000) {
return next(new BadRequest(messages.expiredRequestErrorMessage))
}
}
steed.waterfall([
function getSecret (cb) {
const maybePromise = secretCallback(request, signatureParams.keyid, cb)
if (util.types.isPromise(maybePromise)) {
maybePromise.then(user => cb(null, user), cb)
}
},
function verify (secret, cb) {
const algorithm = signatureParams.algorithm.split('-')[1]
const hmac = crypto.createHmac(algorithm, secret)
hmac.update(signatureParams.signingString)
/* Use double hmac to protect against timing attacks */
let h1 = crypto.createHmac(algorithm, secret)
h1 = h1.update(hmac.digest()).digest()
let h2 = crypto.createHmac(algorithm, secret)
h2 = h2.update(Buffer.from(signatureParams.signature, 'base64')).digest()
if (!h1.equals(h2)) {
return cb(new Unauthorized(messages.invalidSignatureErrorMessage))
}
cb()
}], next)
}
}
module.exports = fp(fastifyApiKey, {
fastify: '>=3.x',
name: 'fastify-api-key'
})