| | |
| | | <?php |
| | | // Thrown when internal error occurs |
| | | class JasigException extends Exception {} |
| | | // Thrown when CAS server return authentication error |
| | | // Thrown when CAS server returns authentication error |
| | | class JasigAuthException extends JasigException {} |
| | | |
| | | class JasigUser { |
| | |
| | | } |
| | | } |
| | | |
| | | public function getServerUrl($serverUrl) { |
| | | return $this->serverUrl; |
| | | } |
| | | public function setServerUrl($serverUrl) { |
| | | $this->serverUrl = $serverUrl; |
| | | } |
| | | |
| | | public function getServiceUrl() { |
| | | return $this->serviceUrl; |
| | | } |
| | | public function setServiceUrl($serviceUrl) { |
| | | $this->serviceUrl = $serviceUrl; |
| | | } |
| | |
| | | return $this->serverUrl.'/login?method=POST&service='.urlencode($this->serviceUrl); |
| | | } |
| | | |
| | | public function logoutUrl() { |
| | | return $this->serverUrl.'/logout'; |
| | | public function logoutUrl($returnUrl = NULL) { |
| | | return $this->serverUrl.'/logout'.($returnUrl ? '?service='.urlencode($returnUrl) : ''); |
| | | } |
| | | |
| | | public function logout() { |
| | |
| | | die(); |
| | | } |
| | | |
| | | public function isAuthenticated() { |
| | | return isset($_SESSION['uphpCAS-user']); |
| | | } |
| | | |
| | | public function authenticate() { |
| | | session_start(); |
| | | if(isset($_SESSION['uphpCAS-user'])) { |
| | | if($this->isAuthenticated()) { |
| | | return $_SESSION['uphpCAS-user']; |
| | | } elseif(isset($_REQUEST['ticket'])) { |
| | | $user = $this->verifyTicket($_REQUEST['ticket']); |
| | |
| | | ), |
| | | ); |
| | | |
| | | if(version_compare(PHP_VERSION, '5.6', '<')) { |
| | | $cafiles = array( |
| | | '/etc/ssl/certs/ca-certificates.crt', |
| | | '/etc/ssl/certs/ca-bundle.crt', |
| | | '/etc/pki/tls/certs/ca-bundle.crt', |
| | | ); |
| | | $cafile = NULL; |
| | | foreach($cafiles as $file) { |
| | | if(is_file($file)) { |
| | | $cafile = $file; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | $url = parse_url($this->serverUrl); |
| | | $context['ssl']['cafile'] = $cafile; |
| | | $context['ssl']['ciphers'] = 'ECDH:DH:AES:CAMELLIA:!SSLv2:!aNULL' |
| | | .':!eNULL:!EXPORT:!DES:!3DES:!MD5:!RC4:!ADH:!PSK:!SRP'; |
| | | $context['ssl']['CN_match'] = $url['host']; |
| | | } |
| | | |
| | | $data = file_get_contents($this->serverUrl |
| | | .'/serviceValidate?service='.urlencode($this->serviceUrl) |
| | | .'&ticket='.urlencode($ticket), |
| | |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | catch(Exception $e) { |
| | | } catch(Exception $e) { |
| | | throw new JasigException('Authentication error: CAS server' |
| | | .' response invalid - parse error', 0, $e); |
| | | } finally { |
| | |
| | | } |
| | | |
| | | $user = trim($user->item(0)->textContent); |
| | | if(strlen($user)<1) { |
| | | if(strlen($user) < 1) { |
| | | throw new JasigException('Authentication error: CAS server' |
| | | .' response invalid - user value'); |
| | | } |
| | |
| | | } |
| | | |
| | | return $jusr; |
| | | } |
| | | else |
| | | { |
| | | } else { |
| | | throw new JasigException('Authentication error: CAS server' |
| | | .' response invalid - required tag not found'); |
| | | } |