ACSUrlValidatorAction::doExecute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 28
cp 0
rs 9.392
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-IDP package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the GPL-3 license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Idp\Action\Profile\Inbound\AuthnRequest;
13
14
use LightSaml\Action\Profile\AbstractProfileAction;
15
use LightSaml\Context\Profile\Helper\LogHelper;
16
use LightSaml\Context\Profile\Helper\MessageContextHelper;
17
use LightSaml\Context\Profile\ProfileContext;
18
use LightSaml\Error\LightSamlValidationException;
19
20
class ACSUrlValidatorAction extends AbstractProfileAction
21
{
22
    /**
23
     * @param ProfileContext $context
24
     *
25
     * @return void
26
     */
27
    protected function doExecute(ProfileContext $context)
28
    {
29
        $authnRequest = MessageContextHelper::asAuthnRequest($context->getInboundContext());
0 ignored issues
show
Bug introduced by
It seems like $context->getInboundContext() can be null; however, asAuthnRequest() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
30
31
        if (false == $authnRequest->getAssertionConsumerServiceURL()) {
32
            return;
33
        }
34
35
        $spEntityDescriptor = $context->getPartyEntityDescriptor();
36
37
        foreach ($spEntityDescriptor->getAllSpSsoDescriptors() as $sp) {
38
            if ($sp->getAllAssertionConsumerServicesByUrl($authnRequest->getAssertionConsumerServiceURL())) {
39
                $this->logger->debug(
40
                    sprintf(
41
                        'AuthnRequest has assertion consumer url "%s" that belongs to entity "%s"',
42
                        $authnRequest->getAssertionConsumerServiceURL(),
43
                        $spEntityDescriptor->getEntityID()
44
                    ),
45
                    LogHelper::getActionContext($context, $this)
46
                );
47
48
                return;
49
            }
50
        }
51
52
        $message = sprintf(
53
            "Invalid ACS Url '%s' for '%s' entity",
54
            $authnRequest->getAssertionConsumerServiceURL(),
55
            $spEntityDescriptor->getEntityID()
56
        );
57
        $this->logger->emergency($message, LogHelper::getActionErrorContext($context, $this));
58
        throw new LightSamlValidationException($message);
59
    }
60
}
61