IPAuthenticationProvider::authenticate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.7
cc 3
nc 4
nop 1
1
<?php
2
3
namespace Kaliop\IdentityManagementBundle\Security\Authentication\Provider;
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\User\UserProviderInterface;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Kaliop\IdentityManagementBundle\Security\Authentication\Token\IPToken;
10
11
class IPAuthenticationProvider implements AuthenticationProviderInterface
12
{
13
    protected $ipToUserMapper;
14
    protected $userProvider;
15
16
    public function __construct($ipToUserMapper, UserProviderInterface $userProvider)
17
    {
18
        $this->ipToUserMapper = $ipToUserMapper;
19
        $this->userProvider = $userProvider;
20
    }
21
22
    public function supports(TokenInterface $token)
23
    {
24
        return $token instanceof \Kaliop\IdentityManagementBundle\Security\Authentication\Token\IPToken;
25
    }
26
27
    public function authenticate(TokenInterface $token)
28
    {
29
        $user = $this->ipToUserMapper->getInternalUser($token->getClientIp());
30
        if ($user) {
31
            $user = $this->userProvider->loadUserByUsername($user);
32
        }
33
        if ($user)
34
        {
35
            $authenticatedToken = new IPToken($user->getRoles());
36
            $authenticatedToken->setClientIp($token->getClientIp())->setUser($user);
37
            $authenticatedToken->setAuthenticated(true);
38
39
            return $authenticatedToken;
40
        }
41
42
        throw new AuthenticationException('No valid IP auth found');
43
    }
44
}