|
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\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface; |
|
11
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
|
13
|
|
|
|
|
14
|
|
|
class TwilioProvider implements TwoFactorProviderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var EventDispatcherInterface */ |
|
17
|
|
|
private $dispatcher; |
|
18
|
|
|
|
|
19
|
|
|
/** @var SessionInterface */ |
|
20
|
|
|
private $session; |
|
21
|
|
|
|
|
22
|
|
|
/** @var TwoFactorFormRendererInterface */ |
|
23
|
|
|
private $renderer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* TwilioProvider constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
29
|
|
|
* @param SessionInterface $session |
|
30
|
|
|
* @param TwoFactorFormRendererInterface $renderer |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function __construct( |
|
33
|
|
|
EventDispatcherInterface $dispatcher, |
|
34
|
|
|
SessionInterface $session, |
|
35
|
|
|
TwoFactorFormRendererInterface $renderer |
|
36
|
|
|
) |
|
37
|
|
|
{ |
|
38
|
4 |
|
$this->dispatcher = $dispatcher; |
|
39
|
4 |
|
$this->session = $session; |
|
40
|
4 |
|
$this->renderer = $renderer; |
|
41
|
4 |
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public function beginAuthentication(AuthenticationContextInterface $context): bool |
|
44
|
|
|
{ |
|
45
|
1 |
|
$user = $context->getUser(); |
|
46
|
1 |
|
return $user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
public function prepareAuthentication($user): void |
|
50
|
|
|
{ |
|
51
|
1 |
|
if ($user instanceof Twilio\TwoFactorInterface && $user->isTwilioAuthEnabled()) |
|
52
|
|
|
{ |
|
53
|
1 |
|
if( $this->dispatcher instanceof ContractsEventDispatcherInterface) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->dispatcher->dispatch(new GenericEvent($user), '2fa.twilio.start'); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
else |
|
58
|
|
|
{ |
|
59
|
|
|
$this->dispatcher->dispatch('2fa.twilio.start', new GenericEvent($user)); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
1 |
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
public function validateAuthenticationCode($user, string $authenticationCode): bool |
|
65
|
|
|
{ |
|
66
|
1 |
|
if ( ! $user instanceof Twilio\TwoFactorInterface || ! $this->session->has('twilio_code')) |
|
67
|
|
|
{ |
|
68
|
1 |
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
if(hash_equals($this->session->get('twilio_code'), $authenticationCode)) |
|
72
|
|
|
{ |
|
73
|
1 |
|
$this->session->remove('twilio_code'); |
|
74
|
1 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function getFormRenderer(): TwoFactorFormRendererInterface |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->renderer; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|