-
Notifications
You must be signed in to change notification settings - Fork 45
/
idm.js
129 lines (105 loc) · 4.27 KB
/
idm.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
var config = require('../config.js'),
proxy = require('./HTTPClient.js');
var log = require('./logger').logger.getLogger("IDM-Client");
var IDM = (function() {
var my_token;
var check_conn = function(callback, callbackError) {
var options = {
host: config.idm.host,
port: config.idm.port,
path: '/v3',
method: 'GET'
};
var protocol = config.idm.ssl ? 'https' : 'http';
proxy.sendData(protocol, options, undefined, undefined, callback, callbackError);
};
var authenticate = function(callback, callbackError) {
var options = {
host: config.idm.host,
port: config.idm.port,
path: '/v3/auth/tokens',
method: 'POST',
headers: {'Content-Type': 'application/json'}
};
var protocol = config.idm.ssl ? 'https' : 'http';
var body = {
auth: {
identity: {
methods: ['password'],
password: {
user: {
name: config.pep.username,
password: config.pep.password
}
}
}
}
};
proxy.sendData(protocol, options, JSON.stringify(body), undefined, function (status, resp, headers) {
my_token = headers['x-subject-token'];
callback(my_token);
}, callbackError);
};
var check_token = function(token, callback, callbackError, cache) {
var options = {
host: config.idm.host,
port: config.idm.port,
path: '/user?access_token=' + encodeURIComponent(token),
method: 'GET',
headers: {'X-Auth-Token': my_token, 'Accept': 'application/json'}
};
var protocol = config.idm.ssl ? 'https' : 'http';
if (cache[token]) {
log.info('Token in cache, checking timestamp...');
var current_time = (new Date()).getTime();
var token_time = cache[token].date.getTime();
if (current_time - token_time < config.cache_time * 1000) {
callback(cache[token].user_info);
return;
} else {
log.info('Token in cache expired');
delete cache[token];
}
}
log.info('Checking token with IDM...');
proxy.sendData(protocol, options, undefined, undefined, function (status, resp) {
var user_info = JSON.parse(resp);
if (!check_application(user_info.app_id)) {
log.error('User not authorized in application', config.pep.app_id);
callbackError(401, 'User not authorized in application', config.pep.app_id);
} else {
cache[token] = {};
cache[token].date = new Date();
cache[token].user_info = user_info;
callback(user_info);
}
}, function (status, e) {
if (status === 401) {
log.error('Error validating token. Proxy not authorized in keystone. Keystone authentication ...');
authenticate (function (status, resp) {
my_token = JSON.parse(resp).access.token.id;
log.info('Success authenticating PEP proxy. Proxy Auth-token: ', my_token);
check_token(token, callback, callbackError);
}, function (status, e) {
log.error('Error in IDM communication ', e);
callbackError(503, 'Error in IDM communication');
});
} else {
callbackError(status, e);
}
});
};
var check_application = function (app_id) {
log.debug('Token created in application: ', app_id);
log.debug('PEP Proxy application: ', config.pep.app_id);
log.debug('PEP Proxy trusted_apps: ', config.pep.trusted_apps);
if (app_id === config.pep.app_id || config.pep.trusted_apps.indexOf(app_id) !== -1) return true;
else return false;
}
return {
check_conn: check_conn,
authenticate: authenticate,
check_token: check_token
}
})();
exports.IDM = IDM;