Completed
Pull Request — develop (#57)
by A.
03:21
created

AuthenticatedUser::getAuthenticatingAuthorities()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
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\InvalidEptiAttributeException;
23
use OpenConext\Profile\Exception\RuntimeException;
24
use OpenConext\Profile\Value\EntityId;
25
use Surfnet\SamlBundle\SAML2\Attribute\Attribute;
26
use Surfnet\SamlBundle\SAML2\Attribute\AttributeSet;
27
use Surfnet\SamlBundle\SAML2\Response\AssertionAdapter;
28
29
final class AuthenticatedUser
30
{
31
    /**
32
     * @var string
33
     */
34
    private $nameId;
35
36
    /**
37
     * @var AttributeSet
38
     */
39
    private $attributes;
40
41
    /**
42
     * @var EntityId[]
43
     */
44
    private $authenticatingAuthorities;
45
46
    /**
47
     * @param AssertionAdapter $assertionAdapter
48
     * @param EntityId[] $authenticatingAuthorities
49
     *
50
     * @return AuthenticatedUser
51
     * @throws RuntimeException
52
     */
53
    public static function createFrom(AssertionAdapter $assertionAdapter, array $authenticatingAuthorities)
54
    {
55
        $attributes = [];
56
57
        /** @var Attribute $attribute */
58
        foreach ($assertionAdapter->getAttributeSet() as $attribute) {
59
            $definition = $attribute->getAttributeDefinition();
60
61
            // We only want to replace the eduPersonTargetedID attribute value as that is a nested NameID attribute
62
            if ($definition->getName() !== 'eduPersonTargetedID') {
63
                $attributes[] = $attribute;
64
                continue;
65
            }
66
67
            $eptiValues = $attribute->getValue();
68
            $attributes[] = new Attribute($definition, [$eptiValues[0]['Value']]);
69
        }
70
71
        return new self($assertionAdapter->getNameId(), AttributeSet::create($attributes), $authenticatingAuthorities);
72
    }
73
74
    /**
75
     * @param string $nameId
76
     * @param AttributeSet $attributes
77
     * @param EntityId[] $authenticatingAuthorities
78
     */
79
    private function __construct($nameId, AttributeSet $attributes, array $authenticatingAuthorities)
80
    {
81
        Assert::string($nameId);
82
        Assert::allIsInstanceOf($authenticatingAuthorities, '\OpenConext\Profile\Value\EntityId');
83
84
        $this->nameId                    = $nameId;
85
        $this->attributes                = $attributes;
86
        $this->authenticatingAuthorities = $authenticatingAuthorities;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getNameId()
93
    {
94
        return $this->nameId;
95
    }
96
97
    /**
98
     * @return AttributeSet
99
     */
100
    public function getAttributes()
101
    {
102
        return $this->attributes;
103
    }
104
105
    /**
106
     * @return EntityId[]
107
     */
108
    public function getAuthenticatingAuthorities()
109
    {
110
        return $this->authenticatingAuthorities;
111
    }
112
113
    /**
114
     * Using toString in order to comply with AbstractToken's setUser method,
115
     * which uses the string representation to detect changes in the user object.
116
     * Not implementing a UserInterface, because methods defined there will not be used.
117
     *
118
     * @return string
119
     */
120
    public function __toString()
121
    {
122
        return $this->nameId;
123
    }
124
}
125