GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

LdapAuthenticationProvider   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 69
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A authenticate() 0 10 3
A checkLogin() 0 14 2
A supports() 0 4 1
1
<?php
2
namespace SOG\Dashboard\Authentication;
3
4
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
7
use Symfony\Component\Security\Core\Exception\AuthenticationException;
8
use Symfony\Component\Security\Core\User\UserProviderInterface;
9
use Zend\Ldap\Exception\LdapException;
10
use Zend\Ldap\Ldap;
11
12
/**
13
 * This class attempts the authentication of a given token using the LDAP resource.
14
 * The implementation follows https://github.com/DerManoMann/ldap-auth-service-provider - thank you!
15
 *
16
 * Class LdapAuthenticationProvider
17
 * @package SOG\Dashboard\Authentication
18
 */
19
class LdapAuthenticationProvider implements AuthenticationProviderInterface
20
{
21
22
    /**
23
     * @var Ldap The actual LDAP resource to call methods on
24
     */
25
    private $ldap;
26
    /**
27
     * @var UserProviderInterface The instance of our LDAP User Provider
28
     */
29
    private $user_provider;
30
31
    /**
32
     * LdapAuthenticationProvider constructor, set members.
33
     *
34
     * @param Ldap $ldap
35
     * @param UserProviderInterface $user_provider
36
     */
37
    public function __construct(Ldap $ldap, UserProviderInterface $user_provider)
38
    {
39
        $this->ldap = $ldap;
40
        $this->user_provider = $user_provider;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function authenticate(TokenInterface $token)
47
    {
48
        $user = $this->user_provider->loadUserByUsername($token->getUsername());
49
        if ($user && $this->checkLogin($token->getUsername(), $token->getCredentials())) {
50
            $roles = array_unique(array_merge($token->getRoles(), $user->getRoles()));
51
            return new UsernamePasswordToken($user, null, 'ldap', $roles);
52
        }
53
54
        throw new AuthenticationException('Der Login war nicht erfolgreich, bitte überprüfe deinen Benutzernamen und Passwort.');
55
    }
56
57
    /**
58
     * This method tries to authenticate the given user, the LDAP resource is then bound to this account.
59
     * By calling `$ldap->bind()` (without parameters) afterwards, the resource is again bound to the privileged account.
60
     *
61
     * @param string $user The username to use for binding
62
     * @param string $password The password to use for binding
63
     * @return bool Returns true if the bind was successful, false otherwise.
64
     */
65
    private function checkLogin($user, $password)
66
    {
67
        $success = false;
68
        try {
69
            $this->ldap->bind($user, $password);
70
            $success = true;
71
        } catch (LdapException $ex) {
72
            $success = false;
73
        } finally {
74
            // rebind to privileged user
75
            $this->ldap->bind();
76
        }
77
        return $success;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function supports(TokenInterface $token)
84
    {
85
        return ($token instanceof UsernamePasswordToken);
86
    }
87
}