105 lines
No EOL
2.2 KiB
PHP
105 lines
No EOL
2.2 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use Google2FA;
|
|
|
|
class MyGoogle2FA
|
|
{
|
|
private $fileName = 'google2fasecret.key';
|
|
|
|
private $name = 'mein.sterntours.de';
|
|
|
|
private $email = '';
|
|
|
|
private $secretKey;
|
|
|
|
private $keySize = 32;
|
|
|
|
private $keyPrefix = '';
|
|
|
|
private $inlineUrl = '';
|
|
|
|
private $valid = '';
|
|
|
|
private $user = null;
|
|
|
|
|
|
public function init($user){
|
|
$this->name = str_replace('https://', '', config('app.url'));
|
|
$this->user = $user;
|
|
$this->email = $user->email;
|
|
return $this;
|
|
}
|
|
|
|
public function generate()
|
|
{
|
|
$this->generateSecretKey();
|
|
$this->inlineUrl = $this->getQRCodeInline();
|
|
}
|
|
|
|
public function check2Fa($code)
|
|
{
|
|
if(!$this->getSecretKey()){
|
|
return false;
|
|
}
|
|
$this->valid = $this->validateInput($code);
|
|
return $this->valid;
|
|
}
|
|
|
|
public function getBy($key){
|
|
return isset($this->{$key}) ? $this->{$key} : '';
|
|
}
|
|
|
|
private function getQRCodeInline()
|
|
{
|
|
return Google2FA::getQRCodeInline(
|
|
$this->name,
|
|
$this->email,
|
|
$this->secretKey
|
|
);
|
|
}
|
|
private function getSecretKey()
|
|
{
|
|
if (! $this->secretKey = $this->getStoredKey())
|
|
{
|
|
return null;
|
|
}
|
|
return $this->secretKey;
|
|
}
|
|
|
|
private function generateSecretKey()
|
|
{
|
|
if (! $this->secretKey = $this->getStoredKey())
|
|
{
|
|
$this->secretKey = Google2FA::generateSecretKey($this->keySize, $this->keyPrefix);
|
|
$this->user->secret_key = $this->secretKey;
|
|
$this->user->save();
|
|
}
|
|
return $this->secretKey;
|
|
}
|
|
|
|
private function getStoredKey()
|
|
{
|
|
// No need to read it from disk it again if we already have it
|
|
if ($this->secretKey)
|
|
{
|
|
return $this->secretKey;
|
|
}
|
|
if(! $this->user->secret_key){
|
|
return null;
|
|
}
|
|
return $this->user->secret_key;
|
|
}
|
|
|
|
private function validateInput($code)
|
|
{
|
|
// Verify the code
|
|
return Google2FA::verifyKey($this->secretKey, $code);
|
|
}
|
|
|
|
public static function logout()
|
|
{
|
|
(new AuthGoogle2FA(request()))->logout();
|
|
}
|
|
|
|
} |