You are here

function simplesamlphp_auth_loginpage in simpleSAMLphp Authentication 6.2

Same name and namespace in other branches
  1. 6.3 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;
  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('/');
  }
  $_simplesamlphp_auth_authparms = NULL;

  // Support for deep linking.
  // See if a URL has been provided in ReturnTo.
  if (isset($_REQUEST['ReturnTo']) && $_REQUEST['ReturnTo'] && valid_url($_REQUEST['ReturnTo'])) {

    // we'll only use the REFERER if it points to this Drupal site.
    if (preg_match(base_path(), $_REQUEST['ReturnTo'])) {
      $_simplesamlphp_auth_authparms = array(
        'ReturnTo' => $_REQUEST['ReturnTo'],
      );
    }
  }
  elseif (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] && valid_url($_SERVER['HTTP_REFERER'])) {

    // we'll only use the REFERER if it points to this Drupal site.
    if (preg_match(base_path(), $_SERVER['HTTP_REFERER'])) {
      $_simplesamlphp_auth_authparms = array(
        'ReturnTo' => $_SERVER['HTTP_REFERER'],
      );
    }
  }
  if ($user->uid == 0) {

    // Require the user to be authentcated.
    if (is_array($_simplesamlphp_auth_authparms)) {
      $_simplesamlphp_auth_as
        ->requireAuth($_simplesamlphp_auth_authparms);
    }
    else {
      $_simplesamlphp_auth_as
        ->requireAuth();
    }
  }
  else {

    // See if a ReturnTo has been set.
    if (isset($_simplesamlphp_auth_authparms['ReturnTo']) && $_simplesamlphp_auth_authparms['ReturnTo']) {

      // Using header() here feels like a kuldge. Drupal might have a more appropriate way to do this, but there is the possibility that the ReturnTo URL might be outside of the Drupal site.
      header('Location: ' . $_simplesamlphp_auth_authparms['ReturnTo']);
    }
    else {
      drupal_goto('user/' . $user->uid);
    }
  }
  return $output;
}