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
Branch develop (0737e3)
by Maximilian
11:49
created

ApiKeyAuthenticationHandler::authenticate()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

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