Skip to content

Commit

Permalink
Merge pull request #45 from appirio-tech/feat/universal
Browse files Browse the repository at this point in the history
add additional config parameters
  • Loading branch information
eisbilir committed May 12, 2023
2 parents 2d87a80 + 61ffaf7 commit 682f425
Showing 1 changed file with 51 additions and 33 deletions.
84 changes: 51 additions & 33 deletions lib/auth/m2m.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,41 @@ const getTokenExpiryTime = function (token) {
return expiryTime
}

let cachedToken = {}
const cachedToken = {}

module.exports = function (config) {
let auth0Url = _.get(config, 'AUTH0_URL', '')
let auth0Audience = _.get(config, 'AUTH0_AUDIENCE', '')
let auth0ProxyServerUrl = _.get(config, 'AUTH0_PROXY_SERVER_URL', auth0Url)

const auth0Url = _.get(config, 'AUTH0_URL')
const auth0Audience = _.get(config, 'AUTH0_AUDIENCE')
const auth0ProxyServerUrl = _.get(config, 'AUTH0_PROXY_SERVER_URL', auth0Url)
const authScope = _.get(config, 'AUTH_SCOPE')
const authProvider = _.get(config, 'AUTH_PROVIDER')
const contentType = _.get(config, 'AUTH_CONTENT_TYPE')

const options = {
url: auth0ProxyServerUrl,
headers: { 'content-type': 'application/json' },
body: {
grant_type: 'client_credentials',
client_id: '',
client_secret: '',
auth0_url: auth0Url
},
json: true
}

if (!_.isUndefined(auth0Audience)) {
options.body.audience = auth0Audience
}
if (!_.isUndefined(authScope)) {
options.body.scope = authScope
}
if (!_.isUndefined(authProvider)) {
options.body.provider = authProvider
}
if (!_.isUndefined(contentType)) {
options.body.content_type = contentType
}

return {

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

var options = {
url: auth0ProxyServerUrl,
headers: { 'content-type': 'application/json' },
body: {
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret,
audience: auth0Audience,
auth0_url: auth0Url
},
json: true
}
options.body.client_id = clientId
options.body.client_secret = clientSecret

return new Promise(function (resolve, reject) {

Expand All @@ -57,24 +75,24 @@ module.exports = function (config) {
appCachedTokenExpired = true
}
}
if (!appCachedToken || appCachedTokenExpired ) {
request.post(options, function (error, response, body) {
if (error) {
return reject(new Error(error))
}
if (body.access_token) {
cachedToken[clientId] = body.access_token
resolve(cachedToken[clientId])
} else if (body.error && body.error_description) {
reject(new Error(
body.error + ': ' + body.error_description +
if (!appCachedToken || appCachedTokenExpired) {
request.post(options, function (error, response, body) {
if (error) {
return reject(new Error(error))
}
if (body.access_token) {
cachedToken[clientId] = body.access_token
resolve(cachedToken[clientId])
} else if (body.error) {
reject(new Error(
body.error + ': ' +
' ;Please check your auth credential i.e. AUTH0_URL, AUTH0_CLIENT_ID,' +
' AUTH0_CLIENT_SECRET, AUTH0_AUDIENCE, AUTH0_PROXY_SERVER_URL')
)
} else {
reject(new Error(body))
}
})
)
} else {
reject(new Error(body))
}
})
}
else {
resolve(appCachedToken)
Expand Down

0 comments on commit 682f425

Please sign in to comment.