View source
<?php
function oauth_common_user_consumers($uid) {
$result = db_query('SELECT c.secret, c.configuration, pc.*
FROM {oauth_common_consumer} c
INNER JOIN {oauth_common_provider_consumer} pc ON pc.csid = c.csid
WHERE pc.uid = %d', array(
':uid' => $uid,
));
$consumers = array();
while ($consumer = DrupalOAuthConsumer::fromResult($result)) {
$consumers[] = $consumer;
}
return $consumers;
}
function oauth_common_user_access_tokens($uid) {
$result = db_query("SELECT * FROM {oauth_common_token} WHERE uid = %d AND type = '%d'", array(
':uid' => $uid,
':type' => OAUTH_COMMON_TOKEN_TYPE_ACCESS,
));
$tokens = array();
while ($token = DrupalOAuthToken::fromResult($result)) {
$tokens[] = $token;
}
return $tokens;
}
function oauth_common_verify_request() {
$req = DrupalOAuthRequest::from_request();
$consumer_key = $req
->get_parameter('oauth_consumer_key');
if (!empty($consumer_key)) {
$consumer = DrupalOAuthConsumer::loadProviderByKey($consumer_key);
if ($consumer) {
$context = oauth_common_context_load($consumer->context);
if (!$context) {
throw new Exception('No OAuth context found');
}
_oauth_common_verify_body_hash($req);
$signature = $req
->get_parameter('oauth_signature');
if (!empty($signature)) {
$server = new DrupalOAuthServer($context);
return array_merge(array(
TRUE,
), $server
->verify_request($req));
}
else {
$token_key = $req
->get_parameter('oauth_token');
if (empty($token_key) || !($token = DrupalOAuthToken::loadbyKey($token_key, $consumer))) {
$token = NULL;
}
return array(
FALSE,
$consumer,
$token,
);
}
}
}
return array(
FALSE,
NULL,
NULL,
);
}
function oauth_common_ahah_secret($object) {
$cached_form_state = array();
$form = form_get_cache($_POST['form_build_id'], $cached_form_state);
form_set_cache($_POST['form_build_id'], $form, $cached_form_state);
$form_state = array(
'submitted' => FALSE,
);
$secret = array(
'#prefix' => '',
'#value' => $object->secret,
) + $form['secret'];
$secret = form_builder('oauth_common_ahah_secret', $secret, $form_state);
$output = drupal_render($secret);
print drupal_to_js(array(
'status' => TRUE,
'data' => $output,
));
exit;
}
function _oauth_common_verify_body_hash($req) {
$body_hash = $req
->get_parameter('oauth_body_hash');
if ($body_hash && module_exists('inputstream')) {
$hres = hash_init('sha1');
$stream = fopen('drupal://input', 'r');
hash_update_stream($hres, $stream);
fclose($stream);
$sha1 = hash_final($hres, TRUE);
if ($sha1 !== base64_decode($body_hash)) {
throw new OAuthException("Invalid body hash");
}
}
}