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
02:46
created

ApiKeyAuthenticationHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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