-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathX509Authenticator.php
61 lines (54 loc) · 2.03 KB
/
X509Authenticator.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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Http\Authenticator;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* This authenticator authenticates pre-authenticated (by the
* webserver) X.509 certificates.
*
* @author Wouter de Jong <[email protected]>
* @author Fabien Potencier <[email protected]>
*
* @final
*/
class X509Authenticator extends AbstractPreAuthenticatedAuthenticator
{
public function __construct(
UserProviderInterface $userProvider,
TokenStorageInterface $tokenStorage,
string $firewallName,
private string $userKey = 'SSL_CLIENT_S_DN_Email',
private string $credentialsKey = 'SSL_CLIENT_S_DN',
?LoggerInterface $logger = null,
private string $credentialUserIdentifier = 'emailAddress',
) {
parent::__construct($userProvider, $tokenStorage, $firewallName, $logger);
}
protected function extractUsername(Request $request): string
{
$username = null;
if ($request->server->has($this->userKey)) {
$username = $request->server->get($this->userKey);
} elseif (
$request->server->has($this->credentialsKey)
&& preg_match('#'.preg_quote($this->credentialUserIdentifier, '#').'=([^,/]++)#', $request->server->get($this->credentialsKey), $matches)
) {
$username = trim($matches[1]);
}
if (null === $username) {
throw new BadCredentialsException(\sprintf('SSL credentials not found: "%s", "%s".', $this->userKey, $this->credentialsKey));
}
return $username;
}
}