-
Notifications
You must be signed in to change notification settings - Fork 53
MLE-30263: Fix Uncaught Exception in getAccessToken Error Handler #1096
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,18 @@ function getAuthenticatorKerberos(client) { | |
| function getAccessToken(operation){ | ||
| const postData = `${encodeURI('grant_type')}=${encodeURI('apikey')}&${encodeURI('key')}=${encodeURI(operation.client.connectionParams.apiKey)}`; | ||
| const path = operation.client.connectionParams.accessTokenDuration?'/token?duration='+encodeURIComponent(operation.client.connectionParams.accessTokenDuration):'/token'; | ||
| function handleTokenError(error, contextMessage) { | ||
| if (!operation.lockAccessToken) { | ||
| return; | ||
| } | ||
|
|
||
| operation.lockAccessToken = false; | ||
|
|
||
| const baseMessage = (contextMessage == null) ? 'Failed to obtain access token' : contextMessage; | ||
| const errorMessage = (error && error.message) ? `${baseMessage}: ${error.message}` : baseMessage; | ||
| operation.errorListener(new Error(errorMessage)); | ||
| } | ||
|
|
||
| const options = { | ||
| hostname: operation.client.connectionParams.host, | ||
| port: 443, | ||
|
|
@@ -58,26 +70,51 @@ function getAccessToken(operation){ | |
| }; | ||
|
|
||
| const req = https.request(options, (res) => { | ||
| let responseBody = ''; | ||
|
|
||
| res.on('data', (d) => { | ||
| responseBody += d.toString(); | ||
| }); | ||
|
|
||
| res.on('end', () => { | ||
| if(res.statusCode === 400){ | ||
| throw new Error(d.toString()); | ||
| handleTokenError(null, 'Token endpoint returned 400: ' + responseBody); | ||
| return; | ||
| } | ||
| const responseValue = JSON.parse(d.toString()); | ||
| operation.accessToken = responseValue.access_token; | ||
| operation.expiration = new Date(responseValue['.expires']); | ||
| if(operation.lockAccessToken){ | ||
|
|
||
| try { | ||
| const responseValue = JSON.parse(responseBody); | ||
| if (!responseValue.access_token || !responseValue['.expires']) { | ||
| throw new Error('missing required token fields'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw new Error is used here. Should it be replaced with handleTokenError and return earlier? |
||
| } | ||
|
|
||
| const expiration = new Date(responseValue['.expires']); | ||
| if (Number.isNaN(expiration.getTime())) { | ||
| throw new Error('invalid .expires value'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw new Error is used here. Should it be replaced with handleTokenError? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this is intentional to be picked up by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the throw new Error would be caught by catch(error) block (line 100) later. It then will call handleTokenError. So I think using throw new Error is not a bug, but replacing with handleTokenError and return earlier would be clearer and more explicit. |
||
| } | ||
|
|
||
| operation.accessToken = responseValue.access_token; | ||
| operation.expiration = expiration; | ||
| operation.lockAccessToken = false; | ||
| } catch (error) { | ||
| handleTokenError(error, 'Invalid token endpoint response'); | ||
| return; | ||
| } | ||
|
|
||
| authenticatedRequest(operation); | ||
| }); | ||
|
|
||
| res.on('error', (e) => { | ||
| operation.accessToken = new Error(e); | ||
| throw new Error(operation.accessToken); | ||
| handleTokenError(e, 'Failed to obtain access token'); | ||
| }); | ||
| }); | ||
|
|
||
| req.write(postData); | ||
| req.end(); | ||
| req.on('error', (e) => { | ||
| handleTokenError(e, 'Failed to obtain access token'); | ||
| }); | ||
|
|
||
| req.write(postData); | ||
| req.end(); | ||
| } | ||
|
|
||
| function startRequest(operation) { | ||
|
|
@@ -296,14 +333,10 @@ function authenticatedRequest(operation) { | |
| break; | ||
| case 'CLOUD': | ||
| operation.logger.debug('cloud authentication'); | ||
| if(operation.accessToken instanceof Error){ | ||
| throw new Error(operation.accessToken); | ||
| } else { | ||
| request.setHeader( | ||
| 'authorization', | ||
| 'bearer ' +operation.accessToken | ||
| ); | ||
| } | ||
| request.setHeader( | ||
| 'authorization', | ||
| 'bearer ' + operation.accessToken | ||
| ); | ||
| break; | ||
| case 'OAUTH': | ||
| request.setHeader( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.