Passed
Pull Request — master (#1)
by Tim
02:03
created

AttributeServer   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 70
c 1
b 0
f 0
dl 0
loc 132
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B main() 0 99 9
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\exampleattributeserver\Controller;
6
7
use SAML2\Binding;
8
use SAML2\Constants;
9
use SimpleSAML\Metadata\MetaDataStorageHandler;
10
use SimpleSAML\Error;
11
use SimpleSAML\Logger;
12
use SimpleSAML\Module\saml\Message;
13
use SimpleSAML\SAML2\Assertion;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Assertion was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use SimpleSAML\SAML2\AttributeQuery;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\AttributeQuery was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use SimpleSAML\SAML2\HTTPPost;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\HTTPPost was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SimpleSAML\SAML2\Response;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SimpleSAML\SAML2\XML\saml\Issuer;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\Issuer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SimpleSAML\SAML2\XML\saml\SubjectConfirmation;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\SubjectConfirmation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use SimpleSAML\SAML2\XML\saml\SubjectConfirmationData;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\SubjectConfirmationData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * Controller class for the exampleattributeserver module.
23
 *
24
 * This class serves the attribute server available in the module.
25
 *
26
 * @package SimpleSAML\Module\exampleattributeserver
27
 */
28
class AttributeServer
29
{
30
    /** @var \SimpleSAML\Configuration */
31
    protected $config;
32
33
    /**
34
     * @var \SimpleSAML\Utils\Auth|string
35
     * @psalm-var \SimpleSAML\Utils\Auth|class-string
36
     */
37
    protected $authUtils = Utils\Auth::class;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\exampl...r\Controller\Utils\Auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
38
39
    /** @var \SimpleSAML\Session */
40
    protected $session;
41
42
43
    /**
44
     * ConfigController constructor.
45
     *
46
     * @param \SimpleSAML\Configuration $config The configuration to use.
47
     * @param \SimpleSAML\Session $session The current user session.
48
     */
49
    public function __construct(Configuration $config, Session $session)
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\exampl...ontroller\Configuration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type SimpleSAML\Module\exampl...rver\Controller\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
    {
51
        $this->config = $config;
52
        $this->session = $session;
53
    }
54
55
56
    /**
57
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
58
     *
59
     * @return \SimpleSAML\XHTML\Template
60
     */
61
    public function main(/** @scrutinizer ignore-unused */ Request $request): Template
0 ignored issues
show
Bug introduced by
The type SimpleSAML\Module\exampl...rver\Controller\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type SimpleSAML\Module\exampl...ver\Controller\Template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
62
    {
63
        $metadata = MetaDataStorageHandler::getMetadataHandler();
64
65
        $binding = Binding::getCurrentBinding();
66
        $query = $binding->receive();
67
        if (!($query instanceof AttributeQuery)) {
68
            throw new Error\BadRequest('Invalid message received to AttributeQuery endpoint.');
69
        }
70
71
        $idpEntityId = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted');
72
73
        $issuer = $query->getIssuer();
74
        if ($issuer === null) {
75
            throw new Error\BadRequest('Missing <saml:Issuer> in <samlp:AttributeQuery>.');
76
        } else {
77
            $spEntityId = $issuer->getValue();
78
            if ($spEntityId === '') {
79
                throw new Error\BadRequest('Empty <saml:Issuer> in <samlp:AttributeQuery>.');
80
            }
81
        }
82
83
        $idpMetadata = $metadata->getMetaDataConfig($idpEntityId, 'saml20-idp-hosted');
84
        $spMetadata = $metadata->getMetaDataConfig($spEntityId, 'saml20-sp-remote');
85
86
        // The endpoint we should deliver the message to
87
        $endpoint = $spMetadata->getString('testAttributeEndpoint');
88
89
        // The attributes we will return
90
        $attributes = [
91
            'name' => ['value1', 'value2', 'value3'],
92
            'test' => ['test'],
93
        ];
94
95
        // The name format of the attributes
96
        $attributeNameFormat = Constants::NAMEFORMAT_UNSPECIFIED;
97
98
        // Determine which attributes we will return
99
        $returnAttributes = array_keys($query->getAttributes());
0 ignored issues
show
Bug introduced by
The method getAttributes() does not exist on SAML2\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributes() does not exist on SAML2\LogoutRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributes() does not exist on SAML2\LogoutResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributes() does not exist on SAML2\AuthnRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributes() does not exist on SAML2\ArtifactResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributes() does not exist on SAML2\ArtifactResolve. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        $returnAttributes = array_keys($query->/** @scrutinizer ignore-call */ getAttributes());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
        if (count($returnAttributes) === 0) {
101
            Logger::debug('No attributes requested - return all attributes.');
102
            $returnAttributes = $attributes;
103
        } elseif ($query->getAttributeNameFormat() !== $attributeNameFormat) {
0 ignored issues
show
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\LogoutRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\ArtifactResolve. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\AuthnRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\LogoutResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getAttributeNameFormat() does not exist on SAML2\ArtifactResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
        } elseif ($query->/** @scrutinizer ignore-call */ getAttributeNameFormat() !== $attributeNameFormat) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
            Logger::debug('Requested attributes with wrong NameFormat - no attributes returned.');
105
            $returnAttributes = [];
106
        } else {
107
            /** @var array $values */
108
            foreach ($returnAttributes as $name => $values) {
109
                if (!array_key_exists($name, $attributes)) {
110
                    // We don't have this attribute
111
                    unset($returnAttributes[$name]);
112
                    continue;
113
                }
114
                if (count($values) === 0) {
115
                    // Return all attributes
116
                    $returnAttributes[$name] = $attributes[$name];
117
                    continue;
118
                }
119
120
                // Filter which attribute values we should return
121
                $returnAttributes[$name] = array_intersect($values, $attributes[$name]);
122
            }
123
        }
124
125
        // $returnAttributes contains the attributes we should return. Send them
126
        $issuer = new Issuer();
127
        $issuer->setValue($idpEntityId);
128
129
        $assertion = new Assertion();
130
        $assertion->setIssuer($issuer);
131
        $assertion->setNameId($query->getNameId());
0 ignored issues
show
Bug introduced by
The method getNameId() does not exist on SAML2\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        $assertion->setNameId($query->/** @scrutinizer ignore-call */ getNameId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getNameId() does not exist on SAML2\ArtifactResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        $assertion->setNameId($query->/** @scrutinizer ignore-call */ getNameId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getNameId() does not exist on SAML2\ArtifactResolve. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        $assertion->setNameId($query->/** @scrutinizer ignore-call */ getNameId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getNameId() does not exist on SAML2\LogoutResponse. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

131
        $assertion->setNameId($query->/** @scrutinizer ignore-call */ getNameId());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
132
        $assertion->setNotBefore(time());
133
        $assertion->setNotOnOrAfter(time() + 300); // 60*5 = 5min
134
        $assertion->setValidAudiences([$spEntityId]);
135
        $assertion->setAttributes($returnAttributes);
136
        $assertion->setAttributeNameFormat($attributeNameFormat);
137
138
        $sc = new SubjectConfirmation();
139
        $sc->setMethod(Constants::CM_BEARER);
140
141
        $scd = new SubjectConfirmationData();
142
        $scd->setNotOnOrAfter(time() + 300); // 60*5 = 5min
143
        $scd->setRecipient($endpoint);
144
        $scd->setInResponseTo($query->getId());
145
        $sc->setSubjectConfirmationData($scd);
146
        $assertion->setSubjectConfirmation([$sc]);
147
148
        Message::addSign($idpMetadata, $spMetadata, $assertion);
149
150
        $response = new Response();
151
        $response->setRelayState($query->getRelayState());
152
        $response->setDestination($endpoint);
153
        $response->setIssuer($issuer);
154
        $response->setInResponseTo($query->getId());
155
        $response->setAssertions([$assertion]);
156
        Message::addSign($idpMetadata, $spMetadata, $response);
157
158
        $binding = new HTTPPost();
159
        $binding->send($response);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return SimpleSAML\Module\exampl...ver\Controller\Template. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
160
    }
161
}
162