Passed
Pull Request — master (#39)
by Cesar
10:31 queued 04:08
created

MagiclinkMiddleware::badResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace MagicLink\Middlewares;
4
5
use Closure;
6
use Illuminate\Contracts\Encryption\DecryptException;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Hash;
9
use MagicLink\MagicLink;
10
use MagicLink\Responses\Response;
11
12
class MagiclinkMiddleware
13
{
14
    public function handle(Request $request, Closure $next)
15
    {
16
        $token = $request->route('token');
17
18
        $magicLink = MagicLink::getValidMagicLinkByToken($token);
0 ignored issues
show
Bug introduced by
It seems like $token can also be of type Illuminate\Routing\Route and object; however, parameter $token of MagicLink\MagicLink::getValidMagicLinkByToken() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        $magicLink = MagicLink::getValidMagicLinkByToken(/** @scrutinizer ignore-type */ $token);
Loading history...
19
20
        if (! $magicLink) {
21
            return $this->badResponse();
22
        }
23
24
        if ($magicLink->protectedWithAcessCode()) {
25
            if ($magicLink->checkAccessCode($request->get('access-code'))) {
26
                // access code is valid
27
                return redirect($request->url())->withCookie(
28
                    cookie(
29
                        'magic-link-access-code',
30
                        encrypt($request->get('access-code')),
31
                        0,
32
                        '/'
33
                    )
34
                );
35
            }
36
37
            try {
38
                $accessCode = decrypt($request->cookie('magic-link-access-code'));
0 ignored issues
show
Bug introduced by
It seems like $request->cookie('magic-link-access-code') can also be of type array; however, parameter $value of decrypt() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
                $accessCode = decrypt(/** @scrutinizer ignore-type */ $request->cookie('magic-link-access-code'));
Loading history...
39
40
                // Validate access_code
41
                if ($magicLink->checkAccessCode($accessCode)) {
42
                    $magicLink->visited();
43
44
                    return $next($request);
45
                }
46
            } catch (DecryptException $e) {
47
                // empty value in cookie
48
            }
49
50
            return response(view('magiclink::ask-for-access-code-form'), 403);
51
        }
52
53
        $magicLink->visited();
54
55
        return $next($request);
56
    }
57
58
    // private function isAccessCodeValid(string $token, ?string $accessCode): bool
59
    // {
60
    //     if ($accessCode === null) {
61
    //         return false;
62
    //     }
63
64
    //     $magicLink = MagicLink::getValidMagicLinkByToken($token);
65
66
    //     return Hash::check($accessCode, $magicLink->access_code);
67
    // }
68
69
    protected function badResponse()
70
    {
71
        $responseClass = config('magiclink.invalid_response.class', Response::class);
72
73
        $response = new $responseClass;
74
75
        return $response(config('magiclink.invalid_response.options', []));
76
    }
77
}
78