1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2014 SURFnet bv |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
|
|
|
|
20
|
|
|
|
21
|
|
|
namespace Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Provider; |
22
|
|
|
|
23
|
|
|
use BadMethodCallException; |
24
|
|
|
use Psr\Log\LoggerInterface; |
25
|
|
|
use SAML2\Assertion; |
26
|
|
|
use Surfnet\SamlBundle\SAML2\Attribute\AttributeDictionary; |
27
|
|
|
use Surfnet\SamlBundle\SAML2\Response\AssertionAdapter; |
28
|
|
|
use Surfnet\SamlBundle\Security\Authentication\Provider\SamlProviderInterface; |
29
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
30
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Uuid\Uuid; |
31
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\MissingRequiredAttributeException; |
32
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Locale\PreferredLocaleProvider; |
33
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\AuthenticatedIdentity; |
34
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Security\Authentication\Token\SamlToken; |
35
|
|
|
use Surfnet\StepupSelfService\SelfServiceBundle\Service\IdentityService; |
36
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
37
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) -- Hard to reduce due to different commands and queries used. |
41
|
|
|
*/ |
|
|
|
|
42
|
|
|
class SamlProvider implements SamlProviderInterface, UserProviderInterface |
43
|
|
|
{ |
44
|
|
|
public function __construct( |
|
|
|
|
45
|
|
|
private readonly IdentityService $identityService, |
46
|
|
|
private readonly AttributeDictionary $attributeDictionary, |
47
|
|
|
private readonly PreferredLocaleProvider $preferredLocaleProvider, |
48
|
|
|
private readonly LoggerInterface $logger |
49
|
|
|
) { |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
private function getSingleStringValue(string $attribute, AssertionAdapter $translatedAssertion): string |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$values = $translatedAssertion->getAttributeValue($attribute); |
58
|
|
|
|
59
|
|
|
if (empty($values)) { |
60
|
|
|
throw new MissingRequiredAttributeException( |
61
|
|
|
sprintf('Missing value for required attribute "%s"', $attribute) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// see https://www.pivotaltracker.com/story/show/121296389 |
66
|
|
|
if (count($values) > 1) { |
67
|
|
|
$this->logger->warning(sprintf( |
|
|
|
|
68
|
|
|
'Found "%d" values for attribute "%s", using first value', |
69
|
|
|
count($values), |
70
|
|
|
$attribute |
71
|
|
|
)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$value = reset($values); |
75
|
|
|
|
76
|
|
|
if (!is_string($value)) { |
77
|
|
|
$message = sprintf( |
78
|
|
|
'First value of attribute "%s" must be a string, "%s" given', |
79
|
|
|
$attribute, |
80
|
|
|
get_debug_type($value) |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->logger->warning($message); |
84
|
|
|
|
85
|
|
|
throw new MissingRequiredAttributeException($message); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getNameId(Assertion $assertion): string |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
return $this->attributeDictionary->translate($assertion)->getNameID(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getUser(Assertion $assertion): UserInterface |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$translatedAssertion = $this->attributeDictionary->translate($assertion); |
99
|
|
|
|
100
|
|
|
$nameId = $translatedAssertion->getNameID(); |
101
|
|
|
$institution = $this->getSingleStringValue('schacHomeOrganization', $translatedAssertion); |
102
|
|
|
$email = $this->getSingleStringValue('mail', $translatedAssertion); |
103
|
|
|
$commonName = $this->getSingleStringValue('commonName', $translatedAssertion); |
104
|
|
|
|
105
|
|
|
$identity = $this->identityService->findByNameIdAndInstitution($nameId, $institution); |
|
|
|
|
106
|
|
|
|
107
|
|
|
if (!$identity instanceof Identity) { |
108
|
|
|
$identity = new Identity(); |
109
|
|
|
$identity->id = Uuid::generate(); |
110
|
|
|
$identity->nameId = $nameId; |
111
|
|
|
$identity->institution = $institution; |
112
|
|
|
$identity->email = $email; |
113
|
|
|
$identity->commonName = $commonName; |
114
|
|
|
$identity->preferredLocale = $this->preferredLocaleProvider->providePreferredLocale(); |
115
|
|
|
|
116
|
|
|
$this->identityService->createIdentity($identity); |
117
|
|
|
} elseif ($identity->email !== $email || $identity->commonName !== $commonName) { |
118
|
|
|
$identity->email = $email; |
119
|
|
|
$identity->commonName = $commonName; |
120
|
|
|
|
121
|
|
|
$this->identityService->updateIdentity($identity); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$authenticatedIdentity = new AuthenticatedIdentity($identity); |
125
|
|
|
$authenticatedToken = new SamlToken(['ROLE_USER']); |
126
|
|
|
|
127
|
|
|
$authenticatedToken->setUser($authenticatedIdentity); |
128
|
|
|
|
129
|
|
|
// return $authenticatedToken; |
|
|
|
|
130
|
|
|
return $authenticatedIdentity; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
public function refreshUser(UserInterface $user): UserInterface |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
// TODO: Implement refreshUser() method. |
137
|
|
|
return $user; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function supportsClass(string $class): bool |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
return $class === AuthenticatedIdentity::class; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function loadUserByIdentifier(string $identifier): UserInterface |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
throw new BadMethodCallException('Use `getUser` to load a user by username'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|