1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\UserBundle\Confirmation\OTP; |
4
|
|
|
|
5
|
|
|
use DoS\UserBundle\Confirmation\ConfirmationAbstract; |
6
|
|
|
use DoS\UserBundle\Confirmation\ConfirmationSubjectInterface; |
7
|
|
|
use DoS\UserBundle\Confirmation\Exception\NotFoundChannelException; |
8
|
|
|
use DoS\UserBundle\Confirmation\Model\OtpResend; |
9
|
|
|
use DoS\UserBundle\Confirmation\Model\OtpVerification; |
10
|
|
|
use DoS\UserBundle\Confirmation\Model\VerificationInterface; |
11
|
|
|
use DoS\UserBundle\Model\OneTimePasswordInterface; |
12
|
|
|
use Symfony\Component\Form\FormError; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
15
|
|
|
|
16
|
|
|
class Confirmation extends ConfirmationAbstract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
protected function sendToken(ConfirmationSubjectInterface $subject, $token) |
22
|
|
|
{ |
23
|
|
|
if (!$mobile = $subject->getConfirmationChannel($this->options['channel_path'])) { |
24
|
|
|
throw new NotFoundChannelException(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$verify = $this->tokenProvider->generateUniqueToken(); |
28
|
|
|
|
29
|
|
|
$this->sender->send( |
30
|
|
|
$this->options['token_send_template'], |
31
|
|
|
array($mobile), |
32
|
|
|
array('subject' => $subject, 'token' => $token, 'verify' => $verify) |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
/** @var OneTimePasswordInterface $otp */ |
36
|
|
|
$otp = new $this->options['otp_class'](); |
37
|
|
|
$otp->setSubject($subject); |
38
|
|
|
$otp->setToken($token); |
39
|
|
|
$otp->setVerify($verify); |
40
|
|
|
|
41
|
|
|
$this->manager->persist($otp); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function verify(Request $request, $token) |
48
|
|
|
{ |
49
|
|
|
$form = $this->createVerifyForm(); |
50
|
|
|
|
51
|
|
|
/** @var VerificationInterface $data */ |
52
|
|
|
$data = $form->getData(); |
53
|
|
|
$data->setToken($token); |
54
|
|
|
|
55
|
|
|
if (in_array($request->getMethod(), array('POST', 'PUT', 'PATCH'))) { |
56
|
|
|
$form->submit($request, !$request->isMethod('PATCH')); |
57
|
|
|
|
58
|
|
|
$data->setSubject($this->findSubjectWithToken($data->getToken())); |
59
|
|
|
|
60
|
|
|
if (!$form->isValid()) { |
61
|
|
|
return $form; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (!$this->validateTimeAware($subject = $data->getSubject())) { |
65
|
|
|
$form->addError(new FormError('ui.trans.user.confirmation.verify.invalid_time')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$otp = $this->findOtp($data->getSubject())) { |
69
|
|
|
$form->addError(new FormError('ui.trans.user.confirmation.verify.invalid_token')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($otp->getVerify() !== $data->getVerifyValue()) { |
73
|
|
|
$form->addError(new FormError('ui.trans.user.confirmation.verify.invalid_otp')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$form->getErrors(true)->count()) { |
77
|
|
|
$otp->setConfirmed(true); |
78
|
|
|
$this->successVerify($subject); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $form; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ConfirmationSubjectInterface $subject |
87
|
|
|
* |
88
|
|
|
* @return \DoS\UserBundle\Model\OneTimePasswordInterface |
89
|
|
|
*/ |
90
|
|
|
protected function findOtp(ConfirmationSubjectInterface $subject) |
91
|
|
|
{ |
92
|
|
|
$er = $this->manager->getRepository($this->options['otp_class']); |
93
|
|
|
|
94
|
|
|
return $er->findOneBy(array( |
95
|
|
|
'subject' => $subject, |
96
|
|
|
'token' => $subject->getConfirmationToken(), |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
104
|
|
|
{ |
105
|
|
|
parent::configureOptions($resolver); |
106
|
|
|
|
107
|
|
|
$resolver->setDefaults(array( |
108
|
|
|
'otp_class' => 'DoS\UserBundle\Model\OneTimePassword', |
109
|
|
|
'channel_path' => 'customer.mobile', |
110
|
|
|
'token_resend_form' => 'dos_resend_confirmation_otp', |
111
|
|
|
'token_verify_form' => 'dos_verification_otp', |
112
|
|
|
)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritdoc} |
117
|
|
|
*/ |
118
|
|
|
public function getResendModel() |
119
|
|
|
{ |
120
|
|
|
return new OtpResend(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getVerifyModel() |
127
|
|
|
{ |
128
|
|
|
return new OtpVerification(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getType() |
135
|
|
|
{ |
136
|
|
|
return 'otp'; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|