/** * Checks if the authentication credentials currently stored in hydra.yml are correct or not. * * @return boolean */ public function isAccessTokenValid() { if (empty($this->authentication['bearer'])) { return false; } $this->api->apponly_request(array('method' => 'GET', 'url' => $this->api->url('youtube/v3/activities', ''), 'params' => array('part' => 'id', 'mine' => 'true'))); // HTTP 200 means we were successful return $this->api->response['code'] == 200; }
/** * @return string * @throws Exception */ public function getAuthenticationUrl() { $authConf = Config::$a['oauth']['providers'][$this->authProvider]; $callback = sprintf(Config::$a['oauth']['callback'], $this->authProvider); $tmhOAuth = new \tmhOAuth(array('consumer_key' => $authConf['clientId'], 'consumer_secret' => $authConf['clientSecret'], 'token' => $authConf['token'], 'secret' => $authConf['secret'], 'curl_connecttimeout' => Config::$a['curl']['connecttimeout'], 'curl_timeout' => Config::$a['curl']['timeout'], 'curl_ssl_verifypeer' => Config::$a['curl']['verifypeer'])); $code = $tmhOAuth->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $tmhOAuth->url('oauth/request_token', ''), 'params' => array('oauth_callback' => $callback))); if ($code != 200) { throw new Exception('There was an error communicating with Twitter.'); } $response = $tmhOAuth->extract_params($tmhOAuth->response['response']); if ($response['oauth_callback_confirmed'] !== 'true') { throw new Exception('The callback was not confirmed by Twitter so we cannot continue.'); } Session::set('oauth', $response); return $tmhOAuth->url('oauth/authorize', '') . "?oauth_token={$response['oauth_token']}"; }
function authorize_twitter() { if (!App::import('Lib', 'tmh_oauth')) { $this->Session->setFlash(sprintf(__('Failed to load the %s library! Contact your system administrator.', true), 'Twitter OAuth'), 'default', array('class' => 'error')); $this->redirect(array('action' => 'preferences')); } define('__DIR__', ROOT . DS . APP_DIR . DS . 'libs'); $tmhOAuth = new tmhOAuth(array('consumer_key' => Configure::read('twitter.consumer_key'), 'consumer_secret' => Configure::read('twitter.consumer_secret'))); if (!empty($this->params['url']['oauth_token'])) { $response = $this->Session->read('Twitter.response'); $this->Session->delete('Twitter.response'); if ($this->params['url']['oauth_token'] !== $response['oauth_token']) { $this->Session->setFlash(__('The oauth token you started with doesn\'t match the one you\'ve been redirected with. Do you have multiple tabs open?', true), 'default', array('class' => 'warning')); $this->redirect(array('action' => 'preferences')); } if (!isset($this->params['url']['oauth_verifier'])) { $this->Session->setFlash(__('The oauth verifier is missing so we cannot continue. Did you deny the appliction access?', true), 'default', array('class' => 'warning')); $this->redirect(array('action' => 'preferences')); } // Update with the temporary token and secret $tmhOAuth->reconfigure(array_merge($tmhOAuth->config, array('token' => $response['oauth_token'], 'secret' => $response['oauth_token_secret']))); $code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('oauth/access_token', ''), 'params' => array('oauth_verifier' => trim($this->params['url']['oauth_verifier'])))); if ($code == 200) { $oauth_creds = $tmhOAuth->extract_params($tmhOAuth->response['response']); if ($this->Person->updateAll(array('twitter_token' => "'{$oauth_creds['oauth_token']}'", 'twitter_secret' => "'{$oauth_creds['oauth_token_secret']}'"), array('Person.id' => $this->UserCache->currentId()))) { $this->Session->setFlash(sprintf(__('Your Twitter authorization has been completed. You can always revoke this at any time through the preferences page.', true), __('person', true)), 'default', array('class' => 'success')); } else { $this->Session->setFlash(sprintf(__('Twitter authorization was received, but the database failed to update.', true), __('person', true)), 'default', array('class' => 'warning')); } } else { $this->Session->setFlash(__('There was an error communicating with Twitter.', true) . ' ' . $tmhOAuth->response['response'], 'default', array('class' => 'warning')); } $this->redirect(array('action' => 'preferences')); } else { $code = $tmhOAuth->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $tmhOAuth->url('oauth/request_token', ''), 'params' => array('oauth_callback' => Router::url(Router::normalize($this->here), true)))); if ($code != 200) { $this->Session->setFlash(__('There was an error communicating with Twitter.', true) . ' ' . $tmhOAuth->response['response'], 'default', array('class' => 'warning')); $this->redirect(array('action' => 'preferences')); } // store the params into the session so they are there when we come back after the redirect $response = $tmhOAuth->extract_params($tmhOAuth->response['response']); // check the callback has been confirmed if ($response['oauth_callback_confirmed'] !== 'true') { $this->Session->setFlash(__('The callback was not confirmed by Twitter so we cannot continue.', true) . ' ' . $tmhOAuth->response['response'], 'default', array('class' => 'warning')); $this->redirect(array('action' => 'preferences')); } else { $this->Session->write('Twitter.response', $response); $this->redirect($tmhOAuth->url('oauth/authorize', '') . "?oauth_token={$response['oauth_token']}"); } } }
<?php session_name("OAuth"); session_start(); require_once 'config.php'; require_once 'tmhOAuth.php'; if (isset($_REQUEST['id'])) { $_SESSION['id'] = $_REQUEST['id']; } if (!isset($_REQUEST['oauth_verifier'])) { // Step 1: Request a temporary token and // Step 2: Direct the user to the authorize web page $tmhOAuth = new tmhOAuth(array('consumer_key' => CONSUMER_KEY, 'consumer_secret' => CONSUMER_SECRET)); $callback_url = "http" . (!empty($_SERVER['HTTPS']) ? "s" : "") . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; $code = $tmhOAuth->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $tmhOAuth->url('oauth/request_token', ''), 'params' => array('oauth_callback' => $callback_url))); if ($code != 200) { error("There was an error communicating with Twitter. {$tmhOAuth->response['response']}"); return; } // store the params into the session so they are there when we come back after the redirect $_SESSION['oauth'] = $tmhOAuth->extract_params($tmhOAuth->response['response']); // check the callback has been confirmed if ($_SESSION['oauth']['oauth_callback_confirmed'] !== 'true') { error('The callback was not confirmed by Twitter so we cannot continue.'); } else { $url = $tmhOAuth->url('oauth/authorize', '') . "?oauth_token={$_SESSION['oauth']['oauth_token']}"; header("Location: " . $url); exit; } } else { // Step 3: This is the code that runs when Twitter redirects the user to the callback. Exchange the temporary token for a permanent access token
public function post($method, $params) { $code = $this->api->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $this->api->url($method, ''), 'params' => $params)); $response = array('code' => $code, 'data' => $this->api->response['response']); return $response; }