@@ -13,13 +13,41 @@ const getTokenExpiryTime = function (token) {
13
13
return expiryTime
14
14
}
15
15
16
- let cachedToken = { }
16
+ const cachedToken = { }
17
17
18
18
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
+
23
51
return {
24
52
25
53
/**
@@ -31,18 +59,8 @@ module.exports = function (config) {
31
59
*/
32
60
getMachineToken : ( clientId , clientSecret ) => {
33
61
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
46
64
47
65
return new Promise ( function ( resolve , reject ) {
48
66
@@ -57,24 +75,24 @@ module.exports = function (config) {
57
75
appCachedTokenExpired = true
58
76
}
59
77
}
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 + ': ' +
71
89
' ;Please check your auth credential i.e. AUTH0_URL, AUTH0_CLIENT_ID,' +
72
90
' 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
+ } )
78
96
}
79
97
else {
80
98
resolve ( appCachedToken )
0 commit comments