View source
<?php
define('OAUTH_COMMON_CODE_BRANCH', '6.x-3.x');
define('OAUTH_COMMON_TOKEN_TYPE_REQUEST', 0);
define('OAUTH_COMMON_TOKEN_TYPE_ACCESS', 1);
define('OAUTH_COMMON_VERSION_1', 1);
define('OAUTH_COMMON_VERSION_1_RFC', 2);
function oauth_common_theme() {
return array(
'oauth_common_auth_level' => array(
'template' => 'oauth_common_auth_level',
'arguments' => array(
'element' => array(),
'value' => NULL,
),
),
);
}
function oauth_common_perm() {
$permissions = array(
'oauth authorize any consumers',
'oauth register any consumers',
'administer oauth',
'administer consumers',
);
foreach (oauth_common_context_list() as $name => $title) {
$permissions[] = sprintf('oauth register consumers in %s', $name);
$permissions[] = sprintf('oauth authorize consumers in %s', $name);
}
return $permissions;
}
function oauth_common_menu() {
$menu = array();
$admin_base = array(
'access arguments' => array(
'administer oauth',
),
'file' => 'oauth_common.admin.inc',
);
$menu['admin/settings/oauth'] = array(
'title' => 'OAuth',
'description' => 'Settings for OAuth',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_oauth_common_admin',
),
'type' => MENU_NORMAL_ITEM,
) + $admin_base;
$menu['admin/settings/oauth/settings'] = array(
'title' => 'Settings',
'description' => 'Settings for OAuth',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_oauth_common_admin',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 0,
) + $admin_base;
$provider_base = array(
'access callback' => 'oauth_commmon_is_provider',
'file' => 'oauth_common.pages.inc',
'type' => MENU_CALLBACK,
);
$menu['oauth/request_token'] = array(
'page callback' => 'oauth_common_callback_request_token',
) + $provider_base;
$menu['oauth/authorize'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'oauth_common_form_authorize',
),
) + $provider_base;
$menu['oauth/access_token'] = array(
'page callback' => 'oauth_common_callback_access_token',
) + $provider_base;
$menu['oauth/authorized'] = array(
'title' => 'Authorization finished',
'page callback' => 'oauth_common_page_authorized',
'access arguments' => array(
'access content',
),
'file' => 'oauth_common.pages.inc',
'type' => MENU_CALLBACK,
);
$menu['oauth/test/valid-consumer'] = array(
'file' => 'oauth_common.pages.inc',
'page callback' => '_oauth_common_validate_request_callback',
'page arguments' => array(
'consumer',
),
'access callback' => 'oauth_commmon_is_provider',
'type' => MENU_CALLBACK,
);
$menu['oauth/test/valid-access-token'] = array(
'file' => 'oauth_common.pages.inc',
'page callback' => '_oauth_common_validate_request_callback',
'page arguments' => array(
'access token',
),
'access callback' => 'oauth_commmon_is_provider',
'type' => MENU_CALLBACK,
);
return $menu;
}
function oauth_common_consumer_load($csid) {
$consumer = DrupalOAuthConsumer::loadById($csid, TRUE);
if (!$consumer) {
$consumer = FALSE;
}
return $consumer;
}
function oauth_common_provider_token_load($tid) {
if (strlen($tid) == 32) {
$token = DrupalOAuthToken::loadByKey($tid);
}
else {
$token = DrupalOAuthToken::loadByID($tid);
}
if (!$token) {
$token = FALSE;
}
return $token;
}
function oauth_common_cron() {
$now = time();
db_query("DELETE FROM {oauth_common_provider_token} WHERE tid IN\n (SELECT tid FROM {oauth_common_token}\n WHERE expires != 0 AND expires <= %d)", $now);
db_query("DELETE FROM {oauth_common_token}\n WHERE expires != 0 AND expires <= %d", $now);
db_query("DELETE FROM {oauth_common_nonce}\n WHERE timestamp < %d", $now - 300);
}
function oauth_common_default_oauth_common_context() {
$contexts = array();
$context = new stdClass();
$context->disabled = FALSE;
$context->name = 'default';
$context->title = 'Default context';
$context->authorization_options = array();
$context->authorization_levels = array(
'*' => array(
'title' => 'Full access',
'description' => 'This will give @appname the same permissions that you normally have and will allow it to access the full range of services that @sitename provides.',
),
'read' => array(
'title' => 'Read access',
'description' => 'This will allow @appname to fetch content that you have access to on @sitename.',
),
'update' => array(
'title' => 'Update access',
'description' => 'This will allow @appname to update content that you have permissions to edit.',
),
'create' => array(
'title' => 'Create access',
'description' => 'This will allow @appname to create new content on @sitename.',
),
'delete' => array(
'title' => 'Delete access',
'description' => 'This will allow @appname to delete content from @sitename.',
),
);
$contexts[$context->name] = $context;
return $contexts;
}
function oauth_common_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'delete') {
module_load_include('inc', 'oauth_common');
db_query("DELETE c, pc, t, pt FROM {oauth_common_consumer} c\n INNER JOIN {oauth_common_provider_consumer} pc ON pc.csid = c.csid\n LEFT JOIN {oauth_common_token} t ON t.csid = c.csid\n LEFT JOIN {oauth_common_provider_token} pt ON pt.tid = t.tid\n WHERE pc.uid = %d", array(
':uid' => $account->uid,
));
db_query("DELETE t, pt FROM {oauth_common_token} t\n LEFT JOIN {oauth_common_provider_token} pt ON pt.tid = t.tid\n WHERE uid = %d", array(
':uid' => $account->uid,
));
}
}
function services_oauth_xrds() {
$xrds = array();
$xrds['oauth'] = array(
'services' => array(
array(
'data' => array(
'Type' => array(
'http://oauth.net/discovery/1.0',
),
'URI' => array(
'#main',
),
),
),
array(
'data' => array(
'Type' => array(
'http://oauth.net/core/1.0/endpoint/request',
'http://oauth.net/core/1.0/parameters/auth-header',
'http://oauth.net/core/1.0/parameters/uri-query',
'http://oauth.net/core/1.0/signature/HMAC-SHA1',
),
'URI' => array(
url('oauth/request_token', array(
'absolute' => TRUE,
)),
),
),
),
array(
'data' => array(
'Type' => array(
'http://oauth.net/core/1.0/endpoint/authorize',
'http://oauth.net/core/1.0/parameters/uri-query',
),
'URI' => array(
url('oauth/authorize', array(
'absolute' => TRUE,
)),
),
),
),
array(
'data' => array(
'Type' => array(
'http://oauth.net/core/1.0/endpoint/access',
'http://oauth.net/core/1.0/parameters/auth-header',
'http://oauth.net/core/1.0/parameters/uri-query',
'http://oauth.net/core/1.0/signature/HMAC-SHA1',
),
'URI' => array(
url('oauth/access_token', array(
'absolute' => TRUE,
)),
),
),
),
),
);
return $xrds;
}
function _oauth_common_user_access($user, $permission = NULL, $token = NULL) {
$user_permission_access = empty($permission) || user_access($permission);
if (!$user_permission_access && !is_null($token)) {
$permission = str_replace('any ', '', $permission) . ' in ' . $token->consumer->context;
$user_permission_access = user_access($permission);
}
return user_edit_access($user) && $user_permission_access;
}
function oauth_common_can_edit_consumer($consumer) {
$may_edit = user_access('administer consumers');
if (!$may_edit && $consumer->uid) {
$user = user_load($consumer->uid);
$may_edit = user_edit_access($user);
}
return $may_edit;
}
function oauth_common_can_create_consumers($account = NULL) {
global $user;
if (!$account) {
$account = $user;
}
$can_register_consumers = user_access('oauth register any consumers', $account);
if (!$can_register_consumers) {
foreach (oauth_common_context_list() as $context => $title) {
$can_register_consumers = $can_register_consumers || user_access(sprintf('oauth register consumers in %s', $context), $account);
}
}
return $can_register_consumers;
}
function _oauth_common_always_true() {
return TRUE;
}
function oauth_common_can_authorize_consumer($consumer) {
return user_access(sprintf('oauth authorize consumers in %s', $consumer->context));
}
function oauth_commmon_is_provider() {
return variable_get('oauth_common_enable_provider', TRUE);
}
function oauth_common_get_request_token($consumer_token, $request_endpoint = '/oauth/request_token', $authorize_endpoint = '/oauth/authorize') {
$client = new DrupalOAuthClient($consumer_token);
$request_token = $client
->getRequestToken($request_endpoint);
$request_token
->write();
return $client
->getAuthorizationUrl($authorize_endpoint);
}
function oauth_common_get_user_provider_tokens($uid) {
$res = db_query("SELECT t.*, pt.created, pt.changed, pt.services, pt.authorized FROM {oauth_common_token} t\n INNER JOIN {oauth_common_provider_token} pt WHERE t.uid = %d AND t.type = %d", array(
':uid' => $uid,
':type' => OAUTH_COMMON_TOKEN_TYPE_ACCESS,
));
$tokens = array();
while ($token = DrupalOAuthToken::fromResult($res)) {
$tokens[] = $token;
}
return $tokens;
}
function oauth_common_context_new() {
if (!module_exists('ctools')) {
return FALSE;
}
ctools_include('export');
return ctools_export_new_object('oauth_common_context');
}
function oauth_common_context_load($name) {
if (!module_exists('ctools')) {
return FALSE;
}
ctools_include('export');
$result = ctools_export_load_object('oauth_common_context', 'names', array(
$name,
));
if (isset($result[$name])) {
return $result[$name];
}
else {
return FALSE;
}
}
function oauth_common_context_from_request($request) {
$context = NULL;
$consumer_key = $request
->get_parameter('oauth_consumer_key');
$token_key = $request
->get_parameter('oauth_token');
if (empty($consumer_key) && !empty($token_key)) {
$token = DrupalOAuthToken::loadByKey($token_key, FALSE, OAUTH_COMMON_TOKEN_TYPE_REQUEST);
if ($token) {
$consumer = $token->consumer;
}
}
if (!empty($consumer_key)) {
$consumer = DrupalOAuthConsumer::loadProviderByKey($consumer_key);
}
if (!empty($consumer)) {
$context = oauth_common_context_load($consumer->context);
}
return $context;
}
function oauth_common_context_load_all() {
if (!module_exists('ctools')) {
return FALSE;
}
ctools_include('export');
return ctools_export_load_object('oauth_common_context');
}
function oauth_common_context_save($context) {
$update = isset($context->cid) ? array(
'cid',
) : array();
drupal_write_record('oauth_common_context', $context, $update);
}
function oauth_common_context_delete($context) {
db_query("DELETE FROM {oauth_common_context} WHERE name = '%s' AND cid = %d", $context->name, $context->cid);
}
function oauth_common_context_export($context, $indent = '') {
if (!module_exists('ctools')) {
return FALSE;
}
ctools_include('export');
$output = ctools_export_object('oauth_common_context', $context, $indent);
return $output;
}
function oauth_common_context_list() {
$return = array();
$contexts = oauth_common_context_load_all();
if ($contexts) {
foreach ($contexts as $context) {
$return[$context->name] = $context->title;
}
}
return $return;
}
function _oauth_common_version() {
static $version;
if (!isset($version)) {
$info = db_result(db_query("SELECT info FROM {system} WHERE name = 'oauth_common'"));
$info = $info ? unserialize($info) : FALSE;
if (!$info || empty($info['version'])) {
$version = OAUTH_COMMON_CODE_BRANCH;
}
else {
$version = $info['version'];
}
}
return $version;
}