-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcas.php
61 lines (52 loc) · 1.79 KB
/
cas.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
<?php
require 'vendor/autoload.php';
use \SimpleXMLElement;
use \Httpful\Request;
use \Payutc\Exception\AuthenticationFailure;
class Cas
{
protected $url;
protected $timeout;
public function __construct($url, $timeout=10)
{
$this->url = $url;
$this->timeout = $timeout;
}
public function authenticate($ticket, $service)
{
$r = Request::get($this->getValidateUrl($ticket, $service))
->sendsXml()
->timeoutIn($this->timeout)
->send();
$r->body = str_replace("\n", "", $r->body);
try {
$xml = new SimpleXMLElement($r->body);
}
catch (\Exception $e) {
throw new \UnexpectedValueException("Return cannot be parsed : '{$r->body}'");
}
$namespaces = $xml->getNamespaces();
$serviceResponse = $xml->children($namespaces['cas']);
$user = $serviceResponse->authenticationSuccess->user;
if ($user) {
return (string)$user; // cast simplexmlelement to string
}
else {
$authFailed = $serviceResponse->authenticationFailure;
if ($authFailed) {
$attributes = $authFailed->attributes();
Log::warning("AuthenticationFailure : ".$attributes['code']." ($ticket, $service)");
throw new AuthenticationFailure((string)$attributes['code']);
}
else {
Log::error("Cas return is weird : '{$r->body}'");
throw new \UnexpectedValueException($r->body);
}
}
// never reach there
}
public function getValidateUrl($ticket, $service)
{
return $this->url."serviceValidate?ticket=".urlencode($ticket)."&service=".urlencode($service);
}
}