You are here

function cas_server_service_validate in CAS 7

Same name and namespace in other branches
  1. 5.4 cas_server.module \cas_server_service_validate()
  2. 5.3 cas_server.module \cas_server_service_validate()
  3. 6.3 cas_server.module \cas_server_service_validate()
  4. 6.2 cas_server.module \cas_server_service_validate()

serviceValidate method using cas 2.0 Returns data in xml

1 string reference to 'cas_server_service_validate'
cas_server_menu in ./cas_server.module
Implementation of hook_menu

File

./cas_server.module, line 216
Provides a protocol compliant version of CAS server 2.x

Code

function cas_server_service_validate() {

  // Prevent this page from being cached.
  drupal_page_is_cacheable(FALSE);

  // Set content type.
  drupal_add_http_header('Content-Type', 'text/xml; charset=utf-8');
  $ticket = isset($_REQUEST['ticket']) ? $_REQUEST['ticket'] : '';
  $service = isset($_REQUEST['service']) ? $_REQUEST['service'] : '';

  // Check service against whitelist
  if (!_cas_server_check_service_whitelist($service)) {
    $cas_error = 'INVALID_REQUEST';
    print theme('cas_server_validate_whitelist_failure', array(
      'service' => $service,
      'error_code' => $cas_error,
    ));
    watchdog('cas', 'Service %service validation failed!', array(
      '%service' => $service,
    ));
    return;
  }
  $user_name = _cas_server_validate($service, $ticket);
  if (!$user_name) {
    $cas_error = 'INVALID_TICKET';
  }
  if (!$ticket || !$service) {
    $cas_error = 'INVALID_REQUEST';
  }
  if ($user_name) {

    //@TODO Generate proxy granting ticket
    $account = user_load_by_name($user_name);

    // Generate a list of attributes to return.
    $attributes = module_invoke_all('cas_server_user_attributes', $account, $service, $ticket);

    // Let other modules alter the list of attributes.
    $context = array(
      'service' => $service,
      'ticket' => $ticket,
    );
    drupal_alter('cas_server_user_attributes', $attributes, $account, $context);
    print theme('cas_service_validate_success', array(
      'name' => $user_name,
      'attributes' => $attributes,
    ));
    watchdog('cas', 'User %name CAS successfully authenticated.', array(
      '%name' => $user_name,
    ));
  }
  else {
    print theme('cas_service_validate_failure', array(
      'ticket' => $ticket,
      'error_code' => $cas_error,
    ));
    watchdog('cas', 'Ticket %ticket for service %service not recognized.', array(
      '%ticket' => $ticket,
      '%service' => $service,
    ));
  }
}