Completed
Push — master ( f56ae9...d1d973 )
by Janusz
33:48 queued 16:16
created

TwilioProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
crap 1
1
<?php
2
3
namespace FlyingColours\TwilioTwoFactorBundle\Security\TwoFactor\Provider;
4
5
use Scheb\TwoFactorBundle\Security\TwoFactor\AuthenticationContextInterface;
6
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
7
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
8
use FlyingColours\TwilioTwoFactorBundle\Model\Twilio;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
use Symfony\Component\EventDispatcher\GenericEvent;
11
use Symfony\Component\HttpFoundation\Session\SessionInterface;
12
13
class TwilioProvider implements TwoFactorProviderInterface
14
{
15
    /** @var EventDispatcherInterface */
16
    private $dispatcher;
17
18
    /** @var SessionInterface */
19
    private $session;
20
21
    /** @var TwoFactorFormRendererInterface */
22
    private $renderer;
23
24
    /**
25
     * TwilioProvider constructor.
26
     *
27
     * @param EventDispatcherInterface $dispatcher
28
     * @param SessionInterface $session
29
     * @param TwoFactorFormRendererInterface $renderer
30
     */
31 4
    public function __construct(
32
        EventDispatcherInterface $dispatcher,
33
        SessionInterface $session,
34
        TwoFactorFormRendererInterface $renderer
35
    )
36
    {
37 4
        $this->dispatcher = $dispatcher;
38 4
        $this->session = $session;
39 4
        $this->renderer = $renderer;
40 4
    }
41
42 1
    public function beginAuthentication(AuthenticationContextInterface $context): bool
43
    {
44 1
        $user = $context->getUser();
45
46 1
        if ($user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled())
47
        {
48 1
            $this->dispatcher->dispatch('2fa.twilio.start', new GenericEvent($user));
49
50 1
            return true;
51
        }
52
53 1
        return false;
54
    }
55
56 1
    public function validateAuthenticationCode($user, string $authenticationCode): bool
57
    {
58 1
        if ( ! $user instanceof Twilio\TwoFactorInterface || ! $this->session->has('twilio_code'))
59
        {
60 1
            return false;
61
        }
62
63 1
        if(hash_equals($this->session->get('twilio_code'), $authenticationCode))
64
        {
65 1
            $this->session->remove('twilio_code');
66 1
            return true;
67
        }
68
69 1
        return false;
70
    }
71
72 1
    public function getFormRenderer(): TwoFactorFormRendererInterface
73
    {
74 1
        return $this->renderer;
75
    }
76
}
77