1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/TwoFactorController.php

60 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use PragmaRX\Google2FA\Google2FA;
use Crypt;
2017-11-02 09:43:36 +01:00
class TwoFactorController extends Controller
{
2017-11-02 09:43:36 +01:00
public function setupTwoFactor()
{
$user = auth()->user();
2017-11-05 10:54:32 +01:00
if ($user->google_2fa_secret || ! $user->phone || ! $user->confirmed) {
return redirect('/settings/user_details');
}
$google2fa = new Google2FA();
if ($secret = session('2fa:secret')) {
// do nothing
} else {
$secret = $google2fa->generateSecretKey();
session(['2fa:secret' => $secret]);
}
$qrCode = $google2fa->getQRCodeGoogleUrl(
APP_NAME,
$user->email,
$secret
);
$data = [
'secret' => $secret,
'qrCode' => $qrCode,
];
return view('users.two_factor', $data);
}
2017-11-02 09:43:36 +01:00
public function enableTwoFactor()
{
$user = auth()->user();
$secret = session('2fa:secret');
2017-11-24 10:13:34 +01:00
$oneTimePassword = request('one_time_password');
2017-11-02 09:43:36 +01:00
2017-11-24 10:13:34 +01:00
if (! $secret || ! \Google2FA::verifyKey($secret, $oneTimePassword)) {
return redirect('settings/enable_two_factor')->withError(trans('texts.invalid_one_time_password'));
2017-11-24 10:13:34 +01:00
} elseif (! $user->google_2fa_secret && $user->phone && $user->confirmed) {
2017-11-02 09:43:36 +01:00
$user->google_2fa_secret = Crypt::encrypt($secret);
$user->save();
session()->forget('2fa:secret');
2017-11-02 09:43:36 +01:00
session()->flash('message', trans('texts.enabled_two_factor'));
}
return redirect('settings/user_details');
}
}