This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from DarkGhostHunter/master
Now it works. Entirely.
- Loading branch information
Showing
11 changed files
with
261 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace DarkGhostHunter\Laraguard\Listeners; | ||
|
||
use DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable; | ||
|
||
trait ChecksTwoFactorCode | ||
{ | ||
/** | ||
* Returns if the login attempt should enforce Two Factor Authentication. | ||
* | ||
* @param null|\DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable|\Illuminate\Contracts\Auth\Authenticatable $user | ||
* @return bool | ||
*/ | ||
protected function shouldUseTwoFactorAuth($user = null) | ||
{ | ||
if (! $user instanceof TwoFactorAuthenticatable) { | ||
return false; | ||
} | ||
|
||
$shouldUse = $user->hasTwoFactorEnabled(); | ||
|
||
if ($this->config['laraguard.safe_devices.enabled']) { | ||
return $shouldUse && ! $user->isSafeDevice($this->request); | ||
} | ||
|
||
return $shouldUse; | ||
} | ||
|
||
/** | ||
* Returns if the Request is from a Safe Device. | ||
* | ||
* @param \DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable $user | ||
* @return bool | ||
*/ | ||
protected function isSafeDevice(TwoFactorAuthenticatable $user) | ||
{ | ||
return $this->config['laraguard.safe_devices.enabled'] && $user->isSafeDevice($this->request); | ||
} | ||
|
||
/** | ||
* Returns if the Request has the Two Factor Code. | ||
* | ||
* @return bool | ||
*/ | ||
protected function hasCode() | ||
{ | ||
return $this->request->has($this->input); | ||
} | ||
|
||
/** | ||
* Checks if the Request has a Two Factor Code and is correct (even if is invalid). | ||
* | ||
* @param \DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable $user | ||
* @return bool | ||
*/ | ||
protected function hasValidCode(TwoFactorAuthenticatable $user) | ||
{ | ||
return ! validator($this->request->only($this->input), [$this->input => 'alphanum'])->fails() | ||
&& $user->validateTwoFactorCode($this->request->input($this->input)); | ||
} | ||
|
||
/** | ||
* Adds a safe device to Two Factor Authentication data. | ||
* | ||
* @param \DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable $user | ||
* @return void | ||
*/ | ||
protected function addSafeDevice(TwoFactorAuthenticatable $user) | ||
{ | ||
if ($this->config['laraguard.safe_devices.enabled']) { | ||
$user->addSafeDevice($this->request); | ||
} | ||
} | ||
} |
Oops, something went wrong.