Projet

Général

Profil

Paste
Télécharger (7,43 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / cas / tests / cas_test.module @ c12e7e6a

1
<?php
2

    
3
/**
4
 * @file
5
 * Dummy module implementing a CAS Server.
6
 */
7

    
8
/**
9
 *
10
 */
11
function cas_test_cas_phpcas_alter() {
12

    
13
  // Set the User-Agent field which is used by SimpleTest to identify testing
14
  // requests.
15
  $test_info = &$GLOBALS['drupal_test_info'];
16
  if (!empty($test_info['test_run_id'])) {
17
    phpCAS::setExtraCurlOption(CURLOPT_USERAGENT, drupal_generate_test_ua($test_info['test_run_id']));
18
  }
19

    
20
  // Set all CAS server URLs manually, as this is the only way to specify an
21
  // HTTP (i.e., not HTTPS) connection.
22
  $service_url = phpCAS::getServiceURL();
23
  phpCAS::setServerLoginURL(url('cas_test/login', array(
24
    'query' => array('service' => $service_url),
25
    'absolute' => TRUE,
26
  )));
27

    
28
  switch (variable_get('cas_version', '2.0')) {
29
    case CAS_VERSION_1_0:
30
      phpCAS::setServerServiceValidateURL(url('cas_test/validate', array('absolute' => TRUE)));
31
      break;
32
    case CAS_VERSION_2_0:
33
      phpCAS::setServerServiceValidateURL(url('cas_test/serviceValidate', array('absolute' => TRUE)));
34
      phpCAS::setServerProxyValidateURL(url('cas_test/proxyValidate',  array('absolute' => TRUE)));
35
      break;
36
    default:
37
      throw new Exception('Unknown CAS server version.');
38
      break;
39
  }
40

    
41
  phpCAS::setServerLogoutURL(url('cas_test/logout',  array('absolute' => TRUE)));
42

    
43
  // SAML not currently implemented.
44
  // phpCAS::setServerSamlValidateURL(url('cas_test/samlValidate', array('absolute' => TRUE)));
45
}
46

    
47
/**
48
 * Implements hook_menu().
49
 */
50
function cas_test_menu() {
51
  $items = array();
52
  $items['cas_test/login'] = array(
53
    'page callback' => 'cas_test_login',
54
    'title' => 'CAS Login',
55
    'access callback' => TRUE,
56
    'type' => MENU_CALLBACK,
57
  );
58
  $items['cas_test/validate'] = array(
59
    'page callback' => 'cas_test_validate',
60
    'title' => 'CAS Validate',
61
    'access callback' => TRUE,
62
    'type' => MENU_CALLBACK,
63
  );
64
  $items['cas_test/serviceValidate'] = array(
65
    'page callback' => 'cas_test_service_validate',
66
    'title' => 'CAS Service Validate',
67
    'access callback' => TRUE,
68
    'type' => MENU_CALLBACK,
69
  );
70
  $items['cas_test/proxyValidate'] = array(
71
    'page callback' => 'cas_test_service_validate',
72
    'title' => 'CAS Proxy Ticket Validate',
73
    'access callback' => TRUE,
74
    'type' => MENU_CALLBACK,
75
  );
76
  $items['cas_test/logout'] = array(
77
    'page callback' => 'cas_test_logout',
78
    'title' => 'CAS Logout',
79
    'access callback' => TRUE,
80
    'type' => MENU_CALLBACK,
81
  );
82
  $items['cas_test/token'] = array(
83
    'page callback' => 'cas_test_token_evaluate',
84
    'title' => 'CAS Token Test',
85
    'access callback' => TRUE,
86
    'type' => MENU_CALLBACK,
87
  );
88
  return $items;
89
}
90

    
91
/**
92
 * Initiate a login request.
93
 *
94
 * Set the 'cas_test_cas_user' variable to an associative array containing:
95
 * - 'name': CAS username.
96
 * - 'attributes': (optional) Any other name-value pairs to be returned by the
97
 *   CAS server.
98
 */
99
function cas_test_login() {
100
  // Get the service and make a ticket.
101
  $service = $_REQUEST['service'];
102
  $cas_user = variable_get('cas_test_cas_user', '');
103

    
104
  if ($cas_user) {
105
    if (!is_array($cas_user)) {
106
      $cas_user = array('name' => $cas_user);
107
    }
108
    // Generate a ticket and redirect to the service URL with the login ticket.
109
    $ticket = _cas_test_ticket_generate($service, $cas_user);
110
    // Force redirection.
111
    unset($_GET['destination']);
112
    drupal_goto($service, array('query' => array('ticket' => $ticket)));
113
  }
114
  elseif (isset($_GET['gateway']) && $_GET['gateway'] == 'true') {
115
    // We were not able to log in the user, so redirect to the service URL.
116
    // Force redirection.
117
    unset($_GET['destination']);
118
    drupal_goto($service);
119
  }
120
  else {
121
    // No CAS name was provided, print an error message.
122
    print "Warning: No CAS name provided.\n";
123
    exit();
124
  }
125
}
126

    
127
/**
128
 * Validate a ticket using the CAS 1.x protocol.
129
 */
130
function cas_test_validate() {
131
  //Obtain the ticket from the url and validate it.
132
  $ticket = $_GET['ticket'];
133
  $service = $_GET['service'];
134
  $cas_user = _cas_test_ticket_validate($service, $ticket);
135
  if ($cas_user) {
136
    $cas_name = $cas_user['name'];
137
    print "yes\n";
138
    print "$cas_name\n";
139
  }
140
  else {
141
    print "no\n";
142
    print "\n";
143
  }
144
  exit();
145
}
146

    
147
/**
148
 * Validate a ticket using the CAS 2.0 protocol.
149
 */
150
function cas_test_service_validate() {
151
  $ticket = $_GET['ticket'];
152
  $service = $_GET['service'];
153

    
154
  header('Content-type:', 'text/xml');
155
  if ($cas_user = _cas_test_ticket_validate($service, $ticket)) {
156
    print theme('cas_service_validate_success', $cas_user);
157
  }
158
  else {
159
    $error_code = (!$ticket || !$service) ? 'INVALID_REQUEST' : 'INVALID_TICKET';
160
    print theme('cas_service_validate_failure', array('ticket' => $ticket, 'error_code' => $error_code));
161
  }
162
  exit();
163
}
164

    
165
/**
166
 * Log out a user.
167
 */
168
function cas_test_logout() {
169
  if (isset($_GET['url'])) {
170
    print t('Logged out. Continue to @url.', array('@url' => $_GET['url']));
171
  }
172
  else {
173
    print t('Logged out. No redirection provided.');
174
  }
175
  exit();
176
}
177

    
178
/**
179
 * Generate a login ticket.
180
 *
181
 * @param $service
182
 *   The service URL.
183
 * @param $cas_user
184
 *   An associative array containing the following keys:
185
 *     - 'name': The CAS username.
186
 *     - 'attributes': Any other key-value pairs the CAS server should return.
187
 *
188
 * @return
189
 *   A login ticket which may be used to authenticate the CAS username at the
190
 *   service URL.
191
 */
192
function _cas_test_ticket_generate($service, $cas_user) {
193
  // Generate a one-time ticket.
194
  $ticket = 'ST-' . user_password(32);
195

    
196
  // Save the ticket in the database.
197
  $tickets = variable_get('cas_test_tickets', array());
198
  $tickets[$service][$ticket] = $cas_user;
199
  variable_set('cas_test_tickets', $tickets);
200

    
201
  // Save the name in the database for single sign-out.
202
  $sso = variable_get('cas_test_sso', array());
203
  $sso[$cas_user['name']][$service][] = $ticket;
204
  variable_set('cas_test_sso', $sso);
205

    
206
  return $ticket;
207
}
208

    
209
/**
210
 * Validate a one-time-use login ticket.
211
 *
212
 * @param $service
213
 *   The service URL.
214
 * @param $ticket
215
 *   The login or proxy ticket.
216
 *
217
 * @return
218
 *   The CAS username corresponding to the ticket, or FALSE if the ticket is
219
 *   invalid.
220
 */
221
function _cas_test_ticket_validate($service, $ticket) {
222
  // Look up the ticket
223
  $cas_name = FALSE;
224
  $tickets = variable_get('cas_test_tickets', array());
225
  if (isset($tickets[$service][$ticket])) {
226
    $cas_name = $tickets[$service][$ticket];
227
    unset($tickets[$service][$ticket]);
228
  }
229
  return $cas_name;
230
}
231

    
232
/**
233
 * Sign out the specified CAS user.
234
 *
235
 * @param $cas_user
236
 */
237
function cas_test_single_sign_out($cas_name) {
238
  $sso = variable_get('cas_test_sso', array());
239
  foreach ($sso[$cas_name] as $service => $tickets) {
240
    foreach ($tickets as $ticket) {
241
      // Generate posting:
242
      $data = array(
243
        'logoutRequest' => t("<samlp:LogoutRequest ID=\"!id\" Version=\"2.0\" IssueInstant=\"!time\">\n<saml:NameID>@NOT_USED@</saml:NameID>\n<samlp:SessionIndex>!ticket</samlp:SessionIndex>\n</samlp:LogoutRequest>", array('!id' => user_password(10), '!time' => time(), '!ticket' => $ticket)),
244
      );
245

    
246
      // Sign out the user.
247
      $options = array(
248
        'method' => 'POST',
249
        'headers' => array(
250
          'Content-Type' => 'application/x-www-form-urlencoded',
251
        ),
252
        'data' => drupal_http_build_query($data),
253
      );
254
      drupal_http_request($service, $options);
255
    }
256
  }
257
  unset($sso[$cas_name]);
258
  variable_set('cas_test_sso', $sso);
259
}
260

    
261
/**
262
 * Evaluate the specified token.
263
 */
264
function cas_test_token_evaluate() {
265
  print token_replace($_GET['token'], array('cas' => $_GET['name']));
266
  exit(0);
267
}