-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathenvHelper.js
More file actions
119 lines (110 loc) · 3.18 KB
/
envHelper.js
File metadata and controls
119 lines (110 loc) · 3.18 KB
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
/**
* This file defines methods for getting access tokens
*/
const _ = require('lodash')
const axios = require('axios')
const config = require('config')
const m2mAuth = require('tc-core-library-js').auth.m2m
const m2m = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))
/**
* Get m2mToken
* @returns {String} the M2MToken
*/
async function getM2MToken () {
return m2m.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}
/**
* Get user tokens from V2 API
* @param {String} userName the user name
* @param {String} userPassword the user password
* @returns {Object} the user tokens
*/
async function getUserTokenV2 (userName, userPassword) {
const { data } = await axios({
method: 'post',
url: config.AUTH_V2_URL,
data: {
username: userName,
password: userPassword,
client_id: config.AUTH_V2_CLIENT_ID,
sso: false,
scope: 'openid profile offline_access',
response_type: 'token',
connection: 'TC-User-Database',
grant_type: 'password',
device: 'Browser'
},
headers: {
'cache-control': 'no-cache',
'content-type': 'application/json'
}
})
return data
}
/**
* Get user token from V3 API
* @param {String} idToken the id_token
* @param {String} refreshToken the refresh_token
* @returns {String} the user token
*/
async function getUserTokenV3 (idToken, refreshToken) {
const { data } = await axios({
method: 'post',
url: config.AUTH_V3_URL,
data: {
param: {
externalToken: idToken,
refreshToken: refreshToken
}
},
headers: {
'cache-control': 'no-cache',
authorization: `Bearer ${this.v2_token}`,
'content-type': 'application/json;charset=UTF-8'
}
})
return data
}
/**
* Get admin token from V3 API
* @returns {String} The admin token
*/
async function getAdminToken () {
const v2 = await getUserTokenV2(config.ADMIN_CREDENTIALS_USERNAME, config.ADMIN_CREDENTIALS_PASSWORD)
const v3 = await getUserTokenV3(v2.id_token, v2.refresh_token)
return _.get(v3, 'result.content.token')
}
/**
* Get manager token from V3 API
* @returns {String} The manager token
*/
async function getManagerToken () {
const v2 = await getUserTokenV2(config.MANAGER_CREDENTIALS_USERNAME, config.MANAGER_CREDENTIALS_PASSWORD)
const v3 = await getUserTokenV3(v2.id_token, v2.refresh_token)
return _.get(v3, 'result.content.token')
}
/**
* Get copilot token from V3 API
* @returns {String} The copilot token
*/
async function getCopilotToken () {
const v2 = await getUserTokenV2(config.COPILOT_CREDENTIALS_USERNAME, config.COPILOT_CREDENTIALS_PASSWORD)
const v3 = await getUserTokenV3(v2.id_token, v2.refresh_token)
return _.get(v3, 'result.content.token')
}
/**
* Get regular user token from V3 API
* @returns {String} The user token
*/
async function getUserToken () {
const v2 = await getUserTokenV2(config.USER_CREDENTIALS_USERNAME, config.USER_CREDENTIALS_PASSWORD)
const v3 = await getUserTokenV3(v2.id_token, v2.refresh_token)
return _.get(v3, 'result.content.token')
}
module.exports = {
getM2MToken,
getAdminToken,
getManagerToken,
getCopilotToken,
getUserToken
}