Skip to content

Commit ff57e2f

Browse files
committed
Cleanup recaptcha middleware
1 parent 451dd7e commit ff57e2f

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

app/Http/Middleware/VerifyReCaptcha.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,46 @@ class VerifyReCaptcha
1212
*
1313
* @param \Illuminate\Http\Request $request
1414
* @param \Closure $next
15-
* @return mixed
15+
* @return \Illuminate\Http\RediectResponse
1616
*/
1717
public function handle($request, Closure $next)
1818
{
1919
if (! config('recaptcha.enabled')) {
2020
return $next($request);
2121
}
2222

23-
$response_domain = null;
24-
2523
if ($request->has('g-recaptcha-response')) {
26-
$response = $request->get('g-recaptcha-response');
27-
2824
$client = new \GuzzleHttp\Client();
29-
$res = $client->post('https://www.google.com/recaptcha/api/siteverify', [
25+
$res = $client->post(config('recaptcha.domain'), [
3026
'form_params' => [
3127
'secret' => config('recaptcha.secret_key'),
32-
'response' => $response,
28+
'response' => $request->input('g-recaptcha-response'),
3329
],
3430
]);
3531

3632
if ($res->getStatusCode() === 200) {
3733
$result = json_decode($res->getBody());
3834

39-
$response_domain = $result->hostname;
35+
$verified = function ($result, $request) {
36+
if (! config('recaptcha.verify_domain')) {
37+
return false;
38+
}
4039

41-
// Compare the domain received by google with the app url
42-
$domain_verified = false;
43-
if (config('recaptcha.verify_domain')) {
44-
$matches;
45-
preg_match('/^(?:https?:\/\/)?((?:www\.)?[^:\/\n]+)/', config('app.url'), $matches);
46-
$domain = $matches[1];
47-
$domain_verified = $response_domain === $domain;
48-
}
40+
$url = parse_url($request->url());
41+
42+
if (array_key_exists('host', $url)) {
43+
return $result->hostname === $url['host'];
44+
}
45+
};
4946

50-
if ($result->success && (! config('recaptcha.verify_domain') || $domain_verified)) {
47+
if ($result->success && (! config('recaptcha.verify_domain') || $verified($result, $request))) {
5148
return $next($request);
5249
}
5350
}
5451
}
5552

5653
// Emit an event and return to the previous view with an error (only the captcha error will be shown!)
57-
event(new FailedCaptcha($request->ip(), $response_domain));
54+
event(new FailedCaptcha($request->ip(), (! isset($result->hostname) ?: $result->hostname)));
5855

5956
return back()->withErrors(['g-recaptcha-response' => trans('strings.captcha_invalid')])->withInput();
6057
}

config/recaptcha.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
*/
88
'enabled' => env('RECAPTCHA_ENABLED', true),
99

10+
/*
11+
* API endpoint for recaptcha checks. You should not edit this.
12+
*/
13+
'domain' => 'https://www.google.com/recaptcha/api/siteverify',
14+
1015
/*
1116
* Use a custom secret key, we use our public one by default
1217
*/

0 commit comments

Comments
 (0)