|
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 |
|
|
|
|
|
|
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(), '_'); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
if (! $request->user($guard) || ! $request->session()) { |
|
|
|
|
|
|
48
|
|
|
return $next($request); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if ($this->auth->viaRemember()) { |
|
|
|
|
|
|
52
|
|
|
$passwordHash = explode('|', $request->cookies->get($this->auth->getRecallerName()))[2]; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
if ($passwordHash !== $request->user($guard)->getAuthPassword()) { |
|
|
|
|
|
|
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()) { |
|
|
|
|
|
|
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)) { |
|
|
|
|
|
|
85
|
|
|
return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$request->session()->put([ |
|
|
|
|
|
|
89
|
|
|
$passwordHashKey => $request->user($guard)->getAuthPassword(), |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
101
|
|
|
*/ |
|
102
|
|
|
protected function logout($request) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->auth->logout(); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
$request->session()->flush(); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
throw new AuthenticationException(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Adding a
@returnannotation 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.