ADmad / cakephp-jwt-auth
Showing 1 of 16 files from the diff.

@@ -1,4 +1,6 @@
Loading
1 1
<?php
2 +
declare(strict_types=1);
3 +
2 4
namespace ADmad\JwtAuth\Auth;
3 5
4 6
use Cake\Auth\BaseAuthenticate;
@@ -26,9 +28,8 @@
Loading
26 28
 *  ]);
27 29
 * ```
28 30
 *
29 -
 * @copyright 2015-2018 ADmad
31 +
 * @copyright 2015-Present ADmad
30 32
 * @license MIT
31 -
 *
32 33
 * @see http://jwt.io
33 34
 * @see http://tools.ietf.org/html/draft-ietf-oauth-json-web-token
34 35
 */
@@ -51,7 +52,7 @@
Loading
51 52
    /**
52 53
     * Exception.
53 54
     *
54 -
     * @var \Exception
55 +
     * @var \Throwable|null
55 56
     */
56 57
    protected $_error;
57 58
@@ -87,7 +88,7 @@
Loading
87 88
     *   used on this request.
88 89
     * @param array $config Array of config to use.
89 90
     */
90 -
    public function __construct(ComponentRegistry $registry, $config)
91 +
    public function __construct(ComponentRegistry $registry, array $config)
91 92
    {
92 93
        $defaultConfig = [
93 94
            'cookie' => false,
@@ -100,10 +101,6 @@
Loading
100 101
            'key' => null,
101 102
        ];
102 103
103 -
        if (!class_exists(UnauthorizedException::class)) {
104 -
            $defaultConfig['unauthenticatedException'] = 'Cake\Network\Exception\UnauthorizedException';
105 -
        }
106 -
107 104
        $this->setConfig($defaultConfig);
108 105
109 106
        if (empty($config['allowedAlgs'])) {
@@ -118,8 +115,7 @@
Loading
118 115
     *
119 116
     * @param \Cake\Http\ServerRequest $request The request object.
120 117
     * @param \Cake\Http\Response $response Response object.
121 -
     *
122 -
     * @return bool|array User record array or false on failure.
118 +
     * @return false|array User record array or false on failure.
123 119
     */
124 120
    public function authenticate(ServerRequest $request, Response $response)
125 121
    {
@@ -130,8 +126,7 @@
Loading
130 126
     * Get user record based on info available in JWT.
131 127
     *
132 128
     * @param \Cake\Http\ServerRequest $request Request object.
133 -
     *
134 -
     * @return bool|array User record array or false on failure.
129 +
     * @return false|array User record array or false on failure.
135 130
     */
136 131
    public function getUser(ServerRequest $request)
137 132
    {
@@ -149,7 +144,7 @@
Loading
149 144
            return false;
150 145
        }
151 146
152 -
        $user = $this->_findUser($payload->sub);
147 +
        $user = $this->_findUser((string)$payload->sub);
153 148
        if (!$user) {
154 149
            return false;
155 150
        }
@@ -163,10 +158,9 @@
Loading
163 158
     * Get payload data.
164 159
     *
165 160
     * @param \Cake\Http\ServerRequest|null $request Request instance or null
166 -
     *
167 161
     * @return object|null Payload object on success, null on failurec
168 162
     */
169 -
    public function getPayload($request = null)
163 +
    public function getPayload(?ServerRequest $request = null)
170 164
    {
171 165
        if (!$request) {
172 166
            return $this->_payload;
@@ -186,10 +180,9 @@
Loading
186 180
     * Get token from header or query string.
187 181
     *
188 182
     * @param \Cake\Http\ServerRequest|null $request Request object.
189 -
     *
190 183
     * @return string|null Token string if found else null.
191 184
     */
192 -
    public function getToken($request = null)
185 +
    public function getToken(?ServerRequest $request = null)
193 186
    {
194 187
        $config = $this->_config;
195 188
@@ -205,6 +198,7 @@
Loading
205 198
        if (!empty($this->_config['cookie'])) {
206 199
            $token = $request->getCookie($this->_config['cookie']);
207 200
            if ($token !== null) {
201 +
                /** @psalm-suppress PossiblyInvalidCast */
208 202
                $token = (string)$token;
209 203
            }
210 204
@@ -214,6 +208,7 @@
Loading
214 208
        if (!empty($this->_config['parameter'])) {
215 209
            $token = $request->getQuery($this->_config['parameter']);
216 210
            if ($token !== null) {
211 +
                /** @psalm-suppress PossiblyInvalidCast */
217 212
                $token = (string)$token;
218 213
            }
219 214
@@ -227,10 +222,9 @@
Loading
227 222
     * Decode JWT token.
228 223
     *
229 224
     * @param string $token JWT token to decode.
230 -
     *
231 225
     * @return object|null The JWT's payload as a PHP object, null on failure.
232 226
     */
233 -
    protected function _decode($token)
227 +
    protected function _decode(string $token)
234 228
    {
235 229
        $config = $this->_config;
236 230
        try {
@@ -247,6 +241,8 @@
Loading
247 241
            }
248 242
            $this->_error = $e;
249 243
        }
244 +
245 +
        return null;
250 246
    }
251 247
252 248
    /**
@@ -256,10 +252,8 @@
Loading
256 252
     *
257 253
     * @param \Cake\Http\ServerRequest $request A request object.
258 254
     * @param \Cake\Http\Response $response A response object.
259 -
     *
260 255
     * @throws \Cake\Http\Exception\UnauthorizedException Or any other
261 256
     *   configured exception.
262 -
     *
263 257
     * @return void
264 258
     */
265 259
    public function unauthenticated(ServerRequest $request, Response $response)
@@ -272,6 +266,7 @@
Loading
272 266
            ? $this->_error->getMessage()
273 267
            : $this->_registry->get('Auth')->getConfig('authError');
274 268
269 +
        /** @var \Throwable $exception */
275 270
        $exception = new $this->_config['unauthenticatedException']($message);
276 271
        throw $exception;
277 272
    }
Files Complexity Coverage
src/Auth/JwtAuthenticate.php 28 93.94%
Project Totals (1 files) 28 93.94%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading