1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This code is part of the Apiary SMS Login Provider project. |
4
|
|
|
* |
5
|
|
|
* (c) Darren Mothersele <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Apiary\SmsLoginProvider; |
12
|
|
|
|
13
|
|
|
use Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface; |
14
|
|
|
use Silex\Application; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class SmsLoginController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* An array of countries for the select list options. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $countries; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* SMS Handler used to send validate numbers and send SMS verification |
31
|
|
|
* |
32
|
|
|
* @var \Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface |
33
|
|
|
*/ |
34
|
|
|
protected $handler; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* String used to create verification SMS message, must contain `%s` which |
38
|
|
|
* is replaced by the verification code. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $messageTemplate; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The name of the view template to use to render the login form. |
46
|
|
|
* Defaults to 'login.twig' |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $viewName; |
51
|
|
|
|
52
|
|
|
protected $debug; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* SmsLoginController constructor. |
56
|
|
|
* @param \Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface $handler |
57
|
|
|
* @param string|NULL $view |
58
|
|
|
* @param string|NULL $message |
59
|
|
|
* @param null $debug |
|
|
|
|
60
|
|
|
*/ |
61
|
9 |
|
public function __construct( |
62
|
|
|
SmsHandlerInterface $handler, |
63
|
|
|
$view = null, |
64
|
|
|
$message = null |
65
|
|
|
) |
66
|
|
|
{ |
67
|
9 |
|
$this->messageTemplate = $message ?: 'Security code: %s'; |
68
|
9 |
|
$countryData = file_get_contents(__DIR__ . '/../data/countries.json'); |
69
|
9 |
|
$this->countries = json_decode($countryData); |
|
|
|
|
70
|
9 |
|
$this->handler = $handler; |
71
|
9 |
|
$this->viewName = $view ?: 'login.twig'; |
72
|
9 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Login controller action |
76
|
|
|
* Step 1: Login shows form to enter mobile phone number. |
77
|
|
|
* |
78
|
|
|
* @param \Silex\Application $app |
79
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
3 |
|
public function loginAction(Application $app, Request $request) |
83
|
|
|
{ |
84
|
3 |
|
return $app['twig']->render($this->viewName, [ |
85
|
3 |
|
'country_list' => $this->countries, |
86
|
3 |
|
'error' => $app['security.last_error']($request), |
87
|
3 |
|
'form_action' => $app['url_generator']->generate('sms.login'), |
88
|
3 |
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Verify controller action |
93
|
|
|
* Step 2: Send code to number by SMS and show form to verify code |
94
|
|
|
* |
95
|
|
|
* @param \Silex\Application $app |
96
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
3 |
|
public function verifyAction(Application $app, Request $request) |
100
|
|
|
{ |
101
|
3 |
|
$mobile = $request->get('mobile'); |
102
|
3 |
|
$country = $request->get('country'); |
103
|
|
|
|
104
|
3 |
|
if (empty($mobile)) { |
105
|
|
|
throw new AuthenticationException('Mobile number is required'); |
106
|
|
|
} |
107
|
|
|
|
108
|
3 |
|
$code = sprintf("%'.05d", rand(0, 99999)); |
109
|
3 |
|
$app['session']->set('code', $code); |
110
|
|
|
|
111
|
3 |
|
$number = $this->handler->lookupNumber($mobile, $country); |
112
|
3 |
|
$message = sprintf($this->messageTemplate, $code); |
113
|
3 |
|
$this->handler->sendSMS($number, $message); |
114
|
|
|
|
115
|
3 |
|
return $app['twig']->render($this->viewName, [ |
116
|
3 |
|
'mobile' => $number, |
117
|
3 |
|
'form_action' => $app['url_generator']->generate('sms.login') . '/check', |
118
|
3 |
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
|
|
|
|
122
|
|
|
|