Skip to content

Commit fc7e72b

Browse files
committed
phpstan and phpcs fixes
1 parent 7824da0 commit fc7e72b

37 files changed

+223
-99
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@
3535
"firebase/php-jwt": "^6.2"
3636
},
3737
"require-dev": {
38-
"cakephp/cakephp-codesniffer": "~4.4.0",
38+
"cakephp/cakephp-codesniffer": "^4.5",
3939
"league/flysystem-vfs": "^1.0",
40+
"phpstan/phpstan": "^1.8",
4041
"phpunit/phpunit": "^9.5",
42+
"robthree/twofactorauth": "^1.6",
43+
"web-auth/webauthn-lib": "^3.3.11",
4144
"vlucas/phpdotenv": "^3.3"
4245
},
4346
"autoload": {

phpstan.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 4
6-
autoload_files:
5+
level: 2
6+
bootstrapFiles:
77
- tests/bootstrap.php
88
ignoreErrors:
99

src/Command/ServiceRoutesCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function execute(Arguments $args, ConsoleIo $io): ?int
6868
$service = ServiceRegistry::getServiceLocator()->get($serviceName);
6969
if ($service === null) {
7070
$io->error(__('Service "{0}" not found', $serviceName));
71+
7172
return Command::CODE_ERROR;
7273
}
7374

src/Model/Entity/AuthStore.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* AuthStore Entity
1010
*
1111
* @property string $id
12-
* @property string|null $store
12+
* @property array|null $store
1313
* @property \Cake\I18n\FrozenTime $created
1414
* @property \Cake\I18n\FrozenTime $modified
1515
*/

src/Model/Entity/JwtRefreshToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class JwtRefreshToken extends Entity
3535
* be mass assigned. For security purposes, it is advised to set '*' to false
3636
* (or remove it), and explicitly make individual fields accessible as needed.
3737
*
38-
* @var array
38+
* @var array<string, bool>
3939
*/
4040
protected $_accessible = [
4141
'*' => true,
@@ -45,7 +45,7 @@ class JwtRefreshToken extends Entity
4545
/**
4646
* Fields that are excluded from JSON versions of the entity.
4747
*
48-
* @var array
48+
* @var array<string>
4949
*/
5050
protected $_hidden = [
5151
'token',

src/Model/Table/AuthStoreTable.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
namespace CakeDC\Api\Model\Table;
55

66
use Cake\Database\Schema\TableSchemaInterface;
7-
use Cake\ORM\Query;
8-
use Cake\ORM\RulesChecker;
97
use Cake\ORM\Table;
108
use Cake\Validation\Validator;
119

@@ -25,7 +23,6 @@
2523
* @method \CakeDC\Api\Model\Entity\AuthStore[]|\Cake\Datasource\ResultSetInterface saveManyOrFail(iterable $entities, $options = [])
2624
* @method \CakeDC\Api\Model\Entity\AuthStore[]|\Cake\Datasource\ResultSetInterface|false deleteMany(iterable $entities, $options = [])
2725
* @method \CakeDC\Api\Model\Entity\AuthStore[]|\Cake\Datasource\ResultSetInterface deleteManyOrFail(iterable $entities, $options = [])
28-
*
2926
* @mixin \Cake\ORM\Behavior\TimestampBehavior
3027
*/
3128
class AuthStoreTable extends Table
@@ -62,8 +59,6 @@ public function validationDefault(Validator $validator): Validator
6259
return $validator;
6360
}
6461

65-
66-
6762
/**
6863
* Initialize schema
6964
*

src/Rbac/Rules/TwoFactorPassedScope.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
namespace CakeDC\Api\Rbac\Rules;
1414

1515
use Authentication\Authenticator\JwtAuthenticator;
16-
use CakeDC\Auth\Rbac\Rules\AbstractRule;
17-
use Cake\Utility\Hash;
1816
use Cake\Routing\Router;
19-
use OutOfBoundsException;
17+
use CakeDC\Auth\Rbac\Rules\AbstractRule;
2018
use Psr\Http\Message\ServerRequestInterface;
2119

2220
/**
2321
* Owner rule class, used to match ownership permissions
2422
*/
2523
class TwoFactorPassedScope extends AbstractRule
2624
{
27-
2825
protected $_defaultConfig = [
2926
];
3027

src/Rbac/Rules/TwoFactorScope.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@
1313
namespace CakeDC\Api\Rbac\Rules;
1414

1515
use Authentication\Authenticator\JwtAuthenticator;
16-
use CakeDC\Auth\Rbac\Rules\AbstractRule;
17-
use Cake\Utility\Hash;
1816
use Cake\Routing\Router;
19-
use OutOfBoundsException;
17+
use CakeDC\Auth\Rbac\Rules\AbstractRule;
2018
use Psr\Http\Message\ServerRequestInterface;
2119

2220
/**
2321
* Owner rule class, used to match ownership permissions
2422
*/
2523
class TwoFactorScope extends AbstractRule
2624
{
27-
2825
protected $_defaultConfig = [
2926
];
3027

src/Routing/ApiRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ class ApiRouter extends Router
9595
/**
9696
* A hash of request context data.
9797
*
98-
* @var array
98+
* @var array<string, mixed>
9999
*/
100100
protected static $_requestContext = [];
101101

102102
/**
103103
* Named expressions
104104
*
105-
* @var array
105+
* @var array<string, string>
106106
*/
107107
protected static $_namedExpressions = [
108108
'Action' => Router::ACTION,
@@ -132,14 +132,14 @@ class ApiRouter extends Router
132132
* The stack of URL filters to apply against routing URLs before passing the
133133
* parameters to the route collection.
134134
*
135-
* @var array
135+
* @var array<callable(): mixed>
136136
*/
137137
protected static $_urlFilters = [];
138138

139139
/**
140140
* Default extensions defined with Router::extensions()
141141
*
142-
* @var array
142+
* @var array<string>
143143
*/
144144
protected static $_defaultExtensions = [];
145145
}

src/Service/Action/Auth/JwtSocialLoginAction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class JwtSocialLoginAction extends Action
2828
* Execute action.
2929
*
3030
* @return mixed
31-
* @throws \CakeDC\Api\Service\Action\Exception
31+
* @throws \Exception
3232
*/
3333
public function execute()
3434
{
@@ -38,6 +38,6 @@ public function execute()
3838
return false;
3939
}
4040

41-
return $this->generateTokenResponse($user);
41+
return $this->generateTokenResponse($user, 'login');
4242
}
4343
}

src/Service/Action/Auth/JwtTokenTrait.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@
1818
use Cake\Routing\Router;
1919
use Cake\Utility\Hash;
2020
use CakeDC\Api\Service\Auth\TwoFactorAuthentication\OneTimePasswordAuthenticationCheckerFactory;
21-
use CakeDC\Api\Service\Auth\TwoFactorAuthentication\Webauthn2fAuthenticationCheckerFactory;
21+
use CakeDC\Api\Service\Auth\TwoFactorAuthentication\Webauthn2FAuthenticationCheckerFactory;
2222
use DateInterval;
2323
use DateTimeImmutable;
2424
use Lcobucci\JWT\Configuration;
2525
use Lcobucci\JWT\Signer\Hmac\Sha512;
2626
use Lcobucci\JWT\Signer\Key\InMemory;
2727

28+
/**
29+
* JwtTokenTrait
30+
*/
2831
trait JwtTokenTrait
2932
{
30-
3133
/**
3234
* Generates token response.
3335
*
3436
* @param \Cake\Datasource\EntityInterface|array $user User info.
37+
* @param string|null $type The type of token being generated.
3538
* @return array
3639
*/
3740
public function generateTokenResponse($user, $type)
@@ -49,6 +52,13 @@ public function generateTokenResponse($user, $type)
4952
]);
5053
}
5154

55+
/**
56+
* Generates refresh token response.
57+
*
58+
* @param \Cake\Datasource\EntityInterface|array $user User info.
59+
* @param array $payload Additional payload data.
60+
* @return array
61+
*/
5262
public function generateRefreshTokenResponse($user, $payload)
5363
{
5464
$timestamp = new DateTimeImmutable();
@@ -65,6 +75,8 @@ public function generateRefreshTokenResponse($user, $payload)
6575
*
6676
* @param \Cake\Datasource\EntityInterface|array $user User info.
6777
* @param \DateTimeImmutable $timestamp Timestamp.
78+
* @param string|null $type The type of token being generated.
79+
* @param array|null $payload Additional payload data.
6880
* @return bool|string
6981
*/
7082
public function generateAccessToken($user, $timestamp, $type, $payload = null)
@@ -92,6 +104,14 @@ public function generateAccessToken($user, $timestamp, $type, $payload = null)
92104
return $token->toString();
93105
}
94106

107+
/**
108+
* Get the audience for the token.
109+
*
110+
* @param \Cake\Datasource\EntityInterface|array $user User info.
111+
* @param string|null $type The type of token being generated.
112+
* @param array|null $payload Additional payload data.
113+
* @return string
114+
*/
95115
public function getAudience($user, $type, $payload)
96116
{
97117
if ($type === null && is_array($payload) && isset($payload['aud'])) {
@@ -106,11 +126,23 @@ public function getAudience($user, $type, $payload)
106126
return $audience;
107127
}
108128

129+
/**
130+
* Check if 2FA is enabled for the user.
131+
*
132+
* @param \Cake\Datasource\EntityInterface|array $user User info.
133+
* @return bool
134+
*/
109135
protected function is2FAEnabled($user)
110136
{
111137
return $this->isEnabledWebauthn2faAuthentication($user) || $this->isEnabledOneTimePasswordAuthentication($user);
112138
}
113139

140+
/**
141+
* Check if Webauthn 2FA authentication is enabled for the user.
142+
*
143+
* @param \Cake\Datasource\EntityInterface|array $user User info.
144+
* @return bool
145+
*/
114146
public function isEnabledWebauthn2faAuthentication($user)
115147
{
116148
$enabledTwoFactorVerify = Configure::read('Api.2fa.enabled');
@@ -122,6 +154,12 @@ public function isEnabledWebauthn2faAuthentication($user)
122154
return false;
123155
}
124156

157+
/**
158+
* Check if One-Time Password authentication is enabled for the user.
159+
*
160+
* @param \Cake\Datasource\EntityInterface|array $user User info.
161+
* @return bool
162+
*/
125163
public function isEnabledOneTimePasswordAuthentication($user)
126164
{
127165
$enabledTwoFactorVerify = Configure::read('Api.2fa.enabled');
@@ -133,6 +171,11 @@ public function isEnabledOneTimePasswordAuthentication($user)
133171
return false;
134172
}
135173

174+
/**
175+
* Get the One-Time Password Authentication Checker.
176+
*
177+
* @return \CakeDC\Auth\Authentication\OneTimePasswordAuthenticationCheckerInterface
178+
*/
136179
protected function getOneTimePasswordAuthenticationChecker()
137180
{
138181
return (new OneTimePasswordAuthenticationCheckerFactory())->build();
@@ -153,6 +196,8 @@ protected function getWebauthn2fAuthenticationChecker()
153196
*
154197
* @param \Cake\Datasource\EntityInterface|array $user User info.
155198
* @param \DateTimeImmutable $timestamp Timestamp.
199+
* @param string|null $type The type of token being generated.
200+
* @param array|null $payload Additional payload data.
156201
* @return bool|string
157202
*/
158203
public function generateRefreshToken($user, $timestamp, $type, $payload = null)
@@ -184,6 +229,7 @@ public function generateRefreshToken($user, $timestamp, $type, $payload = null)
184229
$model = $UsersTable->getAlias();
185230

186231
$table = TableRegistry::getTableLocator()->get('CakeDC/Api.JwtRefreshTokens');
232+
/** @var \CakeDC\Api\Model\Entity\JwtRefreshToken $entity */
187233
$entity = $table->find()->where([
188234
'model' => $model,
189235
'foreign_key' => $subject,

src/Service/Action/Auth/OtpVerifyAction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
namespace CakeDC\Api\Service\Action\Auth;
1515

16+
use Cake\Core\Configure;
1617
use CakeDC\Api\Service\Action\Action;
1718
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
18-
use Cake\Core\Configure;
1919
use RobThree\Auth\TwoFactorAuth;
2020

2121
/**
@@ -32,6 +32,12 @@ abstract class OtpVerifyAction extends Action
3232
*/
3333
public $tfa;
3434

35+
/**
36+
* initialize
37+
*
38+
* @param array $config Configuration.
39+
* @return void
40+
*/
3541
public function initialize(array $config): void
3642
{
3743
$this->tfa = new TwoFactorAuth(
@@ -78,5 +84,4 @@ public function getQRCodeImageAsDataUri($issuer, $secret)
7884
{
7985
return $this->tfa->getQRCodeImageAsDataUri($issuer, $secret);
8086
}
81-
8287
}

src/Service/Action/Auth/OtpVerifyCheckAction.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CakeDC\Api\Service\Action\Auth;
1515

16-
use CakeDC\Api\Service\Action\Action;
1716
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
1817

1918
/**
@@ -57,5 +56,4 @@ public function execute()
5756

5857
return $this->generateTokenResponse($user->toArray(), '2fa');
5958
}
60-
6159
}

src/Service/Action/Auth/OtpVerifyGetAction.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CakeDC\Api\Service\Action\Auth;
1515

16-
use CakeDC\Api\Service\Action\Action;
1716
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
1817

1918
/**
@@ -54,6 +53,12 @@ public function execute()
5453
return $result;
5554
}
5655

56+
/**
57+
* onVerifyGetSecret
58+
*
59+
* @param array $user User.
60+
* @return string
61+
*/
5762
protected function onVerifyGetSecret($user)
5863
{
5964
if (isset($user['secret']) && $user['secret']) {

src/Service/Action/Auth/ResetPasswordRequestAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function execute()
8383
'ensureActive' => Configure::read('Users.Registration.ensureActive'),
8484
];
8585
if (!empty($baseUrl)) {
86-
$options['linkGenerator'] = function($token) use ($baseUrl) {
86+
$options['linkGenerator'] = function ($token) use ($baseUrl) {
8787
return $baseUrl . '?token=' . $token;
8888
};
8989
}

src/Service/Action/Auth/TwoFactorAuthAction.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
namespace CakeDC\Api\Service\Action\Auth;
1515

1616
use CakeDC\Api\Service\Action\Action;
17-
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
18-
use Cake\Core\Configure;
1917
use CakeDC\Api\Webauthn\RegisterAdapter;
18+
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
2019

2120
/**
2221
* Class LoginAction
@@ -49,6 +48,5 @@ public function execute()
4948
'enabledWebauthn' => $this->isEnabledWebauthn2faAuthentication((array)$user),
5049
'enabledOtp' => $this->isEnabledOneTimePasswordAuthentication((array)$user),
5150
];
52-
}
53-
51+
}
5452
}

0 commit comments

Comments
 (0)