2017-08-31 02:11:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Users;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-11-18 18:35:33 +00:00
|
|
|
use Carbon\Carbon;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Tests\TestCase;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-11-18 18:35:33 +00:00
|
|
|
use PragmaRX\Google2FA\Google2FA;
|
|
|
|
use Illuminate\Contracts\Config\Repository;
|
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
class ToggleTwoFactorServiceTest extends TestCase
|
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
const TEST_WINDOW_INT = 4;
|
|
|
|
const USER_TOTP_SECRET = 'encryptedValue';
|
|
|
|
const DECRYPTED_USER_SECRET = 'decryptedValue';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2017-08-31 02:11:14 +00:00
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \PragmaRX\Google2FA\Google2FA|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $google2FA;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $repository;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-11-18 18:35:33 +00:00
|
|
|
Carbon::setTestNow(Carbon::now());
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->config = m::mock(Repository::class);
|
|
|
|
$this->encrypter = m::mock(Encrypter::class);
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->google2FA = m::mock(Google2FA::class);
|
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->config->shouldReceive('get')->with('pterodactyl.auth.2fa.window')->once()->andReturn(self::TEST_WINDOW_INT);
|
|
|
|
$this->encrypter->shouldReceive('decrypt')->with(self::USER_TOTP_SECRET)->once()->andReturn(self::DECRYPTED_USER_SECRET);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that 2FA can be enabled for a user.
|
|
|
|
*/
|
|
|
|
public function testTwoFactorIsEnabledForUser()
|
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
2017-11-18 18:35:33 +00:00
|
|
|
'totp_authenticated_at' => Carbon::now(),
|
|
|
|
'use_totp' => true,
|
|
|
|
])->once()->andReturnNull();
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->assertTrue($this->getService()->handle($model, 'test-token'));
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that 2FA can be disabled for a user.
|
|
|
|
*/
|
|
|
|
public function testTwoFactorIsDisabled()
|
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => true]);
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
2017-11-18 18:35:33 +00:00
|
|
|
'totp_authenticated_at' => Carbon::now(),
|
|
|
|
'use_totp' => false,
|
|
|
|
])->once()->andReturnNull();
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->assertTrue($this->getService()->handle($model, 'test-token'));
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that 2FA will remain disabled for a user.
|
|
|
|
*/
|
|
|
|
public function testTwoFactorRemainsDisabledForUser()
|
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
|
2017-11-18 18:35:33 +00:00
|
|
|
'totp_authenticated_at' => Carbon::now(),
|
|
|
|
'use_totp' => false,
|
|
|
|
])->once()->andReturnNull();
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->assertTrue($this->getService()->handle($model, 'test-token', false));
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the token provided is invalid.
|
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfTokenIsInvalid()
|
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET]);
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->google2FA->shouldReceive('verifyKey')->once()->andReturn(false);
|
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->getService()->handle($model, 'test-token');
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* Return an instance of the service with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Users\ToggleTwoFactorService
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private function getService(): ToggleTwoFactorService
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
2017-11-18 18:35:33 +00:00
|
|
|
return new ToggleTwoFactorService($this->encrypter, $this->google2FA, $this->config, $this->repository);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
}
|