-
Notifications
You must be signed in to change notification settings - Fork 701
Expand file tree
/
Copy pathtest.php
More file actions
executable file
·637 lines (500 loc) · 16.1 KB
/
test.php
File metadata and controls
executable file
·637 lines (500 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/env php
<?php
/*
* This script can be used to test a login-logout sequence to a specific IdP.
* It is configured from the config/test.php file. A template for that file
* can be found in config/test-template.php.
*/
$tests = array();
/* The configuration file is relative to this script. */
$configFile = dirname(dirname(__FILE__)) . '/config/test.php';
/* Check if the configuration file exists. */
if(!file_exists($configFile)) {
echo('Missing configuration file: ' . $configFile . "\n");
echo('Maybe you need to copy config/test-template.php to config/test.php and update it?.' . "\n");
exit(1);
}
/* Load the configuration file. */
require_once($configFile);
/**
* This function creates a curl handle and initializes it.
*
* @return A curl handler.
*/
function curlCreate() {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
return $ch;
}
/**
* This function requests a url with a GET request.
*
* @param $curl The curl handle which should be used.
* @param $url The url which should be requested.
* @param $parameters Associative array with parameters which should be appended to the url.
* @return The content of the returned page.
*/
function urlGet($curl, $url, $parameters = array()) {
$p = '';
foreach($parameters as $k => $v) {
if($p != '') {
$p .= '&';
}
$p .= urlencode($k) . '=' . urlencode($v);
}
if(strpos($url, '?') === FALSE) {
$url .= '?' . $p;
} else {
$url .= '&' . $p;
}
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
curl_setopt($curl, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($curl);
if($curl_scraped_page === FALSE) {
echo('Failed to get url: ' . $url . "\n");
echo('Curl error: ' . curl_error($curl) . "\n");
return FALSE;
}
return $curl_scraped_page;
}
/**
* This function posts data to a specific url.
*
* @param $curl The curl handle which should be used for the request.
* @param $url The url the POST request should be directed to.
* @param $post Associative array with the post parameters.
* $return The returned page.
*/
function urlPost($curl, $url, $post) {
$postparams = '';
foreach($post as $k => $v) {
if($postparams != '') {
$postparams .= '&';
}
$postparams .= urlencode($k) . '=' . urlencode($v);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $postparams);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_URL, $url);
$curl_scraped_page = curl_exec($curl);
if($curl_scraped_page === FALSE) {
echo('Failed to get url: ' . $url . "\n");
echo('Curl error: ' . curl_error($curl) . "\n");
return FALSE;
}
return $curl_scraped_page;
}
/**
* This function parses a simpleSAMLphp HTTP-REDIRECT debug page.
*
* @param $page The content of the page.
* @return FALSE if $page isn't a HTTP-REDIRECT debug page, destination url if it is.
*/
function parseSimpleSamlHttpRedirectDebug($page) {
if(strpos($page, '<h2>Sending a SAML message using HTTP-REDIRECT</h2>') === FALSE) {
return FALSE;
}
if(!preg_match('/<a id="sendlink" href="([^"]*)">send SAML message<\\/a>/', $page, $matches)) {
echo('Invalid simpleSAMLphp debug page. Missing link.' . "\n");
return FALSE;
}
$url = $matches[1];
$url = html_entity_decode($url);
return $url;
}
/**
* This function parses a simpleSAMLphp HTTP-POST page.
*
* @param $page The content of the page.
* @return FALSE if $page isn't a HTTP-POST page. If it is a HTTP-POST page, it will return an associative array with
* the post destination in 'url' and the post arguments as an associative array in 'post'.
*/
function parseSimpleSamlHttpPost($page) {
if(strpos($page, '<title>SAML 2.0 POST</title>') === FALSE
&& strpos($page, '<title>SAML Response Debug-mode</title>') === FALSE
&& strpos($page, '<title>SAML (Shibboleth 1.3) Response Debug-mode</title>') === FALSE) {
return FALSE;
}
if(!preg_match('/<form method="post" action="([^"]*)">/', $page, $matches)) {
echo('Invalid simpleSAMLphp HTTP-POST page. Missing form target.' . "\n");
return FALSE;
}
$url = html_entity_decode($matches[1]);
$params = array();
if(!preg_match('/<input type="hidden" name="SAMLResponse" value="([^"]*)" \\/>/', $page, $matches)) {
echo('Invalid simpleSAMLphp HTTP-POST page. Missing SAMLResponse.' . "\n");
return FALSE;
}
$params['SAMLResponse'] = html_entity_decode($matches[1]);
if(preg_match('/<input type="hidden" name="RelayState" value="([^"]*)" \\/>/', $page, $matches)) {
$params['RelayState'] = html_entity_decode($matches[1]);
}
if(preg_match('/<input type="hidden" name="TARGET" value="([^"]*)" \\/>/', $page, $matches)) {
$params['TARGET'] = html_entity_decode($matches[1]);
}
return array('url' => $url, 'post' => $params);
}
/**
* This function parses a simpleSAMLphp HTTP-POST debug page.
*
* @param $page The content of the page.
* @return FALSE if $page isn't a HTTP-POST page. If it is a HTTP-POST page, it will return an associative array with
* the post destination in 'url' and the post arguments as an associative array in 'post'.
*/
function parseSimpleSamlHttpPostDebug($page) {
if(strpos($page, '<title>SAML Response Debug-mode</title>') === FALSE) {
return FALSE;
}
if(!preg_match('/<form method="post" action="([^"]*)">/', $page, $matches)) {
echo('Invalid simpleSAMLphp HTTP-POST page. Missing form target.' . "\n");
return FALSE;
}
$url = html_entity_decode($matches[1]);
if(!preg_match('/<input type="hidden" name="SAMLResponse" value="([^"]*)" \\/>/', $page, $matches)) {
echo('Invalid simpleSAMLphp HTTP-POST page. Missing SAMLResponse.' . "\n");
return FALSE;
}
$samlResponse = html_entity_decode($matches[1]);
if(!preg_match('/<input type="hidden" name="RelayState" value="([^"]*)" \\/>/', $page, $matches)) {
echo('Invalid simpleSAMLphp HTTP-POST page. Missing RelayState.' . "\n");
return FALSE;
}
$relayState = html_entity_decode($matches[1]);
return array('url' => $url, 'post' => array('SAMLResponse' => $samlResponse, 'RelayState' => $relayState));
}
/**
* This function parses a simpleSAMLphp login page.
*
* @param $curl The curl handle the page was fetched with.
* @param $page The content of the login page.
* @return FALSE if $page isn't a login page, associative array with the destination url in 'url' and the relaystate in 'relaystate'.
*/
function parseSimpleSamlLoginPage($curl, $page) {
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
$pos = strpos($url, '?');
if($pos === FALSE) {
echo('Unexpected login page url: ' . $url);
return FALSE;
}
$url = substr($url, 0, $pos + 1);
if(!preg_match('/<input type="hidden" name="RelayState" value="([^"]*)" \\/>/', $page, $matches)) {
echo('Could not find relaystate in simpleSAMLphp login page.' . "\n");
return FALSE;
}
$relaystate = $matches[1];
$relaystate = html_entity_decode($relaystate);
return array('url' => $url, 'relaystate' => $relaystate);
}
/**
* This function parses a FEIDE login page.
*
* @param $curl The curl handle the page was fetched with.
* @param $page The content of the login page.
* @return FALSE if $page isn't a login page, associative array with the destination url in 'url' and the goto attribute in 'goto'.
*/
function parseFeideLoginPage($curl, $page) {
if(strpos($page, '<title> Moria-innlogging </title>') === FALSE) {
echo('Not a FEIDE login page.' . "\n");
return FALSE;
}
$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
$pos = strpos($url, '/amserver/UI/Login');
if($pos === FALSE) {
echo('Unexpected login page url: ' . $url);
return FALSE;
}
$url = substr($url, 0, $pos) . '/amserver/UI/Login';
if(!preg_match('/<input type="hidden" name="goto" value="([^"]*)"\\/>/', $page, $matches)) {
echo('Could not find goto in FEIDE login page.' . "\n");
return FALSE;
}
$goto = $matches[1];
$goto = html_entity_decode($goto);
return array('url' => $url, 'goto' => $goto);
}
/**
* This function parses the FEIDE HTTP-POST page.
*
* @param $page The content of the page.
* @return FALSE if $page isn't a HTTP-POST page. If it is a HTTP-POST page, it will return an associative array with
* the post destination in 'url' and the post arguments as an associative array in 'post'.
*/
function parseFeideHttpPost($page) {
if(strpos($page, '<TITLE>Access rights validated</TITLE>') === FALSE) {
return FALSE;
}
if(!preg_match('/<FORM METHOD="POST" ACTION="([^"]*)">/', $page, $matches)) {
echo('Invalid FEIDE HTTP-POST page. Missing form target.' . "\n");
return FALSE;
}
$url = html_entity_decode($matches[1]);
if(!preg_match('/<INPUT TYPE="HIDDEN" NAME="SAMLResponse" VALUE="([^"]*)">/m', $page, $matches)) {
echo('Invalid FEIDE HTTP-POST page. Missing SAMLResponse.' . "\n");
return FALSE;
}
$samlResponse = html_entity_decode($matches[1]);
if(!preg_match('/<INPUT TYPE="HIDDEN" NAME="RelayState" VALUE="([^"]*)">/', $page, $matches)) {
echo('Invalid FEIDE HTTP-POST page. Missing RelayState.' . "\n");
return FALSE;
}
$relayState = html_entity_decode($matches[1]);
return array('url' => $url, 'post' => array('SAMLResponse' => $samlResponse, 'RelayState' => $relayState));
}
/**
* This function handles simpleSAMLphp debug pages, and follows redirects in them.
*
* @param $curl The curl handle we should use.
* @param $page The page which may be a simpleSAMLphp debug page. $page may be FALSE, in which case this function
* will return FALSE.
* @return $page if $page isn't a debug page, or the result from following the redirect if not.
* FALSE will be returned on failure.
*/
function skipDebugPage($curl, $page) {
if($page === FALSE) {
return FALSE;
}
$url = parseSimpleSamlHttpRedirectDebug($page);
if($url !== FALSE) {
$page = urlGet($curl, $url);
}
return $page;
}
/**
* This function contacts the test page to initialize SSO.
*
* @param $test The test we are running.
* @param $curl The curl handle we should use.
* @return TRUE on success, FALSE on failure.
*/
function initSSO($test, $curl) {
if(!array_key_exists('url', $test)) {
echo('Missing required attribute url in test.' . "\n");
return FALSE;
}
$params = array('op' => 'login');
if(array_key_exists('idp', $test)) {
$params['idp'] = $test['idp'];
}
/* Add the protocol which simpleSAMLphp should use to authenticate. */
if(array_key_exists('protocol', $test)) {
$params['protocol'] = $test['protocol'];
}
/* Add attribute tests. */
if(array_key_exists('attributes', $test)) {
$i = 0;
foreach($test['attributes'] as $name => $values) {
if(!is_array($values)) {
$values = array($values);
}
foreach($values as $value) {
$params['attr_test_' . $i] = $name . ':' . $value;
$i++;
}
}
}
echo('Initializing SSO.' . "\n");
$loginPage = urlGet($curl, $test['url'], $params);
if($loginPage === FALSE) {
echo('Failed to initialize SSO.' . "\n");
return FALSE;
}
/* Skip HTTP-REDIRECT debug page if it appears. */
$loginPage = skipDebugPage($curl, $loginPage);
return $loginPage;
}
/**
* This function handles login to a simpleSAMLphp login page.
*
* @param $test The current test.
* @param $curl The curl handle in use.
* @param $page The login page.
* @return FALSE on failure, or the resulting page on success.
*/
function doSimpleSamlLogin($test, $curl, $page) {
if(!array_key_exists('username', $test)) {
echo('Missing username in test.' . "\n");
return FALSE;
}
if(!array_key_exists('password', $test)) {
echo('Missing password in test.' . "\n");
return FALSE;
}
$info = parseSimpleSamlLoginPage($curl, $page);
if($info === FALSE) {
return FALSE;
}
$post = array();
$post['username'] = $test['username'];
$post['password'] = $test['password'];
$post['RelayState'] = $info['relaystate'];
$page = urlPost($curl, $info['url'], $post);
/* Follow HTTP-POST redirect. */
$pi = parseSimpleSamlHttpPost($page);
if($pi === FALSE) {
echo($page);
echo('Didn\'t get a simpleSAMLphp post redirect page.' . "\n");
return FALSE;
}
$page = urlPost($curl, $pi['url'], $pi['post']);
return $page;
}
/**
* This function handles login to the FEIDE login page.
*
* @param $test The current test.
* @param $curl The curl handle in use.
* @param $page The login page.
* @return FALSE on failure, or the resulting page on success.
*/
function doFeideLogin($test, $curl, $page) {
if(!array_key_exists('username', $test)) {
echo('Missing username in test.' . "\n");
return FALSE;
}
if(!array_key_exists('password', $test)) {
echo('Missing password in test.' . "\n");
return FALSE;
}
if(!array_key_exists('organization', $test)) {
echo('Missing organization in test.' . "\n");
return FALSE;
}
$info = parseFeideLoginPage($curl, $page);
if($info === FALSE) {
return FALSE;
}
$post = array();
$post['username'] = $test['username'];
$post['password'] = $test['password'];
$post['organization'] = $test['organization'];
$post['goto'] = $info['goto'];
$page = urlPost($curl, $info['url'], $post);
/* Follow HTTP-POST redirect. */
$pi = parseFeideHttpPost($page);
if($pi === FALSE) {
echo('Unable to parse FEIDE HTTP-POST redirect page.' . "\n");
return FALSE;
}
$page = urlPost($curl, $pi['url'], $pi['post']);
return $page;
}
/**
* This function logs in using the configuration of the given test.
*
* @param $test The current test.
* @param $curl The curl handle in use.
* @param $page The login page.
* @return FALSE on failure, or the resulting page on success.
*/
function doLogin($test, $curl, $page) {
if(!array_key_exists('logintype', $test)) {
echo('Missing option \'logintype\' in test configuration.' . "\n");
return FALSE;
}
switch($test['logintype']) {
case 'simplesaml':
return doSimpleSamlLogin($test, $curl, $page);
case 'feide':
return doFeideLogin($test, $curl, $page);
default:
echo('Unknown login type: ' . $test['logintype'] . "\n");
echo($page);
return FALSE;
}
}
/**
* This function contacts the test page to initialize SSO.
*
* @param $test The test we are running.
* @param $curl The curl handle we should use.
* @return TRUE on success, FALSE on failure.
*/
function doLogout($test, $curl) {
if(!array_key_exists('url', $test)) {
echo('Missing required attribute url in test.' . "\n");
return FALSE;
}
$params = array('op' => 'logout');
$page = urlGet($curl, $test['url'], $params);
if($page === FALSE) {
echo('Failed to log out.' . "\n");
return FALSE;
}
/* Skip HTTP-REDIRECT debug pagess. */
while(TRUE) {
$newPage = skipDebugPage($curl, $page);
if($newPage === $page) {
break;
}
$page = $newPage;
}
return $page;
}
/**
* This function runs the specified test.
*
* @param $test Associative array with the test parameters.
* @return TRUE on success, FALSE on failure.
*/
function doTest($test) {
$curl = curlCreate();
$res = TRUE;
/* Initialize SSO. */
do {
$loginPage = initSSO($test, $curl);
if($loginPage === FALSE) {
$res = FALSE;
break;
}
echo('Logging in.' . "\n");
$result = doLogin($test, $curl, $loginPage);
if($result !== "OK") {
if(is_string($result)) {
echo('Failed to log in. Result from SP: ' . $result . "\n");
} else {
echo('Failed to log in.' . "\n");
}
$res = FALSE;
break;
}
echo('Logged in, attributes OK' . "\n");
if(array_key_exists('protocol', $test) && $test['protocol'] === 'shib13') {
echo('Shib13: Logout not implemented.' . "\n");
break;
}
echo('Logging out.' . "\n");
$result = doLogout($test, $curl);
if($result !== "OK") {
if(is_string($result)) {
echo('Failed to log out. Result from SP: ' . $result . "\n");
} else {
echo('Failed to log out.' . "\n");
}
$res = FALSE;
break;
}
echo('Logged out.' . "\n");
} while(0);
curl_close($curl);
return $res;
}
$ret = 0;
/* Run the tests. */
foreach($tests as $i => $test) {
echo('############################################################' . "\n");
echo('Running test #' . ($i + 1) . '.' . "\n");
$res = doTest($test);
if($res === FALSE) {
$ret = 1;
echo('Test #' . ($i + 1) . ' failed.' . "\n");
} else {
echo('Test #' . ($i + 1) . ' succeeded.' . "\n");
}
}
echo('############################################################' . "\n");
exit($ret);
?>