Completed
Push — feature/fine-grained-authoriza... ( 47d334...98a8e7 )
by
unknown
04:59
created

SamlToken::getCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
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
     * @var string
52
     */
53
    private $schacHomeOrganization;
54
55
    /**
56
     * The identity institution is set with the SHO of the identity. This value is not overridden like the user
57
     * institution can be. This value can be used to get the identities institution regardless of the scope it
58
     * is performing RAA tasks for at this moment.
59
     *
60
     * @var string
61
     */
62
    private $identityInstitution;
63
64
    public function __construct(
65
        Loa $loa,
66
        array $roles = [],
67
        InstitutionConfigurationOptions $institutionConfigurationOptions = null,
68
        $schacHomeOrganization = ''
69
    ) {
70
        parent::__construct($roles);
71
72
        $this->loa = $loa;
73
        $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...
74
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
75
        $this->schacHomeOrganization = $schacHomeOrganization;
76
    }
77
78
    /**
79
     * @return InstitutionConfigurationOptions
80
     */
81
    public function getInstitutionConfigurationOptions()
82
    {
83
        return $this->institutionConfigurationOptions;
84
    }
85
86
    /**
87
     * @param string $institution
88
     * @param InstitutionConfigurationOptions $institutionConfigurationOptions
89
     */
90
    public function changeInstitutionScope(
91
        $institution,
92
        InstitutionConfigurationOptions $institutionConfigurationOptions
93
    ) {
94
        if ($this->getUser() === null) {
95
            throw new LogicException('Cannot change institution scope: token does not contain a user');
96
        }
97
98
        $roles = array_map(function (RoleInterface $role) {
99
            return $role->getRole();
100
        }, $this->getRoles());
101
102
        if (!in_array('ROLE_SRAA', $roles) && !in_array('ROLE_RAA', $roles) && !in_array('ROLE_RA', $roles)) {
103
            throw new RuntimeException(sprintf(
104
                'Unauthorized to change institution scope to "%s": role (S)RA(A) required, found roles "%s"',
105
                $institution,
106
                implode(', ', $roles)
107
            ));
108
        }
109
110
        $this->getUser()->institution = $institution;
111
        $this->institutionConfigurationOptions = $institutionConfigurationOptions;
112
    }
113
114
    /**
115
     * Returns the user credentials.
116
     *
117
     * @return mixed The user credentials
118
     */
119
    public function getCredentials()
120
    {
121
        return '';
122
    }
123
124
    /**
125
     * @return Loa
126
     */
127
    public function getLoa()
128
    {
129
        return $this->loa;
130
    }
131
132
    public function serialize()
133
    {
134
        return serialize(
135
            [
136
                parent::serialize(),
137
                $this->loa,
138
                $this->institutionConfigurationOptions,
139
                $this->raManagementInstitution,
140
                $this->identityInstitution,
141
                $this->schacHomeOrganization,
142
            ]
143
        );
144
    }
145
146
    public function unserialize($serialized)
147
    {
148
        list($parent, $this->loa, $this->institutionConfigurationOptions, $this->raManagementInstitution, $this->identityInstitution, $this->schacHomeOrganization) = unserialize(
149
            $serialized
150
        );
151
152
        parent::unserialize($parent);
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getIdentityInstitution()
159
    {
160
        // If the identityInstitution is not yet set, fill it with the institution of the identity.
161
        if (!$this->identityInstitution) {
162
            $this->identityInstitution = $this->getUser()->institution;
163
        }
164
        return $this->identityInstitution;
165
    }
166
167
168
    /**
169
     * @return string
170
     */
171
    public function getRaManagementInstitution()
172
    {
173
        if (!$this->raManagementInstitution) {
174
            return $this->getUser()->institution;
175
        }
176
        return $this->raManagementInstitution;
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getSchacHomeInstitution()
183
    {
184
        return $this->schacHomeOrganization;
185
    }
186
}
187