-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
BlockadeMiddleware.php
58 lines (48 loc) · 1.62 KB
/
BlockadeMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace romanzipp\Blockade\Http\Middleware;
use romanzipp\Blockade\Handlers\Contracts\HandlerContract;
use romanzipp\Blockade\Services\Blockade;
class BlockadeMiddleware
{
/**
* @var \romanzipp\Blockade\Handlers\Contracts\HandlerContract
*/
private $handler;
public function __construct(HandlerContract $handler)
{
$this->handler = $handler;
}
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle($request, \Closure $next)
{
if ( ! Blockade::isEnabled()) {
return $next($request);
}
// Check if the current request should be skipped
if ($this->handler->isExcludedForRequest($request)) {
return $next($request);
}
// Check if the current request is already authenticated
if ($this->handler->isAuthenticated($request)) {
return $next($request);
}
// Send a failed response if the request is not attempting authentication
if ( ! $this->handler->requestAttemptsAuthentication($request)) {
return $this->handler->getFailedResponse($request);
}
// Attempt authenticating with the given authentication method
if ( ! $this->handler->attemptAuthentication($request)) {
return $this->handler->getFailedResponse($request, [
'message' => trans('blockade::messages.errors.wrong_password'),
]);
}
return $this->handler->getSuccessResponse($request, $next);
}
}