Completed
Push — master ( 367e9f...6db52c )
by Sébastien
02:11
created

JwtAuthMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\Wallit\Middleware;
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\Token\Provider\ServerRequestCookieProvider;
11
use Soluble\Wallit\Token\Provider\ServerRequestAuthBearerProvider;
12
use Soluble\Wallit\Token\Provider\ServerRequestLazyChainProvider;
13
use Soluble\Wallit\Service\JwtService;
14
use Zend\Diactoros\Response\JsonResponse;
15
use Zend\Diactoros\Response\RedirectResponse;
16
use Psr\Http\Message\ResponseInterface;
17
18
class JwtAuthMiddleware implements ServerMiddlewareInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $options = [
24
      'secure'  => true,
25
      'relaxed' => [
26
      ]
27
    ];
28
29
    /**
30
     * @var JwtService
31
     */
32
    protected $jwtService;
33
34
    /**
35
     * JwtAuthMiddleware constructor.
36
     *
37
     * @param JwtService $jwtService
38
     */
39
    public function __construct(JwtService $jwtService)
40
    {
41
        $this->jwtService = $jwtService;
42
    }
43
44
    /**
45
     * @param ServerRequestInterface $request
46
     * @param DelegateInterface      $delegate
47
     *
48
     * @return ResponseInterface|RedirectResponse
49
     */
50
    public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
51
    {
52
        $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...
53
54
        // 1. Check for secure scheme
55
56
        // 2. Fetch token from server request
57
58
        $tokenProvider = new ServerRequestLazyChainProvider($request, [
59
            [ServerRequestAuthBearerProvider::class => [
60
                'httpHeader'       => ServerRequestAuthBearerProvider::DEFAULT_OPTIONS['httpHeader'],
61
                'httpHeaderPrefix' => ServerRequestAuthBearerProvider::DEFAULT_OPTIONS['httpHeaderPrefix'],
62
            ]],
63
            [ServerRequestCookieProvider::class => [
64
                'cookieName' => ServerRequestCookieProvider::DEFAULT_OPTIONS['cookieName']
65
            ]]
66
        ]);
67
68
        $plainToken = $tokenProvider->getPlainToken();
69
70
        // 3. Validate the token
71
        if ($plainToken !== null) {
72
            try {
73
                $token = $this->jwtService->parsePlainToken($plainToken);
74
75
                if ($token->verify($this->jwtService->getSigner(), $this->jwtService->getPrivateKey())) {
76
                    if ($token->isExpired()) {
77
                        $message = 'Token has expired';
78
                    } else {
79
                        $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...
80
                        // log Something ?
81
                        $response = $delegate->process($request->withAttribute(self::class, $token));
82
                        // do something with the response (writing cookie, refresh token ?)
83
                        return $response;
84
                    }
85
                } else {
86
                    $message = 'Token is invalid';
87
                }
88
            } catch (\Throwable $e) {
89
                // log something ?
90
                $message = 'Token error';
91
            }
92
        } else {
93
            $message = 'No token provided';
94
        }
95
96
        // @todo: ask the correct way with PSR-15 ?
97
        $error = new JsonResponse([
98
            'message' => 'Unauthorized.',
99
            'reason'  => $message,
100
            'code'    => 401
101
        ], 401, []);
102
103
        return $error;
104
    }
105
}
106