This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
Client.php
executable file
·3888 lines (3549 loc) · 134 KB
/
Client.php
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* PHP Version 5
*
* @file CAS/Client.php
* @category Authentication
* @package PhpCAS
* @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
* @author Olivier Berger <olivier.berger@it-sudparis.eu>
* @author Brett Bieber <brett.bieber@gmail.com>
* @author Joachim Fritschi <jfritschi@freenet.de>
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
/**
* The CAS_Client class is a client interface that provides CAS authentication
* to PHP applications.
*
* @class CAS_Client
* @category Authentication
* @package PhpCAS
* @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
* @author Olivier Berger <olivier.berger@it-sudparis.eu>
* @author Brett Bieber <brett.bieber@gmail.com>
* @author Joachim Fritschi <jfritschi@freenet.de>
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*
*/
class CAS_Client
{
// ########################################################################
// HTML OUTPUT
// ########################################################################
/**
* @addtogroup internalOutput
* @{
*/
/**
* This method filters a string by replacing special tokens by appropriate values
* and prints it. The corresponding tokens are taken into account:
* - __CAS_VERSION__
* - __PHPCAS_VERSION__
* - __SERVER_BASE_URL__
*
* Used by CAS_Client::PrintHTMLHeader() and CAS_Client::printHTMLFooter().
*
* @param string $str the string to filter and output
*
* @return void
*/
private function _htmlFilterOutput($str)
{
$str = str_replace('__CAS_VERSION__', $this->getServerVersion(), $str);
$str = str_replace('__PHPCAS_VERSION__', phpCAS::getVersion(), $str);
$str = str_replace('__SERVER_BASE_URL__', $this->_getServerBaseURL(), $str);
echo $str;
}
/**
* A string used to print the header of HTML pages. Written by
* CAS_Client::setHTMLHeader(), read by CAS_Client::printHTMLHeader().
*
* @hideinitializer
* @see CAS_Client::setHTMLHeader, CAS_Client::printHTMLHeader()
*/
private $_output_header = '';
/**
* This method prints the header of the HTML output (after filtering). If
* CAS_Client::setHTMLHeader() was not used, a default header is output.
*
* @param string $title the title of the page
*
* @return void
* @see _htmlFilterOutput()
*/
public function printHTMLHeader($title)
{
$this->_htmlFilterOutput(
str_replace(
'__TITLE__', $title,
(empty($this->_output_header)
? '<html><head><title>__TITLE__</title></head><body><h1>__TITLE__</h1>'
: $this->_output_header)
)
);
}
/**
* A string used to print the footer of HTML pages. Written by
* CAS_Client::setHTMLFooter(), read by printHTMLFooter().
*
* @hideinitializer
* @see CAS_Client::setHTMLFooter, CAS_Client::printHTMLFooter()
*/
private $_output_footer = '';
/**
* This method prints the footer of the HTML output (after filtering). If
* CAS_Client::setHTMLFooter() was not used, a default footer is output.
*
* @return void
* @see _htmlFilterOutput()
*/
public function printHTMLFooter()
{
$lang = $this->getLangObj();
$this->_htmlFilterOutput(
empty($this->_output_footer)?
('<hr><address>phpCAS __PHPCAS_VERSION__ '
.$lang->getUsingServer()
.' <a href="__SERVER_BASE_URL__">__SERVER_BASE_URL__</a> (CAS __CAS_VERSION__)</a></address></body></html>')
:$this->_output_footer
);
}
/**
* This method set the HTML header used for all outputs.
*
* @param string $header the HTML header.
*
* @return void
*/
public function setHTMLHeader($header)
{
// Argument Validation
if (gettype($header) != 'string')
throw new CAS_TypeMismatchException($header, '$header', 'string');
$this->_output_header = $header;
}
/**
* This method set the HTML footer used for all outputs.
*
* @param string $footer the HTML footer.
*
* @return void
*/
public function setHTMLFooter($footer)
{
// Argument Validation
if (gettype($footer) != 'string')
throw new CAS_TypeMismatchException($footer, '$footer', 'string');
$this->_output_footer = $footer;
}
/** @} */
// ########################################################################
// INTERNATIONALIZATION
// ########################################################################
/**
* @addtogroup internalLang
* @{
*/
/**
* A string corresponding to the language used by phpCAS. Written by
* CAS_Client::setLang(), read by CAS_Client::getLang().
* @note debugging information is always in english (debug purposes only).
*/
private $_lang = PHPCAS_LANG_DEFAULT;
/**
* This method is used to set the language used by phpCAS.
*
* @param string $lang representing the language.
*
* @return void
*/
public function setLang($lang)
{
// Argument Validation
if (gettype($lang) != 'string')
throw new CAS_TypeMismatchException($lang, '$lang', 'string');
phpCAS::traceBegin();
$obj = new $lang();
if (!($obj instanceof CAS_Languages_LanguageInterface)) {
throw new CAS_InvalidArgumentException(
'$className must implement the CAS_Languages_LanguageInterface'
);
}
$this->_lang = $lang;
phpCAS::traceEnd();
}
/**
* Create the language
*
* @return CAS_Languages_LanguageInterface object implementing the class
*/
public function getLangObj()
{
$classname = $this->_lang;
return new $classname();
}
/** @} */
// ########################################################################
// CAS SERVER CONFIG
// ########################################################################
/**
* @addtogroup internalConfig
* @{
*/
/**
* a record to store information about the CAS server.
* - $_server['version']: the version of the CAS server
* - $_server['hostname']: the hostname of the CAS server
* - $_server['port']: the port the CAS server is running on
* - $_server['uri']: the base URI the CAS server is responding on
* - $_server['base_url']: the base URL of the CAS server
* - $_server['login_url']: the login URL of the CAS server
* - $_server['service_validate_url']: the service validating URL of the
* CAS server
* - $_server['proxy_url']: the proxy URL of the CAS server
* - $_server['proxy_validate_url']: the proxy validating URL of the CAS server
* - $_server['logout_url']: the logout URL of the CAS server
*
* $_server['version'], $_server['hostname'], $_server['port'] and
* $_server['uri'] are written by CAS_Client::CAS_Client(), read by
* CAS_Client::getServerVersion(), CAS_Client::_getServerHostname(),
* CAS_Client::_getServerPort() and CAS_Client::_getServerURI().
*
* The other fields are written and read by CAS_Client::_getServerBaseURL(),
* CAS_Client::getServerLoginURL(), CAS_Client::getServerServiceValidateURL(),
* CAS_Client::getServerProxyValidateURL() and CAS_Client::getServerLogoutURL().
*
* @hideinitializer
*/
private $_server = array(
'version' => -1,
'hostname' => 'none',
'port' => -1,
'uri' => 'none');
/**
* This method is used to retrieve the version of the CAS server.
*
* @return string the version of the CAS server.
*/
public function getServerVersion()
{
return $this->_server['version'];
}
/**
* This method is used to retrieve the hostname of the CAS server.
*
* @return string the hostname of the CAS server.
*/
private function _getServerHostname()
{
return $this->_server['hostname'];
}
/**
* This method is used to retrieve the port of the CAS server.
*
* @return string the port of the CAS server.
*/
private function _getServerPort()
{
return $this->_server['port'];
}
/**
* This method is used to retrieve the URI of the CAS server.
*
* @return string a URI.
*/
private function _getServerURI()
{
return $this->_server['uri'];
}
/**
* This method is used to retrieve the base URL of the CAS server.
*
* @return string a URL.
*/
private function _getServerBaseURL()
{
// the URL is build only when needed
if ( empty($this->_server['base_url']) ) {
$this->_server['base_url'] = 'https://' . $this->_getServerHostname();
if ($this->_getServerPort()!=443) {
$this->_server['base_url'] .= ':'
.$this->_getServerPort();
}
$this->_server['base_url'] .= $this->_getServerURI();
}
return $this->_server['base_url'];
}
/**
* This method is used to retrieve the login URL of the CAS server.
*
* @param bool $gateway true to check authentication, false to force it
* @param bool $renew true to force the authentication with the CAS server
*
* @return a URL.
* @note It is recommended that CAS implementations ignore the "gateway"
* parameter if "renew" is set
*/
public function getServerLoginURL($gateway=false,$renew=false)
{
phpCAS::traceBegin();
// the URL is build only when needed
if ( empty($this->_server['login_url']) ) {
$this->_server['login_url'] = $this->_getServerBaseURL();
$this->_server['login_url'] .= 'login?service=';
$this->_server['login_url'] .= urlencode($this->getURL());
}
$url = $this->_server['login_url'];
if ($renew) {
// It is recommended that when the "renew" parameter is set, its
// value be "true"
$url = $this->_buildQueryUrl($url, 'renew=true');
} elseif ($gateway) {
// It is recommended that when the "gateway" parameter is set, its
// value be "true"
$url = $this->_buildQueryUrl($url, 'gateway=true');
}
phpCAS::traceEnd($url);
return $url;
}
/**
* This method sets the login URL of the CAS server.
*
* @param string $url the login URL
*
* @return string login url
*/
public function setServerLoginURL($url)
{
// Argument Validation
if (gettype($url) != 'string')
throw new CAS_TypeMismatchException($url, '$url', 'string');
return $this->_server['login_url'] = $url;
}
/**
* This method sets the serviceValidate URL of the CAS server.
*
* @param string $url the serviceValidate URL
*
* @return string serviceValidate URL
*/
public function setServerServiceValidateURL($url)
{
// Argument Validation
if (gettype($url) != 'string')
throw new CAS_TypeMismatchException($url, '$url', 'string');
return $this->_server['service_validate_url'] = $url;
}
/**
* This method sets the proxyValidate URL of the CAS server.
*
* @param string $url the proxyValidate URL
*
* @return string proxyValidate URL
*/
public function setServerProxyValidateURL($url)
{
// Argument Validation
if (gettype($url) != 'string')
throw new CAS_TypeMismatchException($url, '$url', 'string');
return $this->_server['proxy_validate_url'] = $url;
}
/**
* This method sets the samlValidate URL of the CAS server.
*
* @param string $url the samlValidate URL
*
* @return string samlValidate URL
*/
public function setServerSamlValidateURL($url)
{
// Argument Validation
if (gettype($url) != 'string')
throw new CAS_TypeMismatchException($url, '$url', 'string');
return $this->_server['saml_validate_url'] = $url;
}
/**
* This method is used to retrieve the service validating URL of the CAS server.
*
* @return string serviceValidate URL.
*/
public function getServerServiceValidateURL()
{
phpCAS::traceBegin();
// the URL is build only when needed
if ( empty($this->_server['service_validate_url']) ) {
switch ($this->getServerVersion()) {
case CAS_VERSION_1_0:
$this->_server['service_validate_url'] = $this->_getServerBaseURL()
.'validate';
break;
case CAS_VERSION_2_0:
$this->_server['service_validate_url'] = $this->_getServerBaseURL()
.'serviceValidate';
break;
case CAS_VERSION_3_0:
$this->_server['service_validate_url'] = $this->_getServerBaseURL()
.'p3/serviceValidate';
break;
}
}
$url = $this->_buildQueryUrl(
$this->_server['service_validate_url'],
'service='.urlencode($this->getURL())
);
phpCAS::traceEnd($url);
return $url;
}
/**
* This method is used to retrieve the SAML validating URL of the CAS server.
*
* @return string samlValidate URL.
*/
public function getServerSamlValidateURL()
{
phpCAS::traceBegin();
// the URL is build only when needed
if ( empty($this->_server['saml_validate_url']) ) {
switch ($this->getServerVersion()) {
case SAML_VERSION_1_1:
$this->_server['saml_validate_url'] = $this->_getServerBaseURL().'samlValidate';
break;
}
}
$url = $this->_buildQueryUrl(
$this->_server['saml_validate_url'],
'TARGET='.urlencode($this->getURL())
);
phpCAS::traceEnd($url);
return $url;
}
/**
* This method is used to retrieve the proxy validating URL of the CAS server.
*
* @return string proxyValidate URL.
*/
public function getServerProxyValidateURL()
{
phpCAS::traceBegin();
// the URL is build only when needed
if ( empty($this->_server['proxy_validate_url']) ) {
switch ($this->getServerVersion()) {
case CAS_VERSION_1_0:
$this->_server['proxy_validate_url'] = '';
break;
case CAS_VERSION_2_0:
$this->_server['proxy_validate_url'] = $this->_getServerBaseURL().'proxyValidate';
break;
case CAS_VERSION_3_0:
$this->_server['proxy_validate_url'] = $this->_getServerBaseURL().'p3/proxyValidate';
break;
}
}
$url = $this->_buildQueryUrl(
$this->_server['proxy_validate_url'],
'service='.urlencode($this->getURL())
);
phpCAS::traceEnd($url);
return $url;
}
/**
* This method is used to retrieve the proxy URL of the CAS server.
*
* @return string proxy URL.
*/
public function getServerProxyURL()
{
// the URL is build only when needed
if ( empty($this->_server['proxy_url']) ) {
switch ($this->getServerVersion()) {
case CAS_VERSION_1_0:
$this->_server['proxy_url'] = '';
break;
case CAS_VERSION_2_0:
case CAS_VERSION_3_0:
$this->_server['proxy_url'] = $this->_getServerBaseURL().'proxy';
break;
}
}
return $this->_server['proxy_url'];
}
/**
* This method is used to retrieve the logout URL of the CAS server.
*
* @return string logout URL.
*/
public function getServerLogoutURL()
{
// the URL is build only when needed
if ( empty($this->_server['logout_url']) ) {
$this->_server['logout_url'] = $this->_getServerBaseURL().'logout';
}
return $this->_server['logout_url'];
}
/**
* This method sets the logout URL of the CAS server.
*
* @param string $url the logout URL
*
* @return string logout url
*/
public function setServerLogoutURL($url)
{
// Argument Validation
if (gettype($url) != 'string')
throw new CAS_TypeMismatchException($url, '$url', 'string');
return $this->_server['logout_url'] = $url;
}
/**
* An array to store extra curl options.
*/
private $_curl_options = array();
/**
* This method is used to set additional user curl options.
*
* @param string $key name of the curl option
* @param string $value value of the curl option
*
* @return void
*/
public function setExtraCurlOption($key, $value)
{
$this->_curl_options[$key] = $value;
}
/** @} */
// ########################################################################
// Change the internal behaviour of phpcas
// ########################################################################
/**
* @addtogroup internalBehave
* @{
*/
/**
* The class to instantiate for making web requests in readUrl().
* The class specified must implement the CAS_Request_RequestInterface.
* By default CAS_Request_CurlRequest is used, but this may be overridden to
* supply alternate request mechanisms for testing.
*/
private $_requestImplementation = 'CAS_Request_CurlRequest';
/**
* Override the default implementation used to make web requests in readUrl().
* This class must implement the CAS_Request_RequestInterface.
*
* @param string $className name of the RequestImplementation class
*
* @return void
*/
public function setRequestImplementation ($className)
{
$obj = new $className;
if (!($obj instanceof CAS_Request_RequestInterface)) {
throw new CAS_InvalidArgumentException(
'$className must implement the CAS_Request_RequestInterface'
);
}
$this->_requestImplementation = $className;
}
/**
* @var boolean $_clearTicketsFromUrl; If true, phpCAS will clear session
* tickets from the URL after a successful authentication.
*/
private $_clearTicketsFromUrl = true;
/**
* Configure the client to not send redirect headers and call exit() on
* authentication success. The normal redirect is used to remove the service
* ticket from the client's URL, but for running unit tests we need to
* continue without exiting.
*
* Needed for testing authentication
*
* @return void
*/
public function setNoClearTicketsFromUrl ()
{
$this->_clearTicketsFromUrl = false;
}
/**
* @var callback $_postAuthenticateCallbackFunction;
*/
private $_postAuthenticateCallbackFunction = null;
/**
* @var array $_postAuthenticateCallbackArgs;
*/
private $_postAuthenticateCallbackArgs = array();
/**
* Set a callback function to be run when a user authenticates.
*
* The callback function will be passed a $logoutTicket as its first parameter,
* followed by any $additionalArgs you pass. The $logoutTicket parameter is an
* opaque string that can be used to map a session-id to the logout request
* in order to support single-signout in applications that manage their own
* sessions (rather than letting phpCAS start the session).
*
* phpCAS::forceAuthentication() will always exit and forward client unless
* they are already authenticated. To perform an action at the moment the user
* logs in (such as registering an account, performing logging, etc), register
* a callback function here.
*
* @param string $function callback function to call
* @param array $additionalArgs optional array of arguments
*
* @return void
*/
public function setPostAuthenticateCallback ($function, array $additionalArgs = array())
{
$this->_postAuthenticateCallbackFunction = $function;
$this->_postAuthenticateCallbackArgs = $additionalArgs;
}
/**
* @var callback $_signoutCallbackFunction;
*/
private $_signoutCallbackFunction = null;
/**
* @var array $_signoutCallbackArgs;
*/
private $_signoutCallbackArgs = array();
/**
* Set a callback function to be run when a single-signout request is received.
*
* The callback function will be passed a $logoutTicket as its first parameter,
* followed by any $additionalArgs you pass. The $logoutTicket parameter is an
* opaque string that can be used to map a session-id to the logout request in
* order to support single-signout in applications that manage their own sessions
* (rather than letting phpCAS start and destroy the session).
*
* @param string $function callback function to call
* @param array $additionalArgs optional array of arguments
*
* @return void
*/
public function setSingleSignoutCallback ($function, array $additionalArgs = array())
{
$this->_signoutCallbackFunction = $function;
$this->_signoutCallbackArgs = $additionalArgs;
}
// ########################################################################
// Methods for supplying code-flow feedback to integrators.
// ########################################################################
/**
* Ensure that this is actually a proxy object or fail with an exception
*
* @throws CAS_OutOfSequenceProxyException
*
* @return void
*/
public function ensureIsProxy()
{
if (!$this->isProxy()) {
throw new CAS_OutOfSequenceProxyException();
}
}
/**
* Mark the caller of authentication. This will help client integraters determine
* problems with their code flow if they call a function such as getUser() before
* authentication has occurred.
*
* @param bool $auth True if authentication was successful, false otherwise.
*
* @return null
*/
public function markAuthenticationCall ($auth)
{
// store where the authentication has been checked and the result
$dbg = debug_backtrace();
$this->_authentication_caller = array (
'file' => $dbg[1]['file'],
'line' => $dbg[1]['line'],
'method' => $dbg[1]['class'] . '::' . $dbg[1]['function'],
'result' => (boolean)$auth
);
}
private $_authentication_caller;
/**
* Answer true if authentication has been checked.
*
* @return bool
*/
public function wasAuthenticationCalled ()
{
return !empty($this->_authentication_caller);
}
/**
* Ensure that authentication was checked. Terminate with exception if no
* authentication was performed
*
* @throws CAS_OutOfSequenceBeforeAuthenticationCallException
*
* @return void
*/
private function _ensureAuthenticationCalled()
{
if (!$this->wasAuthenticationCalled()) {
throw new CAS_OutOfSequenceBeforeAuthenticationCallException();
}
}
/**
* Answer the result of the authentication call.
*
* Throws a CAS_OutOfSequenceException if wasAuthenticationCalled() is false
* and markAuthenticationCall() didn't happen.
*
* @return bool
*/
public function wasAuthenticationCallSuccessful ()
{
$this->_ensureAuthenticationCalled();
return $this->_authentication_caller['result'];
}
/**
* Ensure that authentication was checked. Terminate with exception if no
* authentication was performed
*
* @throws CAS_OutOfSequenceBeforeAuthenticationCallException
*
* @return void
*/
public function ensureAuthenticationCallSuccessful()
{
$this->_ensureAuthenticationCalled();
if (!$this->_authentication_caller['result']) {
throw new CAS_OutOfSequenceException(
'authentication was checked (by '
. $this->getAuthenticationCallerMethod()
. '() at ' . $this->getAuthenticationCallerFile()
. ':' . $this->getAuthenticationCallerLine()
. ') but the method returned false'
);
}
}
/**
* Answer information about the authentication caller.
*
* Throws a CAS_OutOfSequenceException if wasAuthenticationCalled() is false
* and markAuthenticationCall() didn't happen.
*
* @return array Keys are 'file', 'line', and 'method'
*/
public function getAuthenticationCallerFile ()
{
$this->_ensureAuthenticationCalled();
return $this->_authentication_caller['file'];
}
/**
* Answer information about the authentication caller.
*
* Throws a CAS_OutOfSequenceException if wasAuthenticationCalled() is false
* and markAuthenticationCall() didn't happen.
*
* @return array Keys are 'file', 'line', and 'method'
*/
public function getAuthenticationCallerLine ()
{
$this->_ensureAuthenticationCalled();
return $this->_authentication_caller['line'];
}
/**
* Answer information about the authentication caller.
*
* Throws a CAS_OutOfSequenceException if wasAuthenticationCalled() is false
* and markAuthenticationCall() didn't happen.
*
* @return array Keys are 'file', 'line', and 'method'
*/
public function getAuthenticationCallerMethod ()
{
$this->_ensureAuthenticationCalled();
return $this->_authentication_caller['method'];
}
/** @} */
// ########################################################################
// CONSTRUCTOR
// ########################################################################
/**
* @addtogroup internalConfig
* @{
*/
/**
* CAS_Client constructor.
*
* @param string $server_version the version of the CAS server
* @param bool $proxy true if the CAS client is a CAS proxy
* @param string $server_hostname the hostname of the CAS server
* @param int $server_port the port the CAS server is running on
* @param string $server_uri the URI the CAS server is responding on
* @param bool $changeSessionID Allow phpCAS to change the session_id
* (Single Sign Out/handleLogoutRequests
* is based on that change)
*
* @return a newly created CAS_Client object
*/
public function __construct(
$server_version,
$proxy,
$server_hostname,
$server_port,
$server_uri,
$changeSessionID = true
) {
// Argument validation
if (gettype($server_version) != 'string')
throw new CAS_TypeMismatchException($server_version, '$server_version', 'string');
if (gettype($proxy) != 'boolean')
throw new CAS_TypeMismatchException($proxy, '$proxy', 'boolean');
if (gettype($server_hostname) != 'string')
throw new CAS_TypeMismatchException($server_hostname, '$server_hostname', 'string');
if (gettype($server_port) != 'integer')
throw new CAS_raTypeMismatchException($server_port, '$server_port', 'integer');
if (gettype($server_uri) != 'string')
throw new CAS_TypeMismatchException($server_uri, '$server_uri', 'string');
if (gettype($changeSessionID) != 'boolean')
throw new CAS_TypeMismatchException($changeSessionID, '$changeSessionID', 'boolean');
phpCAS::traceBegin();
// true : allow to change the session_id(), false session_id won't be
// change and logout won't be handle because of that
$this->_setChangeSessionID($changeSessionID);
// skip Session Handling for logout requests and if don't want it'
if (session_id()=="" && !$this->_isLogoutRequest()) {
session_start();
phpCAS :: trace("Starting a new session " . session_id());
}
// are we in proxy mode ?
$this->_proxy = $proxy;
// Make cookie handling available.
if ($this->isProxy()) {
if (!isset($_SESSION['phpCAS'])) {
$_SESSION['phpCAS'] = array();
}
if (!isset($_SESSION['phpCAS']['service_cookies'])) {
$_SESSION['phpCAS']['service_cookies'] = array();
}
$this->_serviceCookieJar = new CAS_CookieJar(
$_SESSION['phpCAS']['service_cookies']
);
}
//check version
switch ($server_version) {
case CAS_VERSION_1_0:
if ( $this->isProxy() ) {
phpCAS::error(
'CAS proxies are not supported in CAS '.$server_version
);
}
break;
case CAS_VERSION_2_0:
case CAS_VERSION_3_0:
break;
case SAML_VERSION_1_1:
break;
default:
phpCAS::error(
'this version of CAS (`'.$server_version
.'\') is not supported by phpCAS '.phpCAS::getVersion()
);
}
$this->_server['version'] = $server_version;
// check hostname
if ( empty($server_hostname)
|| !preg_match('/[\.\d\-abcdefghijklmnopqrstuvwxyz]*/', $server_hostname)
) {
phpCAS::error('bad CAS server hostname (`'.$server_hostname.'\')');
}
$this->_server['hostname'] = $server_hostname;
// check port
if ( $server_port == 0
|| !is_int($server_port)
) {
phpCAS::error('bad CAS server port (`'.$server_hostname.'\')');
}
$this->_server['port'] = $server_port;
// check URI
if ( !preg_match('/[\.\d\-_abcdefghijklmnopqrstuvwxyz\/]*/', $server_uri) ) {
phpCAS::error('bad CAS server URI (`'.$server_uri.'\')');
}
// add leading and trailing `/' and remove doubles
$server_uri = preg_replace('/\/\//', '/', '/'.$server_uri.'/');
$this->_server['uri'] = $server_uri;
// set to callback mode if PgtIou and PgtId CGI GET parameters are provided
if ( $this->isProxy() ) {
$this->_setCallbackMode(!empty($_GET['pgtIou'])&&!empty($_GET['pgtId']));
}
if ( $this->_isCallbackMode() ) {
//callback mode: check that phpCAS is secured
if ( !$this->_isHttps() ) {
phpCAS::error(
'CAS proxies must be secured to use phpCAS; PGT\'s will not be received from the CAS server'
);
}
} else {
//normal mode: get ticket and remove it from CGI parameters for
// developers
$ticket = (isset($_GET['ticket']) ? $_GET['ticket'] : null);
if (preg_match('/^[SP]T-/', $ticket) ) {
phpCAS::trace('Ticket \''.$ticket.'\' found');
$this->setTicket($ticket);
unset($_GET['ticket']);
} else if ( !empty($ticket) ) {
//ill-formed ticket, halt
phpCAS::error(
'ill-formed ticket found in the URL (ticket=`'
.htmlentities($ticket).'\')'
);
}
}
phpCAS::traceEnd();