Skip to content

Commit

Permalink
In the process of porting the stuff done in the oauth module.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo Wetterberg committed Feb 20, 2009
1 parent 3fbf6b6 commit 8ab9aa0
Show file tree
Hide file tree
Showing 8 changed files with 687 additions and 0 deletions.
124 changes: 124 additions & 0 deletions includes/DrupalOAuthDataStore.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
// $Id$

/**
* Database abstraction class
*/
class DrupalOAuthDataStore extends OAuthDataStore {
/**
* Check if consumer exists from a given consumer key.
*
* @param $consumer_key
* String. The consumer key.
*/
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);
}
throw new OAuthException('Consumer not found');
}

/**
* Check if the token exists.
*
* @param $consumer
* Object. The service consumer information.
* @param $token_type
* Strint. The type of the token: 'request' or 'access'.
* @param $token
* Strint. The token value.
* @return
* String or NULL. The existing token or NULL in
* case it doesnt exist.
*/
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);
}
throw new OAuthException('Token not found');
}

/**
* Check if the nonce value exists. If not, generate one.
*
* @param $consumer
* Object. The service consumer information with both key
* and secret values.
* @param $token
* Strint. The current token.
* @param $nonce
* Strint. A new nonce value, in case a one doesnt current exit.
* @param $timestamp
* Number. The current time.
* @return
* String or NULL. The existing nonce value or NULL in
* case it doesnt exist.
*/
function lookup_nonce($consumer, $token, $nonce, $timestamp) {
if (!$nonce_1 = db_result(db_query("SELECT nonce FROM {oauth_nonce}
WHERE timestamp <= %d and token = '%s'", $timestamp, $token))) {
$sql = array(
'nonce' => $nonce,
'timestamp' => $timestamp,
'token' => $token,
);
drupal_write_record('oauth_nonce', $sql);
return NULL;
}
return $nonce_1;
}

/**
* Generate a new request token.
*
* @param $consumer
* Object. The service consumer information.
*/
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));
$sql = array(
'consumer_key' => $consumer->key,
'type' => 'request',
'token_key' => $token->key,
'token_secret' => $token->secret,
'uid' => $user_id
);
drupal_write_record('oauth_token', $sql);
return $token;
}

/**
* Generate a new access token and delete the old request token.
*
* @param $token_old
* Strint. The old request token.
* @param $consumer
* Object. The service consumer information.
*/
function new_access_token($token_old, $consumer) {
if ($object = db_fetch_array(db_query("SELECT * FROM {oauth_token}
WHERE type = 'request' AND token_key = '%s'", $token_old->key))) {
if ($object['authorized']) {
$token_new = new OAuthToken(user_password(32), user_password(32));
$sql = array(
'consumer_key' => $consumer->key,
'type' => 'access',
'token_key' => $token_new->key,
'token_secret' => $token_new->secret,
'uid' => $object['uid']
);
drupal_write_record('oauth_token', $sql, array('uid', 'consumer_key'));

return $token_new;
}
}
throw new OAuthException('Invalid request token');
}
}
10 changes: 10 additions & 0 deletions includes/DrupalOAuthServer.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
// $Id$

class DrupalOAuthServer extends OAuthServer {
function __construct() {
parent::__construct(new DrupalOAuthDataStore());
$this->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
$this->add_signature_method(new OAuthSignatureMethod_RSA_SHA1());
}
}
22 changes: 22 additions & 0 deletions lib/LICENCE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License

Copyright (c) 2007 Andy Smith

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

85 changes: 85 additions & 0 deletions services_oauth.admin.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/**
* @file
* Services OAuth's admin pages.
*/

/**
* Main module settings.
*/
function _services_oauth_admin() {
$form['services_oauth_crypt'] = array(
'#default_value' => variable_get('services_oauth_crypt', 'HMAC-SHA1'),
'#description' => t('When enabled, all method calls must include a valid OAuth access token and consumer token.'),
'#options' => array(
'HMAC-SHA1' => t('HMAC-SHA1'),
'RSA-SHA1' => t('RSA-SHA1'),
),
'#title' => t('OAuth cryptography'),
'#type' => 'radios',
);

return system_settings_form($form);
}

/**
* Implementation of hook_user().
*/
function _oauth_user($op, &$edit, &$account, $category = NULL) {
global $user;

module_load_include('inc', 'oauth');
$consumer = _oauth_consumer_get($account->uid);
$account->content['oauth'] = array(
'#attributes' => array('class' => 'user-member'),
'#title' => t('Web services'),
'#type' => 'user_profile_category',
'#weight' => 5,
);
$account->content['oauth']['consumer_key'] = array(
'#title' => t('Consumer key'),
'#type' => 'user_profile_item',
'#value' => $consumer->key,
'#weight' => 1,
);
$account->content['oauth']['consumer_secret'] = array(
'#title' => t('Consumer secret'),
'#type' => 'user_profile_item',
'#value' => $consumer->secret,
'#weight' => 2,
);

// Specially useful for developers, it will create and show
// a Access Token for the own user. Remember that all services
// are authorized when consumer and the user are the same.
if (user_access('view own access token', $account)) {
if (!$token = db_fetch_object(db_query("SELECT token_key, token_secret AS secret
FROM {oauth_token}
WHERE uid = %d AND consumer_key = '%s' AND type = 'access'",
$account->uid, $consumer->key))) {
$token = new OAuthToken(user_password(32), user_password(32));
$sql = array(
'consumer_key' => $consumer->key,
'type' => 'access',
'token_key' => $token->key,
'token_secret' => $token->secret,
'uid' => $account->uid,
'webservices' => serialize(array())
);
drupal_write_record('oauth_token', $sql);
}
$account->content['oauth']['own_access_token_key'] = array(
'#title' => t('Access token key'),
'#type' => 'user_profile_item',
'#value' => empty($token->key) ? $token->token_key : $token->key,
'#weight' => 3,
);
$account->content['oauth']['own_access_token_secret'] = array(
'#title' => t('Access token secret'),
'#type' => 'user_profile_item',
'#value' => empty($token->secret) ? $token->token_secret : $token->secret,
'#weight' => 4,
);
}
}
Loading

0 comments on commit 8ab9aa0

Please sign in to comment.