Skip to content

Commit

Permalink
Fixed snipe#6834 and snipe#6402 - use inline QR code generation for
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Mar 20, 2019
1 parent 1451b4f commit cf92618
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 103 deletions.
65 changes: 40 additions & 25 deletions app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,23 @@ public function getTwoFactorEnroll()
return redirect()->route('login')->with('error', 'You must be logged in.');
}


$settings = Setting::getSettings();
$user = Auth::user();
$google2fa = app()->make('PragmaRX\Google2FA\Contracts\Google2FA');

if ($user->two_factor_secret=='') {
$user->two_factor_secret = $google2fa->generateSecretKey(32);
$user->save();
if (($user->two_factor_secret!='') && ($user->two_factor_enrolled==1)) {
return redirect()->route('two-factor')->with('error', 'Your device is already enrolled.');
}


$google2fa_url = $google2fa->getQRCodeGoogleUrl(
urlencode(Setting::getSettings()->site_name),
urlencode($user->username),
$user->two_factor_secret
);
$google2fa = new Google2FA();
$secret = $google2fa->generateSecretKey();
$user->two_factor_secret = $secret;
$user->save();

return view('auth.two_factor_enroll')->with('google2fa_url', $google2fa_url);
$barcode = new \Com\Tecnick\Barcode\Barcode();
$barcode_obj = $barcode->getBarcodeObj('QRCODE', 'otpauth://totp/'.urlencode($settings->site_name).':'.urlencode($user->username).'?secret='.urlencode($secret).'&issuer=Snipe-IT&period=30', 300, 300, 'black', array(-2, -2, -2, -2));
return view('auth.two_factor_enroll')->with('barcode_obj', $barcode_obj);

}

Expand All @@ -240,6 +241,16 @@ public function getTwoFactorEnroll()
*/
public function getTwoFactorAuth()
{
if (!Auth::check()) {
return redirect()->route('login')->with('error', 'You must be logged in.');
}

$user = Auth::user();

if (($user->two_factor_secret=='') || ($user->two_factor_enrolled!=1)) {
return redirect()->route('two-factor-enroll');
}

return view('auth.two_factor');
}

Expand All @@ -255,18 +266,22 @@ public function postTwoFactorAuth(Request $request)
return redirect()->route('login')->with('error', 'You must be logged in.');
}

if (!$request->has('two_factor_secret')) {
return redirect()->route('two-factor')->with('error', 'Two-factor code is required.');
}

$user = Auth::user();
$secret = $request->get('two_factor_secret');
$google2fa = app()->make('PragmaRX\Google2FA\Contracts\Google2FA');
$valid = $google2fa->verifyKey($user->two_factor_secret, $secret);
$google2fa = new Google2FA();
$secret = $request->input('two_factor_secret');

if ($valid) {
if ($google2fa->verifyKey($user->two_factor_secret, $secret)) {
$user->two_factor_enrolled = 1;
$user->save();
$request->session()->put('2fa_authed', 'true');
return redirect()->route('home')->with('success', 'You are logged in!');
}

\Log::debug('Did not match');
return redirect()->route('two-factor')->with('error', 'Invalid two-factor code');


Expand Down Expand Up @@ -315,11 +330,11 @@ public function username()
}

/**
* Redirect the user after determining they are locked out.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
* Redirect the user after determining they are locked out.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
protected function sendLockoutResponse(Request $request)
{
$seconds = $this->limiter()->availableIn(
Expand All @@ -330,18 +345,18 @@ protected function sendLockoutResponse(Request $request)

$message = \Lang::get('auth/message.throttle', ['minutes' => $minutes]);

return redirect()->back()
return redirect()->back()
->withInput($request->only($this->username(), 'remember'))
->withErrors([$this->username() => $message]);
}


/**
* Override the lockout time and duration
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse
*/
* Override the lockout time and duration
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function hasTooManyLoginAttempts(Request $request)
{
$lockoutTime = config('auth.throttle.lockout_duration');
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"patchwork/utf8": "~1.2",
"phpdocumentor/reflection-docblock": "3.2.2",
"phpspec/prophecy": "1.6.2",
"pragmarx/google2fa": "^1.0",
"pragmarx/google2fa": "^5.0",
"pragmarx/google2fa-laravel": "^0.3.0",
"predis/predis": "^1.1",
"rollbar/rollbar-laravel": "2.4.1",
"schuppo/password-strength": "~1.5",
Expand Down
Loading

0 comments on commit cf92618

Please sign in to comment.