Skip to content

Commit 1044ba3

Browse files
author
Gaetano Giunta
committed
Allow the auth error message to be customized (it was almost impossible before)
1 parent 2e073d5 commit 1044ba3

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

Adapter/ClientInterface.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kaliop\IdentityManagementBundle\Adapter;
44

5+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
56
use Kaliop\IdentityManagementBundle\Security\User\RemoteUser;
67

78
interface ClientInterface
@@ -10,7 +11,7 @@ interface ClientInterface
1011
* @param string $login
1112
* @param string $password
1213
* @return RemoteUser
13-
* @throws BadCredentialsException|AuthenticationServiceException
14+
* @throws AuthenticationException
1415
*/
1516
public function authenticateUser($login, $password);
16-
}
17+
}

Resources/config/services.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
parameters:
2-
#kaliop_identity.interactive_event_listener.class: Kaliop\IdentityManagementBundle\EventListener\InteractiveLoginListener
32
kaliop_identity.security.ip_to_user_mapper.class: Kaliop\IdentityManagementBundle\Security\Helper\IpToUserMapper
43
kaliop_identity.security.authentication.provider.ip: Kaliop\IdentityManagementBundle\Security\Authentication\Provider\IPAuthenticationProvider
54
kaliop_identity.security.authentication.listener.ip.class: Kaliop\IdentityManagementBundle\Security\Firewall\IPListener
@@ -14,6 +13,9 @@ parameters:
1413
# Kaliop\IdentityManagementBundle\Adapter\AMS\RemoteUser: kaliop_identity.security.remoteuser_handler.ams
1514
kaliop_identity.remoteuser_service_map: {}
1615

16+
# When set to true, authentication errors of type UsernameNotFoundException, BadCredentialsException and unknown
17+
# exceptions will be masked with a standard error message
18+
kaliop_identity.mask_user_exceptions: true
1719

1820
services:
1921

@@ -49,7 +51,7 @@ services:
4951
- "" # A client service. Will be injected from the config of the firewall
5052
- "" # A user provider service. Will be injected from the config of the firewall
5153
- "" # Firewall id. Will be injected from the config of the firewall
52-
- true
54+
- %kaliop_identity.mask_user_exceptions%
5355
abstract: true
5456
# the security listener is just a plain form-based one
5557
kaliop_identity.security.authentication.listener.remoteuser:

Security/Authentication/Provider/RemoteUserAuthenticationProvider.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Symfony\Component\Security\Core\User\UserInterface;
88
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
99
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
10+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
11+
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
12+
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1013
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1114
use Kaliop\IdentityManagementBundle\Adapter\ClientInterface;
1215
use Psr\Log\LoggerInterface;
@@ -19,11 +22,18 @@
1922
*/
2023
class RemoteUserAuthenticationProvider implements AuthenticationProviderInterface
2124
{
25+
/**
26+
* @var bool $hideUserNotFoundExceptions when true, auth exceptions of type UsernameNotFoundException,
27+
* BadCredentialsException and unkown (non-AuthenticationException) will be masked with a standard error message
28+
*/
2229
protected $hideUserNotFoundExceptions;
2330
//protected $userChecker;
2431
protected $providerKey;
32+
/** @var ClientInterface $client */
2533
protected $client;
34+
/** @var UserProviderInterface $userProvider */
2635
protected $userProvider;
36+
/** @var LoggerInterface|null $logger */
2737
protected $logger;
2838

2939
public function __construct(ClientInterface $client, UserProviderInterface $userProvider, $providerKey, $hideUserNotFoundExceptions = true)
@@ -52,10 +62,8 @@ public function supports(TokenInterface $token)
5262
* fetch 1st, then check pwd, we do fetch-while-checking-pwd
5363
*
5464
* @param TokenInterface $token
55-
* @return UsernamePasswordToken|void
56-
* @throws AuthenticationServiceException
57-
* @throws UsernameNotFoundException
58-
* @throws \Exception
65+
* @return UsernamePasswordToken
66+
* @throws AuthenticationException
5967
*
6068
* @see DaoAuthenticationProvider
6169
*/
@@ -65,14 +73,14 @@ public function authenticate(TokenInterface $token)
6573
return;
6674
}
6775

76+
/* we can not fetch the user 1st based on his login
6877
/// @todo throw a BadCredentialsException instead ?
6978
$username = $token->getUsername();
7079
if ('' === $username || null === $username) {
7180
$username = 'NONE_PROVIDED';
7281
}
7382
74-
// we can not fetch the user 1st based on his login
75-
/* try {
83+
try {
7684
$user = $this->retrieveUser($username, $token);
7785
} catch (UsernameNotFoundException $e) {
7886
if ($this->hideUserNotFoundExceptions) {
@@ -92,6 +100,12 @@ public function authenticate(TokenInterface $token)
92100
$user = $this->retrieveUserAndCheckAuthentication($token);
93101
/// @todo !important reintroduce this check?
94102
//$this->userChecker->checkPostAuth($user);
103+
} catch (UsernameNotFoundException $e) {
104+
if ($this->hideUserNotFoundExceptions) {
105+
throw new BadCredentialsException('Bad credentials.', 0, $e);
106+
}
107+
108+
throw $e;
95109
} catch (BadCredentialsException $e) {
96110
if ($this->hideUserNotFoundExceptions) {
97111
throw new BadCredentialsException('Bad credentials.', 0, $e);
@@ -109,6 +123,7 @@ public function authenticate(TokenInterface $token)
109123
/**
110124
* @param UsernamePasswordToken $token
111125
* @return mixed|UserInterface
126+
* @throws BadCredentialsException|AuthenticationException
112127
*/
113128
protected function retrieveUserAndCheckAuthentication(UsernamePasswordToken $token)
114129
{
@@ -119,6 +134,7 @@ protected function retrieveUserAndCheckAuthentication(UsernamePasswordToken $tok
119134
if ($currentUser->getPassword() !== $token->getCredentials()) {
120135
throw new BadCredentialsException('The credentials were changed from another session.');
121136
}
137+
122138
return $currentUser;
123139

124140
} else {
@@ -140,8 +156,14 @@ protected function retrieveUserAndCheckAuthentication(UsernamePasswordToken $tok
140156
//$user = $this->userProvider->loadUserByUsername($username);
141157
return $user;
142158

159+
} catch(AuthenticationException $e) {
160+
// let through any exception of the expected authentication type
161+
throw $e;
143162
} catch(\Exception $e) {
144-
throw new BadCredentialsException('The presented username or password is invalid.');
163+
// we mask any internal, unexpected error from the Client
164+
/// @todo we should log a message here: the Client used an unexpected exception type...
165+
/// @tood we should really be using an AuthenticationServiceException here
166+
throw new BadCredentialsException('The presented username or password is invalid.', 0, $e);
145167
}
146168

147169
// no need to check the password after loading the user: the remote ws does that

ezpublish_legacy/identitymanagementextension/login_handler/ezremoteuserloginuser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ protected static function _loginUser( $login, $password, $authenticationMatch =
6161
return self::fetch($user->id);
6262

6363
} catch(\Exception $e) {
64+
/// @todo make it easier to tell apart system error from user errors such as bad password...
65+
6466
eZDebug::writeError($e->getMessage(), __METHOD__ );
6567

6668
return false;
6769
}
68-
69-
$checker = $container->get('security.authorization_checker');
7070
}
7171

7272
}

todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Kaliop Identity Management Bundle
2727
- to do: check if it is a good idea to remove the 'remoteuser' provider in app/security.yml. Remoteusers after all are
2828
not meant to be used as actual logged in users anyway
2929
- to do: add support for forgotpassword
30-
- to do: add more comprehensive logging support
30+
- to do: add more comprehensive logging
3131
- to do: add an interface for RemoteUserHandler classes
3232

3333
out of scope (but could be done):

0 commit comments

Comments
 (0)