You are here

function _oauthconnector_button in OAuth Connector 7

Same name and namespace in other branches
  1. 6 oauthconnector.module \_oauthconnector_button()
1 call to _oauthconnector_button()
_oauthconnector_connect_button in ./oauthconnector.module
1 string reference to '_oauthconnector_button'
oauthconnector_connector in ./oauthconnector.module
Implements hook_connector().

File

./oauthconnector.module, line 587
OAuth Connector module

Code

function _oauthconnector_button($form, &$form_state, $login = TRUE) {
  global $user;

  //TODO: Move some of the contens of this function to oauth_common_get_request_token()?
  $provider = $form_state['clicked_button']['connector']['#value']['oauthconnector provider'];
  $callback_url = url('oauth/authorized', array(
    'absolute' => TRUE,
  ));
  $consumer = DrupalOAuthConsumer::loadById($provider->csid, FALSE);
  $sig_method = DrupalOAuthClient::signatureMethod(substr(strtolower($provider->consumer_advanced['signature method']), 5));
  if (!$consumer->configuration['oauth2']) {
    $client = new DrupalOAuthClient($consumer, NULL, $sig_method);

    //TODO: Deal with errors! Add a try-catch
    $request_token = $client
      ->getRequestToken($provider->consumer_advanced['request token endpoint'], array(
      'realm' => $provider->consumer_advanced['authentication realm'],
      'callback' => $callback_url,
    ));
    if (!$login) {
      $request_token->uid = $user->uid;
    }
    $request_token
      ->write();
    $auth_url = $client
      ->getAuthorizationUrl($provider->consumer_advanced['authorization endpoint'], array(
      'callback' => $callback_url,
    ));
  }
  else {
    $callback_url = url('oauth/authorized2/' . $consumer->csid, array(
      'absolute' => TRUE,
    ));
    $request_token = new DrupalOAuthToken(md5(mt_rand()), md5(mt_rand()), $consumer, array(
      'type' => OAUTH_COMMON_TOKEN_TYPE_REQUEST,
    ));
    $client = new DrupalOAuth2Client($consumer, $request_token, $sig_method);
    $auth_url = $client
      ->getAuthorizationUrl($provider->consumer_advanced['authorization endpoint'], array(
      'params' => array(
        'redirect_uri' => $callback_url,
        'response_type' => 'code',
        'client_id' => $consumer->key,
        'scope' => $provider->consumer_advanced['authorization scope'],
      ),
    ));
    $request_token
      ->write();
  }

  //TODO: If 'oauthconnector_request_key' is already set - then remove the old one - we can only use one at a time
  $_SESSION['oauthconnector_request_key'] = $request_token->key;
  $_SESSION['oauthconnector_login'] = $login;
  $_SESSION['oauthconnector_action'] = $form_state['values']['action'];
  if (isset($_GET['destination'])) {
    $destination = $_GET['destination'];
    unset($_GET['destination']);
  }
  elseif (isset($_REQUEST['edit']['destination'])) {
    $destination = $_REQUEST['edit']['destination'];
    unset($_REQUEST['edit']['destination']);
  }
  else {
    $destination = isset($_GET['q']) ? $_GET['q'] : '';
    $query = drupal_http_build_query(drupal_get_query_parameters($_GET, array(
      'q',
    )));
    if ($query != '') {
      $destination .= '?' . $query;
    }
  }
  $_SESSION['oauthconnector_destination'] = $destination;
  drupal_goto($auth_url);
}