/**
  * attempt to build up a request from what was passed to the server
  */
 public static function from_request($http_method = NULL, $http_url = NULL, $parameters = NULL)
 {
     $scheme = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ? 'http' : 'https';
     $http_url = $http_url ? $http_url : $scheme . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
     $http_method = $http_method ? $http_method : $_SERVER['REQUEST_METHOD'];
     // We weren't handed any parameters, so let's find the ones relevant to
     // this request.
     // If you run XML-RPC or similar you should use this to provide your own
     // parsed parameter-list
     if (!$parameters) {
         // Find request headers
         $request_headers = OAuthUtil::get_headers();
         // Parse the query-string to find GET parameters
         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
         // It's a POST request of the proper content-type, so parse POST
         // parameters and add those overriding any duplicates from GET
         if ($http_method == "POST" && isset($request_headers['Content-Type']) && strstr($request_headers['Content-Type'], 'application/x-www-form-urlencoded')) {
             $post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT));
             $parameters = array_merge($parameters, $post_data);
         }
         // We have a Authorization-header with OAuth data. Parse the header
         // and add those overriding any duplicates from GET or POST
         if (isset($request_headers['Authorization']) && substr($request_headers['Authorization'], 0, 6) == 'OAuth ') {
             $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
             $parameters = array_merge($parameters, $header_parameters);
         }
     }
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
Example #2
0
 /**
  * (non-PHPdoc)
  * @see plugins/sfDoctrineOAuthPlugin/lib/sfOAuth::getAccessToken()
  */
 public function getAccessToken($verifier, $parameters = array())
 {
     $url = $this->getAccessTokenUrl();
     $this->setAccessParameter('client_id', $this->getKey());
     $this->setAccessParameter('client_secret', $this->getSecret());
     $this->setAccessParameter('redirect_uri', $this->getCallback());
     $this->setAccessParameter('code', $verifier);
     $this->addAccessParameters($parameters);
     $params = $this->call($url, $this->getAccessParameters(), 'GET');
     $params = OAuthUtil::parse_parameters($params);
     $access_token = isset($params['access_token']) ? $params['access_token'] : null;
     if (is_null($access_token) && $this->getLogger()) {
         $error = sprintf('{OAuth} access token failed - %s returns %s', $this->getName(), print_r($params, true));
         $this->getLogger()->err($error);
     } elseif ($this->getLogger()) {
         $message = sprintf('{OAuth} %s return %s', $this->getName(), print_r($params, true));
         $this->getLogger()->info($message);
     }
     $token = new Token();
     $token->setTokenKey($access_token);
     $token->setName($this->getName());
     $token->setStatus(Token::STATUS_ACCESS);
     $token->setOAuthVersion($this->getVersion());
     unset($params['access_token']);
     if (count($params) > 0) {
         $token->setParams($params);
     }
     $this->setExpire($token);
     $this->setToken($token);
     // get identifier maybe need the access token
     $token->setIdentifier($this->getIdentifier());
     $this->setToken($token);
     return $token;
 }
 public function getAccessToken($consumerName, $accessTokenURL, $requestToken, $httpMethod = 'POST', $parameters = array())
 {
     $this->url = $accessTokenURL;
     $queryStringParams = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
     $parameters['oauth_verifier'] = $queryStringParams['oauth_verifier'];
     $request = $this->createRequest($consumerName, $httpMethod, $accessTokenURL, $requestToken, $parameters);
     return $this->doRequest($request);
 }
Example #4
0
 function __construct($http_method, $http_url, $parameters = NULL)
 {
     $parameters = $parameters ? $parameters : array();
     $parameters = array_merge(OAuthUtil::parse_parameters(parse_url($http_url, PHP_URL_QUERY)), $parameters);
     $this->parameters = $parameters;
     $this->http_method = $http_method;
     $this->http_url = $http_url;
 }
Example #5
0
 public function testParseParameter()
 {
     // Tests taken from
     // http://wiki.oauth.net/TestCases ("Normalize Request Parameters")
     $this->assertEquals(array('name' => ''), OAuthUtil::parse_parameters('name'));
     $this->assertEquals(array('a' => 'b'), OAuthUtil::parse_parameters('a=b'));
     $this->assertEquals(array('a' => 'b', 'c' => 'd'), OAuthUtil::parse_parameters('a=b&c=d'));
     $this->assertEquals(array('a' => array('x!y', 'x y')), OAuthUtil::parse_parameters('a=x!y&a=x+y'));
     $this->assertEquals(array('x!y' => 'a', 'x' => 'a'), OAuthUtil::parse_parameters('x!y=a&x=a'));
 }
 function get_access_token($username, $password)
 {
     $parameters = array();
     $parameters['x_auth_username'] = $username;
     $parameters['x_auth_password'] = $password;
     $parameters['x_auth_mode'] = 'client_auth';
     $request = $this->oAuthRequest($this->host . "oauth/access_token", 'POST', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #7
0
 public function getAccessToken($verifier = false)
 {
     $params = array();
     if (!empty($verifier)) {
         $params['oauth_verifier'] = $verifier;
     }
     $response = $this->_request($this->_accessTokenUrl, 'GET', $params);
     $token = OAuthUtil::parse_parameters($response);
     $this->_token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
 /**
  * gets the request token for the first time
  */
 function getRequestToken($oauth_callback = NULL)
 {
     $params = array();
     if (!empty($oauth_callback)) {
         $params['oauth_callback'] = $oauth_callback;
     }
     $request = $this->makeRequest($this->requestTokenURL, false, $params);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
 function getAccessToken($oauth_verifier = FALSE)
 {
     $parameters = array();
     if (!empty($oauth_verifier)) {
         $parameters['oauth_verifier'] = $oauth_verifier;
     }
     $request = $this->oAuthRequest($this->accessTokenURL(), 'GET', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #10
0
 function requestToken($callback = null)
 {
     $parameters = array('scope' => 'read_public');
     if ($callback) {
         $this->redirect_uri = $parameters['oauth_callback'] = $callback;
     }
     $request = $this->signedRequest($this->request_token_url, $this->request_token_method, $parameters);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #11
0
 /**
  * pretty much a helper function to set up the request
  */
 public static function from_consumer_and_token($consumer, $token, $http_method, $http_url, $parameters = NULL)
 {
     @$parameters or $parameters = array();
     $defaults = array("oauth_version" => OAuthRequest::$version, "oauth_nonce" => OAuthRequest::generate_nonce(), "oauth_timestamp" => OAuthRequest::generate_timestamp(), "oauth_consumer_key" => $consumer->key);
     if ($token) {
         $defaults['oauth_token'] = $token->key;
     }
     $parameters = array_merge($defaults, $parameters);
     // Parse the query-string to find and add GET parameters
     $parts = parse_url($http_url);
     if (isset($parts['query'])) {
         $qparms = OAuthUtil::parse_parameters($parts['query']);
         $parameters = array_merge($qparms, $parameters);
     }
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
Example #12
0
 /**
  * Extract token data from a MMI_Curl_Response object and create an
  * OAuthToken object.
  *
  * @param	MMI_Curl_Response	the response object
  * @return	OAuthToken
  */
 protected function _extract_token($response)
 {
     if (!$response instanceof MMI_Curl_Response) {
         return NULL;
     }
     $token = NULL;
     if (intval($response->http_status_code()) === 200) {
         $body = $response->body();
         if (!empty($body)) {
             $parms = OAuthUtil::parse_parameters($body);
             $oauth_token = Arr::get($parms, 'oauth_token');
             $oauth_token_secret = Arr::get($parms, 'oauth_token_secret');
             if (!empty($oauth_token) and !empty($oauth_token_secret)) {
                 $token = new OAuthToken($oauth_token, $oauth_token_secret);
                 unset($parms['oauth_token'], $parms['oauth_token_secret']);
                 $token->attributes = $parms;
             }
         }
     }
     return $token;
 }
Example #13
0
 private function getMyUrl()
 {
     $server_data = $this->getHttpRequest()->getServerData();
     $url = true === isset($server_data["HTTPS"]) && 'on' == $server_data["HTTPS"] ? "https://" : "http://";
     $url .= $server_data['HTTP_HOST'] . parse_url($server_data['REQUEST_URI'], PHP_URL_PATH);
     $params = parse_url($server_data['REQUEST_URI'], PHP_URL_QUERY);
     $strip = array('nkconnect_state', 'code', 'state', 'error', 'error_description');
     if ($params) {
         $p = array();
         foreach (OAuthUtil::parse_parameters($params) as $k => $v) {
             if (false === in_array($k, $strip)) {
                 $p[$k] = $v;
             }
         }
         if (0 != count($p)) {
             $url .= '?' . OAuthUtil::build_http_query($p);
         }
     }
     return $url;
 }
Example #14
0
 /**
  * One time exchange of username and password for access token and secret.
  *
  * @returns ["oauth_token" => "the-access-token",
  *			 "oauth_token_secret" => "the-access-secret",
  *			 "user_id" => "9436992",
  *			 "screen_name" => "abraham",
  *			 "x_auth_expires" => "0"]
  * @throws Exception if the OAuth request fails
  */
 function getXAuthToken($username, $password)
 {
     $parameters = array('x_auth_username' => $username, 'x_auth_password' => $password, 'x_auth_mode' => 'client_auth');
     $request = $this->oAuthRequest($this->getUrl('access_token'), 'POST', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     if (empty($token['oauth_token']) || empty($token['oauth_token_secret'])) {
         throw new \Exception('OAuth request failed.');
     }
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
 /**
  *
  *
  * @param Gdn_Controller $Sender
  * @param array $Args
  */
 public function base_connectData_handler($Sender, $Args)
 {
     if (val(0, $Args) != 'twitter') {
         return;
     }
     $Form = $Sender->Form;
     //new Gdn_Form();
     $RequestToken = val('oauth_token', $_GET);
     $AccessToken = $Form->getFormValue('AccessToken');
     if ($AccessToken) {
         $AccessToken = $this->getOAuthToken($AccessToken);
         $this->accessToken($AccessToken);
     }
     // Get the access token.
     if ($RequestToken && !$AccessToken) {
         // Get the request secret.
         $RequestToken = $this->getOAuthToken($RequestToken);
         $Consumer = new OAuthConsumer(c('Plugins.Twitter.ConsumerKey'), c('Plugins.Twitter.Secret'));
         $Url = 'https://api.twitter.com/oauth/access_token';
         $Params = array('oauth_verifier' => val('oauth_verifier', $_GET));
         $Request = OAuthRequest::from_consumer_and_token($Consumer, $RequestToken, 'POST', $Url, $Params);
         $SignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
         $Request->sign_request($SignatureMethod, $Consumer, $RequestToken);
         $Post = $Request->to_postdata();
         $Curl = $this->_Curl($Request);
         $Response = curl_exec($Curl);
         if ($Response === false) {
             $Response = curl_error($Curl);
         }
         $HttpCode = curl_getinfo($Curl, CURLINFO_HTTP_CODE);
         curl_close($Curl);
         if ($HttpCode == '200') {
             $Data = OAuthUtil::parse_parameters($Response);
             $AccessToken = new OAuthToken(val('oauth_token', $Data), val('oauth_token_secret', $Data));
             // Save the access token to the database.
             $this->setOAuthToken($AccessToken->key, $AccessToken->secret, 'access');
             $this->accessToken($AccessToken->key, $AccessToken->secret);
             // Delete the request token.
             $this->deleteOAuthToken($RequestToken);
         } else {
             // There was some sort of error.
             throw new Exception('There was an error authenticating with twitter.', 400);
         }
         $NewToken = true;
     }
     // Get the profile.
     try {
         $Profile = $this->getProfile($AccessToken);
     } catch (Exception $Ex) {
         if (!isset($NewToken)) {
             // There was an error getting the profile, which probably means the saved access token is no longer valid. Try and reauthorize.
             if ($Sender->deliveryType() == DELIVERY_TYPE_ALL) {
                 redirect($this->_AuthorizeHref());
             } else {
                 $Sender->setHeader('Content-type', 'application/json');
                 $Sender->deliveryMethod(DELIVERY_METHOD_JSON);
                 $Sender->RedirectUrl = $this->_authorizeHref();
             }
         } else {
             throw $Ex;
         }
     }
     $ID = val('id', $Profile);
     $Form->setFormValue('UniqueID', $ID);
     $Form->setFormValue('Provider', self::ProviderKey);
     $Form->setFormValue('ProviderName', 'Twitter');
     $Form->setValue('ConnectName', val('screen_name', $Profile));
     $Form->setFormValue('Name', val('screen_name', $Profile));
     $Form->setFormValue('FullName', val('name', $Profile));
     $Form->setFormValue('Photo', val('profile_image_url_https', $Profile));
     $Form->addHidden('AccessToken', $AccessToken->key);
     // Save some original data in the attributes of the connection for later API calls.
     $Attributes = array(self::ProviderKey => array('AccessToken' => array($AccessToken->key, $AccessToken->secret), 'Profile' => $Profile));
     $Form->setFormValue('Attributes', $Attributes);
     $Sender->setData('Verified', true);
 }
Example #16
0
 /**
  * Gets the OAuth access tokens
  *
  * @param string|bool $oauthVerifier oauth_verifier string
  */
 public function getAccessToken($oauthVerifier = FALSE)
 {
     $parameters = array();
     if ($oauthVerifier !== FALSE) {
         $parameters['oauth_verifier'] = $oauthVerifier;
     }
     $request = $this->_parseRequest($this->accessUrl, 'GET', $parameters);
     $token = \OAuthUtil::parse_parameters($request);
     $this->token = new \OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
 /**
  * Exchange request token and secret for an access token and
  * secret, to sign API calls.
  *
  * @since 1.0
  * @access public
  *
  * @param string
  * @return array containing token and secret as values ( 'oauth_token' => 'the-access-token', 'oauth_token_secret' => 'the-access-secret' )
  */
 function get_access_token($oauth_verifier = '')
 {
     $parameters = array();
     if (!empty($oauth_verifier)) {
         $parameters['oauth_verifier'] = $oauth_verifier;
     }
     $request = $this->oauth_request(self::access_token_url, 'GET', $parameters);
     $token = OAuthUtil::parse_parameters(wp_remote_retrieve_body($request));
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #18
0
 /**
  * 获得访问令牌
  *
  * @param string
  */
 public function getAccessToken($oAuthVerifier = false, $oAuthToken = false)
 {
     $parameters = array();
     if (!empty($oAuthVerifier)) {
         $parameters['oauth_verifier'] = $oAuthVerifier;
     }
     $this->token = new OAuthConsumer($oAuthToken['oauth_token'], $oAuthToken['oauth_token_secret']);
     $request = $this->oAuthRequest($this->getAccessTokenURL(), 'GET', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     if (!isset($token['oauth_token']) || !isset($token['oauth_token_secret'])) {
         return false;
     }
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #19
0
 /**
  * attempt to build up a request from what was passed to the server
  */
 public static function from_request($http_method = NULL, $http_url = NULL, $parameters = NULL)
 {
     $scheme = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ? 'http' : 'https';
     @$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
     @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
     // We weren't handed any parameters, so let's find the ones relevant to
     // this request.
     // If you run XML-RPC or similar you should use this to provide your own
     // parsed parameter-list
     if (!$parameters) {
         // Find request headers
         $request_headers = OAuthUtil::get_headers();
         // Parse the query-string to find GET parameters
         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
         // It's a POST request of the proper content-type, so parse POST
         // parameters and add those overriding any duplicates from GET
         if ($http_method == "POST" && @strstr($request_headers["Content-Type"], "application/x-www-form-urlencoded")) {
             $post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT));
             $parameters = array_merge($parameters, $post_data);
         }
         // We have a Authorization-header with OAuth data. Parse the header
         // and add those overriding any duplicates from GET or POST
         if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
             $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
             $parameters = array_merge($parameters, $header_parameters);
         }
     }
     // fix for friendica redirect system
     // FIXME or don't, but figure out if this is absolutely necessary and act accordingly
     $http_url = substr($http_url, 0, strpos($http_url, $parameters['q']) + strlen($parameters['q']));
     unset($parameters['q']);
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
 /**
  * Fetch an access token. Sets the token for this object with the fetched token.
  *
  * The object should be initialized with the consumer token and user access token.
  *
  * @param string $oauth_verifier
  *   The 'oauth_verifier' that was sent back in the callback URL after the authorization step.
  * @return array
  *   An associative array containing the token, secret and callback confirmed status.
  *   Array keys are 'oauth_callback_confirmed', 'oauth_token' and 'oauth_token_secret'.
  *
  * @throws CultureFeed_ParseException
  *   If the result could not be parsed.
  */
 public function getAccessToken($oauth_verifier)
 {
     $response = $this->oauth_client->authenticatedPost('accessToken', array('oauth_verifier' => $oauth_verifier));
     $token = OAuthUtil::parse_parameters($response);
     if (!isset($token['oauth_token']) || !isset($token['oauth_token'])) {
         throw new CultureFeed_ParseException($response, 'token');
     }
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #21
0
 /**
  * attempt to build up a request from what was passed to the server
  */
 public static function from_request($http_method = NULL, $http_url = NULL, $parameters = NULL)
 {
     $scheme = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ? 'http' : 'https';
     @$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
     @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
     if (!$parameters) {
         $request_headers = OAuthUtil::get_headers();
         // Parse the query-string to find GET parameters
         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
         // It's a POST request of the proper content-type, so parse POST
         // parameters and add those overriding any duplicates from GET
         if ($http_method == "POST" and @strstr($request_headers["Content-Type"], "application/x-www-form-urlencoded")) {
             $post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT));
             $parameters = array_merge($parameters, $post_data);
         }
         // We have a Authorization-header with OAuth data. Parse the header
         // and add those overriding any duplicates from GET or POST
         if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
             $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
             $parameters = array_merge($parameters, $header_parameters);
         }
     }
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
Example #22
0
 /** 
  * Exchange the request token and secret for an access token and secret, to sign API calls. 
  * 
  * @return array array('oauth_token' => the access token, 'oauth_token_secret' => the access secret) 
  */
 function accessToken($oauth_verifier = false, $oauth_token = false)
 {
     $parameters = array();
     // 1.0a
     if ($oauth_verifier) {
         $parameters['oauth_verifier'] = $oauth_verifier;
     }
     $request = $this->signedRequest($this->access_token_url, $this->access_token_method, $parameters);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
Example #23
0
 /**
  * One time exchange of username and password for access token and secret.
  *
  * @returns array("oauth_token" => "the-access-token",
  *                "oauth_token_secret" => "the-access-secret",
  *                "user_id" => "9436992",
  *                "screen_name" => "abraham",
  *                "x_auth_expires" => "0")
  */
 function getXAuthToken($username, $password)
 {
     $parameters = array();
     $parameters['x_auth_username'] = $username;
     $parameters['x_auth_password'] = $password;
     $parameters['x_auth_mode'] = 'client_auth';
     $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     if (empty($token['oauth_token']) || empty($token['oauth_token_secret'])) {
         throw new TwitterOAuthException("Count not get oauth token and secret from response. Response: " . $request);
     }
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }
 /** Fetches a access authorisation token after a request token has been granted authorisation by the user.
  *
  * This is the 3rd and final step of the OAuth authorisation process.
  *
  * @param object $request_token Instance of JK_RequestToken
  * @param int|string $user_id An _optional_ property that identifies the user on the local system, that $request_token was used for.
  * @return object JK_UserAuthorisation instance
  *
  * @see Jabbakam::requestAuthorisation()
  *
  * @throws InvalidArgumentException If $request_token is NULL or not an instance of JK_RequestToken.
  * @throws Exception If unable to contact the API server.
  * @throws JK_Exception If Jabbakam is unable or unwilling to grant an access token, e.g. expired request token.
  * */
 public static final function fromRequestToken(JK_RequestToken $request_token, $user_id = NULL)
 {
     if ($request_token == NULL) {
         throw new InvalidArgumentException('NULL $request_token passed to JK_UserAuthorisation::fromRequestToken()');
     }
     if (!is_a($request_token, 'JK_RequestToken')) {
         throw new InvalidArgumentException('non JK_RequestToken passed to JK_UserAuthorisation::fromRequestToken()');
     }
     $consumer = new OAuthConsumer(JABBAKAM_KEY, JABBAKAM_SECRET_KEY, NULL);
     $sig_method = new OAuthSignatureMethod_HMAC_SHA1();
     $req = OAuthRequest::from_consumer_and_token($consumer, $request_token, 'GET', Jabbakam::ACCESS_TOKEN_URL);
     $req->sign_request($sig_method, $consumer, $request_token);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_HEADER, 0);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_URL, $req->to_url());
     $answer = curl_exec($ch);
     if (!$answer) {
         $err = curl_error($ch);
         $err_no = curl_errno($ch);
         curl_close($ch);
         throw new Exception('Could not fetch acess token - ' . $err, $err_no);
     }
     if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
         curl_close($ch);
         throw new JK_Exception('No access token returned: ' . $answer);
     }
     curl_close($ch);
     echo $answer . "\n";
     $answer = OAuthUtil::parse_parameters($answer);
     return new JK_UserAuthorisation($answer['oauth_token'], $answer['oauth_token_secret'], $user_id);
 }
 /**
  *
  * @param Gdn_Controller $Sender
  * @param array $Args
  */
 public function Base_ConnectData_Handler($Sender, $Args)
 {
     if (GetValue(0, $Args) != 'sinaconnect') {
         return;
     }
     $RequestToken = GetValue('oauth_token', $_GET);
     // Get the access token.
     if ($RequestToken || !($AccessToken = $this->AccessToken())) {
         // Get the request secret.
         $RequestToken = $this->GetOAuthToken($RequestToken);
         $Consumer = new OAuthConsumer(C('Plugins.SinaConnect.ConsumerKey'), C('Plugins.SinaConnect.Secret'));
         $Url = 'http://api.t.sina.com.cn/oauth/access_token';
         $Params = array('oauth_verifier' => GetValue('oauth_verifier', $_GET));
         $Request = OAuthRequest::from_consumer_and_token($Consumer, $RequestToken, 'POST', $Url, $Params);
         $SignatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
         $Request->sign_request($SignatureMethod, $Consumer, $RequestToken);
         $Post = $Request->to_postdata();
         $Curl = $this->_Curl($Request);
         $Response = curl_exec($Curl);
         if ($Response === FALSE) {
             $Response = curl_error($Curl);
         }
         $HttpCode = curl_getinfo($Curl, CURLINFO_HTTP_CODE);
         curl_close($Curl);
         if ($HttpCode == '200') {
             $Data = OAuthUtil::parse_parameters($Response);
             $AccessToken = $this->AccessToken(GetValue('oauth_token', $Data), GetValue('oauth_token_secret', $Data));
             // Save the access token to the database.
             $this->SetOAuthToken($AccessToken);
             // Delete the request token.
             $this->DeleteOAuthToken($RequestToken);
         } else {
             // There was some sort of error.
         }
         $NewToken = TRUE;
     }
     // Get the profile.
     try {
         $Profile = $this->GetProfile($AccessToken);
     } catch (Exception $Ex) {
         if (!isset($NewToken)) {
             // There was an error getting the profile, which probably means the saved access token is no longer valid. Try and reauthorize.
             if ($Sender->DeliveryType() == DELIVERY_TYPE_ALL) {
                 Redirect($this->_AuthorizeHref());
             } else {
                 $Sender->SetHeader('Content-type', 'application/json');
                 $Sender->DeliveryMethod(DELIVERY_METHOD_JSON);
                 $Sender->RedirectUrl = $this->_AuthorizeHref();
             }
         } else {
             $Sender->Form->AddError($Ex);
         }
     }
     //print_r($Profile);
     $Form = $Sender->Form;
     //new Gdn_Form();
     $ID = GetValue('id', $Profile);
     $Form->SetFormValue('UniqueID', $ID);
     $Form->SetFormValue('Provider', self::$ProviderKey);
     $Form->SetFormValue('ProviderName', 'Sina');
     $Form->SetFormValue('Name', GetValue('screen_name', $Profile));
     $Form->SetFormValue('FullName', GetValue('name', $Profile));
     $Form->SetFormValue('Email', GetValue('id', $Profile) . '@weibo.com');
     $Form->SetFormValue('Photo', GetValue('profile_image_url', $Profile));
     $Sender->SetData('Verified', TRUE);
 }
Example #26
0
 /**
  * Avoid the notices if the token is not set
  * @param  $request
  * @return array
  */
 function getToken($request)
 {
     $token = OAuthUtil::parse_parameters($request);
     if (isset($token['oauth_token'], $token['oauth_token_secret'])) {
         $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     }
     return $token;
 }
 function oauth_connect($count = 0)
 {
     global $aiosp_activation;
     if (!class_exists('OAuthConsumer')) {
         require_once 'OAuth.php';
     }
     $url = '';
     $callback_url = NULL;
     $consumer_key = "anonymous";
     $consumer_secret = "anonymous";
     $oauth_request_token = "https://www.google.com/accounts/OAuthGetRequestToken";
     $oauth_authorize = "https://www.google.com/accounts/OAuthAuthorizeToken";
     $oauth_access_token = "https://www.google.com/accounts/OAuthGetAccessToken";
     if ($aiosp_activation) {
         $oauth_current = false;
     } else {
         $oauth_current = get_transient("aioseop_oauth_current");
     }
     if (!empty($this->token) && $this->token != 'anonymous' && $oauth_current) {
         return $oauth_authorize . '?oauth_token=' . $this->token;
     } else {
         set_transient("aioseop_oauth_current", 1, 3600);
         unset($this->token);
         unset($this->secret);
     }
     $args = array('scope' => 'https://www.google.com/analytics/feeds/', 'xoauth_displayname' => AIOSEOP_PLUGIN_NAME . ' ' . __('Google Analytics', 'all_in_one_seo_pack'));
     $req_req = $this->oauth_get_creds($oauth_request_token, NULL, $args, admin_url("admin.php?page=all-in-one-seo-pack/aioseop_class.php"));
     $reqData = wp_remote_get($req_req->to_url());
     $reqOAuthData = OAuthUtil::parse_parameters(wp_remote_retrieve_body($reqData));
     if (!empty($reqOAuthData['oauth_token'])) {
         $this->token = $reqOAuthData['oauth_token'];
     }
     if (!empty($reqOAuthData['oauth_token_secret'])) {
         $this->secret = $reqOAuthData['oauth_token_secret'];
     }
     if (!empty($this->token) && $this->token != 'anonymous' && $oauth_current) {
         $url = $oauth_authorize . "?oauth_token={$this->token}";
     } else {
         if (!$count) {
             return $this->oauth_connect(1);
         }
     }
     return $url;
 }
Example #28
0
 public static function from_request($http_method = null, $http_url = null, $parameters = null)
 {
     $scheme = !isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on" ? 'http' : 'https';
     @$http_url or $http_url = $scheme . '://' . $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
     @$http_method or $http_method = $_SERVER['REQUEST_METHOD'];
     if (!$parameters) {
         $request_headers = OAuthUtil::get_headers();
         $parameters = OAuthUtil::parse_parameters($_SERVER['QUERY_STRING']);
         if ($http_method == "POST" && @strstr($request_headers["Content-Type"], "application/x-www-form-urlencoded")) {
             $post_data = OAuthUtil::parse_parameters(file_get_contents(self::$POST_INPUT));
             $parameters = array_merge($parameters, $post_data);
         }
         if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
             $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
             $parameters = array_merge($parameters, $header_parameters);
         }
     }
     return new OAuthRequest($http_method, $http_url, $parameters);
 }
 /**
  * One time exchange of username and password for access token and secret.
  * 
  * @param unknown_type $username
  * @param unknown_type $password
  * @return array("oauth_token" => "the-access-token",
  *								"oauth_token_secret" => "the-access-secret",
  *								"user_id" => "9436992",
  *								"screen_name" => "abraham",
  *								"x_auth_expires" => "0")
  */
 function getXAuthToken($username, $password)
 {
     $parameters = array();
     $parameters['x_auth_username'] = $username;
     $parameters['x_auth_password'] = $password;
     $parameters['x_auth_mode'] = 'client_auth';
     $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
     twitterlogin_debug("getXAuthToken, oAuthRequest ", $request);
     $token = OAuthUtil::parse_parameters($request);
     twitterlogin_debug("getXAuthToken, OAuthUtil::parse_parameters ", $request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     twitterlogin_debug("getXAuthToken, OAuthConsumer ", $this->token);
     return $token;
 }
Example #30
0
 /**
  * One time exchange of username and password for access token and secret.
  *
  * @returns array("oauth_token" => "the-access-token",
  *								"oauth_token_secret" => "the-access-secret",
  *								"user_id" => "9436992",
  *								"screen_name" => "abraham",
  *								"x_auth_expires" => "0")
  */
 public function getXAuthToken($username, $password)
 {
     $parameters = [];
     $parameters['x_auth_username'] = $username;
     $parameters['x_auth_password'] = $password;
     $parameters['x_auth_mode'] = 'client_auth';
     $request = $this->oAuthRequest($this->accessTokenURL(), 'POST', $parameters);
     $token = OAuthUtil::parse_parameters($request);
     $this->token = new OAuthConsumer($token['oauth_token'], $token['oauth_token_secret']);
     return $token;
 }