1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\casserver\Controller; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Configuration; |
8
|
|
|
use SimpleSAML\Logger; |
9
|
|
|
use SimpleSAML\Module\casserver\Cas\Protocol\Cas20; |
10
|
|
|
use SimpleSAML\Module\casserver\Cas\Protocol\SamlValidateResponder; |
11
|
|
|
use SimpleSAML\Module\casserver\Cas\TicketValidator; |
12
|
|
|
use SimpleSAML\Module\casserver\Controller\Traits\UrlTrait; |
13
|
|
|
use SimpleSAML\Module\casserver\Http\XmlResponse; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
17
|
|
|
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter; |
18
|
|
|
|
19
|
|
|
#[AsController] |
20
|
|
|
class Cas30Controller |
21
|
|
|
{ |
22
|
|
|
use UrlTrait; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** @var Logger */ |
25
|
|
|
protected Logger $logger; |
26
|
|
|
|
27
|
|
|
/** @var Configuration */ |
28
|
|
|
protected Configuration $casConfig; |
29
|
|
|
|
30
|
|
|
/** @var Cas20 */ |
31
|
|
|
protected Cas20 $cas20Protocol; |
32
|
|
|
|
33
|
|
|
/** @var TicketValidator */ |
34
|
|
|
protected TicketValidator $ticketValidator; |
35
|
|
|
|
36
|
|
|
/** @var SamlValidateResponder */ |
37
|
|
|
protected SamlValidateResponder $validateResponder; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Configuration $sspConfig |
41
|
|
|
* @param Configuration|null $casConfig |
42
|
|
|
* |
43
|
|
|
* @throws \Exception |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
private readonly Configuration $sspConfig, |
47
|
|
|
Configuration $casConfig = null, |
48
|
|
|
TicketValidator $ticketValidator = null, |
49
|
|
|
) { |
50
|
|
|
// We are using this work around in order to bypass Symfony's autowiring for cas configuration. Since |
51
|
|
|
// the configuration class is the same, it loads the ssp configuration twice. Still, we need the constructor |
52
|
|
|
// argument in order to facilitate testin. |
53
|
|
|
$this->casConfig = ($casConfig === null || $casConfig === $sspConfig) |
54
|
|
|
? Configuration::getConfig('module_casserver.php') : $casConfig; |
55
|
|
|
$this->cas20Protocol = new Cas20($this->casConfig); |
56
|
|
|
$this->ticketValidator = $ticketValidator ?? new TicketValidator($this->casConfig); |
57
|
|
|
$this->validateResponder = new SamlValidateResponder(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* POST /casserver/samlValidate?TARGET= |
63
|
|
|
* Host: cas.example.com |
64
|
|
|
* Content-Length: 491 |
65
|
|
|
* Content-Type: text/xml |
66
|
|
|
* |
67
|
|
|
* @param Request $request |
68
|
|
|
* @param string $TARGET URL encoded service identifier of the back-end service. |
69
|
|
|
* |
70
|
|
|
* @throws \RuntimeException |
71
|
|
|
* @return XmlResponse |
72
|
|
|
* @link https://apereo.github.io/cas/7.1.x/protocol/CAS-Protocol-Specification.html#42-samlvalidate-cas-30 |
73
|
|
|
*/ |
74
|
|
|
public function samlValidate( |
75
|
|
|
Request $request, |
76
|
|
|
#[MapQueryParameter] string $TARGET, |
77
|
|
|
): XmlResponse { |
78
|
|
|
// From SAML2\SOAP::receive() |
79
|
|
|
$postBody = $request->getContent(); |
80
|
|
|
if (empty($postBody)) { |
81
|
|
|
throw new \RuntimeException('samlValidate expects a soap body.'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// SAML request values |
85
|
|
|
// |
86
|
|
|
// samlp:Request |
87
|
|
|
// - RequestID [REQUIRED] - unique identifier for the request |
88
|
|
|
// - IssueInstant [REQUIRED] - timestamp of the request |
89
|
|
|
// samlp:AssertionArtifact [REQUIRED] - the valid CAS Service |
90
|
|
|
|
91
|
|
|
$ticketParser = xml_parser_create(); |
92
|
|
|
xml_parser_set_option($ticketParser, XML_OPTION_CASE_FOLDING, 0); |
93
|
|
|
xml_parser_set_option($ticketParser, XML_OPTION_SKIP_WHITE, 1); |
94
|
|
|
xml_parse_into_struct($ticketParser, $postBody, $values, $tags); |
95
|
|
|
xml_parser_free($ticketParser); |
96
|
|
|
|
97
|
|
|
// Check for the required saml attributes |
98
|
|
|
$samlRequestAttributes = $values[ $tags['samlp:Request'][0] ]['attributes']; |
99
|
|
|
if (!isset($samlRequestAttributes['RequestID'])) { |
100
|
|
|
throw new \RuntimeException('Missing RequestID samlp:Request attribute.'); |
101
|
|
|
} elseif (!isset($samlRequestAttributes['IssueInstant'])) { |
102
|
|
|
throw new \RuntimeException('Missing IssueInstant samlp:Request attribute.'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ( |
106
|
|
|
!isset($tags['samlp:AssertionArtifact']) |
107
|
|
|
|| empty($values[$tags['samlp:AssertionArtifact'][0]]['value']) |
108
|
|
|
) { |
109
|
|
|
throw new \RuntimeException('Missing ticketId in AssertionArtifact'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$ticketId = $values[$tags['samlp:AssertionArtifact'][0]]['value']; |
113
|
|
|
Logger::debug('samlvalidate: Checking ticket ' . $ticketId); |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
// validateAndDeleteTicket might throw a CasException. In order to avoid third party modules |
117
|
|
|
// dependencies, we will catch and rethrow the Exception. |
118
|
|
|
$ticket = $this->ticketValidator->validateAndDeleteTicket($ticketId, $TARGET); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
throw new \RuntimeException($e->getMessage()); |
121
|
|
|
} |
122
|
|
|
if (!\is_array($ticket)) { |
|
|
|
|
123
|
|
|
throw new \RuntimeException('Error loading ticket'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$response = $this->validateResponder->convertToSaml($ticket); |
127
|
|
|
$soap = $this->validateResponder->wrapInSoap($response); |
128
|
|
|
|
129
|
|
|
return new XmlResponse( |
130
|
|
|
(string)$soap, |
131
|
|
|
Response::HTTP_OK, |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|