View source
<?php
require_once "OAuth.php";
require_once "common.inc.php";
function oauth_menu() {
$items = array();
$items['admin/build/oauth'] = array(
'title' => t('Test OAuth'),
'description' => t('Test OAuth calls to server'),
'page callback' => 'oauth_test_calls',
'access arguments' => array(
'consume provided services',
),
'weight' => 10,
);
$items['oauth/request'] = array(
'title' => t('Request token'),
'page callback' => 'oauth_request_token',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['oauth/auth'] = array(
'title' => t('Authorize remote service'),
'page callback' => 'oauth_auth_token',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['oauth/access'] = array(
'title' => t('Request'),
'page callback' => 'oauth_access_token',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function oauth_perm() {
return array(
"authorize external services",
"consume provided services",
);
}
function oauth_user($op, &$edit, $account, $category) {
global $user;
switch ($op) {
case 'view':
if ($account->uid == $user->uid && user_access("consume provided services") || $user->uid == 1) {
$consumer = oauth_get_consumer($account->uid);
$account->content['consumer_key'] = array();
$account->content['consumer_key'] += array(
'#type' => 'user_profile_category',
'#attributes' => array(
'class' => 'user-member',
),
'#weight' => 5,
'#title' => t('External services'),
);
$account->content['consumer_key']['request_url'] = array(
'#type' => 'user_profile_item',
'#title' => t('Request token URL'),
'#value' => url('oauth/request', array(
'absolute' => TRUE,
)),
'#weight' => 1,
);
$account->content['consumer_key']['auth_url'] = array(
'#type' => 'user_profile_item',
'#title' => t('User authentication URL'),
'#value' => url('oauth/auth', array(
'absolute' => TRUE,
)),
'#weight' => 2,
);
$account->content['consumer_key']['access_url'] = array(
'#type' => 'user_profile_item',
'#title' => t('Access token URL'),
'#value' => url('oauth/access', array(
'absolute' => TRUE,
)),
'#weight' => 3,
);
$account->content['consumer_key']['key'] = array(
'#type' => 'user_profile_item',
'#title' => t('Consumer key'),
'#value' => $consumer->key,
'#weight' => 4,
);
$account->content['consumer_key']['secret'] = array(
'#type' => 'user_profile_item',
'#title' => t('Consumer secret'),
'#value' => $consumer->secret,
'#weight' => 5,
);
}
break;
}
}
function _oauth_init_server() {
static $server = null;
require_once "OAuth.php";
require_once "OAuth_TestServer.php";
$server = new OAuthServer(new DrupalOAuthDataStore());
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$plaintext_method = new OAuthSignatureMethod_PLAINTEXT();
$rsa_method = new TestOAuthSignatureMethod_RSA_SHA1();
$server
->add_signature_method($hmac_method);
$server
->add_signature_method($plaintext_method);
$server
->add_signature_method($rsa_method);
return $server;
}
function oauth_request_token() {
$server = _oauth_init_server();
$q = $_GET['q'];
unset($_GET['q']);
try {
$req = OAuthRequest::from_request();
$token = $server
->fetch_request_token($req);
print $token;
} catch (OAuthException $e) {
print $e
->getMessage() . "\n<hr />\n";
print_r($req);
die;
}
$_GET['q'] = $q;
}
function oauth_auth_token() {
global $user;
if ($user->uid != 0) {
if (user_access("authorize external services")) {
return drupal_get_form("oauth_grant_access");
}
else {
drupal_set_message("error", t("You are not authorized to allow external services access to this system."));
drupal_access_denied();
}
}
else {
return drupal_get_form("user_login");
}
}
function oauth_access_token() {
$server = _oauth_init_server();
$q = $_GET['q'];
unset($_GET['q']);
try {
global $user;
$req = OAuthRequest::from_request();
$token = $server
->fetch_access_token($req);
parse_str($token);
print $token;
} catch (OAuthException $e) {
print $e
->getMessage() . "\n<hr />\n";
print_r($req);
die;
}
$_GET['q'] = $q;
}
function oauth_grant_access() {
module_invoke('services');
$services = services_get_all();
$form['oauth_callback'] = array(
'#type' => 'hidden',
'#value' => $_GET['oauth_callback'],
);
$form['oauth_token'] = array(
'#type' => 'hidden',
'#value' => $_GET['oauth_token'],
);
$form['oauth_consumer_key'] = array(
'#type' => 'hidden',
'#value' => $_GET['oauth_consumer_key'],
);
$form['oauth_nonce'] = array(
'#type' => 'hidden',
'#value' => $_GET['oauth_nonce'],
);
$form['oauth_nonce_timestamp'] = array(
'#type' => 'hidden',
'#value' => $_GET['oauth_timestamp'],
);
$form['services'] = array(
'#title' => t('Select services'),
'#type' => 'fieldset',
);
foreach ($services as $service) {
$method_name = $service['#method'];
$form['services'][$method_name] = array(
'#title' => $service['#method'],
'#type' => 'radios',
'#options' => array(
0 => t('access'),
1 => t('block'),
),
'#default_value' => 1,
);
}
$form['confirm'] = array(
'#type' => 'submit',
'#value' => t('Grant access'),
'#weight' => 10,
);
$form['#tree'] = TRUE;
return $form;
}
function oauth_grant_access_submit($form, &$form_state) {
global $user;
module_invoke('services');
$services = services_get_all();
foreach ($services as $service) {
$method_name = $service['#method'];
if ($form_state['values']['services'][$method_name] == 0) {
$services_oauth[$method_name]['name'] = $method_name;
$services_oauth[$method_name]['permission'] = 1;
}
elseif ($form_state['values']['services'][$method_name] == 1) {
$services_oauth[$method_name]['name'] = $method_name;
$services_oauth[$method_name]['permission'] = 0;
}
}
$services_oauth_serialized = serialize($services_oauth);
db_query("INSERT INTO {oauth_services} (consumer_key, services, timestamp, session_id ) VALUES ('%s', '%s', %d, '%s')", $form_state['values']['oauth_consumer_key'], $services_oauth_serialized, $form_state['values']['oauth_nonce_timestamp'], $user->sid);
$server = _oauth_init_server();
$q = $_GET['q'];
unset($_GET['q']);
try {
$req = OAuthRequest::from_request();
oauth_authorize_request_token($form_state['values']['oauth_token']);
drupal_goto($req
->get_parameter('oauth_callback'));
} catch (OAuthException $e) {
print $e
->getMessage() . "\n<hr />\n";
print_r($req);
die;
}
$_GET['q'] = $q;
$form_state['redirect'] = $form_state['values']['oauth_callback'];
}
function oauth_authorize_request_token($key) {
global $user;
db_query("UPDATE {oauth_token} SET authorized = 1, uid=%d WHERE token_key = '%s'", $user->uid, $key);
}
function oauth_authorize_access($key, $secret) {
global $user;
$result = db_query("SELECT * FROM {oauth_token} WHERE type='access' AND token_key = '%s' AND token_secret='%s'", $key, $secret);
if ($object = db_fetch_object($result)) {
$user = user_load($object->uid);
return $user->uid;
}
return null;
}
function oauth_services() {
}
function oauth_cron() {
db_query("DELETE FROM {oauth_nonce} WHERE nonce_timestamp > %d", strtotime("-1 day"));
}
function oauth_generate_consumer($uid) {
$key = user_password(32);
$secret = user_password(32);
oauth_save_consumer($uid, $key, $secret);
return new OAuthConsumer($key, $secret);
}
function oauth_save_consumer($uid, $key, $secret) {
db_query("INSERT INTO {oauth_consumer} VALUES (%d, '%s', '%s')", $uid, $key, $secret);
}
function oauth_get_consumer($uid) {
$result = db_query("SELECT * FROM {oauth_consumer} WHERE uid=%d", $uid);
if ($object = db_fetch_object($result)) {
return new OAuthConsumer($object->consumer_key, $object->consumer_secret);
}
else {
return oauth_generate_consumer($uid);
}
}
class DrupalOAuthDataStore extends OAuthDataStore {
function lookup_consumer($consumer_key) {
$result = db_query("SELECT * FROM {oauth_consumer} WHERE consumer_key='%s'", $consumer_key);
if ($object = db_fetch_object($result)) {
return new OAuthConsumer($object->consumer_key, $object->consumer_secret);
}
return null;
}
function lookup_token($consumer, $token_type, $token) {
$result = db_query("SELECT * FROM {oauth_token} WHERE type='%s' AND consumer_key='%s' AND token_key = '%s'", $token_type, $consumer->key, $token);
if ($object = db_fetch_object($result)) {
return new OAuthToken($object->token_key, $object->token_secret);
}
return null;
}
function lookup_nonce($consumer, $token, $nonce, $timestamp) {
$nonce_1 = db_result(db_query("SELECT nonce FROM {oauth_nonce} WHERE nonce_timestamp='%s'", $timestamp));
if (!$nonce_1) {
db_query("INSERT INTO {oauth_nonce} (nonce, nonce_timestamp) VALUES ('%s', %d)", $nonce, $timestamp);
return null;
}
return $nonce_1;
}
function new_request_token($consumer) {
$user_id = db_result(db_query("SELECT uid FROM {oauth_consumer} WHERE consumer_key='%s'", $consumer->key));
$token = new OAuthToken(user_password(32), user_password(32));
db_query("INSERT INTO {oauth_token} (consumer_key, type, token_key, token_secret, uid) VALUES ('%s', '%s', '%s', '%s', %d)", $consumer->key, 'request', $token->key, $token->secret, $user_id);
return $token;
}
function new_access_token($request_token, $consumer, $user = NULL) {
if ($object = db_fetch_object(db_query("SELECT * FROM {oauth_token} WHERE type='request' and token_key = '%s'", $request_token->key))) {
if ($object->authorized) {
$token = new OAuthToken(user_password(32), user_password(32));
$user_id = db_result(db_query("SELECT uid FROM {oauth_consumer} WHERE consumer_key = '%s'", $consumer->key));
$session_id = db_result(db_query("SELECT sid FROM {sessions} WHERE uid=%d", $user_id));
db_query("INSERT INTO {oauth_token} (consumer_key, type, token_key, token_secret, uid) VALUES ('%s', '%s', '%s', '%s', %d)", $consumer->key, 'access', $token->key, $token->secret, $user_id);
db_query("DELETE FROM {oauth_token} WHERE type='request' AND token_key='%s'", $request_token->key);
db_query("UPDATE {oauth_services} SET token_key = '%s' WHERE session_id= '%s'", $token->key, $session_id);
return $token;
}
}
return null;
}
}
function oauth_test_calls() {
return drupal_get_form("oauth_request_call");
}
function oauth_request_call($form_state) {
if (!$_SESSION['oauth']['operation']) {
$_SESSION['oauth']['operation'] = 'request';
}
$form['indicator'] = array(
'#type' => 'fieldset',
'#title' => t('Step @number', array(
'@number' => $_SESSION['oauth']['operation'],
)),
);
if ($_SESSION['oauth']['operation'] == 'request') {
$form['indicator']['consumer'] = array(
'#type' => 'fieldset',
'#title' => t('consumer details'),
);
$form['indicator']['consumer']['consumer_key'] = array(
'#title' => t('consumer key'),
'#description' => t('consumer key of user on test server'),
'#type' => 'textfield',
);
$form['indicator']['consumer']['consumer_secret'] = array(
'#title' => t('consumer secret'),
'#description' => t('consumer secret of user on test server'),
'#type' => 'textfield',
);
$form['indicator']['endpoints'] = array(
'#title' => t('end points of testing server'),
'#type' => 'fieldset',
);
$form['indicator']['endpoints']['request_url'] = array(
'#title' => t('Request URL'),
'#type' => 'textfield',
'#weight' => 5,
'#default_value' => 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?q=oauth/request',
);
$form['indicator']['signature_method'] = array(
'#title' => t('Select Signature Method'),
'#type' => 'fieldset',
);
$form['indicator']['signature_method']['indicator']['sig_method'] = array(
'#title' => t('Please select signature method to use'),
'#type' => 'radios',
'#options' => array(
0 => t('HMAC-SHA1'),
1 => t('PLAINTEXT'),
2 => t('RSA-SHA1'),
),
'#weight' => 9,
);
$form['indicator']['request_call'] = array(
'#value' => t('Request Token Call'),
'#title' => t('Make call to server'),
'#type' => 'submit',
'#weight' => 10,
);
}
if ($_SESSION['oauth']['operation'] == 'auth') {
$form['indicator']['endpoints'] = array(
'#title' => t('end points of testing server'),
'#type' => 'fieldset',
);
$form['indicator']['endpoints']['auth_url'] = array(
'#title' => t('Authentication URL'),
'#type' => 'textfield',
'#weight' => 6,
'#default_value' => 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?q=oauth/auth',
);
$form['indicator']['token'] = array(
'#type' => 'fieldset',
'#title' => t('Token and Token secret'),
'#description' => t('Paste Token and Token Secret from above which are obtained after request call'),
'#weight' => 11,
);
$form['indicator']['token']['oauth_token'] = array(
'#title' => t('OAuth token'),
'#description' => t('Please paste token from above which you obtain from request call'),
'#type' => 'textfield',
'#weight' => 12,
'#default_value' => $form_state['data']['oauth_token'],
);
$form['indicator']['token']['oauth_token_secret'] = array(
'#title' => t('OAuth token secret'),
'#description' => t('Please paste token secret from above which you obtain from request call'),
'#type' => 'textfield',
'#weight' => 13,
'#default_value' => $form_state['data']['oauth_token_secret'],
);
$form['indicator']['auth_call'] = array(
'#value' => t('OAuth Authentication Call'),
'#type' => 'submit',
'#weight' => 14,
);
}
if ($_SESSION['oauth']['operation'] == 'access') {
$form['indicator']['endpoints']['access_url'] = array(
'#title' => t('Access URL'),
'#type' => 'textfield',
'#weight' => 7,
'#default_value' => 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?q=oauth/access',
);
$form['indicator']['access_call'] = array(
'#value' => t('OAuth Access Token Call'),
'#type' => 'submit',
'#weight' => 15,
);
}
switch ($_SESSION['oauth']['operation']) {
case auth:
$form['consumer_key'] = array(
'#type' => 'hidden',
'#value' => $form_state['values']['consumer_key'],
);
$form['consumer_secret'] = array(
'#type' => 'hidden',
'#value' => $form_state['values']['consumer_secret'],
);
$form['request_url'] = array(
'#type' => 'hidden',
'#value' => $form_values['request_url'],
);
break;
}
$form['back'] = array(
'#value' => t('Back To Request Token Page'),
'#type' => 'submit',
'#weight' => 17,
);
return $form;
}
function oauth_request_call_validate($form, &$form_state) {
if ($form_state['values']['op'] == 'Request Token Call') {
if (!$form_state['values']['consumer_key'] || !$form_state['values']['consumer_secret'] || !$form_state['values']['request_url'] || !$form_state['values']['sig_method']) {
form_set_error('form', t('All Fields are Required to produce Request Tokens'));
}
}
if ($form_state['values']['op'] == 'OAuth Authentication Call') {
if (!$form_state['values']['oauth_token'] || !$form_state['values']['oauth_token_secret'] || !$form_state['values']['auth_url']) {
form_set_error('form', t('All Fields are Required to make Authentication Requests to Server'));
}
}
if ($form_state['values']['op'] == 'OAuth Access Token Call') {
if (!$form_state['values']['access_url']) {
form_set_error('form', t('Please fill Access endpoint URL (eg. http://yourdrupalsite.com/?q=oauth/access)'));
}
}
if ($form_state['values']['op'] == 'Back To Request Token Page') {
$_SESSION['oauth']['operation'] = '';
$_SESSION['oauth']['key'] = '';
$_SESSION['oauth']['key_secret'] = '';
$_SESSION['oauth']['oauth_token'] = '';
$_SESSION['oauth']['oauth_token_secret'] = '';
$_SESSION['oauth']['oauth_access_token'] = '';
$_SESSION['oauth']['oauth_access_token_secret'] = '';
$_SESSION['oauth']['oauth_signature_method'] = '';
return;
}
}
function oauth_request_call_submit($form, &$form_state) {
$key = $form_state['values']['consumer_key'];
$secret = $form_state['values']['consumer_secret'];
$request_url = $form_state['values']['request_url'];
global $user;
$plaintext_method = new OAuthSignatureMethod_PLAINTEXT();
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$rsa_method = new TestOAuthSignatureMethod_RSA_SHA1();
if (!$_SESSION['oauth']['oauth_signature_method']) {
if ($form_state['values']['sig_method'] == 0) {
$user_sig_method = 'HMAC-SHA1';
$_SESSION['oauth']['oauth_signature_method'] = 'HMAC-SHA1';
}
elseif ($form_state['values']['sig_method'] == 1) {
$user_sig_method = 'PLAINTEXT';
$_SESSION['oauth']['oauth_signature_method'] = 'PLAINTEXT';
}
elseif ($form_state['values']['sig_method'] == 2) {
$user_sig_method = 'RSA-SHA1';
$_SESSION['oauth']['oauth_signature_method'] = 'RSA-SHA1';
}
}
if ($_SESSION['oauth']['oauth_signature_method'] == 'HMAC-SHA1') {
$sig_method = $hmac_method;
}
else {
if ($_SESSION['oauth']['oauth_signature_method'] == 'PLAINTEXT') {
$sig_method = $plaintext_method;
}
else {
if ($_SESSION['oauth']['oauth_signature_method'] == 'RSA-SHA1') {
$sig_method = $rsa_method;
}
}
}
$test_token = NULL;
$token = $form_state['values']['oauth_token'];
$token_secret = $form_state['values']['oauth_token_secret'];
$test_consumer = new OAuthConsumer($key, $secret, NULL);
if ($form_state['values']['op'] == 'OAuth Authentication Call') {
$_SESSION['oauth']['key'] = $form_state['values']['consumer_key'];
$_SESSION['oauth']['key_secret'] = $form_state['values']['consumer_secret'];
$_SESSION['oauth']['oauth_token'] = $token;
$_SESSION['oauth']['oauth_token_secret'] = $token_secret;
$test_token = new OAuthConsumer($token, $token_secret, $callback_url);
$test_consumer = new OAuthConsumer($form_state['values']['consumer_key'], $form_state['values']['consumer_secret'], $callback_url);
$parsed = parse_url($form_state['values']['auth_url']);
$params = array();
parse_str($parsed['query'], $params);
$req_auth = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $form_state['values']['auth_url'], $params);
$req_auth
->sign_request($sig_method, $test_consumer, $test_token);
$form_state['rebuild'] = TRUE;
$_SESSION['oauth']['operation'] = 'access';
$domain = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$callback_url = 'http://' . $domain;
Header("Location: {$req_auth}&oauth_callback={$callback_url}");
}
elseif ($form_state['values']['op'] == 'OAuth Access Token Call') {
$test_consumer = new OAuthConsumer($_SESSION['oauth']['key'], $_SESSION['oauth']['key_secret'], NULL);
$test_token = new OAuthConsumer($_SESSION['oauth']['oauth_token'], $_SESSION['oauth']['oauth_token_secret']);
$parsed = parse_url($form_state['values']['access_url']);
$params = array();
parse_str($parsed['query'], $params);
$acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $form_state['values']['access_url'], $params);
$acc_req
->sign_request($sig_method, $test_consumer, $test_token);
$result_access = drupal_http_request($acc_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
parse_str($result_access->data);
$_SESSION['oauth']['oauth_access_token'] = $oauth_token;
$_SESSION['oauth']['oauth_access_token_secret'] = $oauth_token_secret;
drupal_set_message('<pre>' . print_r("oauth_access_token {$oauth_token}", TRUE) . '</pre>');
drupal_set_message('<pre>' . print_r("oauth_access_token_secret {$oauth_token_secret}", TRUE) . '</pre>');
drupal_redirect_form($form, 'admin/build/services');
}
if ($form_state['values']['op'] == 'Request Token Call') {
$parsed = parse_url($request_url);
$params = array();
parse_str($parsed['query'], $params);
$req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $request_url, $params);
$req_req
->sign_request($sig_method, $test_consumer, NULL);
$result_request = drupal_http_request($req_req, $headers = array(), $method = 'GET', $data = NULL, $retry = 3);
parse_str($result_request->data);
drupal_set_message('<pre>' . print_r("oauth_token {$oauth_token}", TRUE) . '</pre>');
drupal_set_message('<pre>' . print_r("oauth_token_secret {$oauth_token_secret}", TRUE) . '</pre>');
$form_state['rebuild'] = TRUE;
$form_state['data']['oauth_token'] = $oauth_token;
$form_state['data']['oauth_token_secret'] = $oauth_token_secret;
$_SESSION['oauth']['operation'] = 'auth';
}
}