StatementValidator   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 26
eloc 39
c 1
b 0
f 0
dl 0
loc 88
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B validateAuthnStatement() 0 20 7
A validateAttribute() 0 4 2
B validateAuthnContext() 0 24 11
A validateStatement() 0 8 3
A validateAttributeStatement() 0 8 3
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Validator\Model\Statement;
13
14
use LightSaml\Error\LightSamlValidationException;
15
use LightSaml\Helper;
16
use LightSaml\Model\Assertion\AbstractStatement;
17
use LightSaml\Model\Assertion\Attribute;
18
use LightSaml\Model\Assertion\AttributeStatement;
19
use LightSaml\Model\Assertion\AuthnContext;
20
use LightSaml\Model\Assertion\AuthnStatement;
21
22
class StatementValidator implements StatementValidatorInterface
23
{
24
    /**
25
     * @throws \LightSaml\Error\LightSamlValidationException
26
     *
27
     * @return void
28
     */
29
    public function validateStatement(AbstractStatement $statement)
30
    {
31
        if ($statement instanceof AuthnStatement) {
32
            $this->validateAuthnStatement($statement);
33
        } elseif ($statement instanceof AttributeStatement) {
34
            $this->validateAttributeStatement($statement);
35
        } else {
36
            throw new LightSamlValidationException(sprintf("Unsupported Statement type '%s'", get_class($statement)));
37
        }
38
    }
39
40
    private function validateAuthnStatement(AuthnStatement $statement)
41
    {
42
        if (false == $statement->getAuthnInstantTimestamp()) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $statement->getAuthnInstantTimestamp() of type integer|null against false; this is ambiguous if the integer can be zero. Consider using a strict comparison === instead.
Loading history...
43
            throw new LightSamlValidationException('AuthnStatement MUST have an AuthnInstant attribute');
44
        }
45
        if (false == Helper::validateOptionalString($statement->getSessionIndex())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
46
            throw new LightSamlValidationException('SessionIndex attribute of AuthnStatement must contain at least one non-whitespace character');
47
        }
48
        if ($statement->getSubjectLocality()) {
49
            if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getAddress())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
50
                throw new LightSamlValidationException('Address attribute of SubjectLocality must contain at least one non-whitespace character');
51
            }
52
            if (false == Helper::validateOptionalString($statement->getSubjectLocality()->getDnsName())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
53
                throw new LightSamlValidationException('DNSName attribute of SubjectLocality must contain at least one non-whitespace character');
54
            }
55
        }
56
        if (false == $statement->getAuthnContext()) {
0 ignored issues
show
introduced by
The condition false == $statement->getAuthnContext() is always false.
Loading history...
57
            throw new LightSamlValidationException('AuthnStatement MUST have an AuthnContext element');
58
        }
59
        $this->validateAuthnContext($statement->getAuthnContext());
60
    }
61
62
    private function validateAuthnContext(AuthnContext $authnContext)
63
    {
64
        if (false == $authnContext->getAuthnContextClassRef() &&
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $authnContext->getAuthnContextClassRef() of type null|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
65
            false == $authnContext->getAuthnContextDecl() &&
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $authnContext->getAuthnContextDecl() of type null|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
66
            false == $authnContext->getAuthnContextDeclRef()
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $authnContext->getAuthnContextDeclRef() of type null|string against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
67
        ) {
68
            throw new LightSamlValidationException('AuthnContext element MUST contain at least one AuthnContextClassRef, AuthnContextDecl or AuthnContextDeclRef element');
69
        }
70
71
        if ($authnContext->getAuthnContextClassRef() &&
72
            $authnContext->getAuthnContextDecl() &&
73
            $authnContext->getAuthnContextDeclRef()
74
        ) {
75
            throw new LightSamlValidationException('AuthnContext MUST NOT contain more than two elements.');
76
        }
77
78
        if ($authnContext->getAuthnContextClassRef()) {
79
            if (false == Helper::validateWellFormedUriString($authnContext->getAuthnContextClassRef())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
80
                throw new LightSamlValidationException('AuthnContextClassRef has a value which is not a wellformed absolute uri');
81
            }
82
        }
83
        if ($authnContext->getAuthnContextDeclRef()) {
84
            if (false === Helper::validateWellFormedUriString($authnContext->getAuthnContextDeclRef())) {
85
                throw new LightSamlValidationException('AuthnContextDeclRef has a value which is not a wellformed absolute uri');
86
            }
87
        }
88
    }
89
90
    private function validateAttributeStatement(AttributeStatement $statement)
91
    {
92
        if (false == $statement->getAllAttributes()) {
93
            throw new LightSamlValidationException('AttributeStatement MUST contain at least one Attribute or EncryptedAttribute');
94
        }
95
96
        foreach ($statement->getAllAttributes() as $attribute) {
97
            $this->validateAttribute($attribute);
98
        }
99
    }
100
101
    /**
102
     * @throws LightSamlValidationException
103
     *
104
     * @return void
105
     */
106
    private function validateAttribute(Attribute $attribute)
107
    {
108
        if (false == Helper::validateRequiredString($attribute->getName())) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
109
            throw new LightSamlValidationException('Name attribute of Attribute element MUST contain at least one non-whitespace character');
110
        }
111
    }
112
}
113