-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
125 lines (106 loc) · 3.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
const http = require('http');
const https = require('https');
function makeRequest(options, config) {
const requestor = config.deviseURL.match('https') ? https : http;
return new Promise((resolve, reject) => {
const res = requestor.request(options, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
if (resp.statusCode !== 200) {
reject(new Error(resp.statusCode));
} else {
resolve({
body: JSON.parse(data),
headers: resp.headers,
});
}
});
});
res.on('error', (error) => {
Error(error);
reject(error);
});
res.end();
});
}
function _checkToken(uid, client, token, expiry, correspondent_id = undefined, config) {
let hostname;
let port;
const headers = {
uid,
client,
'access-token': token,
expiry,
};
if (correspondent_id) {
Object.assign(headers, {
correspondent_id,
});
}
[hostname, port] = config.deviseURL.replace('https://', '').replace('http://', '').split(':'); /* eslint-disable-line */
port = port || 80;
port = parseInt(port, 0);
const options = {
hostname,
port,
path: `/${config.deviseScope}/auth/${config.deviseFor}/validate_token`,
method: 'GET',
headers,
};
return makeRequest(options, config);
}
function authentication(config) {
return (req, res, next) => {
const {
client,
uid,
expiry,
correspondent_id,
} = req.headers;
const innerAuthorization = req.headers['inner-authorization'];
if (innerAuthorization) {
console.info('Intern authorization required');
if (innerAuthorization === config.inner_authorization) {
console.info('Attempt Authorized Intern Access');
return next();
}
console.info('Attempt Unauthorized Access');
return res.status(401).send('Unauthorized');
}
const token = req.get('access-token');
if (!client || !uid || !expiry || !token) {
console.info('Attempt Unauthorized Access');
return res.status(401).send('Unauthorized');
}
_checkToken(uid, client, token, expiry, correspondent_id, config)
.then((authInfo) => {
req.user = authInfo.body.data;
/*
* Solve problem with empty headers response from devise-token-auth
* The problem is described here:
* https://github.com/lynndylanhurley/devise_token_auth/issues/1053
*/
if (authInfo.headers.client) {
res.set('access-token', authInfo.headers['access-token']);
res.set('client', authInfo.headers.client);
res.set('expiry', authInfo.headers.expiry);
res.set('uid', authInfo.headers.uid);
res.set('token-type', 'Bearer');
} else {
res.set('access-token', token);
res.set('client', client);
res.set('expiry', expiry);
res.set('uid', uid);
res.set('token-type', 'Bearer');
}
next();
}).catch(() => {
res.status(401).send('Unauthorized');
});
return false;
};
}
module.exports = authentication;