-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth.js
70 lines (56 loc) · 1.83 KB
/
auth.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
//
// Copyright (c) 2017 Cisco Systems
// Licensed under the MIT License
//
const debug = require("debug")("emulator:auth");
const sendError = require('./utils').sendError;
const sendSuccess = require('./utils').sendSuccess;
const authentication = {};
// Load pre-registered tokens
var tokens = require('./tokens.json');
if (!tokens) {
tokens = {};
}
authentication.tokens = tokens;
authentication.middleware = function (req, res, next) {
// Public resources
if ((req.path == "/") || (req.path == "/tokens")) {
next();
}
// Check authentication
const auth = req.get("Authorization");
if (!auth) {
debug("No Authentication header");
return _sendAuthenticationError(res);
}
// Extract token
const splitted = auth.match(/^Bearer\s([0-9a-zA-Z]*)$/);
if (!splitted || (splitted.length != 2)) {
debug("Authentication token does not match 'Bearer [0-9a-zA-Z]*' pattern");
return _sendAuthenticationError(res);
}
// Retreive Person from token
const token = splitted[1];
const account = tokens[token];
if (!account) {
debug("No account found for token: " + token);
return _sendAuthenticationError(res);
}
// Add person to request context
res.locals.person = account;
next();
}
// Webex standard message
// {
// "message": "The request requires a valid access token set in the Authorization request header.",
// "errors": [
// {
// "description": "The request requires a valid access token set in the Authorization request header."
// }
// ],
// "trackingId": "ROUTER_5D1DCAC2-63EF-01BB-007E-AD26DC3A007E"
// }
function _sendAuthenticationError(res) {
sendError(res, 401, "The request requires a valid access token set in the Authorization request header.");
}
module.exports = authentication;