Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/addons/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class LoginController extends BaseController

// Get the credentials for login
$credentials = $this->request->getJsonVar(setting('Auth.validFields'));
$credentials = array_filter($credentials);
$credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== '');
$credentials['password'] = $this->request->getJsonVar('password');

/** @var Session $authenticator */
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/mobile_apps.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class LoginController extends BaseController

// Get the credentials for login
$credentials = $this->request->getPost(setting('Auth.validFields'));
$credentials = array_filter($credentials);
$credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== '');
$credentials['password'] = $this->request->getPost('password');

// Attempt to login
Expand Down
12 changes: 3 additions & 9 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@ parameters:
- CodeIgniter\Shield\Config
additionalServices:
- CodeIgniter\Shield\Config\Services
strictRules:
allRules: false
disallowedLooseComparison: true
booleansInConditions: true
disallowedBacktick: true
disallowedEmpty: true
disallowedImplicitArrayCreation: true
disallowedShortTernary: true
matchingInheritedMethodNames: true
ignoreErrors:
- identifier: function.internal
- identifier: return.internalClass
- identifier: staticMethod.internal
-
message: '#Dynamic call to static method PHPUnit\\#'
identifier: staticMethod.dynamicCall
4 changes: 2 additions & 2 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function routes(RouteCollection &$routes, array $config = []): void
$options = isset($params[3])
? ['as' => $params[3]]
: null;
$routes->{$params[0]}($params[1], $params[2], $options);
$routes->{$params[0]}($params[1], $params[2], $options); // @phpstan-ignore method.dynamicName
}
}
}
Expand Down Expand Up @@ -199,7 +199,7 @@ public function __call(string $method, array $args)
$authenticator = $this->authenticate->factory($this->alias);

if (method_exists($authenticator, $method)) {
return $authenticator->{$method}(...$args);
return $authenticator->{$method}(...$args); // @phpstan-ignore method.dynamicName
}
}
}
4 changes: 2 additions & 2 deletions src/Authentication/Authenticators/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function remember(bool $shouldRemember = true): self
* Attempts to authenticate a user with the given $credentials.
* Logs the user in with a successful check.
*
* @phpstan-param array{email?: string, username?: string, password?: string, ...<string, string>} $credentials
* @param array{email?: string, username?: string, password?: string, ...<string, string>} $credentials
*/
public function attempt(array $credentials): Result
{
Expand Down Expand Up @@ -345,7 +345,7 @@ private function recordLoginAttempt(
* Checks a user's $credentials to see if they match an
* existing user.
*
* @phpstan-param array{email?: string, username?: string, password?: string, ...<string, string>} $credentials
* @param array{email?: string, username?: string, password?: string, ...<string, string>} $credentials
*/
public function check(array $credentials): Result
{
Expand Down
4 changes: 3 additions & 1 deletion src/Authentication/JWT/JWSDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

class JWSDecoder
{
private readonly JWSAdapterInterface $jwsAdapter;

/**
* @var string The key group. The array key of Config\AuthJWT::$keys.
*/
protected $keyset = 'default';

public function __construct(private ?JWSAdapterInterface $jwsAdapter = null)
public function __construct(?JWSAdapterInterface $jwsAdapter = null)
{
$this->jwsAdapter = $jwsAdapter ?? new FirebaseAdapter();
}
Expand Down
7 changes: 5 additions & 2 deletions src/Authentication/JWT/JWSEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

class JWSEncoder
{
public function __construct(protected ?JWSAdapterInterface $jwsAdapter = null, protected ?Time $clock = null)
protected JWSAdapterInterface $jwsAdapter;
protected Time $clock;

public function __construct(?JWSAdapterInterface $jwsAdapter = null, ?Time $clock = null)
Comment thread
michalsn marked this conversation as resolved.
Resolved
{
$this->jwsAdapter = $jwsAdapter ?? new FirebaseAdapter();
$this->clock = $clock ?? new Time();
Expand Down Expand Up @@ -54,7 +57,7 @@ public function encode(
);

if (! array_key_exists('iat', $claims)) {
$payload['iat'] = $this->clock->now()->getTimestamp();
$payload['iat'] = $this->clock::now()->getTimestamp();
}

if (! array_key_exists('exp', $claims)) {
Expand Down
10 changes: 7 additions & 3 deletions src/Authentication/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
*/
class JWTManager
{
protected Time $clock;
protected JWSEncoder $jwsEncoder;
protected JWSDecoder $jwsDecoder;

public function __construct(
protected ?Time $clock = null,
protected ?JWSEncoder $jwsEncoder = null,
protected ?JWSDecoder $jwsDecoder = null,
?Time $clock = null,
?JWSEncoder $jwsEncoder = null,
?JWSDecoder $jwsDecoder = null,
Comment thread
michalsn marked this conversation as resolved.
Resolved
) {
$this->clock = $clock ?? new Time();
$this->jwsEncoder = $jwsEncoder ?? new JWSEncoder(null, $this->clock);
Expand Down
17 changes: 15 additions & 2 deletions src/Authentication/Passwords/NothingPersonalValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Result;
use Stringable;

/**
* Class NothingPersonalValidator
Expand Down Expand Up @@ -94,8 +95,20 @@
$personalFields = $this->config->personalFields;

foreach ($personalFields as $value) {
if (($user->{$value} ?? '') !== '') {
$needles[] = strtolower($user->{$value});
if (! isset($user->{$value})) {
continue;
}

$personalData = $user->{$value};

if (! is_string($personalData) && ! $personalData instanceof Stringable) {

Check warning on line 104 in src/Authentication/Passwords/NothingPersonalValidator.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "InstanceOf_": @@ @@ $personalData = $user->{$value}; - if (! is_string($personalData) && ! $personalData instanceof Stringable) { + if (! is_string($personalData) && $personalData instanceof Stringable) { continue; }

Check warning on line 104 in src/Authentication/Passwords/NothingPersonalValidator.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "LogicalNot": @@ @@ $personalData = $user->{$value}; - if (! is_string($personalData) && ! $personalData instanceof Stringable) { + if (! is_string($personalData) && $personalData instanceof Stringable) { continue; }
continue;
}

$personalData = (string) $personalData;

Check warning on line 108 in src/Authentication/Passwords/NothingPersonalValidator.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "CastString": @@ @@ continue; } - $personalData = (string) $personalData; + $personalData = $personalData; if ($personalData !== '') { $needles[] = mb_strtolower($personalData);

if ($personalData !== '') {
$needles[] = mb_strtolower($personalData);

Check warning on line 111 in src/Authentication/Passwords/NothingPersonalValidator.php

View workflow job for this annotation

GitHub Actions / Infection

Escaped Mutant for Mutator "MBString": @@ @@ $personalData = (string) $personalData; if ($personalData !== '') { - $needles[] = mb_strtolower($personalData); + $needles[] = strtolower($personalData); } }
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ private function setupEmail(): void
}

$config = config(EmailConfig::class);
$fromEmail = (string) $config->fromEmail; // Old Config may return null.
$fromName = (string) $config->fromName;
$fromEmail = $config->fromEmail;
$fromName = $config->fromName;

if ($fromEmail !== '' && $fromName !== '') {
$this->write(CLI::color(' Email Setup: ', 'green') . 'Everything is fine.');
Expand Down
3 changes: 1 addition & 2 deletions src/Config/AuthJWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class AuthJWT extends BaseConfig
* The key of the array is the key group name.
* The first key of the group is used for signing.
*
* @var array<string, array<int, array<string, string>>>
* @phpstan-var array<string, list<array<string, string>>>
* @var array<string, list<array<string, string>>>
*/
public array $keys = [
'default' => [
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function _remap(string $method, ...$params)
throw new PageNotFoundException();
}

return $this->{$method}(...$params);
return $this->{$method}(...$params); // @phpstan-ignore method.dynamicName
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function loginAction(): RedirectResponse

/** @var array $credentials */
$credentials = $this->request->getPost(setting('Auth.validFields')) ?? [];
$credentials = array_filter($credentials);
$credentials = array_filter($credentials, static fn ($value): bool => $value !== null && $value !== '');
$credentials['password'] = $this->request->getPost('password');
$remember = (bool) $this->request->getPost('remember');

Expand Down
2 changes: 1 addition & 1 deletion src/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function setIdentities(?array $identities): void
* Creates a new identity for this user with an email/password
* combination.
*
* @phpstan-param array{email: string, password: string} $credentials
* @param array{email: string, password: string} $credentials
*/
public function createEmailIdentity(array $credentials): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Models/UserIdentityModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function create($data): void
* Creates a new identity for this user with an email/password
* combination.
*
* @phpstan-param array{email: string, password: string} $credentials
* @param array{email: string, password: string} $credentials
*/
public function createEmailIdentity(User $user, array $credentials): void
{
Expand Down
5 changes: 2 additions & 3 deletions src/Models/UserModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ protected function fetchIdentities(array $data): array
* @param array $data Event $data
* @param list<UserIdentity> $identities
*
* @return list<User> UserId => User object
* @phpstan-return array<int|string, User> UserId => User object
* @return array<int|string, User> UserId => User object
*/
private function assignIdentities(array $data, array $identities): array
{
Expand Down Expand Up @@ -280,7 +279,7 @@ private function assignProperties(array $data, array $properties, string $type):
// Assign properties to all users (empty array if no properties found)
foreach ($mappedUsers as $userId => $user) {
$propertyArray = $properties[$userId] ?? [];
$user->{$method}($propertyArray);
$user->{$method}($propertyArray); // @phpstan-ignore method.dynamicName
}
unset($properties);

Expand Down
3 changes: 1 addition & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class Result
protected $extraInfo;

/**
* @phpstan-param array{success: bool, reason?: string|null, extraInfo?: string|User} $details
* @psalm-param array{success: bool, reason?: string|null, extraInfo?: string|User} $details
* @param array{success: bool, reason?: string|null, extraInfo?: string|User} $details
*/
public function __construct(array $details)
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Authentication/AuthHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function setUp(): void
{
parent::setUp();

$this->setPrivateProperty(auth(), 'alias', null);
self::setPrivateProperty(auth(), 'alias', null);
}

public function testAuthReturnsDefaultAuthenticator(): void
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testUserIdReturnsNull(): void
public function testUserIdReturnsId(): void
{
$user = fake(UserModel::class, ['id' => 1]);
$this->setPrivateProperty(auth()->getAuthenticator(), 'user', $user);
self::setPrivateProperty(auth()->getAuthenticator(), 'user', $user);
$_SESSION['user']['id'] = $user->id;

$this->assertTrue(auth()->loggedIn());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function generateJWT(?Time $clock = null): string
{
$this->user = \fake(UserModel::class, ['id' => 1, 'username' => 'John Smith']);

$generator = new JWTManager($clock);
$generator = $clock instanceof Time ? new JWTManager($clock) : new JWTManager();

return $generator->generateToken($this->user);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Authentication/MagicLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testMagicLinkSubmitBadEmail(): void

public function testMagicLinkSubmitSuccess(): void
{
/** @phpstan-var User $user */
/** @var User $user */
$user = fake(UserModel::class);
$user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']);

Expand Down Expand Up @@ -104,7 +104,7 @@ public function testMagicLinkVerifyExpired(): void
{
$identities = new UserIdentityModel();

/** @phpstan-var User $user */
/** @var User $user */
$user = fake(UserModel::class);
$user->createEmailIdentity(['email' => 'foo@example.com', 'password' => 'secret123']);
$identities->insert([
Expand Down
7 changes: 3 additions & 4 deletions tests/Commands/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ private function removeDirectory(string $directory): void
/**
* Set MockInputOutput and user inputs.
*
* @param array<int, string> $inputs User inputs
* @phpstan-param list<string> $inputs
* @param list<string> $inputs User inputs
*/
private function setMockIo(array $inputs): void
{
Expand All @@ -86,7 +85,7 @@ public function testRun(): void

$command = new Setup(Services::logger(), Services::commands());

$this->setPrivateProperty($command, 'distPath', $appFolder);
self::setPrivateProperty($command, 'distPath', $appFolder);

$command->run([]);

Expand Down Expand Up @@ -142,7 +141,7 @@ public function testRunEmailConfigIsFine(): void

$command = new Setup(Services::logger(), Services::commands());

$this->setPrivateProperty($command, 'distPath', $appFolder);
self::setPrivateProperty($command, 'distPath', $appFolder);

$command->run([]);

Expand Down
3 changes: 1 addition & 2 deletions tests/Commands/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ protected function tearDown(): void
/**
* Set MockInputOutput and user inputs.
*
* @param array<int, string> $inputs User inputs
* @phpstan-param list<string> $inputs
* @param list<string> $inputs User inputs
*/
private function setMockIo(array $inputs): void
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/Authentication/JWT/Adapters/FirebaseAdapaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class FirebaseAdapaterTest extends TestCase
{
public function testDecode(): void
{
$token = $this->generateJWT();
$token = self::generateJWT();

$adapter = new FirebaseAdapter();

Expand All @@ -52,7 +52,7 @@ public static function generateJWT(?Time $clock = null): string
{
$user = fake(UserModel::class, ['id' => 1, 'username' => 'John Smith'], false);

$generator = new JWTManager($clock);
$generator = $clock instanceof Time ? new JWTManager($clock) : new JWTManager();

return $generator->generateToken($user);
}
Expand All @@ -77,7 +77,7 @@ public function testDecodeExpiredToken(): void
$adapter = new FirebaseAdapter();

Time::setTestNow('-1 hour');
$token = $this->generateJWT();
$token = self::generateJWT();
Time::setTestNow();

$key = 'default';
Expand All @@ -86,7 +86,7 @@ public function testDecodeExpiredToken(): void

public function testDecodeInvalidTokenExceptionUnexpectedValueException(): void
{
$token = $this->generateJWT();
$token = self::generateJWT();

// Change algorithm and it makes the key invalid.
/** @var AuthJWT $config */
Expand Down Expand Up @@ -115,7 +115,7 @@ public function testDecodeInvalidArgumentException(): void
$this->expectException(ShieldInvalidArgumentException::class);
$this->expectExceptionMessage('Invalid Keyset: "default". Key material must not be empty');

$token = $this->generateJWT();
$token = self::generateJWT();

// Set invalid key.
/** @var AuthJWT $config */
Expand Down
Loading
Loading