|
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
|
|
|
public function __construct( |
|
46
|
|
|
Loa $loa, |
|
47
|
|
|
array $roles = [], |
|
48
|
|
|
InstitutionConfigurationOptions $institutionConfigurationOptions = null |
|
|
|
|
|
|
49
|
|
|
) { |
|
50
|
|
|
parent::__construct($roles); |
|
51
|
|
|
|
|
52
|
|
|
$this->loa = $loa; |
|
53
|
|
|
$this->setAuthenticated(count($roles)); |
|
|
|
|
|
|
54
|
|
|
$this->institutionConfigurationOptions = $institutionConfigurationOptions; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return InstitutionConfigurationOptions |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getInstitutionConfigurationOptions() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->institutionConfigurationOptions; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $institution |
|
67
|
|
|
* @param InstitutionConfigurationOptions $institutionConfigurationOptions |
|
68
|
|
|
*/ |
|
69
|
|
|
public function changeInstitutionScope( |
|
70
|
|
|
$institution, |
|
71
|
|
|
InstitutionConfigurationOptions $institutionConfigurationOptions |
|
|
|
|
|
|
72
|
|
|
) { |
|
73
|
|
|
if ($this->getUser() === null) { |
|
74
|
|
|
throw new LogicException('Cannot change institution scope: token does not contain a user'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$roles = array_map(function (RoleInterface $role) { |
|
78
|
|
|
return $role->getRole(); |
|
79
|
|
|
}, $this->getRoles()); |
|
80
|
|
|
|
|
81
|
|
|
if (!in_array('ROLE_SRAA', $roles)) { |
|
82
|
|
|
throw new RuntimeException(sprintf( |
|
83
|
|
|
'Unauthorized to change institution scope to "%s": role SRAA required, found roles "%s"', |
|
84
|
|
|
$institution, |
|
85
|
|
|
implode(', ', $roles) |
|
86
|
|
|
)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->getUser()->institution = $institution; |
|
90
|
|
|
$this->institutionConfigurationOptions = $institutionConfigurationOptions; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns the user credentials. |
|
95
|
|
|
* |
|
96
|
|
|
* @return mixed The user credentials |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getCredentials() |
|
99
|
|
|
{ |
|
100
|
|
|
return ''; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return Loa |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getLoa() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->loa; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function serialize() |
|
112
|
|
|
{ |
|
113
|
|
|
return serialize([parent::serialize(), $this->loa, $this->institutionConfigurationOptions]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function unserialize($serialized) |
|
117
|
|
|
{ |
|
118
|
|
|
list($parent, $this->loa, $this->institutionConfigurationOptions) = unserialize($serialized); |
|
119
|
|
|
|
|
120
|
|
|
parent::unserialize($parent); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.