2017-03-31 10:19:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2017-11-04 22:16:44 +00:00
|
|
|
use stdClass;
|
2017-10-29 17:37:25 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use Illuminate\Http\Request;
|
2017-04-01 00:48:35 +00:00
|
|
|
use Pterodactyl\Events\Auth\FailedCaptcha;
|
2017-10-29 17:37:25 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2017-03-31 10:19:44 +00:00
|
|
|
|
|
|
|
class VerifyReCaptcha
|
|
|
|
{
|
2017-10-29 17:37:25 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Config\Repository
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VerifyReCaptcha constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config
|
|
|
|
*/
|
|
|
|
public function __construct(Repository $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2017-03-31 10:19:44 +00:00
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2017-08-22 03:10:48 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
2017-10-29 17:37:25 +00:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|mixed
|
2017-03-31 10:19:44 +00:00
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
2017-10-29 17:37:25 +00:00
|
|
|
if (! $this->config->get('recaptcha.enabled')) {
|
2017-04-01 00:48:35 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2017-12-16 19:15:09 +00:00
|
|
|
if ($request->filled('g-recaptcha-response')) {
|
2017-10-29 17:37:25 +00:00
|
|
|
$client = new Client();
|
|
|
|
$res = $client->post($this->config->get('recaptcha.domain'), [
|
2017-03-31 10:19:44 +00:00
|
|
|
'form_params' => [
|
2017-10-29 17:37:25 +00:00
|
|
|
'secret' => $this->config->get('recaptcha.secret_key'),
|
2017-04-01 01:12:49 +00:00
|
|
|
'response' => $request->input('g-recaptcha-response'),
|
2017-03-31 10:19:44 +00:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($res->getStatusCode() === 200) {
|
|
|
|
$result = json_decode($res->getBody());
|
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
if ($result->success && (! $this->config->get('recaptcha.verify_domain') || $this->isResponseVerified($result, $request))) {
|
2017-03-31 10:19:44 +00:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-04-01 00:48:35 +00:00
|
|
|
|
2017-03-31 10:19:44 +00:00
|
|
|
// Emit an event and return to the previous view with an error (only the captcha error will be shown!)
|
2017-10-29 17:37:25 +00:00
|
|
|
event(new FailedCaptcha($request->ip(), (! isset($result) ?: object_get($result, 'hostname'))));
|
|
|
|
|
|
|
|
return redirect()->back()->withErrors(['g-recaptcha-response' => trans('strings.captcha_invalid')])->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if the response from the recaptcha servers was valid.
|
|
|
|
*
|
2017-11-04 22:16:44 +00:00
|
|
|
* @param stdClass $result
|
2017-10-29 17:37:25 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-11-04 22:16:44 +00:00
|
|
|
private function isResponseVerified(stdClass $result, Request $request): bool
|
2017-10-29 17:37:25 +00:00
|
|
|
{
|
|
|
|
if (! $this->config->get('recaptcha.verify_domain')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = parse_url($request->url());
|
2017-04-01 00:48:35 +00:00
|
|
|
|
2017-10-29 17:37:25 +00:00
|
|
|
return $result->hostname === array_get($url, 'host');
|
2017-03-31 10:19:44 +00:00
|
|
|
}
|
|
|
|
}
|