Completed
Pull Request — feature/fine-grained-authoriza... (#184)
by
unknown
26:20 queued 18:36
created

SamlToken::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
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 Surfnet\StepupRa\RaBundle\Security\Authentication\Token;
20
21
use Surfnet\StepupBundle\Value\Loa;
22
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\InstitutionConfigurationOptions;
23
use Surfnet\StepupRa\RaBundle\Exception\LogicException;
24
use Surfnet\StepupRa\RaBundle\Exception\RuntimeException;
25
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
26
use Symfony\Component\Security\Core\Role\RoleInterface;
27
28
class SamlToken extends AbstractToken
29
{
30
    /**
31
     * @var \SAML2\Assertion
32
     */
33
    public $assertion;
34
35
    /**
36
     * @var \Surfnet\StepupBundle\Value\Loa
37
     */
38
    private $loa;
39
40
    /**
41
     * @var InstitutionConfigurationOptions
42
     */
43
    private $institutionConfigurationOptions;
44
45
    /**
46
     * @var string
47
     */
48
    private $raManagementInstitution;
49
50
    /**
51
     * The SHO of the identity
52
     *
53
     * @var string
54
     */
55
    private $schacHomeOrganization;
56
57
    /**
58
     * The identity institution is set with the SHO of the identity. This value is not overridden like the user
59
     * institution can be. This value can be used to get the identities institution regardless of the scope it
60
     * is performing RAA tasks for at this moment.
61
     *
62
     * @var string
63
     */
64
    private $identityInstitution;
65
66
    public function __construct(
67
        Loa $loa,
68
        array $roles = [],
69
        InstitutionConfigurationOptions $institutionConfigurationOptions = null,
70
        $schacHomeOrganization = ''
71
    ) {
72
        parent::__construct($roles);
73
74
        $this->loa = $loa;
75
        $this->setAuthenticated(count($roles));
0 ignored issues
show
Documentation introduced by
count($roles) is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
77
        $this->schacHomeOrganization = $schacHomeOrganization;
78
    }
79
80
    /**
81
     * @return InstitutionConfigurationOptions
82
     */
83
    public function getInstitutionConfigurationOptions()
84
    {
85
        return $this->institutionConfigurationOptions;
86
    }
87
88
    /**
89
     * @param string $institution
90
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
91
     */
92
    public function changeInstitutionScope(
93
        $institution,
94
        InstitutionConfigurationOptions $institutionConfigurationOptions
95
    ) {
96
        if ($this->getUser() === null) {
97
            throw new LogicException('Cannot change institution scope: token does not contain a user');
98
        }
99
100
        $roles = array_map(function (RoleInterface $role) {
101
            return $role->getRole();
102
        }, $this->getRoles());
103
104
        if (!in_array('ROLE_SRAA', $roles) && !in_array('ROLE_RAA', $roles) && !in_array('ROLE_RA', $roles)) {
105
            throw new RuntimeException(sprintf(
106
                'Unauthorized to change institution scope to "%s": role (S)RA(A) required, found roles "%s"',
107
                $institution,
108
                implode(', ', $roles)
109
            ));
110
        }
111
112
        $this->getUser()->institution = $institution;
113
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
114
    }
115
116
    /**
117
     * Returns the user credentials.
118
     *
119
     * @return mixed The user credentials
120
     */
121
    public function getCredentials()
122
    {
123
        return '';
124
    }
125
126
    /**
127
     * @return Loa
128
     */
129
    public function getLoa()
130
    {
131
        return $this->loa;
132
    }
133
134
    public function serialize()
135
    {
136
        return serialize(
137
            [
138
                parent::serialize(),
139
                $this->loa,
140
                $this->institutionConfigurationOptions,
141
                $this->raManagementInstitution,
142
                $this->identityInstitution,
143
                $this->schacHomeOrganization,
144
            ]
145
        );
146
    }
147
148
    public function unserialize($serialized)
149
    {
150
        list(
151
            $parent,
152
            $this->loa,
153
            $this->institutionConfigurationOptions,
154
            $this->raManagementInstitution,
155
            $this->identityInstitution,
156
            $this->schacHomeOrganization
157
            ) = unserialize(
158
                $serialized
159
            );
160
161
        parent::unserialize($parent);
162
    }
163
164
    /**
165
     * @return string
166
     */
167
    public function getIdentityInstitution()
168
    {
169
        // If the identityInstitution is not yet set, fill it with the institution of the identity.
170
        if (!$this->identityInstitution) {
171
            $this->identityInstitution = $this->getUser()->institution;
172
        }
173
        return $this->identityInstitution;
174
    }
175
176
177
    /**
178
     * @return string
179
     */
180
    public function getRaManagementInstitution()
181
    {
182
        if (!$this->raManagementInstitution) {
183
            return $this->getUser()->institution;
184
        }
185
        return $this->raManagementInstitution;
186
    }
187
188
    /**
189
     * @return string
190
     */
191
    public function getSchacHomeInstitution()
192
    {
193
        return $this->schacHomeOrganization;
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function getIdentityOriginalInstitution()
200
    {
201
        if (!$this->isUserSraa()) {
202
            return $this->getSchacHomeInstitution();
203
        }
204
205
        return $this->getUser()->institution;
206
    }
207
208
    /**
209
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
210
     */
211
    private function isUserSraa()
212
    {
213
        /**
214
         * @var SamlToken $token
215
         */
216
        foreach ($this->getRoles() as $role) {
217
            if ($role->getRole() == 'ROLE_SRAA') {
218
                return true;
219
            }
220
        }
221
222
        return false;
223
    }
224
}
225