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.

ApiKeyAuthenticator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 13

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 13
dl 0
loc 135
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A onAuthenticationFailure() 0 4 1
A supportsToken() 0 4 2
A createToken() 0 14 2
B authenticateToken() 0 31 3
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Security;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Ma27\ApiKeyAuthenticationBundle\Event\OnFirewallAuthenticationEvent;
7
use Ma27\ApiKeyAuthenticationBundle\Event\OnFirewallFailureEvent;
8
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents;
9
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
14
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
17
use Symfony\Component\Security\Core\User\UserProviderInterface;
18
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
19
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
20
21
/**
22
 * Concrete implementation of an authentication with an api key.
23
 */
24
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
25
{
26
    /**
27
     * @var ObjectManager
28
     */
29
    private $om;
30
31
    /**
32
     * @var EventDispatcherInterface
33
     */
34
    private $dispatcher;
35
36
    /**
37
     * @var string
38
     */
39
    private $modelName;
40
41
    /**
42
     * @var ClassMetadata
43
     */
44
    private $metadata;
45
46
    /**
47
     * @var string
48
     */
49
    private $header;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param ObjectManager            $om
55
     * @param EventDispatcherInterface $dispatcher
56
     * @param string                   $modelName
57
     * @param ClassMetadata            $metadata
58
     * @param string                   $header
59
     */
60 14
    public function __construct(ObjectManager $om, EventDispatcherInterface $dispatcher, $modelName, ClassMetadata $metadata, $header)
61
    {
62 14
        $this->om = $om;
63 14
        $this->dispatcher = $dispatcher;
64 14
        $this->modelName = (string) $modelName;
65 14
        $this->metadata = $metadata;
66 14
        $this->header = (string) $header;
67 14
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 3
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
73
    {
74 3
        return new JsonResponse(array(), 401);
75
    }
76
77
    /**
78
     * Returns an authenticated token.
79
     *
80
     * @param TokenInterface        $token
81
     * @param UserProviderInterface $userProvider
82
     * @param string                $providerKey
83
     *
84
     * @throws AuthenticationException If the api key does not exist or is invalid
85
     * @throws \RuntimeException       If $userProvider is not an instance of AdvancedUserProviderInterface
86
     *
87
     * @return PreAuthenticatedToken
88
     */
89 10
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
90
    {
91 10
        $apiKey = $token->getCredentials();
92
93
        $user = $this
94 10
            ->om
95 10
            ->getRepository($this->modelName)
96 10
            ->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $apiKey));
97
98 10
        if (!$user) {
99 3
            $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_FAILURE, new OnFirewallFailureEvent());
100
101 3
            throw new AuthenticationException(
102 3
                sprintf('API key %s does not exist!', $apiKey)
103
            );
104
        }
105
106 7
        $token = new PreAuthenticatedToken(
107 7
            $user,
108 7
            $apiKey,
109 7
            $providerKey,
110 7
            $user->getRoles() ?: array()
111
        );
112
113 7
        $firewallEvent = new OnFirewallAuthenticationEvent($user);
114 7
        $firewallEvent->setToken($token);
115
116 7
        $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_LOGIN, $firewallEvent);
117
118 7
        return $token;
119
    }
120
121
    /**
122
     * Checks if the token is supported.
123
     *
124
     * @param TokenInterface $token
125
     * @param string         $providerKey
126
     *
127
     * @return bool
128
     */
129 9
    public function supportsToken(TokenInterface $token, $providerKey)
130
    {
131 9
        return $token instanceof PreAuthenticatedToken && $providerKey === $token->getProviderKey();
132
    }
133
134
    /**
135
     * Creates an api key by the http request.
136
     *
137
     * @param Request $request
138
     * @param string  $providerKey
139
     *
140
     * @throws BadCredentialsException If the request token cannot be found
141
     *
142
     * @return PreAuthenticatedToken
143
     */
144 10
    public function createToken(Request $request, $providerKey)
145
    {
146 10
        $apiKey = $request->headers->get($this->header);
147
148 10
        if (!$apiKey) {
149 1
            throw new BadCredentialsException('No ApiKey found in request!');
150
        }
151
152 9
        return new PreAuthenticatedToken(
153 9
            'unauthorized',
154 9
            $apiKey,
155 9
            $providerKey
156
        );
157
    }
158
}
159