Completed
Pull Request — develop (#49)
by A.
03:24
created

AuthenticatedUser::getNameId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace OpenConext\Profile\Entity;
20
21
use OpenConext\Profile\Assert;
22
use OpenConext\Profile\Exception\RuntimeException;
23
use OpenConext\Profile\Value\EntityId;
24
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
25
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSet;
26
use Surfnet\SamlBundle\SAML2\Response\AssertionAdapter;
27
28
final class AuthenticatedUser
29
{
30
    /**
31
     * @var string
32
     */
33
    private $nameId;
34
35
    /**
36
     * @var AttributeSet
37
     */
38
    private $attributes;
39
40
    /**
41
     * @var EntityId[]
42
     */
43
    private $authenticatingAuthorities;
44
45
    /**
46
     * @param AssertionAdapter $assertionAdapter
47
     * @param EntityId[] $authenticatingAuthorities
48
     *
49
     * @return AuthenticatedUser
50
     * @throws RuntimeException
51
     */
52
    public static function createFrom(AssertionAdapter $assertionAdapter, array $authenticatingAuthorities)
53
    {
54
        $attributes = [];
55
56
        /** @var Attribute $attribute */
57
        foreach ($assertionAdapter->getAttributeSet() as $attribute) {
58
            $definition = $attribute->getAttributeDefinition();
59
60
            // We only want to replace the eduPersonTargetedID attribute value as that is a nested NameID attribute
61
            if ($definition->getName() !== 'eduPersonTargetedID') {
62
                $attributes[] = $attribute;
63
                continue;
64
            }
65
66
            /** @var \DOMNodeList[] $eptiValues */
67
            $eptiValues = $attribute->getValue();
68
            $eptiDomNodeList = $eptiValues[0];
69
70
            if (!$eptiDomNodeList instanceof \DOMNodeList || $eptiDomNodeList->length !== 1) {
71
                throw new RuntimeException(
72
                    sprintf(
73
                        'EPTI attribute must contain exactly one NameID element as value, received: %s',
74
                        print_r($eptiValues, true)
75
                    )
76
                );
77
            }
78
79
            $eptiValue  = $eptiDomNodeList->item(0);
80
            $eptiNameId = \SAML2_Utils::parseNameId($eptiValue);
0 ignored issues
show
Compatibility introduced by
$eptiValue of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
81
82
            $attributes[] = new Attribute($definition, [$eptiNameId['Value']]);
83
        }
84
85
        return new self(
86
            $assertionAdapter->getNameId(),
87
            AttributeSet::create($attributes),
88
            $authenticatingAuthorities
89
        );
90
    }
91
92
    /**
93
     * @param string $nameId
94
     * @param AttributeSet $attributes
95
     * @param EntityId[] $authenticatingAuthorities
96
     */
97
    private function __construct($nameId, AttributeSet $attributes, array $authenticatingAuthorities)
98
    {
99
        Assert::string($nameId);
100
        Assert::allIsInstanceOf($authenticatingAuthorities, '\OpenConext\Profile\Value\EntityId');
101
102
        $this->nameId                    = $nameId;
103
        $this->attributes                = $attributes;
104
        $this->authenticatingAuthorities = $authenticatingAuthorities;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getNameId()
111
    {
112
        return $this->nameId;
113
    }
114
115
    /**
116
     * @return AttributeSet
117
     */
118
    public function getAttributes()
119
    {
120
        return $this->attributes;
121
    }
122
123
    /**
124
     * @return EntityId[]
125
     */
126
    public function getAuthenticatingAuthorities()
127
    {
128
        return $this->authenticatingAuthorities;
129
    }
130
131
    /**
132
     * Using toString in order to comply with AbstractToken's setUser method,
133
     * which uses the string representation to detect changes in the user object.
134
     * Not implementing a UserInterface, because methods defined there will not be used.
135
     *
136
     * @return string
137
     */
138
    public function __toString()
139
    {
140
        return $this->nameId;
141
    }
142
}
143