diff --git a/.gitattributes b/.gitattributes index 96cbc5a3a..28f96ba97 100644 --- a/.gitattributes +++ b/.gitattributes @@ -14,8 +14,8 @@ /phpstan.dist.neon export-ignore /phpunit.dist.xml export-ignore /pyrameter.php export-ignore -/psalm_autoload.php export-ignore /psalm-baseline.xml export-ignore +/psalm_autoload.php export-ignore /psalm.xml export-ignore /rector.php export-ignore /roave-bc-check.yml export-ignore diff --git a/.github/workflows/psalm.yml b/.github/workflows/psalm.yml index 8c9d14eef..79271e784 100644 --- a/.github/workflows/psalm.yml +++ b/.github/workflows/psalm.yml @@ -5,19 +5,28 @@ on: branches: - develop paths: - - '**.php' - - 'composer.*' - - 'psalm*' + - 'src/**.php' + - 'tests/**.php' + - 'composer.json' + - 'psalm.xml' + - 'psalm-baseline.xml' + - 'psalm_autoload.php' - '.github/workflows/psalm.yml' push: branches: - develop paths: - - '**.php' - - 'composer.*' - - 'psalm*' + - 'src/**.php' + - 'tests/**.php' + - 'composer.json' + - 'psalm.xml' + - 'psalm-baseline.xml' + - 'psalm_autoload.php' - '.github/workflows/psalm.yml' +permissions: + contents: read + jobs: psalm: uses: codeigniter4/.github/.github/workflows/psalm.yml@CI47 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 3c9f7c89e..b8938e8f3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,17 +1,493 @@ - - - - $this->where(['user_id' => $user->id])->delete() - + + + + + + - - + + + + + + + + + + false, + 'reason' => $user->getBanMessage() ?? lang('Auth.bannedUser'), + ]]]> + false, + 'reason' => lang( + 'Auth.noToken', + [config('AuthToken')->authenticatorHeader['tokens']], + ), + ]]]> + false, + 'reason' => lang('Auth.badToken'), + ]]]> + false, + 'reason' => lang('Auth.oldToken'), + ]]]> + false, + 'reason' => lang('Auth.oldToken'), + ]]]> + + + + + false, + 'reason' => $user->getBanMessage() ?? lang('Auth.bannedUser'), + ]]]> + false, + 'reason' => lang( + 'Auth.noToken', + [config('AuthToken')->authenticatorHeader['hmac']], + ), + ]]]> + false, + 'reason' => lang('Auth.badToken'), + ]]]> + false, + 'reason' => lang('Auth.badToken'), + ]]]> + false, + 'reason' => lang('Auth.badToken'), + ]]]> + false, + 'reason' => lang('Auth.oldToken'), + ]]]> + + + + + false, + 'reason' => $user->getBanMessage() ?? lang('Auth.bannedUser'), + ]]]> + false, + 'reason' => lang( + 'Auth.noToken', + [config('AuthJWT')->authenticatorHeader], + ), + ]]]> + false, + 'reason' => lang('Auth.invalidUser'), + ]]]> + + + + + false, + 'reason' => $user->getBanMessage() ?? lang('Auth.bannedUser'), + ]]]> + false, + 'reason' => lang('Auth.badAttempt'), + ]]]> + false, + 'reason' => lang('Auth.badAttempt'), + ]]]> + false, + 'reason' => lang('Auth.badAttempt'), + ]]]> + + + + + + + + false, + 'reason' => lang('Auth.errorPasswordEmpty'), + ]]]> + + + + + false, + 'reason' => lang('Auth.errorPasswordLength', [$this->config->minimumPasswordLength]), + 'extraInfo' => lang('Auth.suggestPasswordLength'), + ]]]> + + + + + false, + 'reason' => lang('Auth.errorPasswordCommon'), + 'extraInfo' => lang('Auth.suggestPasswordCommon'), + ]]]> + + + + + false, + 'reason' => lang('Auth.errorPasswordPwned', [$password, $hits, $wording]), + 'extraInfo' => lang('Auth.suggestPasswordPwned', [$password]), + ]]]> + + + + + expires && $identity->expires->isBefore($timeNow)) { + $this->write(sprintf('HMAC Token ID: %s, already expired, skipped.', $identity->id)); + + return; + } + + $identity->expires = $timeNow; + $this->saver->save($identity); + + $this->write(sprintf('HMAC Token ID: %s, set as expired.', $identity->id)); + }]]> + encrypter->isEncrypted($identity->secret2)) { + $this->write(sprintf('id: %s, not encrypted, skipped.', $identity->id)); + + return; + } + + $identity->secret2 = $this->encrypter->decrypt($identity->secret2); + $this->saver->save($identity); + + $this->write(sprintf('id: %s, decrypted.', $identity->id)); + }]]> + encrypter->isEncrypted($identity->secret2)) { + $this->write(sprintf('id: %s, already encrypted, skipped.', $identity->id)); + + return; + } + + try { + $identity->secret2 = $this->encrypter->encrypt($identity->secret2); + $this->saver->save($identity); + + $this->write(sprintf('id: %s, encrypted.', $identity->id)); + } catch (RuntimeException $e) { + $this->error(sprintf('id: %s, %s', $identity->id, $e->getMessage())); + } + }]]> + encrypter->isEncryptedWithCurrentKey($identity->secret2)) { + $this->write(sprintf('id: %s, already encrypted with current key, skipped.', $identity->id)); + + return; + } + + $identity->secret2 = $this->encrypter->decrypt($identity->secret2); + $identity->secret2 = $this->encrypter->encrypt($identity->secret2); + $this->saver->save($identity); + + $this->write(sprintf('id: %s, Re-encrypted.', $identity->id)); + }]]> + + + + + + + + + + + - + + email]]]> + + + + + + + + + + + + + where('success', 1) + ->where('user_id', $user->id) + ->orderBy('id', 'desc') + ->first()]]> + where('success', 1) + ->where('user_id', $user->id) + ->orderBy('id', 'desc') + ->limit(1, 1)->first()]]> + + + + + where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN) + ->where('secret', hash('sha256', $rawToken)) + ->asObject(AccessToken::class) + ->first()]]> + where('type', HmacSha256::ID_TYPE_HMAC_TOKEN) + ->where('secret', $key) + ->asObject(AccessToken::class) + ->first()]]> + where('user_id', $user->id) + ->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN) + ->orderBy($this->primaryKey) + ->asObject(AccessToken::class) + ->findAll()]]> + where('user_id', $user->id) + ->where('type', HmacSha256::ID_TYPE_HMAC_TOKEN) + ->orderBy($this->primaryKey) + ->asObject(AccessToken::class) + ->findAll()]]> + where('type', $type) + ->where('secret', $secret) + ->first()]]> + where('user_id', $user->id) + ->where('type', $type) + ->orderBy($this->primaryKey) + ->first()]]> + where('user_id', $user->id) + ->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN) + ->where('id', $id) + ->asObject(AccessToken::class) + ->first()]]> + where('user_id', $user->id) + ->where('type', AccessTokens::ID_TYPE_ACCESS_TOKEN) + ->where('secret', hash('sha256', $rawToken)) + ->asObject(AccessToken::class) + ->first()]]> + where('user_id', $user->id) + ->where('type', HmacSha256::ID_TYPE_HMAC_TOKEN) + ->where('id', $id) + ->asObject(AccessToken::class) + ->first()]]> + where('user_id', $user->id) + ->where('type', HmacSha256::ID_TYPE_HMAC_TOKEN) + ->where('secret', $key) + ->asObject(AccessToken::class) + ->first()]]> + where('user_id', $user->id) + ->whereIn('type', $types) + ->orderBy($this->primaryKey) + ->findAll()]]> + where('user_id', $user->id)->orderBy($this->primaryKey)->findAll()]]> + whereIn('user_id', $userIds)->orderBy($this->primaryKey)->findAll()]]> + + + ]]> + ]]> + ]]> + ]]> + ]]> + + + + + + + + + + insertID : $result]]> + find($id)]]> + first()]]> + + + + + + + + + $this->config->usernameValidationRules, + 'email' => $this->config->emailValidationRules, + 'password' => $this->getPasswordRules(), + ]]]> + + + + + + + + + + + + + extraInfo()->id]]> + + + + + + + + + extraInfo()->id]]> + + + + + user]]> + + + 1, 'username' => 'John Smith'])]]> + + + extraInfo()->id]]> + + + + + + + + + + + $this->user->email, + 'password' => 'secret', + ]]]> + $this->user->email, + 'password' => 'secret123', + ]]]> + $this->user->email, + 'password' => 'secret123', + ]]]> + $this->user->email, + 'password' => 'secret123', + ]]]> + + + id]]> + id]]> + id]]> + id]]> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 'John Smith'])]]> + + + + + + + + + + + + + + + + + + + + + + + + + + ]]> + + + + + + + + + + + + + + + + + + + + + find($this->user->id)]]> + find($this->user->id)]]> + - - + + + + diff --git a/psalm.xml b/psalm.xml index 61677a083..986c0c662 100644 --- a/psalm.xml +++ b/psalm.xml @@ -1,7 +1,7 @@ diff --git a/src/Authentication/Authenticators/AccessTokens.php b/src/Authentication/Authenticators/AccessTokens.php index a76e4d77e..f754ae661 100644 --- a/src/Authentication/Authenticators/AccessTokens.php +++ b/src/Authentication/Authenticators/AccessTokens.php @@ -74,6 +74,7 @@ public function attempt(array $credentials): Result return $result; } + /** @var User $user */ $user = $result->extraInfo(); $token = $user->getAccessToken($this->getBearerToken()); diff --git a/src/Authentication/Authenticators/HmacSha256.php b/src/Authentication/Authenticators/HmacSha256.php index a8e917f93..a4b740a5d 100644 --- a/src/Authentication/Authenticators/HmacSha256.php +++ b/src/Authentication/Authenticators/HmacSha256.php @@ -75,6 +75,7 @@ public function attempt(array $credentials): Result return $result; } + /** @var User $user */ $user = $result->extraInfo(); $token = $user->getHmacToken($this->getHmacKeyFromToken()); diff --git a/src/Authentication/Authenticators/JWT.php b/src/Authentication/Authenticators/JWT.php index 2a4c2e721..441872c6f 100644 --- a/src/Authentication/Authenticators/JWT.php +++ b/src/Authentication/Authenticators/JWT.php @@ -94,6 +94,7 @@ public function attempt(array $credentials): Result return $result; } + /** @var User $user */ $user = $result->extraInfo(); if ($user->isBanned()) { diff --git a/src/Authentication/Authenticators/Session.php b/src/Authentication/Authenticators/Session.php index c3dd14b60..2b7ca3ba3 100644 --- a/src/Authentication/Authenticators/Session.php +++ b/src/Authentication/Authenticators/Session.php @@ -622,7 +622,7 @@ public function getPendingMessage(): string { $this->checkUserState(); - return $this->getSessionUserKey('auth_action_message') ?? ''; + return (string) ($this->getSessionUserKey('auth_action_message') ?? ''); } /** diff --git a/src/Authorization/PermissionMatcher.php b/src/Authorization/PermissionMatcher.php index 096b85b0a..cc7a10d7d 100644 --- a/src/Authorization/PermissionMatcher.php +++ b/src/Authorization/PermissionMatcher.php @@ -71,8 +71,8 @@ public static function matches(string $permission, array $grants): bool } /** - * @param list $grantSegments - * @param list $permissionSegments + * @param non-empty-list $grantSegments + * @param list $permissionSegments */ private static function matchesWildcardGrant(array $grantSegments, array $permissionSegments, int $permissionSegmentCount): bool { @@ -105,7 +105,7 @@ private static function segmentsMatch(array $grantSegments, array $permissionSeg } /** - * @return list|null + * @return non-empty-list|null */ private static function splitIfValid(string $permission): ?array { diff --git a/src/Entities/AccessToken.php b/src/Entities/AccessToken.php index 3cd48e6ff..f5aabc688 100644 --- a/src/Entities/AccessToken.php +++ b/src/Entities/AccessToken.php @@ -23,7 +23,9 @@ * for authenticating users for an API. * * @property string|Time|null $expires + * @property list $extra * @property string|Time|null $last_used_at + * @property int|string $user_id */ class AccessToken extends Entity { diff --git a/src/Entities/Group.php b/src/Entities/Group.php index bd397ade3..82e26e0ae 100644 --- a/src/Entities/Group.php +++ b/src/Entities/Group.php @@ -19,6 +19,8 @@ /** * Represents a single User Group * and provides utility functions. + * + * @property string $alias */ class Group extends Entity { diff --git a/src/Result.php b/src/Result.php index 5f7a9b939..38f14aff6 100644 --- a/src/Result.php +++ b/src/Result.php @@ -34,7 +34,7 @@ class Result protected $extraInfo; /** - * @param array{success: bool, reason?: string|null, extraInfo?: string|User} $details + * @param array{success: bool, reason?: string|null, extraInfo?: string|User|null} $details */ public function __construct(array $details) { diff --git a/src/Traits/Activatable.php b/src/Traits/Activatable.php index 8721863d0..3171f94aa 100644 --- a/src/Traits/Activatable.php +++ b/src/Traits/Activatable.php @@ -13,6 +13,9 @@ namespace CodeIgniter\Shield\Traits; +/** + * @property bool|int $active + */ trait Activatable { /** diff --git a/src/Traits/Bannable.php b/src/Traits/Bannable.php index 71fe6c6cd..2fab6e445 100644 --- a/src/Traits/Bannable.php +++ b/src/Traits/Bannable.php @@ -13,6 +13,10 @@ namespace CodeIgniter\Shield\Traits; +/** + * @property string|null $status + * @property string|null $status_message + */ trait Bannable { /**