You are here

function simplesamlphp_auth_loginpage in simpleSAMLphp Authentication 7.2

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

The /saml_login which triggers user authentication to SimpleSAMLphp SP.

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

File

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

Code

function simplesamlphp_auth_loginpage() {
  global $user;
  global $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 '';
  }

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

  // Make sure phpsession is NOT being used.
  if ($config_store_type == 'phpsession') {
    watchdog('simplesamlphp_auth', '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', '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 (isset($_REQUEST['ReturnTo']) && $_REQUEST['ReturnTo'] && (valid_url($_REQUEST['ReturnTo']) && stristr($_REQUEST['ReturnTo'], $base_url))) {
    $returnto = $_REQUEST['ReturnTo'];

    // Check if REFERER URL is available and use it if it points to the 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 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) {
      $parsed_gotourl = drupal_parse_url($gotourl);
      drupal_goto($parsed_gotourl['path'], $parsed_gotourl);
    }
    else {
      drupal_goto('user/' . $user->uid);
    }
  }
  return $output;
}