AdAuthProvider::authenticate()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 32
rs 8.439
c 2
b 0
f 1
cc 5
eloc 22
nc 5
nop 1
1
<?php
2
3
namespace Riper\Security\ActiveDirectoryBundle\Security\Authentication;
4
5
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
6
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
7
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
10
use Symfony\Component\Translation\TranslatorInterface;
11
use Riper\Security\ActiveDirectoryBundle\Security\User\AdUserProvider;
12
use Riper\Security\ActiveDirectoryBundle\Security\User\AdUser;
13
use Riper\Security\ActiveDirectoryBundle\Service\AdldapService;
14
15
class AdAuthProvider implements AuthenticationProviderInterface
16
{
17
    /**
18
     * @var AdUserProvider
19
     */
20
    private $userProvider;
21
22
    /**
23
     * @var TranslatorInterface
24
     */
25
    private $translator;
26
27
    public function __construct(
28
        AdUserProvider $userProvider,
29
        array $config,
30
        AdldapService $AdldapService,
31
        TranslatorInterface $translator,
32
        $tokenClasses,
33
        $riperConfig
34
    ) {
35
        $this->userProvider = $userProvider;
36
        $this->config = $config;
37
        $this->AdldapService = $AdldapService;
38
        $this->translator = $translator;
39
        $this->tokenClasses = $tokenClasses;
40
        $this->riperConfig = $riperConfig;
41
    }
42
43
    /**
44
     * Attempts to authenticates a TokenInterface object.
45
     *
46
     * @param TokenInterface $token The TokenInterface instance to authenticate
47
     *
48
     * @return TokenInterface An authenticated TokenInterface instance, never null
49
     *
50
     * @throws AuthenticationException if the authentication fails
51
     */
52
    public function authenticate(TokenInterface $token)
53
    {
54
        $Adldap = $this->AdldapService->getInstance();
55
        $User = $this->userProvider->loadUserByUsername($token->getUsername());
56
        if ($User instanceof AdUser) {
57
            if (!$Adldap->authenticate($User->getUsername(), $token->getCredentials())) {
58
                $msg = $this->translator->trans(
59
                    'riper.security.active_directory.wrong_credential'
60
                ); //'The credentials are wrong'
61
                throw new BadCredentialsException($msg);
62
            }
63
            $this->userProvider->fetchData($User, $token, $Adldap);
64
        }
65
66
        if (isset($this->riperConfig['keep_password_in_token']) && $this->riperConfig['keep_password_in_token']) {
67
            $newToken = new $this->tokenClasses['faulty'](
68
                $User,
69
                $token->getCredentials(),
70
                'riper.security.active.directory.user.provider',
71
                $User->getRoles()
72
            );
73
        } else {
74
            $newToken = new $this->tokenClasses['standard'](
75
                $User,
76
                $token->getCredentials(),
77
                'riper.security.active.directory.user.provider',
78
                $User->getRoles()
79
            );
80
        }
81
82
        return $newToken;
83
    }
84
85
    /**
86
     * Checks whether this provider supports the given token.
87
     *
88
     * @param TokenInterface $token A TokenInterface instance
89
     *
90
     * @return Boolean true if the implementation supports the Token, false otherwise
91
     */
92
    public function supports(TokenInterface $token)
93
    {
94
        return $token instanceof UsernamePasswordToken;
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...n\UsernamePasswordToken does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
95
    }
96
}
97