-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FormHandler.php
145 lines (125 loc) · 3.86 KB
/
FormHandler.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace romanzipp\Blockade\Handlers;
use Illuminate\Http\Request;
use romanzipp\Blockade\Concerns\ValidatesPassword;
use romanzipp\Blockade\Handlers\Contracts\HandlerContract;
use Spatie\Url\Url;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class FormHandler extends AbstractHandler implements HandlerContract
{
use ValidatesPassword;
private const INPUT_RETURN_TO = 'return_to';
private const QUERY_ERROR = 'blockade_error';
/**
* Check if the current request is already authenticated.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public function isAuthenticated(Request $request): bool
{
if ( ! $hash = $this->store->getHash($request)) {
return false;
}
return $this->hashMatchesConfigured($hash);
}
/**
* Check if the current request is attempting authentication.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public function requestAttemptsAuthentication(Request $request): bool
{
return $request->filled(config('blockade.handlers.form.input_field'))
&& $request->isMethod('POST');
}
/**
* Attempt the blockade authentication.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
public function attemptAuthentication(Request $request): bool
{
$password = $request->input(
config('blockade.handlers.form.input_field')
);
if (empty($password)) {
return false;
}
return $this->passwordMatchesConfigured($password);
}
/**
* Store the successful authentication state and return a response.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getSuccessResponse(Request $request, \Closure $next): SymfonyResponse
{
$response = redirect(
$request->fullUrl()
);
if ($this->requestWantsToReturn($request)) {
$response = $this->redirectBack($request);
}
return $this->store->storeSuccessState($request, $response);
}
/**
* Get the response for failed or missing authentication.
*
* @param \Illuminate\Http\Request $request
* @param array<string, mixed> $data
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function getFailedResponse(Request $request, array $data = []): SymfonyResponse
{
if ($this->requestWantsToReturn($request)) {
return $this->redirectBack($request, false);
}
if ($request->has(self::QUERY_ERROR)) {
$data['message'] = trans('blockade::messages.errors.wrong_password');
}
return $this->displayView($request, 'blockade::password-form', $data);
}
/**
* Check if the request has a "return to" field.
*
* @param \Illuminate\Http\Request $request
*
* @return bool
*/
protected function requestWantsToReturn(Request $request): bool
{
return $request->has(self::INPUT_RETURN_TO);
}
/**
* Build a redirect response.
*
* @param \Illuminate\Http\Request $request
* @param bool $success
*
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
protected function redirectBack(Request $request, bool $success = true)
{
$url = Url::fromString(
$request->input(self::INPUT_RETURN_TO)
);
if ($success) {
$url = $url->withoutQueryParameter(self::QUERY_ERROR);
} else {
$url = $url->withQueryParameter(self::QUERY_ERROR, '1');
}
return redirect()->to(
(string) $url
);
}
}