-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored to throw exceptions instead of immediate actions
Added ValidateAuthenticationFilter Added stack of exceptions
- Loading branch information
1 parent
89ee1fa
commit c82ddcc
Showing
8 changed files
with
158 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace hiqdev\yii2\mfa\exceptions; | ||
|
||
use yii\base\Exception; | ||
|
||
abstract class AuthenticationException extends Exception | ||
{ | ||
abstract public function redirect(); | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace hiqdev\yii2\mfa\exceptions; | ||
|
||
use Yii; | ||
|
||
class IpNotAllowedException extends AuthenticationException | ||
{ | ||
public function getName() | ||
{ | ||
return 'IP address is not allowed'; | ||
} | ||
|
||
public function redirect() | ||
{ | ||
Yii::$app->response->redirect('/mfa/allowed-ips/not-allowed-ip'); | ||
Yii::$app->end(); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace hiqdev\yii2\mfa\exceptions; | ||
|
||
use Yii; | ||
|
||
class NotAuthenticatedException extends AuthenticationException | ||
{ | ||
public function getName() | ||
{ | ||
return 'You are not authenticated'; | ||
} | ||
|
||
public function redirect() | ||
{ | ||
Yii::$app->response->redirect('/site/login'); | ||
Yii::$app->end(); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace hiqdev\yii2\mfa\exceptions; | ||
|
||
use Yii; | ||
|
||
class TotpVerificationFailedException extends AuthenticationException | ||
{ | ||
public function getName() | ||
{ | ||
return 'Token verification failed'; | ||
} | ||
|
||
public function redirect() | ||
{ | ||
Yii::$app->response->redirect('/mfa/totp/check'); | ||
Yii::$app->end(); | ||
} | ||
} |
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,63 @@ | ||
<?php | ||
|
||
namespace hiqdev\yii2\mfa\filters; | ||
|
||
use Closure; | ||
use hiqdev\yii2\mfa\exceptions\AuthenticationException; | ||
use hiqdev\yii2\mfa\exceptions\NotAuthenticatedException; | ||
use hiqdev\yii2\mfa\Module; | ||
use Yii; | ||
use yii\base\ActionFilter; | ||
use yii\web\IdentityInterface; | ||
|
||
class ValidateAuthenticationFilter extends ActionFilter | ||
{ | ||
/** | ||
* @var Closure | ||
*/ | ||
public $denyCallback; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
public $invert = false; | ||
|
||
public function beforeAction($action) | ||
{ | ||
if (Yii::$app->user->isGuest) { | ||
return $this->denyAccess(new NotAuthenticatedException()); | ||
} | ||
|
||
$identity = Yii::$app->user->identity; | ||
try { | ||
$this->validateAuthentication($identity); | ||
} catch (AuthenticationException $e) { | ||
return $this->denyAccess($e); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public function validateAuthentication(IdentityInterface $identity) | ||
{ | ||
/** @var Module $module */ | ||
$module = Yii::$app->getModule('mfa'); | ||
|
||
$module->validateIps($identity); | ||
$module->validateTotp($identity); | ||
} | ||
|
||
/** | ||
* @param AuthenticationException $exception | ||
* @return mixed | ||
*/ | ||
protected function denyAccess($exception) | ||
{ | ||
if ($this->denyCallback instanceof Closure) { | ||
return call_user_func($this->denyCallback, $exception); | ||
} | ||
|
||
$exception->redirect(); | ||
} | ||
|
||
} |