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

Add custom validation rule with translation. #30

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,34 +127,40 @@ public function confirmTwoFactor(Request $request)

If the User doesn't issue the correct Code, the method will return `false`. You can tell the User to double-check its device's timezone, or create another Shared Secret with `createTwoFactorAuth()`.

You may validate that Code passed to a controller is a valid Code by using the TwoFactorAuth rule.
You may validate that Code passed to a controller is a valid Code by using the TotpCode rule.

```php
use DarkGhostHunter\Laraguard\Rules\TwoFactorAuth;
use DarkGhostHunter\Laraguard\Rules\TotpCode;

public function confirmTwoFactor(Request $request)
{
$this->validate($request, [
'2fa_code' => ['required', new TwoFactorAuth()],
'2fa_code' => ['required', new TotpCode()],
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
]);

$request->user()->enableTwoFactorAuth();
}
```

You can also use the 'pipe' syntax for rule.

```php
public function confirmTwoFactor(Request $request)
public function setSafeDevice(Request $request)
{
$this->validate($request, [
'2fa_code' => 'required|two_factor_auth',
'2fa_code' => 'required|totp_code',
]);

$request->user()->addSafeDevice($request);

session()->flash('message', "This device has been added as safe and the app won't ask for codes");
}
```

You can also define an error message by adding an entry in the validation language file or you can publish the language files to your resources/lang folder.

```php
'two_factor_auth' => 'The code you have entered is invalid.',
'totp_code' => 'The code you have entered is invalid.',

// The rest of the validation error messages...
```
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/en/validation.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

return [
'two_factor_auth' => 'The code you have entered is invalid.',
'totp_code' => 'The code you have entered is invalid.',
];
8 changes: 4 additions & 4 deletions src/LaraguardServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DarkGhostHunter\Laraguard;

use DarkGhostHunter\Laraguard\Rules\TwoFactorAuth;
use DarkGhostHunter\Laraguard\Rules\TotpCode;
use Illuminate\Routing\Router;
use Illuminate\Auth\Events\Validated;
use Illuminate\Auth\Events\Attempting;
Expand Down Expand Up @@ -82,9 +82,9 @@ protected function registerListener(Repository $config, Dispatcher $dispatcher)
*/
private function registerValidators()
{
$this->app['validator']->extend('two_factor_auth', function ($attribute, $value) {
return (new TwoFactorAuth())->passes($attribute, $value);
}, __('laraguard::validation.two_factor_auth'));
$this->app['validator']->extend('totp_code', function ($attribute, $value) {
return (new TotpCode())->passes($attribute, $value);
}, __('laraguard::validation.totp_code'));
}

/**
Expand Down
32 changes: 25 additions & 7 deletions src/Rules/TwoFactorAuth.php → src/Rules/TotpCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@

namespace DarkGhostHunter\Laraguard\Rules;

use DarkGhostHunter\Laraguard\Contracts\TwoFactorAuthenticatable;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Auth;

class TwoFactorAuth implements Rule
class TotpCode implements Rule
{
/**
* The name of the rule.
*
* @var string
*/
protected $rule = 'two_factor_auth';
protected $rule = 'totp_code';

/**
* The auth user.
*
* @var \Illuminate\Foundation\Auth\User
*/
protected $user;

/**
* Create a new "totp code" rule instance.
*
* @return void
*/
public function __construct()
{
$this->user = Auth::user();
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Determine if the validation rule passes.
Expand All @@ -23,11 +41,11 @@ class TwoFactorAuth implements Rule
*/
public function passes($attribute, $value) : bool
{
if (is_null($value)) {
return false;
if ($this->user instanceof TwoFactorAuthenticatable) {
return is_string($value) && $this->user->twoFactorAuth->validateCode($value);
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
}

return Auth::user()->confirmTwoFactorAuth($value);
return false;
}

/**
Expand All @@ -37,7 +55,7 @@ public function passes($attribute, $value) : bool
*/
public function message() : string
{
return __('laraguard::validation.two_factor_auth');
return __('laraguard::validation.totp_code');
}

/**
Expand Down