Skip to content

Commit 34b3d18

Browse files
committed
More descriptive error message when code exchange fails #86
1 parent fbc19f2 commit 34b3d18

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/Auth0.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,25 +230,31 @@ private function exchangeCode() {
230230
// Generate the url to the API that will give us the access token and id token
231231
$auth_url = $this->generateUrl('token');
232232
// Make the call
233-
$auth0_response = $this->oauth_client->getAccessToken($auth_url, "authorization_code", array(
233+
$response = $this->oauth_client->getAccessToken($auth_url, "authorization_code", array(
234234
"code" => $code,
235235
"redirect_uri" => $this->redirect_uri
236236
), array(
237237
'Auth0-Client' => ApiClient::getInfoHeadersData()->build()
238238
));
239239

240-
// Parse it
241-
$auth0_response = $auth0_response['result'];
240+
$auth0_response = $response['result'];
241+
242+
if ($response['code'] !== 200) {
243+
throw new ApiException($auth0_response['error'] . ': '. $auth0_response['error_description']);
244+
}
245+
242246
$this->debugInfo(json_encode($auth0_response));
243247
$access_token = (isset($auth0_response['access_token']))? $auth0_response['access_token'] : false;
244248
$id_token = (isset($auth0_response['id_token']))? $auth0_response['id_token'] : false;
245249

246250
if (!$access_token) {
247251
throw new ApiException('Invalid access_token - Retry login.');
248252
}
249-
if (!$id_token) {
250-
throw new ApiException('Missing JWT after code exchange. Remember to ask for openid scope.');
253+
254+
if (!$id_token) { // id_token is not mandatory anymore. There is no need to force openid connect
255+
$this->debugInfo('Missing id_token after code exchange. Remember to ask for openid scope.');
251256
}
257+
252258
// Set the access token in the oauth client for future calls to the Auth0 API
253259
$this->oauth_client->setAccessToken($access_token);
254260
$this->oauth_client->setAccessTokenType(Client::ACCESS_TOKEN_BEARER);

tests/AuthApiTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ public function testLogoutLink() {
7878
$api = new Auth0AuthApi($env['DOMAIN'], $env['GLOBAL_CLIENT_ID'], $env['GLOBAL_CLIENT_SECRET']);
7979

8080
$this->assertSame("https://" . $env['DOMAIN'] . "/logout?", $api->get_logout_link());
81+
8182
$this->assertSame("https://" . $env['DOMAIN'] . "/logout?returnTo=http%3A%2F%2Fexample.com", $api->get_logout_link("http://example.com"));
83+
8284
$this->assertSame("https://" . $env['DOMAIN'] . "/logout?returnTo=http%3A%2F%2Fexample.com&client_id=" . $env['GLOBAL_CLIENT_ID'], $api->get_logout_link("http://example.com", $env['GLOBAL_CLIENT_ID']));
8385
}
8486
}

0 commit comments

Comments
 (0)