You are here

function template_preprocess_auth0_lock in Auth0 Single Sign On 7.2

Preprocess the login widget.

File

./auth0.module, line 993

Code

function template_preprocess_auth0_lock(&$vars) {
  $vars['sso_enabled'] = (bool) variable_get("auth0_sso", FALSE);
  $vars['domain'] = check_plain(variable_get("auth0_domain", ''));
  $vars['client_id'] = check_plain(variable_get("auth0_client_id", ''));
  $vars['lock_extra_settings'] = json_decode(variable_get("auth0_lock_extra_settings", ''), true);
  if ($vars['lock_extra_settings'] === null) {
    $vars['lock_extra_settings'] = array();
  }
  $vars['params'] = $vars['lock_extra_settings'];
  $vars['params']['container'] = isset($vars['params']['container']) ? $vars['params']['container'] : 'auth0-login-form';
  $vars['params']['sso'] = isset($vars['sso_enabled']) ? $vars['sso_enabled'] : false;
  $vars['params']['auth'] = isset($vars['params']['auth']) ? $vars['params']['auth'] : array();
  $vars['params']['auth']['redirectUrl'] = isset($vars['params']['auth']['redirectUrl']) ? $vars['params']['auth']['redirectUrl'] : url('auth0/callback', array(
    'absolute' => TRUE,
    'query' => drupal_get_destination(),
  ));
  $vars['params']['auth']['responseType'] = isset($vars['params']['auth']['responseType']) ? $vars['params']['auth']['responseType'] : 'code';
  $vars['params']['auth']['params'] = isset($vars['params']['auth']['params']) ? $vars['params']['auth']['params'] : array();
  $vars['params']['auth']['params']['scope'] = isset($vars['params']['auth']['params']['scope']) ? $vars['params']['auth']['params']['scope'] : 'openid email';
  if (!isset($_SESSION['auth0_session_started'])) {
    $_SESSION['auth0_session_started'] = TRUE;
    drupal_session_start();
  }
  $vars['params']['auth']['params']['state'] = isset($vars['params']['auth']['params']['state']) ? $vars['params']['auth']['params']['state'] : drupal_get_token('auth0_state');
  if (auth0_enabled('signup')) {
    $vars['params']['allowSignUp'] = TRUE;
  }
  else {
    $vars['params']['allowSignUp'] = FALSE;
  }
  if (auth0_enabled('reset')) {
    $vars['params']['disableResetAction'] = TRUE;
  }
  else {
    $vars['params']['disableResetAction'] = FALSE;
  }
  drupal_alter('auth0_params', $vars['params']);

  // Generate a link to a login form to be displayed as a no-js fallback.
  if (isset($vars['params']['auth']['params']['state'])) {
    $query = array(
      'redirect_uri' => $vars['params']['auth']['redirectUrl'],
      'state' => $vars['params']['auth']['params']['state'],
    );
  }
  else {
    $query = array(
      'redirect_uri' => $vars['params']['auth']['redirectUrl'],
    );
  }

  // If auth0_sso is enabled, redirect to the hosted login page
  if (variable_get("auth0_sso", FALSE)) {
    $query['prompt'] = 'none';
    $vars['login_link'] = l(t('Log in'), _auth0_generate_authorize_url($query));

    // Bail early so the login link is all that is shown.
    return;
  }
  else {
    $vars['login_link'] = l(t('Log in'), _auth0_generate_authorize_url($query));
  }

  // Add the custom css if specified.
  if ($css = variable_get("auth0_login_css", AUTH_LOGIN_CSS)) {
    drupal_add_css($css, array(
      'type' => 'inline',
    ));
  }

  // Add the lock.js library from the specified CDN.
  drupal_add_js(filter_var(variable_get('auth0_widget_cdn', AUTH0_WIDGET_CDN), FILTER_VALIDATE_URL), 'external');

  // Add the auth0 js settings.
  drupal_add_js(array(
    'auth0' => array(
      'client_id' => $vars['client_id'],
      'domain' => $vars['domain'],
      'options' => $vars['params'],
    ),
  ), 'setting');

  // Add the Drupal behavior to initialize the widget.
  drupal_add_js(drupal_get_path('module', 'auth0') . '/auth0.lock.js');
}