You are here

function simplesamlphp_auth_loginpage in simpleSAMLphp Authentication 6.3

Same name and namespace in other branches
  1. 6.2 simplesamlphp_auth.module \simplesamlphp_auth_loginpage()
  2. 7.3 simplesamlphp_auth.pages.inc \simplesamlphp_auth_loginpage()
  3. 7 simplesamlphp_auth.module \simplesamlphp_auth_loginpage()
  4. 7.2 simplesamlphp_auth.module \simplesamlphp_auth_loginpage()

Represents the Drupal page (saml_login), which triggers user authentication against the SimpleSAMLphp service provider.

1 string reference to 'simplesamlphp_auth_loginpage'
simplesamlphp_auth_menu in ./simplesamlphp_auth.module
Implementation of hook_menu().

File

./simplesamlphp_auth.module, line 72
simpleSAMLphp authentication module for Drupal.

Code

function simplesamlphp_auth_loginpage() {
  global $user, $base_root, $base_url;
  global $_simplesamlphp_auth_as;
  global $_simplesamlphp_auth_saml_attributes;
  $fail = NULL;
  $output = NULL;
  if (!_simplesamlphp_auth_isEnabled()) {

    // Exit without initializing.
    drupal_set_message(t('We\'re sorry this feature is not yet enabled.'));
    return;
  }

  // Are we forcing https?
  if (variable_get('simplesamlphp_auth_forcehttps', NULL) && 'on' != $_SERVER['HTTPS']) {
    drupal_goto(_simplesamlphp_auth_forcehttps_rewrite($base_root . request_uri()));
  }

  // Do some sanity checking before attempting anything.
  $_simplesamlphp_auth_saml_config = SimpleSAML_Configuration::getInstance();
  $configStoreType = $_simplesamlphp_auth_saml_config
    ->getValue('store.type');

  // Make sure phpsession is NOT being used.
  if ($configStoreType == 'phpsession') {
    watchdog('simplesamlphp_auth', t('A user attempted to login using simplesamlphp but the store.type is phpsession, use memcache or sql for simplesamlphp session storage. See: simplesamlphp/config/config.php.'), NULL, WATCHDOG_WARNING);
    $fail = TRUE;
  }

  // Make sure there is an instance of SimpleSAML_Auth_Simple.
  if (!$_simplesamlphp_auth_as) {
    watchdog('simplesamlphp_auth', t('A user attempted to login using this module but there was a problem'), NULL, WATCHDOG_WARNING);
    $fail = TRUE;
  }

  // There was a problem, we can't go on, but we don't want to tell the user any specifics either.
  if ($fail) {
    drupal_set_message(t('We\'re sorry. There was a problem. The issue has been logged for the administrator.'));
    drupal_goto(base_path());
  }
  $returnto = NULL;

  // Support for deep linking.
  // See if a URL has been explicitly provided in ReturnTo. If so, use it (as long as it points to this site).
  if (isset($_REQUEST['ReturnTo']) && $_REQUEST['ReturnTo'] && (valid_url($_REQUEST['ReturnTo']) && stristr($_REQUEST['ReturnTo'], $base_url))) {
    $returnto = $_REQUEST['ReturnTo'];

    // If not, see if a REFERER URL is available. If so, use it (as long as it points to this site).
  }
  elseif (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] && (valid_url($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'], $base_url))) {
    $returnto = $_SERVER['HTTP_REFERER'];
  }

  // If the user is anonymous, set the cookie (if we can) and require authentication.
  if ($user->uid == 0) {
    if ($returnto) {

      // Set the cookie so we can deliver the user to the place they started
      setrawcookie('simplesamlphp_auth_returnto', $returnto, time() + 60 * 60);
    }

    // Require the user to be authenticated.
    $_simplesamlphp_auth_as
      ->requireAuth();

    // If the user is authenticated, send them along.
  }
  else {
    $gotourl = NULL;

    // Check to see if we've set a cookie. If there is one, give it priority.
    if (isset($_COOKIE['simplesamlphp_auth_returnto']) && $_COOKIE['simplesamlphp_auth_returnto']) {

      // use the cookie for the ReturnTo
      $gotourl = $_COOKIE['simplesamlphp_auth_returnto'];

      // unset the cookie
      setrawcookie('simplesamlphp_auth_returnto', '');
    }
    elseif ($returnto) {
      $gotourl = $returnto;
    }

    // If a ReturnTo has been set.
    if ($gotourl) {
      drupal_goto(str_replace($base_url . '/', '', $gotourl));
    }
    else {
      drupal_goto('user/' . $user->uid);
    }
  }
  return $output;
}