2016-08-29 04:47:45 +05:30
|
|
|
<?php
|
2019-08-02 18:27:17 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
namespace api\tests\unit\modules\authserver\models;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 04:47:45 +05:30
|
|
|
use api\modules\authserver\models\AuthenticationForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2021-03-09 02:51:10 +05:30
|
|
|
use common\models\Account;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\tests\fixtures\OauthClientFixture;
|
2021-03-08 15:56:47 +05:30
|
|
|
use OTPHP\TOTP;
|
2021-03-09 02:51:10 +05:30
|
|
|
use function Ramsey\Uuid\v4 as uuid4;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class AuthenticationFormTest extends TestCase {
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-08-29 04:47:45 +05:30
|
|
|
return [
|
|
|
|
'accounts' => AccountFixture::class,
|
2019-12-11 01:21:11 +05:30
|
|
|
'oauthClients' => OauthClientFixture::class,
|
2016-08-29 04:47:45 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAuthenticateByValidCredentials(): void {
|
2019-12-05 03:22:27 +05:30
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'admin';
|
|
|
|
$authForm->password = 'password_0';
|
2021-03-09 02:51:10 +05:30
|
|
|
$authForm->clientToken = uuid4();
|
2019-12-05 03:22:27 +05:30
|
|
|
$result = $authForm->authenticate()->getResponseData();
|
2021-02-18 05:11:38 +05:30
|
|
|
$this->assertMatchesRegularExpression('/^[\w=-]+\.[\w=-]+\.[\w=-]+$/', $result['accessToken']);
|
2019-12-05 03:22:27 +05:30
|
|
|
$this->assertSame($authForm->clientToken, $result['clientToken']);
|
2021-02-18 05:11:38 +05:30
|
|
|
$this->assertSame('df936908b2e1544d96f82977ec213022', $result['selectedProfile']['id']);
|
2019-12-05 03:22:27 +05:30
|
|
|
$this->assertSame('Admin', $result['selectedProfile']['name']);
|
2019-12-11 01:21:11 +05:30
|
|
|
$this->assertTrue(OauthSession::find()->andWhere([
|
|
|
|
'account_id' => 1,
|
|
|
|
'client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER,
|
|
|
|
])->exists());
|
2021-03-06 15:07:58 +05:30
|
|
|
$this->assertArrayNotHasKey('user', $result);
|
|
|
|
|
|
|
|
$authForm->requestUser = true;
|
|
|
|
$result = $authForm->authenticate()->getResponseData();
|
|
|
|
$this->assertSame([
|
|
|
|
'id' => 'df936908b2e1544d96f82977ec213022',
|
|
|
|
'username' => 'Admin',
|
|
|
|
'properties' => [
|
|
|
|
[
|
|
|
|
'name' => 'preferredLanguage',
|
|
|
|
'value' => 'en',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
], $result['user']);
|
2019-12-05 03:22:27 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAuthenticateByValidCredentialsWith2FA(): void {
|
2021-03-08 15:56:47 +05:30
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'otp@gmail.com';
|
2021-03-09 02:51:10 +05:30
|
|
|
$authForm->password = 'password_0:' . TOTP::create('BBBB')->now();
|
|
|
|
$authForm->clientToken = uuid4();
|
|
|
|
|
|
|
|
// Just ensure that there is no exception
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a special case which ensures that if the user has a password that looks like
|
|
|
|
* a two-factor code passed in the password field, than he can still log in into his account
|
|
|
|
*/
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testAuthenticateEdgyCaseFor2FA(): void {
|
2021-03-09 02:51:10 +05:30
|
|
|
/** @var Account $account */
|
|
|
|
$account = Account::findOne(['email' => 'admin@ely.by']);
|
|
|
|
$account->setPassword('password_0:123456');
|
|
|
|
$account->save();
|
|
|
|
|
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'admin@ely.by';
|
|
|
|
$authForm->password = 'password_0:123456';
|
|
|
|
$authForm->clientToken = uuid4();
|
2021-03-08 15:56:47 +05:30
|
|
|
|
|
|
|
// Just ensure that there is no exception
|
|
|
|
$this->expectNotToPerformAssertions();
|
|
|
|
|
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
/**
|
|
|
|
* @dataProvider getInvalidCredentialsCases
|
|
|
|
*/
|
2021-03-09 02:51:10 +05:30
|
|
|
public function testAuthenticateByWrongCredentials(
|
|
|
|
string $expectedExceptionMessage,
|
|
|
|
string $login,
|
|
|
|
string $password,
|
2024-12-02 15:40:55 +05:30
|
|
|
string $totp = null,
|
|
|
|
): void {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
2019-12-11 01:21:11 +05:30
|
|
|
$this->expectExceptionMessage($expectedExceptionMessage);
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = $login;
|
2021-03-09 02:51:10 +05:30
|
|
|
$authForm->password = $password . ($totp ? ":{$totp}" : '');
|
|
|
|
$authForm->clientToken = uuid4();
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function getInvalidCredentialsCases(): iterable {
|
2019-12-11 01:21:11 +05:30
|
|
|
yield ['Invalid credentials. Invalid nickname or password.', 'wrong-username', 'wrong-password'];
|
|
|
|
yield ['Invalid credentials. Invalid email or password.', 'wrong-email@ely.by', 'wrong-password'];
|
|
|
|
yield ['This account has been suspended.', 'Banned', 'password_0'];
|
|
|
|
yield ['Account protected with two factor auth.', 'AccountWithEnabledOtp', 'password_0'];
|
2021-03-09 02:51:10 +05:30
|
|
|
yield ['Invalid credentials. Invalid nickname or password.', 'AccountWithEnabledOtp', 'password_0', '123456'];
|
2016-08-29 04:47:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|