<?php
// $Id: oauth.module,v 1.11.2.9 2008/11/11 02:13:59 brmassa Exp $
/**
 * @author OAuth module Dev Team
 * @file
 *  OAuth module's main file.
 *
 *  Only hooks and fundamental functions are placed here.
 */

/**
 * Implementation of hook_menu().
 */
function oauth_menu() {
  $items['admin/settings/oauth'] = array(
    'access arguments'  => array('administer site configuration'),
    'file'              => 'oauth.admin.inc',
    'page callback'     => 'drupal_get_form',
    'page arguments'    => array('_oauth_admin'),
    'title'             => 'OAuth',
  );
  $items['webservice/token_auth'] = array(
    'access callback'   => TRUE,
    'file'              => 'oauth.inc',
    'page callback'     => '_oauth_token_auth',
    'type'              => MENU_CALLBACK,
  );
  $items['webservice/token_request'] = array(
    'access callback'   => TRUE,
    'file'              => 'oauth.inc',
    'page callback'     => '_oauth_token_request',
    'page arguments'    => array(NULL, NULL, NULL, NULL, NULL),
    'type'              => MENU_CALLBACK,
  );
  $items['webservice/token_access'] = array(
    'access callback'   => TRUE,
    'file'              => 'oauth.inc',
    'page callback'     => '_oauth_token_access',
    'page arguments'    => array(NULL, NULL, NULL, NULL, NULL, NULL),
    'type'              => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Implementation of hook_perm().
 */
function oauth_perm() {
  return array('view own access token');
}

/**
 * Implementation of hook_webservices_info().
 *
 * It integrates to Web Services module, letting the consumers to get
 * tokens thru Web Services.
 */
function oauth_webservices_info() {
  return array(
    array(
      '#method'           => 'system.tokenRequest',
      '#description'      => t('Request a OAuth request token.'),
      '#callback'         => '_oauth_token_request',
      '#access callback'  => TRUE,
      '#no_auth'          => TRUE,
      '#file'             => array('file' => 'inc', 'module' => 'oauth'),
      '#args'             => array(
        array(
          '#name'           => 'oauth_version',
          '#type'           => 'string',
          '#description'    => t('OAuth version used.'),
        ),
        array(
          '#name'           => 'oauth_timestamp',
          '#type'           => 'int',
          '#description'    => t('The timestamp of the request.'),
        ),
        array(
          '#name'           => 'oauth_nonce',
          '#type'           => 'string',
          '#description'    => t('The random 32 characters long string used on each request.'),
        ),
        array(
          '#name'           => 'oauth_consumer_key',
          '#type'           => 'string',
          '#description'    => t('Consumer key.'),
        ),
        array(
          '#name'           => 'oauth_signature_method',
          '#type'           => 'string',
          '#description'    => t('Request signature method.'),
        ),
        array(
          '#name'           => 'oauth_signature',
          '#type'           => 'string',
          '#description'    => t('Request signature.'),
        ),
      ),
      '#return'           => 'array',
    ),
    array(
      '#method'           => 'system.tokenAccess',
      '#description'      => t('Request a OAuth access token.'),
      '#callback'         => '_oauth_token_access',
      '#no_auth'          => TRUE,
      '#access callback'  => TRUE,
      '#file'             => array('file' => 'inc', 'module' => 'oauth'),
      '#args'             => array(
        array(
          '#name'           => 'oauth_version',
          '#type'           => 'string',
          '#description'    => t('OAuth version used.'),
        ),
        array(
          '#name'           => 'oauth_timestamp',
          '#type'           => 'int',
          '#description'    => t('The timestamp of the request.'),
        ),
        array(
          '#name'           => 'oauth_nonce',
          '#type'           => 'string',
          '#description'    => t('The random 32 characters long string used on each request.'),
        ),
        array(
          '#name'           => 'oauth_consumer_key',
          '#type'           => 'string',
          '#description'    => t('Consumer key.'),
        ),
        array(
          '#name'           => 'oauth_token',
          '#type'           => 'string',
          '#description'    => t('OAuth Resquest token.'),
        ),
        array(
          '#name'           => 'oauth_signature_method',
          '#type'           => 'string',
          '#description'    => t('Request signature method.'),
        ),
        array(
          '#name'           => 'oauth_signature',
          '#type'           => 'string',
          '#description'    => t('Request signature.'),
        ),
      ),
      '#return'           => 'array',
    ),
  );
}

/**
 * Implementation of hook_webservices_auth_info().
 *
 * It integrates to Web Services module, adding the OAuth as a
 * authentication plugin.
 */
function oauth_webservices_auth_info() {
  return array(
    '#name'   => t('OAuth'),
    '#file'   => array('file' => 'inc', 'module' => 'oauth'),
    '#class'  => '_oauth_auth',
  );
}

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

  // If the user that has the permission to use Web Services is seeing
  // his own profile, show his OAuth consumer key and secret.
  if ($op == 'view' and user_access('integrate external application', $account) and
      ($account->uid == $user->uid or user_access('administer webservices'))) {
    module_load_include('admin.inc', 'oauth');
    _oauth_user($op, $edit, $account, $category);
  }

  // Delete all tokens related to a user
  elseif ($op == 'delete') {
    module_load_include('inc', 'oauth');
    $consumer = _oauth_consumer_get($account->uid);
    db_query('DELETE FROM {oauth_consumer}
      WHERE uid = %d', $account->uid);
    db_query("DELETE FROM {oauth_token}
      WHERE uid = %d OR consumer_key = '%s'", $account->uid, $consumer->key);
  }
}
