77use Symfony \Component \Security \Core \User \UserInterface ;
88use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
99use 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 ;
1013use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
1114use Kaliop \IdentityManagementBundle \Adapter \ClientInterface ;
1215use Psr \Log \LoggerInterface ;
1922 */
2023class 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
0 commit comments