Better OAuth 2 errors

This commit is contained in:
Gregory Schier
2018-01-17 13:04:47 +07:00
parent 82621e8fb3
commit 55353b2ecb
3 changed files with 11 additions and 13 deletions

View File

@@ -35,9 +35,15 @@ export default async function (
state
);
// TODO: Handle error
// Handle the error
if (authorizeResults[c.P_ERROR]) {
const code = authorizeResults[c.P_ERROR];
const msg = authorizeResults[c.P_ERROR_DESCRIPTION];
const uri = authorizeResults[c.P_ERROR_URI];
throw new Error(`OAuth 2.0 Error ${code}\n\n${msg}\n\n${uri}`);
}
const tokenResults = await _getToken(
return _getToken(
requestId,
accessTokenUrl,
credentialsInBody,
@@ -47,8 +53,6 @@ export default async function (
redirectUri,
state
);
return tokenResults;
}
async function _authorize (url, clientId, redirectUri = '', scope = '', state = '') {
@@ -130,7 +134,7 @@ async function _getToken (
throw new Error(`[oauth2] Failed to fetch token url=${url} status=${statusCode}`);
}
const results = responseToObject(bodyBuffer.toString('utf8'), [
return responseToObject(bodyBuffer.toString('utf8'), [
c.P_ACCESS_TOKEN,
c.P_REFRESH_TOKEN,
c.P_EXPIRES_IN,
@@ -140,6 +144,4 @@ async function _getToken (
c.P_ERROR_URI,
c.P_ERROR_DESCRIPTION
]);
return results;
}

View File

@@ -42,7 +42,7 @@ export function authorizeUserInWindow (
child.close();
} else if (currentUrl.match(urlFailureRegex)) {
console.log(`[oauth2] Matched error redirect to "${currentUrl}" with ${urlFailureRegex.toString()}`);
hasError = true;
finalUrl = currentUrl;
child.close();
} else if (currentUrl === url) {
// It's the first one, so it's not a redirect
@@ -67,9 +67,6 @@ export function authorizeUserInWindow (
resolve(finalUrl);
} else {
let errorDescription = 'Authorization window closed';
if (hasError) {
errorDescription += ' after oauth error';
}
reject(new Error(errorDescription));
}
});

View File

@@ -7,8 +7,7 @@ import classnames from 'classnames';
import autobind from 'autobind-decorator';
import OneLineEditor from '../../codemirror/one-line-editor';
import * as misc from '../../../../common/misc';
import {GRANT_TYPE_AUTHORIZATION_CODE, GRANT_TYPE_CLIENT_CREDENTIALS, GRANT_TYPE_IMPLICIT, GRANT_TYPE_PASSWORD} from '../../../../network/o-auth-2/constants';
import {RESPONSE_TYPE_ID_TOKEN, RESPONSE_TYPE_TOKEN, RESPONSE_TYPE_ID_TOKEN_TOKEN} from '../../../../network/o-auth-2/constants';
import {GRANT_TYPE_AUTHORIZATION_CODE, GRANT_TYPE_CLIENT_CREDENTIALS, GRANT_TYPE_IMPLICIT, GRANT_TYPE_PASSWORD, RESPONSE_TYPE_ID_TOKEN, RESPONSE_TYPE_ID_TOKEN_TOKEN, RESPONSE_TYPE_TOKEN} from '../../../../network/o-auth-2/constants';
import authorizationUrls from '../../../../datasets/authorization-urls';
import accessTokenUrls from '../../../../datasets/access-token-urls';
import getAccessToken from '../../../../network/o-auth-2/get-token';