Skip to content

Commit 682f425

Browse files
authored
Merge pull request #45 from appirio-tech/feat/universal
add additional config parameters
2 parents 2d87a80 + 61ffaf7 commit 682f425

File tree

1 file changed

+51
-33
lines changed

1 file changed

+51
-33
lines changed

lib/auth/m2m.js

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,41 @@ const getTokenExpiryTime = function (token) {
1313
return expiryTime
1414
}
1515

16-
let cachedToken = {}
16+
const cachedToken = {}
1717

1818
module.exports = function (config) {
19-
let auth0Url = _.get(config, 'AUTH0_URL', '')
20-
let auth0Audience = _.get(config, 'AUTH0_AUDIENCE', '')
21-
let auth0ProxyServerUrl = _.get(config, 'AUTH0_PROXY_SERVER_URL', auth0Url)
22-
19+
const auth0Url = _.get(config, 'AUTH0_URL')
20+
const auth0Audience = _.get(config, 'AUTH0_AUDIENCE')
21+
const auth0ProxyServerUrl = _.get(config, 'AUTH0_PROXY_SERVER_URL', auth0Url)
22+
const authScope = _.get(config, 'AUTH_SCOPE')
23+
const authProvider = _.get(config, 'AUTH_PROVIDER')
24+
const contentType = _.get(config, 'AUTH_CONTENT_TYPE')
25+
26+
const options = {
27+
url: auth0ProxyServerUrl,
28+
headers: { 'content-type': 'application/json' },
29+
body: {
30+
grant_type: 'client_credentials',
31+
client_id: '',
32+
client_secret: '',
33+
auth0_url: auth0Url
34+
},
35+
json: true
36+
}
37+
38+
if (!_.isUndefined(auth0Audience)) {
39+
options.body.audience = auth0Audience
40+
}
41+
if (!_.isUndefined(authScope)) {
42+
options.body.scope = authScope
43+
}
44+
if (!_.isUndefined(authProvider)) {
45+
options.body.provider = authProvider
46+
}
47+
if (!_.isUndefined(contentType)) {
48+
options.body.content_type = contentType
49+
}
50+
2351
return {
2452

2553
/**
@@ -31,18 +59,8 @@ module.exports = function (config) {
3159
*/
3260
getMachineToken: (clientId, clientSecret) => {
3361

34-
var options = {
35-
url: auth0ProxyServerUrl,
36-
headers: { 'content-type': 'application/json' },
37-
body: {
38-
grant_type: 'client_credentials',
39-
client_id: clientId,
40-
client_secret: clientSecret,
41-
audience: auth0Audience,
42-
auth0_url: auth0Url
43-
},
44-
json: true
45-
}
62+
options.body.client_id = clientId
63+
options.body.client_secret = clientSecret
4664

4765
return new Promise(function (resolve, reject) {
4866

@@ -57,24 +75,24 @@ module.exports = function (config) {
5775
appCachedTokenExpired = true
5876
}
5977
}
60-
if (!appCachedToken || appCachedTokenExpired ) {
61-
request.post(options, function (error, response, body) {
62-
if (error) {
63-
return reject(new Error(error))
64-
}
65-
if (body.access_token) {
66-
cachedToken[clientId] = body.access_token
67-
resolve(cachedToken[clientId])
68-
} else if (body.error && body.error_description) {
69-
reject(new Error(
70-
body.error + ': ' + body.error_description +
78+
if (!appCachedToken || appCachedTokenExpired) {
79+
request.post(options, function (error, response, body) {
80+
if (error) {
81+
return reject(new Error(error))
82+
}
83+
if (body.access_token) {
84+
cachedToken[clientId] = body.access_token
85+
resolve(cachedToken[clientId])
86+
} else if (body.error) {
87+
reject(new Error(
88+
body.error + ': ' +
7189
' ;Please check your auth credential i.e. AUTH0_URL, AUTH0_CLIENT_ID,' +
7290
' AUTH0_CLIENT_SECRET, AUTH0_AUDIENCE, AUTH0_PROXY_SERVER_URL')
73-
)
74-
} else {
75-
reject(new Error(body))
76-
}
77-
})
91+
)
92+
} else {
93+
reject(new Error(body))
94+
}
95+
})
7896
}
7997
else {
8098
resolve(appCachedToken)

0 commit comments

Comments
 (0)