View source
<?php
class DrupalOAuthDataStore extends OAuthDataStore {
private $context;
public function __construct($context) {
$this->context = $context;
}
public function lookup_consumer($consumer_key, $provider_consumer = TRUE) {
$consumer = DrupalOAuthConsumer::loadProviderByKey($consumer_key);
if (!$consumer) {
throw new OAuthException('Consumer not found');
}
return $consumer;
}
public function lookup_token($consumer, $token_type, $token) {
$type = $token_type == 'request' ? OAUTH_COMMON_TOKEN_TYPE_REQUEST : OAUTH_COMMON_TOKEN_TYPE_ACCESS;
$token = DrupalOAuthToken::loadByKey($token, $consumer, $type);
if (!$token) {
throw new OAuthException('Token not found');
}
return $token;
}
public function lookup_nonce($consumer, $token, $nonce, $timestamp) {
if (strlen($nonce) > 255) {
throw new OAuthException('Nonces may not be longer than 255 characters');
}
$stored_nonce = db_query("SELECT nonce FROM {oauth_common_nonce}\n WHERE nonce = :nonce AND timestamp <= :timestamp and token_key = :token_key", array(
':nonce' => $nonce,
':timestamp' => $timestamp,
':token_key' => $token ? $token->key : '',
));
if (!$stored_nonce
->rowCount()) {
$values = array(
'nonce' => $nonce,
'timestamp' => $timestamp,
'token_key' => $token ? $token->key : '',
);
drupal_write_record('oauth_common_nonce', $values);
return NULL;
}
return $stored_nonce;
}
function new_request_token($consumer, $callback = NULL) {
$token = new DrupalOAuthToken(user_password(32), user_password(32), $consumer, array(
'type' => OAUTH_COMMON_TOKEN_TYPE_REQUEST,
'uid' => 0,
'expires' => REQUEST_TIME + variable_get('oauth_common_request_token_lifetime', 7200),
'callback_url' => $callback,
));
$token
->write();
return $token;
}
function new_access_token($token_old, $consumer, $verifier = NULL) {
module_load_include('inc', 'oauth_common');
if ($token_old && $token_old->authorized) {
$token_new = new DrupalOAuthToken(user_password(32), user_password(32), $consumer, array(
'type' => OAUTH_COMMON_TOKEN_TYPE_ACCESS,
'uid' => $token_old->uid,
'services' => isset($token_old->services) ? $token_old->services : NULL,
'authorized' => 1,
));
$token_old
->delete();
$token_new
->write();
return $token_new;
}
throw new OAuthException('Invalid request token');
}
}