Test Failed
Push — develop ( 0f3a91...656ecc )
by Janusz
06:59 queued 01:19
created

TwilioProvider::beginAuthentication()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 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
    public function __construct(
32
        EventDispatcherInterface $dispatcher,
33
        SessionInterface $session,
34
        TwoFactorFormRendererInterface $renderer
35
    )
36
    {
37
        $this->dispatcher = $dispatcher;
38
        $this->session = $session;
39
        $this->renderer = $renderer;
40
    }
41
42
    public function beginAuthentication(AuthenticationContextInterface $context): bool
43
    {
44
        $user = $context->getUser();
45
46
        if ($user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled())
47
        {
48
            $this->dispatcher->dispatch('2fa.twilio.start', new GenericEvent($user));
49
50
            return true;
51
        }
52
53
        return false;
54
    }
55
56
    public function validateAuthenticationCode($user, string $authenticationCode): bool
57
    {
58
        if ( ! $user instanceof Twilio\TwoFactorInterface || ! $this->session->has('twilio_code'))
59
        {
60
            return false;
61
        }
62
63
        if(hash_equals($this->session->get('twilio_code'), $authenticationCode))
64
        {
65
            $this->session->remove('twilio_code');
66
            return true;
67
        }
68
69
        return false;
70
    }
71
72
    public function getFormRenderer(): TwoFactorFormRendererInterface
73
    {
74
        return $this->renderer;
75
    }
76
}
77