Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

RepositoryAuthenticationProvider.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Security\Authentication;
8
9 use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\SecurityPass;
10 use eZ\Publish\API\Repository\Repository;
11 use eZ\Publish\Core\MVC\Symfony\Security\UserInterface as EzUserInterface;
12 use JMS\TranslationBundle\Logger\LoggerAwareInterface;
13 use Psr\Log\LoggerAwareTrait;
14 use Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider;
15 use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
17 use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18 use Symfony\Component\Security\Core\User\UserInterface;
19
20 class RepositoryAuthenticationProvider extends DaoAuthenticationProvider implements LoggerAwareInterface
21 {
22 use LoggerAwareTrait;
23
25 private $constantAuthTime;
26
28 private $repository;
29
30 public function setConstantAuthTime(float $constantAuthTime)
31 {
32 $this->constantAuthTime = $constantAuthTime;
33 }
34
35 public function setRepository(Repository $repository)
36 {
37 $this->repository = $repository;
38 }
39
40 protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
41 {
42 if (!$user instanceof EzUserInterface) {
43 return parent::checkAuthentication($user, $token);
44 }
45
46 $apiUser = $user->getAPIUser();
47
48 // $currentUser can either be an instance of UserInterface or just the username (e.g. during form login).
50 $currentUser = $token->getUser();
51 if ($currentUser instanceof UserInterface) {
52 if ($currentUser->getAPIUser()->passwordHash !== $apiUser->passwordHash) {
53 throw new BadCredentialsException('The credentials were changed from another session.');
54 }
55
56 $apiUser = $currentUser->getAPIUser();
57 } else {
58 $credentialsValid = $this->repository->getUserService()->checkUserCredentials($apiUser, $token->getCredentials());
59
60 if (!$credentialsValid) {
61 throw new BadCredentialsException('Invalid credentials', 0);
62 }
63 }
64
65 // Finally inject current user in the Repository
66 $this->repository->setCurrentUser($apiUser);
67 }
68
69 public function authenticate(TokenInterface $token)
70 {
71 $startTime = $this->startConstantTimer();
72
73 try {
74 $result = parent::authenticate($token);
75 } catch (\Exception $e) {
76 $this->sleepUsingConstantTimer($startTime);
77 throw $e;
78 }
79
80 $this->sleepUsingConstantTimer($startTime);
81
82 return $result;
83 }
84
85 private function startConstantTimer()
86 {
87 return microtime(true);
88 }
89
90 private function sleepUsingConstantTimer(float $startTime): void
91 {
92 if ($this->constantAuthTime <= 0.0) {
93 return;
94 }
95
96 $remainingTime = $this->constantAuthTime - (microtime(true) - $startTime);
97 if ($remainingTime > 0) {
98 usleep($remainingTime * 1000000);
99 } elseif ($this->logger) {
100 $this->logger->warning(
101 sprintf(
102 'Authentication took longer than the configured constant time. Consider increasing the value of %s',
103 SecurityPass::CONSTANT_AUTH_TIME_SETTING
104 ),
105 [get_class($this)]
106 );
107 }
108 }
109 }
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\SecurityPass
Security related compiler pass.
Definition: SecurityPass.php:23
eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler\SecurityPass\CONSTANT_AUTH_TIME_SETTING
const CONSTANT_AUTH_TIME_SETTING
Definition: SecurityPass.php:24
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider
Definition: RepositoryAuthenticationProvider.php:21
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\authenticate
authenticate(TokenInterface $token)
Definition: RepositoryAuthenticationProvider.php:69
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\$repository
$repository
Definition: RepositoryAuthenticationProvider.php:28
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\setConstantAuthTime
setConstantAuthTime(float $constantAuthTime)
Definition: RepositoryAuthenticationProvider.php:30
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\startConstantTimer
startConstantTimer()
Definition: RepositoryAuthenticationProvider.php:85
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\sleepUsingConstantTimer
sleepUsingConstantTimer(float $startTime)
Definition: RepositoryAuthenticationProvider.php:90
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\$constantAuthTime
$constantAuthTime
Definition: RepositoryAuthenticationProvider.php:25
eZ\Publish\Core\MVC\Symfony\Security\Authentication\RepositoryAuthenticationProvider\setRepository
setRepository(Repository $repository)
Definition: RepositoryAuthenticationProvider.php:35
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\Core\MVC\Symfony\Security\UserInterface
Interface for Repository based users.
Definition: UserInterface.php:16
eZ\Publish\Core\MVC\Symfony\Security\Authentication
Definition: AnonymousAuthenticationProvider.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.