Skip to content

Commit ee4cfff

Browse files
skiechav170
authored andcommitted
phpstan and phpcs fixes
1 parent 97a5d9d commit ee4cfff

33 files changed

+208
-80
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.3"
3636
},
3737
"require-dev": {
38-
"cakephp/cakephp-codesniffer": "~4.4.0",
38+
"cakephp/cakephp-codesniffer": "^4.5",
3939
"league/flysystem-vfs": "^1.0",
40+
"laminas/laminas-diactoros": "^3.0",
4041
"phpunit/phpunit": "^10.0",
42+
"phpstan/phpstan": "^1.8",
43+
"robthree/twofactorauth": "^1.6",
4144
"vlucas/phpdotenv": "^3.3"
4245
},
4346
"autoload": {

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

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

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/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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class ApiRouter extends Router
134134
* The stack of URL filters to apply against routing URLs before passing the
135135
* parameters to the route collection.
136136
*
137-
* @var array<\Closure>
137+
* @var array<callable(): mixed>
138138
*/
139139
protected static array $_urlFilters = [];
140140

src/Service/Action/Auth/JwtSocialLoginAction.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class JwtSocialLoginAction extends Action
2828
* Execute action.
2929
*
3030
* @return mixed
31+
* @throws \Exception
3132
*/
3233
public function execute()
3334
{
@@ -37,6 +38,6 @@ public function execute()
3738
return false;
3839
}
3940

40-
return $this->generateTokenResponse($user);
41+
return $this->generateTokenResponse($user, 'login');
4142
}
4243
}

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,

0 commit comments

Comments
 (0)