-
Notifications
You must be signed in to change notification settings - Fork 9
/
m2m.js
106 lines (93 loc) · 3.09 KB
/
m2m.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
'use strict'
var _ = require('lodash'),
axios = require('axios'),
jwt = require('jsonwebtoken')
const getTokenExpiryTime = function (token) {
let expiryTime = 0
if (token) {
let decodedToken = jwt.decode(token)
let expiryTimeInMilliSeconds = (decodedToken.exp - 60) * 1000 - (new Date().getTime())
expiryTime = Math.floor(expiryTimeInMilliSeconds / 1000)
}
return expiryTime
}
const cachedToken = {}
module.exports = function (config) {
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' },
data: {
grant_type: 'client_credentials',
client_id: '',
client_secret: '',
auth0_url: auth0Url
},
}
if (!_.isUndefined(auth0Audience)) {
options.data.audience = auth0Audience
}
if (!_.isUndefined(authScope)) {
options.data.scope = authScope
}
if (!_.isUndefined(authProvider)) {
options.data.provider = authProvider
}
if (!_.isUndefined(contentType)) {
options.data.content_type = contentType
}
return {
/**
* Generate machine to machine token from Auth0
* V3 API specification
* @param clientId client Id provided from Auth0
* @param clientSecret client secret provided from Auth0
* @return Promise promise to pass responses
*/
getMachineToken: (clientId, clientSecret) => {
options.data.client_id = clientId
options.data.client_secret = clientSecret
return new Promise(function (resolve, reject) {
// We cached the token to cachedToken variable,
// So we check the variable and the time here.
let appCachedToken = cachedToken[clientId]
let appCachedTokenExpired = false
//Check the token expiry
if (appCachedToken) {
if (getTokenExpiryTime(appCachedToken) <= 0) {
appCachedTokenExpired = true
}
}
if (!appCachedToken || appCachedTokenExpired) {
axios({...options, method: 'post'})
.then(response => {
const body = response.data;
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(JSON.stringify(body)));
}
})
.catch(error => {
reject(new Error(error));
});
}
else {
resolve(appCachedToken)
}
})
}
}
}