Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Configurable model and listener
Browse files Browse the repository at this point in the history
  • Loading branch information
benbjurstrom committed Apr 4, 2020
1 parent 0008226 commit a6daa79
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
22 changes: 18 additions & 4 deletions config/laraguard.php
Expand Up @@ -7,13 +7,27 @@
| Listener hook
|--------------------------------------------------------------------------
|
| If the Listener is enabled, Laraguard will automatically hook into the
| "Attempting" event and magically ask for Two Factor Authentication if
| is necessary. Disable this to use your own 2FA authentication logic.
| If the Listener class is present, Laraguard will automatically hook into
| the "Attempting" event and magically ask for Two Factor Authentication if
| is necessary. Set this value to false to use your own 2FA authentication
| logic.
|
*/

'listener' => true,
'listener' => DarkGhostHunter\Laraguard\Listeners\EnforceTwoFactorAuth::class,

/*
|--------------------------------------------------------------------------
| TwoFactorAuthentication Model
|--------------------------------------------------------------------------
|
| When using the "TwoFactorAuthentication" trait from this package, we need
| to know which Eloquent model should be used to retrieve your two factor
| authentication records.
|
*/

'model' => DarkGhostHunter\Laraguard\Eloquent\TwoFactorAuthentication::class,

/*
|--------------------------------------------------------------------------
Expand Down
12 changes: 4 additions & 8 deletions src/LaraguardServiceProvider.php
Expand Up @@ -70,14 +70,10 @@ protected function registerListener(Repository $config, Dispatcher $dispatcher)
return;
}

$this->app->singleton(Listeners\EnforceTwoFactorAuth::class, function ($app) {
return new Listeners\EnforceTwoFactorAuth($app['config'], $app['request']);
$this->app->singleton($config['laraguard.listener'], function ($app) use ($config) {
return new $config['laraguard.listener']($app['config'], $app['request']);
});
$dispatcher->listen(Attempting::class,
'DarkGhostHunter\Laraguard\Listeners\EnforceTwoFactorAuth@saveCredentials'
);
$dispatcher->listen(Validated::class,
'DarkGhostHunter\Laraguard\Listeners\EnforceTwoFactorAuth@checkTwoFactor'
);
$dispatcher->listen(Attempting::class, $config['laraguard.listener'] . '@saveCredentials');
$dispatcher->listen(Validated::class, $config['laraguard.listener'] . '@checkTwoFactor');
}
}
6 changes: 3 additions & 3 deletions src/TwoFactorAuthentication.php
Expand Up @@ -28,7 +28,7 @@ public function initializeTwoFactorAuthentication()
*/
public function twoFactorAuth()
{
return $this->morphOne(Eloquent\TwoFactorAuthentication::class, 'authenticatable')
return $this->morphOne(config('laraguard.model'), 'authenticatable')
->withDefault(config('laraguard.totp'));
}

Expand Down Expand Up @@ -184,7 +184,7 @@ public function generateRecoveryCodes() : Collection
{
[$enabled, $amount, $length] = array_values(config('laraguard.recovery'));

$this->twoFactorAuth->recovery_codes = Eloquent\TwoFactorAuthentication::generateRecoveryCodes($amount, $length);
$this->twoFactorAuth->recovery_codes = config('laraguard.model')::generateRecoveryCodes($amount, $length);
$this->twoFactorAuth->recovery_codes_generated_at = now();
$this->twoFactorAuth->save();

Expand Down Expand Up @@ -248,7 +248,7 @@ public function addSafeDevice(Request $request) : string
*/
protected function generateTwoFactorRemember()
{
return Eloquent\TwoFactorAuthentication::generateDefaultTwoFactorRemember();
return config('laraguard.model')::generateDefaultTwoFactorRemember();
}

/**
Expand Down

0 comments on commit a6daa79

Please sign in to comment.