-
Notifications
You must be signed in to change notification settings - Fork 13
/
SamlAuthenticator.php
221 lines (184 loc) · 9.18 KB
/
SamlAuthenticator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php declare(strict_types=1);
// SPDX-License-Identifier: BSD-3-Clause
namespace Nbgrp\OneloginSamlBundle\Security\Http\Authenticator;
use Nbgrp\OneloginSamlBundle\Event\UserCreatedEvent;
use Nbgrp\OneloginSamlBundle\Event\UserModifiedEvent;
use Nbgrp\OneloginSamlBundle\Idp\IdpResolverInterface;
use Nbgrp\OneloginSamlBundle\Onelogin\AuthRegistryInterface;
use Nbgrp\OneloginSamlBundle\Security\Http\Authenticator\Passport\Badge\SamlAttributesBadge;
use Nbgrp\OneloginSamlBundle\Security\Http\Authenticator\Token\SamlToken;
use Nbgrp\OneloginSamlBundle\Security\User\SamlUserFactoryInterface;
use Nbgrp\OneloginSamlBundle\Security\User\SamlUserInterface;
use OneLogin\Saml2\Auth;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Core\Exception\LogicException;
use Symfony\Component\Security\Core\Exception\SessionUnavailableException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\HttpUtils;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class SamlAuthenticator implements AuthenticatorInterface, AuthenticationEntryPointInterface
{
public const SESSION_INDEX_ATTRIBUTE = '_saml_session_index';
public const LAST_REQUEST_ID = '_saml_last_request_id';
public function __construct(
private HttpUtils $httpUtils,
private UserProviderInterface $userProvider,
private IdpResolverInterface $idpResolver,
private AuthRegistryInterface $authRegistry,
private AuthenticationSuccessHandlerInterface $successHandler,
private AuthenticationFailureHandlerInterface $failureHandler,
private array $options,
private ?SamlUserFactoryInterface $userFactory,
private ?EventDispatcherInterface $eventDispatcher,
private ?LoggerInterface $logger,
private string $idpParameterName,
) {}
public function supports(Request $request): ?bool
{
return $request->isMethod('POST')
&& $this->httpUtils->checkRequestPath($request, (string) $this->options['check_path']);
}
public function start(Request $request, ?AuthenticationException $authException = null): Response
{
$uri = $this->httpUtils->generateUri($request, (string) $this->options['login_path']);
$idp = $this->idpResolver->resolve($request);
if ($idp) {
$uri .= '?'.$this->idpParameterName.'='.$idp;
}
return new RedirectResponse($uri);
}
public function authenticate(Request $request): Passport
{
if (!$request->hasSession()) {
throw new SessionUnavailableException('This authentication method requires a session.');
}
if ($this->options['require_previous_session'] && !$request->hasPreviousSession()) {
throw new SessionUnavailableException('Your session has timed out, or you have disabled cookies.');
}
$oneLoginAuth = $this->getOneLoginAuth($request);
$this->processResponse($oneLoginAuth, $request->getSession());
if ($oneLoginAuth->getErrors()) {
$errorReason = $oneLoginAuth->getLastErrorReason() ?? 'Undefined OneLogin auth error.';
$this->logger?->error($errorReason);
throw new AuthenticationException($errorReason);
}
return $this->createPassport($oneLoginAuth);
}
public function createToken(Passport $passport, string $firewallName): TokenInterface
{
if (!$passport->hasBadge(SamlAttributesBadge::class)) {
throw new LogicException(sprintf('Passport should contains a "%s" badge.', SamlAttributesBadge::class));
}
$badge = $passport->getBadge(SamlAttributesBadge::class);
$attributes = [];
if ($badge instanceof SamlAttributesBadge) {
$attributes = $badge->getAttributes();
}
return new SamlToken($passport->getUser(), $firewallName, $passport->getUser()->getRoles(), $attributes);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
return $this->successHandler->onAuthenticationSuccess($request, $token);
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
return $this->failureHandler->onAuthenticationFailure($request, $exception);
}
protected function processResponse(Auth $oneLoginAuth, SessionInterface $session): void
{
$requestId = null;
$security = $oneLoginAuth->getSettings()->getSecurityData();
if ($security['rejectUnsolicitedResponsesWithInResponseTo'] ?? false) {
/** @var string $requestId */
$requestId = $session->get(self::LAST_REQUEST_ID);
}
$oneLoginAuth->processResponse($requestId);
}
protected function createPassport(Auth $oneLoginAuth): Passport
{
$attributes = $this->extractAttributes($oneLoginAuth);
$this->logger?->debug('SAML attributes extracted', $attributes);
$userBadge = new UserBadge(
$this->extractIdentifier($oneLoginAuth, $attributes),
function (string $identifier) use ($attributes) {
try {
try {
$user = $this->userProvider->loadUserByIdentifier($identifier);
if ($user instanceof SamlUserInterface) {
$user->setSamlAttributes($attributes);
$this->eventDispatcher?->dispatch(new UserModifiedEvent($user));
}
} catch (UserNotFoundException $exception) {
if (!$this->userFactory instanceof SamlUserFactoryInterface) {
throw $exception;
}
$user = $this->userFactory->createUser($identifier, $attributes);
$this->eventDispatcher?->dispatch(new UserCreatedEvent($user));
}
} catch (\Throwable $exception) {
if ($exception instanceof UserNotFoundException) {
throw $exception;
}
throw new AuthenticationException('The authentication failed.', 0, $exception);
}
return $user;
},
);
return new SelfValidatingPassport($userBadge, [new SamlAttributesBadge($attributes)]);
}
protected function extractAttributes(Auth $oneLoginAuth): array
{
$attributes = $this->options['use_attribute_friendly_name'] ?? false
? $oneLoginAuth->getAttributesWithFriendlyName()
: $oneLoginAuth->getAttributes();
$attributes[self::SESSION_INDEX_ATTRIBUTE] = $oneLoginAuth->getSessionIndex();
return $attributes;
}
protected function extractIdentifier(Auth $oneLoginAuth, array $attributes): string
{
if (\array_key_exists('identifier_attribute', $this->options)) {
$identifierAttribute = (string) $this->options['identifier_attribute'];
if (!\array_key_exists($identifierAttribute, $attributes)) {
throw new \RuntimeException('Attribute "'.$identifierAttribute.'" not found in SAML data.');
}
$identifier = $attributes[$identifierAttribute];
if (\is_array($identifier)) {
/** @var mixed $identifier */
$identifier = reset($identifier);
}
if (!\is_string($identifier)) {
throw new \RuntimeException('Attribute "'.$identifierAttribute.'" does not contain valid user identifier.');
}
return $identifier;
}
return $oneLoginAuth->getNameId();
}
private function getOneLoginAuth(Request $request): Auth
{
try {
$idp = $this->idpResolver->resolve($request);
$authService = $idp
? $this->authRegistry->getService($idp)
: $this->authRegistry->getDefaultService();
} catch (\RuntimeException $exception) {
$this->logger?->error($exception->getMessage());
throw new AuthenticationServiceException($exception->getMessage());
}
return $authService;
}
}