You are here

function auth0_verify_email_page in Auth0 Single Sign On 7.2

Verify email page callback.

1 string reference to 'auth0_verify_email_page'
auth0_menu in ./auth0.module
Implements hook_menu().

File

./auth0.module, line 112

Code

function auth0_verify_email_page() {
  drupal_page_is_cacheable(FALSE);
  if (!auth0_enabled('login')) {
    return drupal_goto();
  }
  $token = $_REQUEST['idToken'];

  /**
   * Validate the ID Token
   */
  $domain = variable_get('auth0_domain', '');
  $client_id = variable_get('auth0_client_id', '');
  $client_secret = variable_get('auth0_client_secret', '');
  $secret_base64_encoded = variable_get('auth0_secret_base64_encoded', FALSE);
  $jwt_signature_alg = variable_get('auth0_jwt_signature_alg', "HS256");
  $auth0_domain = 'https://' . $domain . '/';
  $auth0_settings = array();
  $auth0_settings['authorized_iss'] = [
    $auth0_domain,
  ];
  $auth0_settings['supported_algs'] = [
    $jwt_signature_alg,
  ];
  $auth0_settings['valid_audiences'] = [
    $client_id,
  ];
  $auth0_settings['client_secret'] = $client_secret;
  $auth0_settings['secret_base64_encoded'] = $secret_base64_encoded;
  $jwt_verifier = new JWTVerifier($auth0_settings);
  try {
    $user = $jwt_verifier
      ->verifyAndDecode($token);
  } catch (\Exception $e) {
    drupal_set_message(t('There was a problem re-sending the email.'), 'error');
    watchdog('Auth0', "Error validating the token while resending the email: " . $e
      ->getMessage(), WATCHDOG_ERROR);
    return drupal_goto();
  }
  try {
    $userId = $user->sub;
    $url = "https://{$domain}/api/users/{$userId}/send_verification_email";
    $headers = array(
      'Authorization' => "Bearer {$token}",
    );
    $result = drupal_http_request($url, array(
      'headers' => $headers,
      'method' => 'POST',
    ));
    if ($result->code == 200) {
      drupal_set_message(t('A verification message with further instructions has been sent to your e-mail address.'));
    }
    else {
      drupal_set_message(t('Sorry, we could not send a verification e-mail. Please try again later.'), 'error');
    }
  } catch (Exception $e) {
    drupal_set_message(t('Sorry, we could not send a verification e-mail. Please try again later.'), 'error');
  }
  return drupal_goto();
}