Skip to content

Commit

Permalink
The userId property is normalized to int so no need to type-cast it
Browse files Browse the repository at this point in the history
Resolves
```
 ------ -------------------------------------------------------
  Line   app/User/Manager.php
 ------ -------------------------------------------------------
  129    Unreachable statement - code above always terminates.
 ------ -------------------------------------------------------
```
which happens because nette/database 3.2.0 added `never` return type to Row::__get().

This way, PhpStorm complains "Return value must be of type 'int', 'never' returned" but PHPStan doesn't, so...
  • Loading branch information
spaze committed Dec 26, 2023
1 parent 3c6e639 commit a09eb16
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
10 changes: 10 additions & 0 deletions site/app/Test/PrivateProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ public static function setValue(object $object, string $property, mixed $value):
$property->setValue($object, $value);
}


/**
* @throws ReflectionException
*/
public static function getValue(object $object, string $property): mixed
{
$property = new ReflectionProperty($object, $property);
return $property->getValue($object);
}

}
5 changes: 2 additions & 3 deletions site/app/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,18 @@ private function verifyPassword(string $username, string $password): int
if (!$user) {
throw new AuthenticationException('The username is incorrect.', self::IdentityNotFound);
}
$userId = (int)$user->userId;
try {
$hash = $this->passwordEncryption->decrypt((string)$user->password);
if (!$this->passwords->verify($password, $hash)) {
throw new AuthenticationException('The password is incorrect.', self::InvalidCredential);
} elseif ($this->passwords->needsRehash($hash)) {
$this->updatePassword($userId, $password);
$this->updatePassword($user->userId, $password);
}
} catch (HaliteAlert $e) {
Debugger::log($e);
throw new AuthenticationException('Oops... Something went wrong.', self::Failure);
}
return $userId;
return $user->userId;
}


Expand Down
3 changes: 3 additions & 0 deletions site/config/tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ parameters:
com: burger.test
encryption:
keys:
password:
test: "f015033d6b0b24e77bc9cbd86ec52ed5bc94ca4901c9f1378768423ec0278d66"
email:
test: "17fa3225effc107a689eb72fd8c20983bbc690bf9ea42a2f0306e0c226720845"
activeKeyIds:
password: test
email: test
awsLambda:
upcKeys:
Expand Down
34 changes: 34 additions & 0 deletions site/tests/User/ManagerTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ declare(strict_types = 1);

namespace MichalSpacekCz\User;

use MichalSpacekCz\Test\Database\Database;
use MichalSpacekCz\Test\PrivateProperty;
use MichalSpacekCz\Test\TestCaseRunner;
use MichalSpacekCz\User\Exceptions\IdentityIdNotIntException;
use MichalSpacekCz\User\Exceptions\IdentityNotSimpleIdentityException;
use MichalSpacekCz\User\Exceptions\IdentityUsernameNotStringException;
use MichalSpacekCz\User\Exceptions\IdentityWithoutUsernameException;
use Nette\DI\Container;
use Nette\Security\Passwords;
use Nette\Security\SimpleIdentity;
use Nette\Security\User;
use Spaze\Encryption\Symmetric\StaticKey;
use Tester\Assert;
use Tester\TestCase;

Expand All @@ -23,10 +27,17 @@ require __DIR__ . '/../bootstrap.php';
class ManagerTest extends TestCase
{

private readonly StaticKey $passwordEncryption;


public function __construct(
private readonly Manager $authenticator,
private readonly User $user,
private readonly Database $database,
private readonly Passwords $passwords,
private readonly Container $container,
) {
$this->passwordEncryption = $this->container->getService('passwordEncryption');
}


Expand Down Expand Up @@ -92,6 +103,29 @@ class ManagerTest extends TestCase
}


public function testChangePassword(): void
{
$oldPassword = 'hunter2';
$newPassword = 'hunter3';
PrivateProperty::setValue($this->user, 'authenticated', true);
PrivateProperty::setValue($this->user, 'identity', new SimpleIdentity(1337, [], ['username' => '303']));
$this->database->setFetchResult([
'userId' => 1337,
'username' => '303',
'password' => $this->passwordEncryption->encrypt($this->passwords->hash($oldPassword)),
]);
Assert::noError(function () use ($oldPassword, $newPassword): void {
$this->authenticator->changePassword($this->user, $oldPassword, $newPassword);
});
$encryptedHash = $this->database->getParamsForQuery('UPDATE users SET password = ? WHERE id_user = ?')[0];
if (is_string($encryptedHash)) {
Assert::true($this->passwords->verify($newPassword, $this->passwordEncryption->decrypt($encryptedHash)));
} else {
Assert::fail('Encrypted hash should be a string but is ' . get_debug_type($encryptedHash));
}
}


public function testChangePasswordUserIdNotInt(): void
{
PrivateProperty::setValue($this->user, 'authenticated', true);
Expand Down

0 comments on commit a09eb16

Please sign in to comment.