-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenUtil.js
55 lines (45 loc) · 1.38 KB
/
tokenUtil.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
let AppCredentials = require('mindsphere-sdk-node-core').AppCredentials;
let UserCredentials = require('mindsphere-sdk-node-core').UserCredentials;
let ClientConfig = require('mindsphere-sdk-node-core').ClientConfig;
let TechnicalTokenService = require('mindsphere-sdk-node-core').TechnicalTokenService;
const token = {
USER: 'USER',
APP: 'APP'
};
let proxyValue = 'http://194.138.0.25:9400';
let tokenType = token.USER;
function toggle(req, res) {
if (tokenType == token.USER) {
tokenType = token.APP;
}
else if (tokenType == token.APP) {
tokenType = token.USER;
}
res.send(tokenType);
}
async function getToken(req, res) {
let response =await TechnicalTokenService.getToken(getConfig(req.hostname), getCredential(req));
res.send(response);
}
function getConfig(hostname) {
let config = null;
if (hostname == 'localhost') {
config = new ClientConfig({ proxy: proxyValue, timeout: 1000000 });
} else {
config = new ClientConfig({ timeout: 1000000 });
}
return config;
}
function getCredential(req) {
if (tokenType == 'APP') {
return new AppCredentials();
}else if(tokenType == 'USER' && req.get('authorization') != null){
return new UserCredentials({'authorization':req.get('authorization')});
}
}
module.exports = {
toggle,
getToken,
getConfig,
getCredential
}