-
Notifications
You must be signed in to change notification settings - Fork 10
/
api-key.js
41 lines (31 loc) · 1.09 KB
/
api-key.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
const express = require('express')
const router = express.Router()
const log = require('./../utils/logging')
const config = require('./../config/server').config
const request = require('./../utils/request')
const response = require('./../utils/response')
// The array of ShipFast API keys
let shipFastAPIKeys = [
config.SHIPFAST_API_KEY
]
// Verify the ShipFast API key
router.use(function(req, res, next) {
const log_id = request.log_identifier(req, 'authorization', 'sub', 'api-key.js')
// Retrieve the ShipFast API key from the request header
let shipFastAPIKey = req.get('API-KEY')
if (!shipFastAPIKey) {
log.error('ShipFast API key not specified or in the wrong format', log_id)
res.status(400).json(response.bad_request(log_id))
return
}
// Verify the ShipFast API key
if (!shipFastAPIKeys.includes(shipFastAPIKey)) {
log.error('ShipFast API key invalid', log_id)
res.status(401).json(response.invalid_request(log_id))
return
}
log.success('SHIPFAST: Valid API key.', log_id)
next()
})
// Add the authentication router to the exports
module.exports = router