90 lines
3.4 KiB
PHP
90 lines
3.4 KiB
PHP
<?php
|
|
|
|
use Illuminate\Auth\Events\Failed;
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Validation\ValidationException;
|
|
use IriksIT\Manager\Support\Login\BaseLoginCheck;
|
|
use IriksIT\Manager\Tests\TestCase;
|
|
|
|
uses(TestCase::class);
|
|
|
|
class NeedsCode extends BaseLoginCheck
|
|
{
|
|
public function fields(): array
|
|
{
|
|
return [['name' => 'code', 'type' => 'text', 'label' => 'Code', 'autocomplete' => 'one-time-code']];
|
|
}
|
|
|
|
public function after(Request $request, Authenticatable $user): void
|
|
{
|
|
if ($request->input('code') !== '123456') {
|
|
throw ValidationException::withMessages(['code' => 'The code is not right.']);
|
|
}
|
|
}
|
|
}
|
|
|
|
beforeEach(function () {
|
|
$this->user = makeUser(superAdmin: true);
|
|
$this->user->forceFill(['password' => Hash::make('a-long-enough-password')])->save();
|
|
$this->login = fn (array $extra = []) => $this->postJson('/manager/login', ['email' => $this->user->email, 'password' => 'a-long-enough-password'] + $extra);
|
|
});
|
|
|
|
it('logs in as before when no check is set', function () {
|
|
($this->login)()->assertOk()->assertJsonPath('redirect', '/manager');
|
|
$this->assertAuthenticated();
|
|
});
|
|
|
|
it('asks a second factor of the site after the password, and counts a refusal as a failed login', function () {
|
|
config(['manager.login.checks' => [NeedsCode::class]]);
|
|
$failed = 0;
|
|
Event::listen(Failed::class, function () use (&$failed) {
|
|
$failed++;
|
|
});
|
|
|
|
($this->login)(['code' => '000000'])->assertJsonValidationErrors(['code']);
|
|
$this->assertGuest();
|
|
expect($failed)->toBe(1);
|
|
|
|
($this->login)(['code' => '123456'])->assertOk();
|
|
$this->assertAuthenticated();
|
|
});
|
|
|
|
it('shows the fields of the checks on the form', function () {
|
|
config(['manager.login.checks' => [NeedsCode::class, 'Not\\A\\Check']]);
|
|
|
|
$this->get('/manager/login')->assertOk()->assertSee('"login_fields":[{"name":"code"', false);
|
|
});
|
|
|
|
describe('turnstile', function () {
|
|
beforeEach(fn () => config(['manager.login.turnstile' => ['site_key' => 'site-key', 'secret' => 'secret-key']]));
|
|
|
|
it('needs a token that Cloudflare accepts, and checks before the password', function () {
|
|
Http::fake(['challenges.cloudflare.com/*' => Http::response(['success' => true])]);
|
|
|
|
($this->login)()->assertJsonValidationErrors(['turnstile']);
|
|
($this->login)(['turnstile' => 'token'])->assertOk();
|
|
|
|
Http::assertSent(fn ($request) => $request['secret'] === 'secret-key' && $request['response'] === 'token');
|
|
});
|
|
|
|
it('refuses a token Cloudflare does not accept, and everything when Cloudflare is unreachable', function () {
|
|
Http::fake(['challenges.cloudflare.com/*' => Http::response(['success' => false])]);
|
|
($this->login)(['turnstile' => 'bad'])->assertJsonValidationErrors(['turnstile']);
|
|
|
|
Http::fake(['challenges.cloudflare.com/*' => Http::response('', 500)]);
|
|
($this->login)(['turnstile' => 'token'])->assertJsonValidationErrors(['turnstile']);
|
|
|
|
Http::fake(fn () => throw new ConnectionException('down'));
|
|
($this->login)(['turnstile' => 'token'])->assertJsonValidationErrors(['turnstile']);
|
|
$this->assertGuest();
|
|
});
|
|
|
|
it('puts the widget on the form', function () {
|
|
$this->get('/manager/login')->assertSee('"type":"turnstile","site_key":"site-key"', false);
|
|
});
|
|
});
|