Passed
Pull Request — master (#45)
by
unknown
02:48
created

Cas30Controller   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
eloc 36
c 3
b 0
f 0
dl 0
loc 109
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B samlValidate() 0 51 6
A __construct() 0 13 3
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 SimpleSAML\SAML11\Exception\ProtocolViolationException;
15
use SimpleSAML\SAML11\XML\samlp\Request as SamlRequest;
16
use SimpleSAML\SOAP\XML\env_200106\Envelope;
17
use SimpleSAML\XML\DOMDocumentFactory;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Attribute\AsController;
21
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
22
23
#[AsController]
24
class Cas30Controller
25
{
26
    use UrlTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\Module\casser...troller\Traits\UrlTrait requires some properties which are not provided by SimpleSAML\Module\casser...troller\Cas30Controller: $ticketFactory, $request, $ticketStore, $httpUtils, $query
Loading history...
27
28
    /** @var Logger */
29
    protected Logger $logger;
30
31
    /** @var Configuration */
32
    protected Configuration $casConfig;
33
34
    /** @var Cas20 */
35
    protected Cas20 $cas20Protocol;
36
37
    /** @var TicketValidator */
38
    protected TicketValidator $ticketValidator;
39
40
    /** @var SamlValidateResponder */
41
    protected SamlValidateResponder $validateResponder;
42
43
    /**
44
     * @param   Configuration         $sspConfig
45
     * @param   Configuration|null    $casConfig
46
     * @param   TicketValidator|null  $ticketValidator
47
     *
48
     * @throws \Exception
49
     */
50
    public function __construct(
51
        private readonly Configuration $sspConfig,
52
        Configuration $casConfig = null,
53
        TicketValidator $ticketValidator = null,
54
    ) {
55
        // We are using this work around in order to bypass Symfony's autowiring for cas configuration. Since
56
        // the configuration class is the same, it loads the ssp configuration twice. Still, we need the constructor
57
        // argument in order to facilitate testin.
58
        $this->casConfig = ($casConfig === null || $casConfig === $sspConfig)
59
            ? Configuration::getConfig('module_casserver.php') : $casConfig;
60
        $this->cas20Protocol = new Cas20($this->casConfig);
61
        $this->ticketValidator   = $ticketValidator ?? new TicketValidator($this->casConfig);
62
        $this->validateResponder = new SamlValidateResponder();
63
    }
64
65
66
    /**
67
     * POST /casserver/samlValidate?TARGET=
68
     * Host: cas.example.com
69
     * Content-Length: 491
70
     * Content-Type: text/xml
71
     *
72
     * @param   Request  $request
73
     * @param   string   $TARGET  URL encoded service identifier of the back-end service.
74
     *
75
     * @throw SimpleSAML\SAML11\Exception\ProtocolViolationException
76
     * @throw SimpleSAML\XML\Exception\MissingAttributeException
77
     * @throw \RuntimeException
78
     * @return XmlResponse
79
     * @link https://apereo.github.io/cas/7.1.x/protocol/CAS-Protocol-Specification.html#42-samlvalidate-cas-30
80
     */
81
    public function samlValidate(
82
        Request $request,
83
        #[MapQueryParameter] string $TARGET,
84
    ): XmlResponse {
85
        // From SAML2\SOAP::receive()
86
        $postBody = $request->getContent();
87
        if (empty($postBody)) {
88
            throw new \RuntimeException('samlValidate expects a soap body.');
89
        }
90
91
        // SAML request values
92
        //
93
        // samlp:Request
94
        //  - RequestID [REQUIRED] - unique identifier for the request
95
        //  - IssueInstant [REQUIRED] - timestamp of the request
96
        // samlp:AssertionArtifact [REQUIRED] - the valid CAS Service
97
98
        $documentBody = DOMDocumentFactory::fromString($postBody);
99
        $envelope = Envelope::fromXML($documentBody->documentElement);
100
101
        // The SOAP Envelope must have only one ticket
102
        $elements = $envelope->getBody()->getElements();
103
        if (count($elements) > 1 || count($elements) < 1) {
104
            throw new ProtocolViolationException('samlValidate expects a soap body with only one ticket.');
105
        }
106
107
        // Request Element
108
        $samlpRequestParsed = SamlRequest::fromXML($elements[0]->getXML());
109
        // Assertion Artifact Element
110
        $assertionArtifactParsed = $samlpRequestParsed->getRequest()[0];
111
112
        $ticketId = $assertionArtifactParsed->getContent();
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)) {
0 ignored issues
show
introduced by
The condition is_array($ticket) is always true.
Loading history...
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