SpIsValidAudience::validate()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 6
nop 2
dl 0
loc 31
rs 9.0111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\Assertion\Validation\ConstraintValidator;
6
7
use SimpleSAML\SAML2\Assert\Assert;
8
use SimpleSAML\SAML2\Assertion\Validation\AssertionConstraintValidator;
9
use SimpleSAML\SAML2\Assertion\Validation\Result;
10
use SimpleSAML\SAML2\Configuration\ServiceProvider;
11
use SimpleSAML\SAML2\Configuration\ServiceProviderAware;
12
use SimpleSAML\SAML2\XML\saml\Assertion;
13
14
use function implode;
15
use function sprintf;
16
use function strval;
17
18
class SpIsValidAudience implements
19
    AssertionConstraintValidator,
20
    ServiceProviderAware
21
{
22
    /**
23
     * @var \SimpleSAML\SAML2\Configuration\ServiceProvider
24
     */
25
    private ServiceProvider $serviceProvider;
26
27
28
    /**
29
     * @param \SimpleSAML\SAML2\Configuration\ServiceProvider $serviceProvider
30
     */
31
    public function setServiceProvider(ServiceProvider $serviceProvider): void
32
    {
33
        $this->serviceProvider = $serviceProvider;
34
    }
35
36
37
    /**
38
     * @param \SimpleSAML\SAML2\XML\saml\Assertion $assertion
39
     * @param \SimpleSAML\SAML2\Assertion\Validation\Result $result
40
     *
41
     * @throws \SimpleSAML\Assert\AssertionFailedException if assertions are false
42
     */
43
    public function validate(Assertion $assertion, Result $result): void
44
    {
45
        Assert::notEmpty($this->serviceProvider);
46
47
        $conditions = $assertion->getConditions();
48
        if ($conditions === null) {
49
            return;
50
        }
51
52
        $audienceRestrictions = $conditions->getAudienceRestriction();
53
        if (empty($audienceRestrictions)) {
54
            return;
55
        }
56
57
        $entityId = $this->serviceProvider->getEntityId();
58
59
        $all = [];
60
        foreach ($audienceRestrictions as $audienceRestriction) {
61
            $audiences = $audienceRestriction->getAudience();
62
            foreach ($audiences as $audience) {
63
                if ($entityId === $audience->getContent()->getValue()) {
64
                    return;
65
                }
66
                $all[] = $audience->getContent()->getValue();
67
            }
68
        }
69
70
        $result->addError(sprintf(
71
            'The configured Service Provider [%s] is not a valid audience for the assertion. Audiences: [%s]',
72
            strval($entityId),
73
            implode(', ', $all),
74
        ));
75
    }
76
}
77