Skip to content

[Security] Allow custom user identifier for X509 authenticator #48200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
->replaceArgument(2, $firewallName)
->replaceArgument(3, $config['user'])
->replaceArgument(4, $config['credentials'])
->replaceArgument(6, $config['user_identifier'])
;

return $authenticatorId;
Expand All @@ -58,6 +59,7 @@ public function addConfiguration(NodeDefinition $node)
->scalarNode('provider')->end()
->scalarNode('user')->defaultValue('SSL_CLIENT_S_DN_Email')->end()
->scalarNode('credentials')->defaultValue('SSL_CLIENT_S_DN')->end()
->scalarNode('user_identifier')->defaultValue('emailAddress')->end()
->end()
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
abstract_arg('user key'),
abstract_arg('credentials key'),
service('logger')->nullOnInvalid(),
abstract_arg('credentials user identifier'),
])
->tag('monolog.logger', ['channel' => 'security'])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ class X509Authenticator extends AbstractPreAuthenticatedAuthenticator
{
private string $userKey;
private string $credentialsKey;
private string $credentialUserIdentifier;

public function __construct(UserProviderInterface $userProvider, TokenStorageInterface $tokenStorage, string $firewallName, string $userKey = 'SSL_CLIENT_S_DN_Email', string $credentialsKey = 'SSL_CLIENT_S_DN', LoggerInterface $logger = null)
public function __construct(UserProviderInterface $userProvider, TokenStorageInterface $tokenStorage, string $firewallName, string $userKey = 'SSL_CLIENT_S_DN_Email', string $credentialsKey = 'SSL_CLIENT_S_DN', LoggerInterface $logger = null, string $credentialUserIdentifier = 'emailAddress')
{
parent::__construct($userProvider, $tokenStorage, $firewallName, $logger);

$this->userKey = $userKey;
$this->credentialsKey = $credentialsKey;
$this->credentialUserIdentifier = $credentialUserIdentifier;
}

protected function extractUsername(Request $request): string
Expand All @@ -46,13 +48,13 @@ protected function extractUsername(Request $request): string
$username = $request->server->get($this->userKey);
} elseif (
$request->server->has($this->credentialsKey)
&& preg_match('#emailAddress=([^,/@]++@[^,/]++)#', $request->server->get($this->credentialsKey), $matches)
&& preg_match('#'.preg_quote($this->credentialUserIdentifier, '#').'=([^,/]++)#', $request->server->get($this->credentialsKey), $matches)
) {
$username = $matches[1];
$username = trim($matches[1]);
}

if (null === $username) {
throw new BadCredentialsException(sprintf('SSL credentials not found: %s, %s', $this->userKey, $this->credentialsKey));
throw new BadCredentialsException(sprintf('SSL credentials not found: "%s", "%s".', $this->userKey, $this->credentialsKey));
}

return $username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,35 @@ public function testAuthenticationCustomCredentialsKey()
$this->assertEquals('cert@example.com', $passport->getUser()->getUserIdentifier());
}

/**
* @dataProvider provideServerVarsUserIdentifier
*/
public function testAuthenticationCustomCredentialsUserIdentifier($username, $credentials)
{
$authenticator = new X509Authenticator($this->userProvider, new TokenStorage(), 'main', 'SSL_CLIENT_S_DN_Email', 'SSL_CLIENT_S_DN', null, 'CN');

$request = $this->createRequest([
'SSL_CLIENT_S_DN' => $credentials,
]);
$this->assertTrue($authenticator->supports($request));

$this->userProvider->createUser(new InMemoryUser($username, null));

$passport = $authenticator->authenticate($request);
$this->assertEquals($username, $passport->getUser()->getUserIdentifier());
}

public static function provideServerVarsUserIdentifier()
{
yield ['Sample certificate DN', 'CN=Sample certificate DN/emailAddress=cert@example.com'];
yield ['Sample certificate DN', 'CN=Sample certificate DN/emailAddress=cert+something@example.com'];
yield ['Sample certificate DN', 'CN=Sample certificate DN,emailAddress=cert@example.com'];
yield ['Sample certificate DN', 'CN=Sample certificate DN,emailAddress=cert+something@example.com'];
yield ['Sample certificate DN', 'emailAddress=cert+something@example.com,CN=Sample certificate DN'];
yield ['Firstname.Lastname', 'emailAddress=firstname.lastname@mycompany.co.uk,CN=Firstname.Lastname,OU=london,OU=company design and engineering,OU=Issuer London,OU=Roaming,OU=Interactive,OU=Users,OU=Standard,OU=Business,DC=england,DC=core,DC=company,DC=co,DC=uk'];
yield ['user1', 'C=FR, O=My Organization, CN=user1, emailAddress=user1@myorg.fr'];
}

private function createRequest(array $server)
{
return new Request([], [], [], [], [], $server);
Expand Down