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.
Completed
Pull Request — master (#31)
by Maximilian
14:51 queued 05:03
created

ApiKeyAuthenticationHandler::authenticate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 35
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 35
ccs 22
cts 22
cp 1
rs 8.439
cc 5
eloc 19
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Model\Login\ApiToken;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Ma27\ApiKeyAuthenticationBundle\Event\OnAuthenticationEvent;
7
use Ma27\ApiKeyAuthenticationBundle\Event\OnInvalidCredentialsEvent;
8
use Ma27\ApiKeyAuthenticationBundle\Event\OnLogoutEvent;
9
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException;
10
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents;
11
use Ma27\ApiKeyAuthenticationBundle\Model\Key\KeyFactoryInterface;
12
use Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface;
13
use Ma27\ApiKeyAuthenticationBundle\Model\Password\PasswordHasherInterface;
14
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
/**
18
 * Concrete handler for api key authorization.
19
 */
20
class ApiKeyAuthenticationHandler implements AuthenticationHandlerInterface
21
{
22
    /**
23
     * @var ObjectManager
24
     */
25
    private $om;
26
27
    /**
28
     * @var PasswordHasherInterface
29
     */
30
    private $passwordHasher;
31
32
    /**
33
     * @var KeyFactoryInterface
34
     */
35
    private $keyFactory;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * @var string
44
     */
45
    private $modelName;
46
47
    /**
48
     * @var ClassMetadata
49
     */
50
    private $classMetadata;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param ObjectManager            $om
56
     * @param PasswordHasherInterface  $passwordHasher
57
     * @param KeyFactoryInterface      $keyFactory
58
     * @param EventDispatcherInterface $dispatcher
59
     * @param string                   $modelName
60
     * @param ClassMetadata            $metadata
61
     */
62
    public function __construct(
63
        ObjectManager $om,
64
        PasswordHasherInterface $passwordHasher,
65
        KeyFactoryInterface $keyFactory,
66
        EventDispatcherInterface $dispatcher,
67
        $modelName,
68
        ClassMetadata $metadata
69
    ) {
70
        $this->om = $om;
71
        $this->passwordHasher = $passwordHasher;
72
        $this->keyFactory = $keyFactory;
73
        $this->eventDispatcher = $dispatcher;
74 11
        $this->modelName = (string) $modelName;
75
        $this->classMetadata = $metadata;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function authenticate(array $credentials)
82
    {
83
        $loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY);
84 11
        $passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY);
85 11
86 11
        if (!isset($credentials[$passwordProperty])) {
87 11
            throw new \InvalidArgumentException(
88 11
                sprintf('Unable to find password property "%s" in credential set!', $passwordProperty)
89 11
            );
90 11
        }
91 11
92 11
        if (!isset($credentials[$loginProperty])) {
93
            throw new \InvalidArgumentException(
94
                sprintf('Unable to find login property "%s" in credential set!', $loginProperty)
95
            );
96
        }
97 9
98
        $objectRepository = $this->om->getRepository($this->modelName);
99 9
        $object = $objectRepository->findOneBy(array($loginProperty => $credentials[$loginProperty]));
100 1
101
        if (null === $object || !$this->passwordHasher->compareWith($object->getPassword(), $credentials[$passwordProperty])) {
102
            $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, new OnInvalidCredentialsEvent($object));
103 8
104 8
            throw new CredentialException();
105 5
        }
106 1
107 1
        $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object));
108 1
109
        $this->classMetadata->modifyProperty($object, $this->keyFactory->getKey(), ClassMetadata::API_KEY_PROPERTY);
110
        $this->om->persist($object);
111 4
112 4
        $this->om->flush();
113
114 7
        return $object;
115 3
    }
116 1
117 1
    /**
118 1
     * {@inheritdoc}
119
     */
120
    public function removeSession($user, $purgeJob = false)
121 2
    {
122 2
        $this->classMetadata->modifyProperty($user, null, ClassMetadata::API_KEY_PROPERTY);
123
124 6
        $event = new OnLogoutEvent($user);
125 1
        if ($purgeJob) {
126 1
            $event->markAsPurgeJob();
127 1
        }
128
129
        $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::LOGOUT, $event);
130 5
131
        $this->om->persist($user);
132 5
133
        // on purge jobs one big flush will be commited to the db after the whole action
134 5
        if (!$purgeJob) {
135 2
            $this->om->flush();
136
        }
137 2
    }
138
139
    /**
140 3
     * Getter for the object manager.
141
     *
142 3
     * @return ObjectManager
143 3
     */
144
    protected function getOm()
145 3
    {
146
        return $this->om;
147 3
    }
148
149
    /**
150
     * Getter for the password hasher.
151
     *
152
     * @return PasswordHasherInterface
153 3
     */
154
    protected function getPasswordHasher()
155 3
    {
156
        return $this->passwordHasher;
157 3
    }
158 3
159 1
    /**
160 1
     * Getter for the key factory.
161
     *
162 3
     * @return KeyFactoryInterface
163
     */
164 3
    protected function getKeyFactory()
165
    {
166
        return $this->keyFactory;
167 3
    }
168 2
169 2
    /**
170 3
     * Getter for the dispatcher.
171
     *
172
     * @return EventDispatcherInterface
173
     */
174
    protected function getEventDispatcher()
175
    {
176
        return $this->eventDispatcher;
177
    }
178
179
    /**
180
     * Getter for the model name.
181
     *
182
     * @return string
183
     */
184
    protected function getModelName()
185
    {
186
        return $this->modelName;
187
    }
188
189
    /**
190
     * @return ClassMetadata
191
     */
192
    public function getClassMetadata()
193
    {
194
        return $this->classMetadata;
195
    }
196
}
197