This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestClient.php
132 lines (103 loc) · 3.88 KB
/
testClient.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
<?php
namespace webtools\app\ecas;
class TestClient{
private static $phpcasClientCalled = FALSE;
protected $ecasConfig;
public function __construct($_ecas_config) {
$this->ecasConfig = $_ecas_config;
//require_once $this->ecasConfig['library'];
if (self::$phpcasClientCalled === FALSE) {
// Instantiate or re-instantiate CAS object.
\phpCAS::client(
$this->ecasConfig['version'],
$this->ecasConfig['host'],
(int) $this->ecasConfig['port'],
$this->ecasConfig['uri'],
FALSE
);
self::$phpcasClientCalled = TRUE;
}
// List of allowed proxies for ticket validation.
\phpCAS::allowProxyChain(
new \CAS_ProxyChain($this->ecasConfig['allowed_proxy_chain'])
);
if ($this->ecasConfig['cert']) {
\phpCAS::setCasServerCACert($this->ecasConfig['cert']);
}
else {
\phpCAS::setNoCasServerValidation();
}
$this->setValidatorExtraFields();
}
public function login($force_auth = TRUE) {
if ($force_auth) {
//set the strengths limitation first if any are provided in configuration
$this->setStrengths();
\phpCAS::forceAuthentication();
}
if (!\phpCAS::checkAuthentication()) {
return array(
'success' => FALSE,
'message' => "No Authentication",
'user' => NULL,
);
}
$username = \phpCAS::getUser();
$userData = \phpCAS::getAttributes();
return array(
'success' => TRUE,
'message' => "Login Successfully",
'username' => $username,
'userdata' => $userData,
);
}
/**
* @desc Check whether there are some strengths set in the configuration and if true generate the LoginURL with the limitaion of accepted strengths.
*/
private function setStrengths(){
if(trim($this->ecasConfig['userStrengths'])){
$acceptedStrengths = explode(',', $this->ecasConfig['userStrengths']);
$strengths = '';
foreach ($acceptedStrengths as $strength) {
$strengths = (trim($strengths) === "" ? "" : ",") . urlencode($strength);
}
\phpCAS::setServerLoginURL(\phpCAS::getServerLoginURL() . "&acceptStrengths=" . $strengths);
}
}
public function logout(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
if (isset($_SESSION['phpCAS'])) {
unset($_SESSION['phpCAS']);
}
\phpCAS::logout();
}
/**
* @desc The method will set the ServerProxyValidateURL or ServerServiceValidateURL depending whether allowed proxy chain is present in configuration
* The values which will be sent are:
* - userDetails in order to get all user details
* - groups in order to map all intended groups
* - assuranceLevel in order to filter the desired assuranceLevel.
*/
private function setValidatorExtraFields(){
if(is_array($this->ecasConfig['allowed_proxy_chain']) && sizeof($this->ecasConfig['allowed_proxy_chain']) > 0){
$validateUri = $this->ecasConfig['proxy_validate_uri'] ;
}else{
$validateUri = $this->ecasConfig['service_validate_uri'] ;
}
//added in order to display user details and enforce assurance_level
$validateServerUrl = 'https://' . $this->ecasConfig['host'] . ":" ;
$validateServerUrl .= $this->ecasConfig['port'] ;
$validateServerUrl .= $this->ecasConfig['uri'] ;
$validateServerUrl .= $validateUri . '?' ;
$validateServerUrl .= 'userDetails=' . $this->ecasConfig['user_details'] ;
$validateServerUrl .= '&groups=' . $this->ecasConfig['user_groups'] ;
$validateServerUrl .= '&assuranceLevel=' . $this->ecasConfig['user_assurance_level'] ;
if(is_array($this->ecasConfig['allowed_proxy_chain']) && count($this->ecasConfig['allowed_proxy_chain']) > 0){
\phpCAS::setServerProxyValidateURL($validateServerUrl);
}else{
\phpCAS::setServerServiceValidateURL($validateServerUrl);
}
}
}