AuthenticatedUser   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 123
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createFrom() 0 20 3
A __construct() 0 9 1
A getNameId() 0 4 1
A getAttributes() 0 4 1
A getAuthenticatingAuthorities() 0 4 1
A __toString() 0 4 1
A getAttributesFiltered() 0 14 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
            // We only want to replace the eduPersonTargetedID attribute value as that is a nested NameID attribute
70
            if ($definition->getName() !== 'eduPersonTargetedID') {
71
                $attributes[] = $attribute;
72
                continue;
73
            }
74
75
            $eptiValues = $attribute->getValue();
76
            $attributes[] = new Attribute($definition, [$eptiValues[0]->value]);
77
        }
78
79
        return new self($assertionAdapter->getNameId(), AttributeSet::create($attributes), $authenticatingAuthorities);
80
    }
81
82
    /**
83
     * @param string $nameId
84
     * @param AttributeSet $attributes
85
     * @param EntityId[] $authenticatingAuthorities
86
     */
87
    private function __construct($nameId, AttributeSet $attributes, array $authenticatingAuthorities)
88
    {
89
        Assert::string($nameId);
90
        Assert::allIsInstanceOf($authenticatingAuthorities, '\OpenConext\Profile\Value\EntityId');
91
92
        $this->nameId                    = $nameId;
93
        $this->attributes                = $attributes;
94
        $this->authenticatingAuthorities = $authenticatingAuthorities;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getNameId()
101
    {
102
        return $this->nameId;
103
    }
104
105
    /**
106
     * @return AttributeSet
107
     */
108
    public function getAttributes()
109
    {
110
        return $this->attributes;
111
    }
112
113
    /**
114
     * @return EntityId[]
115
     */
116
    public function getAuthenticatingAuthorities()
117
    {
118
        return $this->authenticatingAuthorities;
119
    }
120
121
    /**
122
     * Using toString in order to comply with AbstractToken's setUser method,
123
     * which uses the string representation to detect changes in the user object.
124
     * Not implementing a UserInterface, because methods defined there will not be used.
125
     *
126
     * @return string
127
     */
128
    public function __toString()
129
    {
130
        return $this->nameId;
131
    }
132
133
    /**
134
     * @return AttributeSet
135
     */
136
    public function getAttributesFiltered()
137
    {
138
        $attributes = $this->getAttributes();
139
        $filtered = [];
140
        /** @var Attribute $attribute */
141
        foreach ($attributes as $attribute) {
142
            // Filter out blacklisted attributes
143
            if (in_array($attribute->getAttributeDefinition()->getUrnOid(), self::$blacklistedAttributes)) {
144
                continue;
145
            }
146
            $filtered[] = $attribute;
147
        }
148
        return AttributeSet::create($filtered);
149
    }
150
}
151