2021-07-18 17:28:14 +00:00
|
|
|
<?php
|
|
|
|
|
2021-07-24 21:01:14 +00:00
|
|
|
namespace Pterodactyl\Tests\Unit\Http\Middleware;
|
2021-07-18 17:28:14 +00:00
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\User;
|
2021-08-08 17:48:35 +00:00
|
|
|
use Pterodactyl\Models\SecurityKey;
|
2021-07-18 17:28:14 +00:00
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
|
|
|
use Pterodactyl\Exceptions\Http\TwoFactorAuthRequiredException;
|
2021-08-07 23:10:24 +00:00
|
|
|
use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
|
2021-07-18 17:28:14 +00:00
|
|
|
|
|
|
|
class RequireTwoFactorAuthenticationTest extends MiddlewareTestCase
|
|
|
|
{
|
|
|
|
private $alerts;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->alerts = m::mock(AlertsMessageBag::class);
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testNoRequirementUserWithout2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_NONE);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => false]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testNoRequirementUserWithTotp2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_NONE);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => true]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testNoRequirementUserWithWebauthn2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_NONE);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = User::factory()
|
2021-08-08 17:48:35 +00:00
|
|
|
->has(SecurityKey::factory()->count(1))
|
2021-07-18 17:28:14 +00:00
|
|
|
->create(['use_totp' => false]);
|
|
|
|
$this->setRequestUserModel($user);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertNotEmpty($user->webauthnKeys);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testNoRequirementGuestUser()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_NONE);
|
|
|
|
|
|
|
|
$this->setRequestUserModel();
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/auth/login');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn('auth.login');
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAllRequirementUserWithout2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
$this->expectException(TwoFactorAuthRequiredException::class);
|
|
|
|
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ALL);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => false]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAllRequirementUserWithTotp2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ALL);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => true]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAllRequirementRuserWithWebauthn2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ALL);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = User::factory()
|
2021-08-08 17:48:35 +00:00
|
|
|
->has(SecurityKey::factory()->count(1))
|
2021-07-18 17:28:14 +00:00
|
|
|
->create(['use_totp' => false]);
|
|
|
|
$this->setRequestUserModel($user);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertNotEmpty($user->webauthnKeys);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAllRequirementGuestUser()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ALL);
|
|
|
|
|
|
|
|
$this->setRequestUserModel();
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/auth/login');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn('auth.login');
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementUserWithout2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => false]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertFalse($user->root_admin);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementAdminUserWithout2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
$this->expectException(TwoFactorAuthRequiredException::class);
|
|
|
|
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => false, 'root_admin' => true]);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertTrue($user->root_admin);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementUserWithTotp2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => true]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertFalse($user->root_admin);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementAdminUserWithTotp2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$user = $this->generateRequestUserModel(['use_totp' => true, 'root_admin' => true]);
|
|
|
|
|
|
|
|
$this->assertTrue($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertTrue($user->root_admin);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementUserWithWebauthn2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
2021-08-08 17:48:35 +00:00
|
|
|
$user = User::factory()->has(SecurityKey::factory()->count(1))->create(['use_totp' => false]);
|
2021-07-18 17:28:14 +00:00
|
|
|
$this->setRequestUserModel($user);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertFalse($user->root_admin);
|
|
|
|
$this->assertNotEmpty($user->webauthnKeys);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementAdminUserWithWebauthn2fa()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = User::factory()
|
2021-08-08 17:48:35 +00:00
|
|
|
->has(SecurityKey::factory()->count(1))
|
2021-07-18 17:28:14 +00:00
|
|
|
->create(['use_totp' => false, 'root_admin' => true]);
|
|
|
|
$this->setRequestUserModel($user);
|
|
|
|
|
|
|
|
$this->assertFalse($user->use_totp);
|
|
|
|
$this->assertEmpty($user->totp_secret);
|
|
|
|
$this->assertEmpty($user->totp_authenticated_at);
|
|
|
|
$this->assertTrue($user->root_admin);
|
|
|
|
$this->assertNotEmpty($user->webauthnKeys);
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn(null);
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
2021-08-07 23:10:24 +00:00
|
|
|
public function testAdminRequirementGuestUser()
|
2021-07-18 17:28:14 +00:00
|
|
|
{
|
|
|
|
// Disable the 2FA requirement
|
|
|
|
config()->set('pterodactyl.auth.2fa_required', RequireTwoFactorAuthentication::LEVEL_ADMIN);
|
|
|
|
|
|
|
|
$this->setRequestUserModel();
|
|
|
|
|
|
|
|
$this->request->shouldReceive('getRequestUri')->withNoArgs()->andReturn('/auth/login');
|
|
|
|
$this->request->shouldReceive('route->getName')->withNoArgs()->andReturn('auth.login');
|
|
|
|
$this->request->shouldReceive('isJson')->withNoArgs()->andReturn(true);
|
|
|
|
|
|
|
|
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMiddleware(): RequireTwoFactorAuthentication
|
|
|
|
{
|
|
|
|
return new RequireTwoFactorAuthentication($this->alerts);
|
|
|
|
}
|
|
|
|
}
|