From a247bf678e90a43ef428460eca1fc6c6c8352fed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 22 Aug 2017 23:50:49 +0200 Subject: [PATCH] MDL-59645 oauth1: Improve reporting of token errors Provide a more meaningful error message and debuginfo allowing to diagnose what is going on. In case of flickr, this is typically thrown when flickr API responses with their "bad, bad panda" HTML page instead of the expected reply. --- lang/en/error.php | 2 ++ lib/oauthlib.php | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lang/en/error.php b/lang/en/error.php index 2c1e61e3ab4..1c4898573de 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -448,6 +448,8 @@ $string['notlocalisederrormessage'] = '{$a}'; $string['notmemberofgroup'] = 'You are not a member of this course group'; $string['notownerofkey'] = 'You are not owner of this key'; $string['nousers'] = 'No such user!'; +$string['oauth1accesstoken'] = 'OAuth 1.0 error: We did not obtain the access token.'; +$string['oauth1requesttoken'] = 'OAuth 1.0 error: We did not obtain the request token - the service provider may be temporarily down.'; $string['onlyadmins'] = 'Only administrators can do that'; $string['onlyeditingteachers'] = 'Only editing teachers can do that'; $string['onlyeditown'] = 'You can only edit your own information'; diff --git a/lib/oauthlib.php b/lib/oauthlib.php index a8181224850..7b7262f681d 100644 --- a/lib/oauthlib.php +++ b/lib/oauthlib.php @@ -232,7 +232,7 @@ class oauth_helper { // oauth_token_secret $result = $this->parse_result($content); if (empty($result['oauth_token'])) { - throw new moodle_exception('Error while requesting an oauth token'); + throw new moodle_exception('oauth1requesttoken', 'core_error', '', null, $content); } // Build oauth authorize url. $result['authorize_url'] = $this->authorize_url . '?oauth_token='.$result['oauth_token']; @@ -265,6 +265,11 @@ class oauth_helper { unset($params['oauth_callback']); $content = $this->http->post($this->access_token_api, $params, $this->http_options); $keys = $this->parse_result($content); + + if (empty($keys['oauth_token']) || empty($keys['oauth_token_secret'])) { + throw new moodle_exception('oauth1accesstoken', 'core_error', '', null, $content); + } + $this->set_access_token($keys['oauth_token'], $keys['oauth_token_secret']); return $keys; }