Completed
Push — master ( b288e2...4e6c2b )
by Michael
05:02 queued 02:08
created

SimplePreAuthenticator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 0
loc 91
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createToken() 0 20 4
A supportsToken() 0 4 2
B authenticateToken() 0 31 4
1
<?php
2
3
/*
4
 * This file is part of the OsLabSecurityApiBundle package.
5
 *
6
 * (c) OsLab <https://github.com/OsLab>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OsLab\SecurityApiBundle\Security\Authentication;
13
14
use OsLab\SecurityApiBundle\Security\User\InMemoryApiUserProvider;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
17
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
20
use Symfony\Component\Security\Core\User\UserProviderInterface;
21
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
22
23
/**
24
 * Class SimplePreAuthenticator
25
 *
26
 * @author Michael COULLERET <[email protected]>
27
 * @author Florent DESPIERRES <[email protected]>
28
 */
29
class SimplePreAuthenticator implements SimplePreAuthenticatorInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $keyName;
35
36
    /**
37
     * @var string
38
     */
39
    protected $method;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string $keyName
45
     * @param string $method
46
     */
47 24
    public function __construct($keyName, $method)
48
    {
49 24
        $this->keyName = $keyName;
50 24
        $this->method  = $method;
51 24
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 9
    public function createToken(Request $request, $providerKey)
57
    {
58 9
        $apiKey = null;
59
60 9
        if ($this->method === 'header') {
61 3
            $apiKey = $request->headers->get($this->keyName);
62 6
        } elseif ($this->method === 'query') {
63 3
            $apiKey = $request->query->get($this->keyName);
64
        }
65
66 9
        if (isset($apiKey) === false) {
67 3
            throw new AccessDeniedException(sprintf('The key "%s" is not provided', $this->keyName));
68
        }
69
70 6
        return new PreAuthenticatedToken(
71 6
            'anon.',
72
            $apiKey,
73
            $providerKey
74
        );
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 6
    public function supportsToken(TokenInterface $token, $providerKey)
81
    {
82 6
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() == $providerKey;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 9
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
89
    {
90 9
        if (!$userProvider instanceof InMemoryApiUserProvider) {
91 3
            throw new \InvalidArgumentException(
92
                sprintf(
93 3
                    'The user provider must be an instance of %s; (%s was given).',
94 3
                    InMemoryApiUserProvider::class,
95
                    get_class($userProvider)
96
                )
97
            );
98
        }
99
100 6
        $apiKey   = $token->getCredentials();
101 6
        $username = $userProvider->getUsernameByApiKey($apiKey);
102
103 6
        if (isset($username) === false && strlen($username) <= 0) {
104 3
            $exception = new UsernameNotFoundException(sprintf('API Key "%s" does not exist.', $apiKey));
105 3
            $exception->setUsername($username);
106
107 3
            throw $exception;
108
        }
109
110 3
        $user = $userProvider->loadUserByUsername($username);
111
112 3
        return new PreAuthenticatedToken(
113
            $user,
114
            $apiKey,
115
            $providerKey,
116 3
            $user->getRoles()
117
        );
118
    }
119
}
120