You are here

public function OAuthStoreSQL::exchangeConsumerRequestForAccessToken in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.2 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::exchangeConsumerRequestForAccessToken()
  2. 7.3 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::exchangeConsumerRequestForAccessToken()
  3. 7.4 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::exchangeConsumerRequestForAccessToken()
  4. 7.5 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::exchangeConsumerRequestForAccessToken()
  5. 7.6 lib/oauth-php/library/store/OAuthStoreSQL.php \OAuthStoreSQL::exchangeConsumerRequestForAccessToken()

* Exchange an authorized request token for new access token. * *

Parameters

string token: * @param array options options for the token, token_ttl * @exception OAuthException2 when token could not be exchanged * @return array (token, token_secret)

Overrides OAuthStoreAbstract::exchangeConsumerRequestForAccessToken

File

lib/oauth-php/library/store/OAuthStoreSQL.php, line 1340

Class

OAuthStoreSQL

Code

public function exchangeConsumerRequestForAccessToken($token, $options = array()) {
  $new_token = $this
    ->generateKey(true);
  $new_secret = $this
    ->generateKey();

  // Maximum time to live for this token
  if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
    $ttl_sql = 'DATE_ADD(NOW(), INTERVAL ' . intval($options['token_ttl']) . ' SECOND)';
  }
  else {
    $ttl_sql = "'9999-12-31'";
  }
  if (isset($options['verifier'])) {
    $verifier = $options['verifier'];

    // 1.0a Compatibility : check token against oauth_verifier
    $this
      ->query('
		 				UPDATE oauth_server_token
		 				SET ost_token			= \'%s\',
		 					ost_token_secret	= \'%s\',
		 					ost_token_type		= \'access\',
		 					ost_timestamp		= NOW(),
		 					ost_token_ttl       = ' . $ttl_sql . '
		 				WHERE ost_token      = \'%s\'
		 				  AND ost_token_type = \'request\'
		 				  AND ost_authorized = 1
		 				  AND ost_token_ttl  >= NOW()
		 				  AND ost_verifier = \'%s\'
		 				', $new_token, $new_secret, $token, $verifier);
  }
  else {

    // 1.0
    $this
      ->query('
		 				UPDATE oauth_server_token
		 				SET ost_token			= \'%s\',
		 					ost_token_secret	= \'%s\',
		 					ost_token_type		= \'access\',
		 					ost_timestamp		= NOW(),
		 					ost_token_ttl       = ' . $ttl_sql . '
		 				WHERE ost_token      = \'%s\'
		 				  AND ost_token_type = \'request\'
		 				  AND ost_authorized = 1
		 				  AND ost_token_ttl  >= NOW()
		 				', $new_token, $new_secret, $token);
  }
  if ($this
    ->query_affected_rows() != 1) {
    throw new OAuthException2('Can\'t exchange request token "' . $token . '" for access token. No such token or not authorized');
  }
  $ret = array(
    'token' => $new_token,
    'token_secret' => $new_secret,
  );
  $ttl = $this
    ->query_one('
					SELECT	IF(ost_token_ttl >= \'9999-12-31\', NULL, UNIX_TIMESTAMP(ost_token_ttl) - UNIX_TIMESTAMP(NOW())) as token_ttl
					FROM oauth_server_token
					WHERE ost_token = \'%s\'', $new_token);
  if (is_numeric($ttl)) {
    $ret['token_ttl'] = intval($ttl);
  }
  return $ret;
}