You are here

public function OAuthStorePostgreSQL::exchangeConsumerRequestForAccessToken in Lingotek Translation 7.5

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

Exchange an authorized request token for new access token.

@exception OAuthException2 when token could not be exchanged

Parameters

string token:

array options options for the token, token_ttl:

Return value

array (token, token_secret)

Overrides OAuthStoreAbstract::exchangeConsumerRequestForAccessToken

File

lib/oauth-php/library/store/OAuthStorePostgreSQL.php, line 1345

Class

OAuthStorePostgreSQL

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 = '(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    (CASE WHEN ost_token_ttl >= \'9999-12-31\' THEN NULL ELSE ost_token_ttl - NOW() END) as token_ttl
                    FROM oauth_server_token
                    WHERE ost_token = \'%s\'', $new_token);
  if (is_numeric($ttl)) {
    $ret['token_ttl'] = intval($ttl);
  }
  return $ret;
}