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(); } }