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

Commit

Permalink
Merge pull request #74 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixes #72
  • Loading branch information
DarkGhostHunter authored Nov 8, 2021
2 parents ff3111c + bf099ba commit e9b8c52
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ public function login(Request $request)

Behind the scenes, once the User is retrieved and validated from your guard of choice, it makes an additional check for a valid TOTP code. If it's invalid, it will return false and no authentication will happen.

> For Laravel Breeze, you may need to edit the `LoginRequest::authenticate()` call.
> For Laravel Fortify and Jetstream, you may need to set a custom callback with the `Fortify::authenticateUsing()` method.
#### Separating the TOTP requirement

In some occasions you will want to tell the user the authentication failed not because the credentials were incorrect, but because of the TOTP code was invalid.
Expand Down
2 changes: 1 addition & 1 deletion src/Laraguard.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function isSafeDevicesEnabled(): bool
protected function requestHasCode(): bool
{
return !validator($this->request->only($this->input), [
$this->input => 'required|numeric',
$this->input => 'required|alpha_num',
])->fails();
}

Expand Down
57 changes: 57 additions & 0 deletions tests/LaraguardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Tests\Stubs\UserStub;
use Tests\Stubs\UserTwoFactorStub;

use function now;

class LaraguardTest extends TestCase
{
use DatabaseMigrations;
Expand Down Expand Up @@ -65,6 +67,40 @@ public function test_authenticates_with_when_with_no_exceptions(): void
static::assertTrue(Auth::attemptWhen($credentials, Laraguard::hasCodeOrFails()));
}

public function test_authenticates_with_when_with_recovery_code(): void
{
$credentials = [
'email' => $this->user->email,
'password' => 'secret'
];

$this->instance('request', Request::create('test', 'POST', [
'2fa_code' => $this->user->getRecoveryCodes()->first()['code']
]));

$this->travelTo($now = now());

static::assertTrue(Auth::attemptWhen($credentials, Laraguard::hasCode()));
static::assertEquals($now->toIso8601ZuluString('microsecond'), $this->user->fresh()->getRecoveryCodes()->first()['used_at']);
}

public function test_authenticates_with_when_with_recovery_code_with_no_exceptions(): void
{
$credentials = [
'email' => $this->user->email,
'password' => 'secret'
];

$this->instance('request', Request::create('test', 'POST', [
'2fa_code' => $this->user->getRecoveryCodes()->first()['code']
]));

$this->travelTo($now = now());

static::assertTrue(Auth::attemptWhen($credentials, Laraguard::hasCodeOrFails()));
static::assertEquals($now->toIso8601ZuluString('microsecond'), $this->user->fresh()->getRecoveryCodes()->first()['used_at']);
}

public function test_authenticates_with_different_input_name(): void
{
$credentials = [
Expand Down Expand Up @@ -142,6 +178,27 @@ public function test_validation_exception_when_code_invalid(): void
}
}

public function test_validation_exception_when_code_empty(): void
{
$this->expectException(ValidationException::class);

$credentials = [
'email' => $this->user->email,
'password' => 'secret'
];

$this->instance('request', Request::create('test', 'POST', [
'2fa_code' => ''
]));

try {
Auth::attemptWhen($credentials, Laraguard::hasCodeOrFails());
} catch (ValidationException $exception) {
static::assertSame(['2fa_code' => ['The Code is invalid or has expired.']], $exception->errors());
throw $exception;
}
}

public function test_validation_exception_with_message_when_code_invalid(): void
{
$this->expectException(ValidationException::class);
Expand Down

0 comments on commit e9b8c52

Please sign in to comment.