Skip to content

Commit

Permalink
Merge pull request #32 from Laragear/feat/remove-lingering-config-key
Browse files Browse the repository at this point in the history
[1.x] Removes lingering config key
  • Loading branch information
DarkGhostHunter authored Jul 29, 2022
2 parents 7f24388 + cda6b92 commit fc05688
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,24 @@ To add your own language, publish the translation files. These will be located i
php artisan vendor:publish --provider="Laragear\TwoFactor\TwoFactorServiceProvider" --tag="translations"
```

## Custom Recovery Codes

Recovery codes are automatically generated using [`Str::random()`](https://laravel.com/docs/9.x/helpers#method-str-random) with [small configuration](#recovery-codes). This will suffice for most applications, but you can also create your own random string code generator.

To create your own generator, set a custom callback to generate a recovery code with `TwoFactorAuthentication::generateRecoveryCodesUsing()`. This callable will receive the configuration string length, iteration, and total amount of codes, and should return an alfa-numeric string.

```php
use Laragear\TwoFactor\Models\TwoFactorAuthentication;
use Illuminate\Support\Str;

TwoFactorAuthentication::generateRecoveryCodesUsing(function ($length, $iteration, $total) {
// Append the index of the code before the string.
return $iteration . Str::random($length);
});
```

This package will handle the recovery code match and usage for you, so you don't need to worry about that.

## Configuration

To further configure the package, publish the configuration file:
Expand Down
14 changes: 0 additions & 14 deletions config/two-factor.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| 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. You can use your own for more advanced logic.
|
*/

'model' => \Laragear\TwoFactor\Models\TwoFactorAuthentication::class,

/*
|--------------------------------------------------------------------------
| Cache Store
Expand Down
9 changes: 4 additions & 5 deletions src/TwoFactorAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function validateTwoFactorCode(?string $code = null, bool $useRecoveryCod
{
return null !== $code
&& $this->hasTwoFactorEnabled()
&& ($this->validateCode($code) || $useRecoveryCodes && $this->useRecoveryCode($code));
&& ($this->validateCode($code) || ($useRecoveryCodes && $this->useRecoveryCode($code)));
}

/**
Expand Down Expand Up @@ -194,14 +194,13 @@ public function getRecoveryCodes(): Collection
public function generateRecoveryCodes(): Collection
{
[
'two-factor.model' => $model,
'two-factor.recovery.codes' => $amount,
'two-factor.recovery.length' => $length
] = config()->get([
'two-factor.model', 'two-factor.recovery.codes', 'two-factor.recovery.length',
'two-factor.recovery.codes', 'two-factor.recovery.length',
]);

$this->twoFactorAuth->recovery_codes = $model::generateRecoveryCodes($amount, $length);
$this->twoFactorAuth->recovery_codes = Models\TwoFactorAuthentication::generateRecoveryCodes($amount, $length);
$this->twoFactorAuth->recovery_codes_generated_at = now();
$this->twoFactorAuth->save();

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

/**
Expand Down

0 comments on commit fc05688

Please sign in to comment.