Completed
Pull Request — develop (#85)
by
unknown
02:09
created

AuthenticatedUser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 3
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
     * A list of blacklisted attributes defined by their Urn OID
47
     * @var array
48
     */
49
    private static $blacklistedAttributes = [
50
        'urn:oid:1.3.6.1.4.1.1076.20.40.40.1',
51
        'urn:oid:1.3.6.1.4.1.1466.115.121.1.15',
52
    ];
53
54
    /**
55
     * @param AssertionAdapter $assertionAdapter
56
     * @param EntityId[] $authenticatingAuthorities
57
     *
58
     * @return AuthenticatedUser
59
     * @throws RuntimeException
60
     */
61
    public static function createFrom(AssertionAdapter $assertionAdapter, array $authenticatingAuthorities)
62
    {
63
        $attributes = [];
64
65
        /** @var Attribute $attribute */
66
        foreach ($assertionAdapter->getAttributeSet() as $attribute) {
67
            $definition = $attribute->getAttributeDefinition();
68
69
            // Filter out blacklisted attributes
70
            if (in_array($definition->getUrnOid(), self::$blacklistedAttributes)) {
71
                continue;
72
            }
73
74
            // We only want to replace the eduPersonTargetedID attribute value as that is a nested NameID attribute
75
            if ($definition->getName() !== 'eduPersonTargetedID') {
76
                $attributes[] = $attribute;
77
                continue;
78
            }
79
80
            $eptiValues = $attribute->getValue();
81
            $attributes[] = new Attribute($definition, [$eptiValues[0]['Value']]);
82
        }
83
84
        return new self($assertionAdapter->getNameId(), AttributeSet::create($attributes), $authenticatingAuthorities);
85
    }
86
87
    /**
88
     * @param string $nameId
89
     * @param AttributeSet $attributes
90
     * @param EntityId[] $authenticatingAuthorities
91
     */
92
    private function __construct($nameId, AttributeSet $attributes, array $authenticatingAuthorities)
93
    {
94
        Assert::string($nameId);
95
        Assert::allIsInstanceOf($authenticatingAuthorities, '\OpenConext\Profile\Value\EntityId');
96
97
        $this->nameId                    = $nameId;
98
        $this->attributes                = $attributes;
99
        $this->authenticatingAuthorities = $authenticatingAuthorities;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getNameId()
106
    {
107
        return $this->nameId;
108
    }
109
110
    /**
111
     * @return AttributeSet
112
     */
113
    public function getAttributes()
114
    {
115
        return $this->attributes;
116
    }
117
118
    /**
119
     * @return EntityId[]
120
     */
121
    public function getAuthenticatingAuthorities()
122
    {
123
        return $this->authenticatingAuthorities;
124
    }
125
126
    /**
127
     * Using toString in order to comply with AbstractToken's setUser method,
128
     * which uses the string representation to detect changes in the user object.
129
     * Not implementing a UserInterface, because methods defined there will not be used.
130
     *
131
     * @return string
132
     */
133
    public function __toString()
134
    {
135
        return $this->nameId;
136
    }
137
138
    /**
139
     * @return AttributeSet
140
     */
141
    public function getAttributesFiltered()
142
    {
143
        $attributes = $this->getAttributes();
144
        $filtered = [];
145
        /** @var Attribute $attribute */
146
        foreach ($attributes as $attribute) {
147
            // Filter out blacklisted attributes
148
            if (in_array($attribute->getAttributeDefinition()->getUrnOid(), self::$blacklistedAttributes)) {
149
                continue;
150
            }
151
            $filtered[] = $attribute;
152
        }
153
        return AttributeSet::create($filtered);
154
    }
155
}
156