You are here

public function OAuthStorePostgreSQL::addServerToken in Lingotek Translation 7.3

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

Add a request token we obtained from a server.

@todo remove old tokens for this user and this ocr_id

_secret

@exception OAuthException2 when server is not known @exception OAuthException2 when we received a duplicate token

Parameters

string consumer_key key of the server in the consumer registry:

string token_type one of 'request' or 'access':

string token:

int user_id the user owning the token:

array options extra options, name and token_ttl:

Overrides OAuthStoreAbstract::addServerToken

File

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

Class

OAuthStorePostgreSQL

Code

public function addServerToken($consumer_key, $token_type, $token, $token_secret, $user_id, $options = array()) {
  if ($token_type != 'request' && $token_type != 'access') {
    throw new OAuthException2('Unknown token type "' . $token_type . '", must be either "request" or "access"');
  }

  // Maximum time to live for this token
  if (isset($options['token_ttl']) && is_numeric($options['token_ttl'])) {
    $ttl = 'NOW() + INTERVAL \'' . intval($options['token_ttl']) . ' SECOND\'';
  }
  else {
    if ($token_type == 'request') {
      $ttl = 'NOW() + INTERVAL \'' . $this->max_request_token_ttl . ' SECOND\'';
    }
    else {
      $ttl = "'9999-12-31'";
    }
  }
  if (isset($options['server_uri'])) {
    $ocr_id = $this
      ->query_one('
                        SELECT ocr_id
                        FROM oauth_consumer_registry
                        WHERE ocr_consumer_key = \'%s\'
                        AND ocr_usa_id_ref = \'%d\'
                        AND ocr_server_uri = \'%s\'
                        ', $consumer_key, $user_id, $options['server_uri']);
  }
  else {
    $ocr_id = $this
      ->query_one('
                        SELECT ocr_id
                        FROM oauth_consumer_registry
                        WHERE ocr_consumer_key = \'%s\'
                        AND ocr_usa_id_ref = \'%d\'
                        ', $consumer_key, $user_id);
  }
  if (empty($ocr_id)) {
    throw new OAuthException2('No server associated with consumer_key "' . $consumer_key . '"');
  }

  // Named tokens, unique per user/consumer key
  if (isset($options['name']) && $options['name'] != '') {
    $name = $options['name'];
  }
  else {
    $name = '';
  }

  // Delete any old tokens with the same type and name for this user/server combination
  $this
    ->query('
                    DELETE FROM oauth_consumer_token
                    WHERE oct_ocr_id_ref = %d
                      AND oct_usa_id_ref = \'%d\'
                      AND oct_token_type::text = LOWER(\'%s\')::text
                      AND oct_name       = \'%s\'
                    ', $ocr_id, $user_id, $token_type, $name);

  // Insert the new token
  $this
    ->query('
            INSERT INTO
               oauth_consumer_token(
                   oct_ocr_id_ref,
                   oct_usa_id_ref,
                   oct_name,
                   oct_token,
                   oct_token_secret,
                   oct_token_type,
                   oct_timestamp,
                   oct_token_ttl
               )
               VALUES (%d,%d,\'%s\',\'%s\',\'%s\',\'%s\',NOW(),' . $ttl . ')', $ocr_id, $user_id, $name, $token, $token_secret, $token_type);
  if (!$this
    ->query_affected_rows()) {
    throw new OAuthException2('Received duplicate token "' . $token . '" for the same consumer_key "' . $consumer_key . '"');
  }
}