@@ -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 }
0 commit comments