2020-04-14 15:58:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
use App\Http\Requests\ValidateOtpRequest;
|
2020-04-14 15:58:24 +02:00
|
|
|
use App\Providers\RouteServiceProvider;
|
2022-09-21 11:17:58 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
|
use lfkeitel\phptotp\Base32;
|
|
|
|
|
use lfkeitel\phptotp\Totp;
|
2020-04-14 15:58:24 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
|
|
class OTPController extends Controller
|
|
|
|
|
{
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* Show verifiy otp page
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
|
|
|
|
|
*/
|
|
|
|
|
public function verify()
|
|
|
|
|
{
|
2020-04-14 15:58:24 +02:00
|
|
|
$user = session()->get('tmpUserOTP');
|
2022-09-21 11:17:58 +00:00
|
|
|
if (!$user) {
|
2020-04-14 15:58:24 +02:00
|
|
|
return redirect(RouteServiceProvider::HOME);
|
2022-09-21 11:17:58 +00:00
|
|
|
}
|
2020-04-14 15:58:24 +02:00
|
|
|
return view('auth.verify');
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* Check OPT request
|
|
|
|
|
*
|
|
|
|
|
* @param ValidateOtpRequest $request
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
|
|
|
|
*/
|
|
|
|
|
public function check(ValidateOtpRequest $request)
|
|
|
|
|
{
|
|
|
|
|
//phpcs:enable
|
|
|
|
|
Auth::login(session()->get('tmpUserOTP'));
|
|
|
|
|
return \response()->redirectTo(RouteServiceProvider::HOME);
|
2020-04-14 15:58:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
/**
|
|
|
|
|
* Send OPT by SMS
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
|
*/
|
|
|
|
|
public function sendOtpBySms()
|
2021-08-03 16:24:49 +02:00
|
|
|
{
|
|
|
|
|
$user = session()->get('tmpUserOTP');
|
2022-09-21 11:17:58 +00:00
|
|
|
if (!$user) {
|
2021-08-03 16:24:49 +02:00
|
|
|
return back()->withErrors('User not found.');
|
2022-09-21 11:17:58 +00:00
|
|
|
}
|
2021-08-03 16:24:49 +02:00
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
if (is_null($user->phone)) {
|
2021-08-03 16:24:49 +02:00
|
|
|
return back()->withErrors('Phone number missing.');
|
2022-09-21 11:17:58 +00:00
|
|
|
}
|
2021-08-03 16:24:49 +02:00
|
|
|
|
|
|
|
|
$otp = (new Totp())->GenerateToken(Base32::decode($user->totp_secret));
|
2022-09-21 11:17:58 +00:00
|
|
|
|
|
|
|
|
if (!$otp) {
|
|
|
|
|
return back()->withErrors('Erreur lors de la génération du code OTP');
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 16:24:49 +02:00
|
|
|
/** @var \App\Lib\LDC\MailjetSms */
|
|
|
|
|
$mj = app('MailjetSms');
|
|
|
|
|
|
2022-09-21 11:17:58 +00:00
|
|
|
try {
|
|
|
|
|
$mjResponse = $mj->send("OTP : " . implode(' ', str_split($otp)), $user->phone);
|
|
|
|
|
/** @var \GuzzleHttp\Psr7\Response $mjResponse */
|
|
|
|
|
$responseStatus = $mjResponse->getStatusCode();
|
|
|
|
|
if ($responseStatus == 200) {
|
|
|
|
|
return back()->with('success', 'SMS envoyé.');
|
|
|
|
|
}
|
|
|
|
|
return back()->withErrors('SMS not sent.');
|
|
|
|
|
} catch (RequestException $e) {
|
|
|
|
|
report($e);
|
|
|
|
|
return back()->withErrors('Erreur lors de l\'envoi du SMS');
|
2021-08-03 16:24:49 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-14 15:58:24 +02:00
|
|
|
}
|