LdapAuthenticationProvider   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 77
ccs 31
cts 31
cp 1
rs 10
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkAuthentication() 0 21 6
A retrieveUser() 0 18 4
A __construct() 0 6 1
1
<?php
2
3
namespace DoL\LdapBundle\Security\Authentication;
4
5
use DoL\LdapBundle\Ldap\LdapManagerInterface;
6
use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
7
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
9
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
10
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
11
use Symfony\Component\Security\Core\User\UserCheckerInterface;
12
use Symfony\Component\Security\Core\User\UserInterface;
13
use Symfony\Component\Security\Core\User\UserProviderInterface;
14
15
/**
16
 * Authentication provider.
17
 *
18
 * @author DarwinOnLine
19
 * @author Maks3w
20
 *
21
 * @see https://github.com/DarwinOnLine/DoLLdapBundle
22
 */
23
class LdapAuthenticationProvider extends UserAuthenticationProvider
24
{
25
    /**
26
     * @var UserProviderInterface
27
     */
28
    private $userProvider;
29
30
    /**
31
     * @var LdapManagerInterface
32
     */
33
    private $ldapManager;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param UserCheckerInterface  $userChecker                An UserCheckerInterface interface
39
     * @param string                $providerKey                A provider key
40
     * @param UserProviderInterface $userProvider               An UserProviderInterface interface
41
     * @param LdapManagerInterface  $ldapManager                An LdapProviderInterface interface
42
     * @param bool                  $hideUserNotFoundExceptions Whether to hide user not found exception or not
43
     */
44 10
    public function __construct(UserCheckerInterface $userChecker, $providerKey, UserProviderInterface $userProvider, LdapManagerInterface $ldapManager, $hideUserNotFoundExceptions = true)
45
    {
46 10
        parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions);
47
48 10
        $this->userProvider = $userProvider;
49 10
        $this->ldapManager = $ldapManager;
50 10
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 10
    protected function retrieveUser($username, UsernamePasswordToken $token)
56
    {
57 10
        $user = $token->getUser();
58 10
        if ($user instanceof UserInterface) {
59 4
            return $user;
60
        }
61
62
        try {
63 6
            $user = $this->userProvider->loadUserByUsername($username);
64
65 4
            return $user;
66 2
        } catch (UsernameNotFoundException $notFound) {
67 1
            throw $notFound;
68 1
        } catch (\Exception $repositoryProblem) {
69 1
            $e = new AuthenticationServiceException($repositoryProblem->getMessage(), (int) $repositoryProblem->getCode(), $repositoryProblem);
70 1
            $e->setToken($token);
71
72 1
            throw $e;
73
        }
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 8
    protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
80
    {
81 8
        $currentUser = $token->getUser();
82 8
        $presentedPassword = $token->getCredentials();
83 8
        if ($currentUser instanceof UserInterface) {
84 4
            if ('' === $presentedPassword) {
85 1
                throw new BadCredentialsException(
86
                    'The password in the token is empty. You may forgive turn off `erase_credentials` in your `security.yml`'
87 1
                );
88
            }
89
90 3
            if (!$this->ldapManager->bind($currentUser, $presentedPassword)) {
91 1
                throw new BadCredentialsException('The credentials were changed from another session.');
92
            }
93 2
        } else {
94 4
            if ('' === $presentedPassword) {
95 1
                throw new BadCredentialsException('The presented password cannot be empty.');
96
            }
97
98 3
            if (!$this->ldapManager->bind($user, $presentedPassword)) {
99 1
                throw new BadCredentialsException('The presented password is invalid.');
100
            }
101
        }
102 4
    }
103
}
104