Completed
Push — master ( c749b5...4fecd8 )
by Sébastien
03:16
created

JwtAuthMiddleware::getTokenString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit;
6
7
use Interop\Http\ServerMiddleware\DelegateInterface;
8
use Interop\Http\ServerMiddleware\MiddlewareInterface as ServerMiddlewareInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Soluble\Wallit\Jwt\Provider\RequestCookieProvider;
11
use Soluble\Wallit\Jwt\Provider\RequestAuthBearerProvider;
12
use Soluble\Wallit\Service\JwtService;
13
use Zend\Diactoros\Response\JsonResponse;
14
use Zend\Diactoros\Response\RedirectResponse;
15
use Psr\Http\Message\ResponseInterface;
16
17
class JwtAuthMiddleware implements ServerMiddlewareInterface
18
{
19
    /**
20
     * @var array
21
     */
22
    protected $options = [
23
      'secure' => true,
24
      'relaxed' => []
25
    ];
26
27
    /**
28
     * @var JwtService
29
     */
30
    protected $jwtService;
31
32
    /**
33
     * JwtAuthMiddleware constructor.
34
     *
35
     * @param JwtService $jwtService
36
     */
37 5
    public function __construct(JwtService $jwtService)
38
    {
39 5
        $this->jwtService = $jwtService;
40 5
    }
41
42
    /**
43
     * @param ServerRequestInterface $request
44
     * @param DelegateInterface      $delegate
45
     *
46
     * @return ResponseInterface|RedirectResponse
47
     */
48 5
    public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
49
    {
50 5
        $authenticated = false;
0 ignored issues
show
Unused Code introduced by
$authenticated is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52 5
        $tokenString = $this->getTokenString($request);
53
54 5
        if ($tokenString !== null) {
55
            try {
56 5
                $token = $this->jwtService->parseTokenString($tokenString);
57
58 4
                if ($token->verify($this->jwtService->getSigner(), $this->jwtService->getPrivateKey())) {
59 3
                    if ($token->isExpired()) {
60 1
                        $message = 'Token has expired';
61
                    } else {
62 2
                        $authenticated = true;
0 ignored issues
show
Unused Code introduced by
$authenticated is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
                        // log Something ?
64 2
                        $response = $delegate->process($request->withAttribute(self::class, $token));
65
                        // do something with the response (writing cookie, refresh token ?)
66 2
                        return $response;
67
                    }
68
                } else {
69 1
                    $message = 'Token is invalid';
70
                }
71 1
            } catch (\Throwable $e) {
72
                // log something ?
73 1
                $message = 'Token error';
74
            }
75
        } else {
76
            $message = 'No token provided';
77
        }
78
79
        // @todo: ask the correct way with PSR-15 ?
80 3
        $error = new JsonResponse([
81 3
            'message' => 'Unauthorized.',
82 3
            'reason' => $message,
83 3
            'code' => 401
84 3
        ], 401, []);
85
86 3
        return $error;
87
    }
88
89
    /**
90
     * Return token string.
91
     *
92
     * Will be read from HTTP "Authentication: Bearer" header
93
     * then from cookie
94
     *
95
     * @param ServerRequestInterface $request
96
     *
97
     * @return null|string
98
     */
99 5
    protected function getTokenString(ServerRequestInterface $request): ?string
100
    {
101 5
        if (null === ($tokenString = (new RequestAuthBearerProvider($request))->getTokenString())) {
102 3
            $tokenString = (new RequestCookieProvider($request, 'jwtcookie'))->getTokenString();
103
        }
104
105 5
        return $tokenString;
106
    }
107
}
108