@@ -18,27 +18,37 @@ authenticator.getAccessToken = function(tenantId, spnAppPrincipalId, spnSymmetri
1818 var key = new Buffer ( spnSymmetricKeyBase64 , 'base64' ) ;
1919 var token = jwt . encode ( payload , key ) ;
2020 var data = {
21- grant_type : 'http://oauth.net/grant_type/jwt/1.0/bearer' ,
22- assertion : token ,
23- resource : '00000002-0000-0000-c000-000000000000/directory.windows.net@' + tenantId
24- } ;
21+ grant_type : 'http://oauth.net/grant_type/jwt/1.0/bearer' ,
22+ assertion : token ,
23+ resource : '00000002-0000-0000-c000-000000000000/directory.windows.net@' + tenantId
24+ } ;
2525
2626 request . post ( 'https://accounts.accesscontrol.windows.net/tokens/OAuth/2' , { form : data } , function ( e , resp , body ) {
2727 if ( e ) return callback ( e , null ) ;
2828
29+ var parsedBody ;
30+
31+ try {
32+ parsedBody = JSON . parse ( body ) ;
33+ } catch ( e ) {
34+ var message = e . message || 'Failed to parse response from graph API' ;
35+ var parseError = new Error ( message ) ;
36+
37+ parseError . name = e . name || 'waad_parse_error' ;
38+ parseError . stack = e . stack ;
39+ parseError . body = body ;
40+ return callback ( parseError ) ;
41+ }
42+
2943 if ( resp . statusCode != 200 ) {
30- try {
31- var response = JSON . parse ( body ) ;
32- if ( response . error ) {
33- return callback ( new OAuthError ( response . error_description || response . error , response ) , null ) ;
34- }
44+ if ( parsedBody . error ) {
45+ return callback ( new OAuthError ( parsedBody . error_description || parsedBody . error , parsedBody ) , null ) ;
3546 }
36- catch ( err ) { }
3747
3848 return callback ( new OAuthError ( body ) , null ) ;
3949 }
4050
41- callback ( null , JSON . parse ( body ) . access_token ) ;
51+ callback ( null , parsedBody . access_token ) ;
4252 } ) ;
4353} ;
4454
@@ -51,28 +61,38 @@ authenticator.getGraphClient = function (tenantId, spnAppPrincipalId, spnSymmetr
5161
5262authenticator . getAccessTokenWithClientCredentials = function ( tenantDomain , appDomain , clientId , clientSecret , callback ) {
5363 var data = {
54- grant_type : 'client_credentials' ,
55- client_id : clientId + '/' + appDomain + '@' + tenantDomain ,
56- client_secret : clientSecret ,
57- resource : '00000002-0000-0000-c000-000000000000/graph.windows.net@' + tenantDomain
58- } ;
64+ grant_type : 'client_credentials' ,
65+ client_id : clientId + '/' + appDomain + '@' + tenantDomain ,
66+ client_secret : clientSecret ,
67+ resource : '00000002-0000-0000-c000-000000000000/graph.windows.net@' + tenantDomain
68+ } ;
5969
6070 request . post ( 'https://accounts.accesscontrol.windows.net/' + tenantDomain + '/tokens/OAuth/2' , { form : data } , function ( e , resp , body ) {
6171 if ( e ) return callback ( e , null ) ;
6272
73+ var parsedBody ;
74+
75+ try {
76+ parsedBody = JSON . parse ( body ) ;
77+ } catch ( e ) {
78+ var message = e . message || 'Failed to parse response from graph API' ;
79+ var parseError = new Error ( message ) ;
80+
81+ parseError . name = e . name || 'waad_parse_error' ;
82+ parseError . stack = e . stack ;
83+ parseError . body = body ;
84+ return callback ( parseError ) ;
85+ }
86+
6387 if ( resp . statusCode != 200 ) {
64- try {
65- var response = JSON . parse ( body ) ;
66- if ( response . error ) {
67- return callback ( new OAuthError ( response . error_description || response . error , response ) , null ) ;
68- }
88+ if ( parsedBody . error ) {
89+ return callback ( new OAuthError ( parsedBody . error_description || parsedBody . error , parsedBody ) , null ) ;
6990 }
70- catch ( exp ) { }
7191
7292 return callback ( new OAuthError ( body ) , null ) ;
7393 }
7494
75- callback ( null , JSON . parse ( body ) . access_token ) ;
95+ callback ( null , parsedBody . access_token ) ;
7696 } ) ;
7797} ;
7898
@@ -87,19 +107,29 @@ authenticator.getAccessTokenWithClientCredentials2 = function(tenantDomain, clie
87107 request . post ( 'https://login.windows.net/' + tenantDomain + '/oauth2/token' , { form : data } , function ( e , resp , body ) {
88108 if ( e ) return callback ( e , null ) ;
89109
110+ var parsedBody ;
111+
112+ try {
113+ parsedBody = JSON . parse ( body ) ;
114+ } catch ( e ) {
115+ var message = e . message || 'Failed to parse response from graph API' ;
116+ var parseError = new Error ( message ) ;
117+
118+ parseError . name = e . name || 'waad_parse_error' ;
119+ parseError . stack = e . stack ;
120+ parseError . body = body ;
121+ return callback ( parseError ) ;
122+ }
123+
90124 if ( resp . statusCode != 200 ) {
91- try {
92- var response = JSON . parse ( body ) ;
93- if ( response . error ) {
94- return callback ( new OAuthError ( response . error_description || response . error , response ) , null ) ;
95- }
125+ if ( parsedBody . error ) {
126+ return callback ( new OAuthError ( parsedBody . error_description || parsedBody . error , parsedBody ) , null ) ;
96127 }
97- catch ( exp ) { }
98128
99129 return callback ( new OAuthError ( body ) , null ) ;
100130 }
101131
102- callback ( null , JSON . parse ( body ) . access_token ) ;
132+ callback ( null , parsedBody . access_token ) ;
103133 } ) ;
104134} ;
105135
@@ -124,4 +154,3 @@ authenticator.getGraphClient10 = function(tenantDomain, clientId, clientSecret,
124154 } ) ;
125155} ;
126156
127-
0 commit comments