-
Notifications
You must be signed in to change notification settings - Fork 4
/
server-auth.js
43 lines (38 loc) · 1.33 KB
/
server-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
var request = require('request'),
debug = require('debug')('openframe:pubsub:server_auth');
module.exports = function(api_url) {
var api_token = process.env.PS_API_TOKEN;
function authenticate(token) {
return new Promise(function(resolve, reject) {
if (token === api_token) {
debug('API Server Token -- Valid');
return resolve();
}
request(api_url + '/users/config?access_token='+token, function(error, response, body) {
if (error) {
debug(error);
reject();
} else {
resolve();
}
});
});
}
return {
incoming: function(message, callback) {
// Let non-subscribe messages through
if (message.channel !== '/meta/subscribe') {
return callback(message);
}
// Get subscribed channel and auth token
var subscription = message.subscription,
msgToken = message.ext && message.ext.accessToken;
authenticate(msgToken).then(function() {
callback(message);
}).catch(function() {
message.error = 'Invalid access token';
callback(message);
});
}
};
};