-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
224 lines (193 loc) · 7.3 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
const http = require('http');
const https = require('https');
const request = require('request');
const log = require('loglevel');
const fs = require('fs');
log.setLevel(0);
module.exports = function(ds_ip, log_level) {
if(ds_ip === null || ds_ip === undefined){
throw "No DSS IP specified in constructor!";
}
if (log_level) {
log.setLevel(log_level);
}
let module = {};
/**
* Sends a GET request to the dss.
* @param {string} path
* @param {Object} query
* @returns {Promise} Promise: resolve(json) | reject(err, statusCode)
*/
function requestFromDSS(path, query) {
return new Promise((resolve, reject) => {
const agent_options = {
rejectUnauthorized: false,
};
const agent = new https.Agent(agent_options);
const options = {
url: 'https://' + ds_ip + ':8080' + path,
protocol: 'https:',
qs: query,
json: true,
agent: agent,
};
request(options, (err, res, body) => {
if (!err && res && res.statusCode === 200) {
resolve(body);
}
else {
reject(
{
error: err,
status_code: res ? res.statusCode : null,
});
}
});
});
}
let last_access = 0;
const session_timeout = 50 * 1000;
let session_token;
/**
* Requests from dss with session token
* @param {string} path path to request
* @param {Object} query params to query
* @param {string} app_token Application token, that is activated
* @returns {Promise} Promise: resolve(body) | reject(error)
*/
function requestWithSessionFromDSS(path, query, app_token) {
return new Promise((resolve, reject) => {
/* check if session is still valid */
if ((last_access + session_timeout) <= Date.now() || session_token === null) {
/* renew session token */
const login_path = '/json/system/loginApplication';
const login_query = {
loginToken: app_token,
};
/**
* Request new sess token
*/
log.debug('requesting new session token...');
requestFromDSS(login_path, login_query).then((body) => {
if (body.ok === true) {
session_token = body.result.token;
last_access = Date.now();
log.debug('obtained session token: ' + session_token);
/* proceed with request */
requestWithSessionFromDSS(path, query, app_token).then((body) => {
resolve(body);
}, (err) => {
reject('error while requesting ' + path + " :" + JSON.stringify(err));
});
}
else {
reject('failed to obtain sess token ' + body.message);
}
}, (err) => {
reject('failed to obtain sess token ' + err);
});
}
else {
log.debug('requesting without refreshing token');
query.token = session_token;
requestFromDSS(path, query).then((body) => {
last_access = Date.now();
resolve(body);
}, (err) => {
reject(err);
});
}
});
}
module.requestWithSessionFromDSS = requestWithSessionFromDSS;
/**
* Retrieves AppToken from dss.
* @param {String} readableAppName Human readable app name
* @returns {Promise} Promise: resolve(token) | reject(err)
*/
function retrieveApplicationToken(readableAppName) {
return new Promise((resolve, reject) => {
const path = '/json/system/requestApplicationToken';
const query = {
applicationName: readableAppName,
};
requestFromDSS(path, query).then((body) => {
let token = body.result.applicationToken;
resolve(
token);
}, (err) => {
reject('AppToken no valid response: ' + err.error + ' status: ' + err.status_code);
});
});
}
module.retrieveApplicationToken = retrieveApplicationToken;
/**
* Activates application token with credentials
* @param {string} username dss username
* @param {string} password dss password
* @param {string} app_token application token
* @returns {Promise} Promise: resolve() | reject(err)
*/
function activateApplicationToken(username, password, app_token) {
return new Promise((resolve, reject) => {
/* login to dss to obtain session token */
const path = '/json/system/login';
const query = {
user: username,
password: password,
};
requestFromDSS(path, query).then((body) => {
if (body.ok === true) {
/* activate application token with session token*/
const path = '/json/system/enableToken';
const query = {
applicationToken: app_token,
token: body.result.token,
};
requestFromDSS(path, query).then((body) => {
if (body.ok === true) {
resolve();
}
else {
reject('Unknown error while activating the token.');
}
}, (err) => {
reject(err);
});
}
else {
reject('Wrong login data!');
}
}, (err) => {
reject(err);
});
});
}
module.activateApplicationToken = activateApplicationToken;
/**
* Automatic authentication of application in the dss
* @param {string} readableName - Name of the application
* @param {string} username - dss username
* @param {string} password - dss password
* @returns {Promise} Promise: resolve(appToken) | reject(err)
*/
function initializeAuthentication(readableName, username, password) {
return new Promise((resolve, reject) => {
retrieveApplicationToken(readableName).then((appToken) => {
log.debug('Retrieved AppToken ' + appToken);
activateApplicationToken(username, password, appToken).then(() => {
log.debug('Successful authentification.');
resolve(appToken);
}, (err) => {
log.debug('Error while activating token ' + err);
reject(err);
});
}, (err) => {
reject(err);
});
});
}
module.initializeAuthentication = initializeAuthentication;
return module;
};