AuthenticateSession::logout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Http\Middleware;
6
7
use Closure;
8
use Illuminate\Auth\AuthenticationException;
9
use Illuminate\Contracts\Auth\Factory as AuthFactory;
10
11
class AuthenticateSession
12
{
13
    /**
14
     * The authentication factory implementation.
15
     *
16
     * @var \Illuminate\Contracts\Auth\Factory
17
     */
18
    protected $auth;
19
20
    /**
21
     * Create a new middleware instance.
22
     *
23
     * @param \Illuminate\Contracts\Auth\Factory $auth
24
     *
25
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
26
     */
27
    public function __construct(AuthFactory $auth)
28
    {
29
        $this->auth = $auth;
30
    }
31
32
    /**
33
     * Handle an incoming request.
34
     *
35
     * @param \Illuminate\Http\Request $request
36
     * @param \Closure                 $next
37
     *
38
     * @throws \Illuminate\Auth\AuthenticationException
39
     *
40
     * @return mixed
41
     */
42
    public function handle($request, Closure $next)
43
    {
44
        $guard = $request->route('guard');
45
        $passwordHashKey = 'hash_'.$guard.mb_strrchr($this->auth->getName(), '_');
0 ignored issues
show
Bug introduced by
The method getName() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
47
        if (! $request->user($guard) || ! $request->session()) {
0 ignored issues
show
Bug introduced by
It seems like $guard defined by $request->route('guard') on line 44 can also be of type object; however, Illuminate\Http\Request::user() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
            return $next($request);
49
        }
50
51
        if ($this->auth->viaRemember()) {
0 ignored issues
show
Bug introduced by
The method viaRemember() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            $passwordHash = explode('|', $request->cookies->get($this->auth->getRecallerName()))[2];
0 ignored issues
show
Bug introduced by
The method getRecallerName() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
            if ($passwordHash !== $request->user($guard)->getAuthPassword()) {
0 ignored issues
show
Bug introduced by
It seems like $guard defined by $request->route('guard') on line 44 can also be of type object; however, Illuminate\Http\Request::user() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55
                $this->logout($request);
56
            }
57
        }
58
59
        if (! $request->session()->has($passwordHashKey)) {
60
            $this->storePasswordHashInSession($request, $passwordHashKey);
61
        }
62
63
        if ($request->session()->get($passwordHashKey) !== $request->user($guard)->getAuthPassword()) {
0 ignored issues
show
Bug introduced by
It seems like $guard defined by $request->route('guard') on line 44 can also be of type object; however, Illuminate\Http\Request::user() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
            $this->logout($request);
65
        }
66
67
        return tap($next($request), function () use ($request, $passwordHashKey) {
68
            $this->storePasswordHashInSession($request, $passwordHashKey);
69
        });
70
    }
71
72
    /**
73
     * Store the user's current password hash in the session.
74
     *
75
     * @param \Illuminate\Http\Request $request
76
     * @param string                   $passwordHashKey
77
     *
78
     * @return void
79
     */
80
    protected function storePasswordHashInSession($request, $passwordHashKey)
81
    {
82
        $guard = $request->route('guard');
83
84
        if (! $request->user($guard)) {
0 ignored issues
show
Bug introduced by
It seems like $guard defined by $request->route('guard') on line 82 can also be of type object; however, Illuminate\Http\Request::user() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
85
            return;
86
        }
87
88
        $request->session()->put([
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
            $passwordHashKey => $request->user($guard)->getAuthPassword(),
0 ignored issues
show
Bug introduced by
It seems like $guard defined by $request->route('guard') on line 82 can also be of type object; however, Illuminate\Http\Request::user() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
90
        ]);
91
    }
92
93
    /**
94
     * Log the user out of the application.
95
     *
96
     * @param \Illuminate\Http\Request $request
97
     *
98
     * @throws \Illuminate\Auth\AuthenticationException
99
     *
100
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
101
     */
102
    protected function logout($request)
103
    {
104
        $this->auth->logout();
0 ignored issues
show
Bug introduced by
The method logout() does not seem to exist on object<Illuminate\Contracts\Auth\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
106
        $request->session()->flush();
0 ignored issues
show
Bug introduced by
The method flush() does not seem to exist on object<Symfony\Component...ssion\SessionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
108
        throw new AuthenticationException();
109
    }
110
}
111