Skip to content

Commit

Permalink
Merge pull request #60 from aryehraber/response-selector-tag
Browse files Browse the repository at this point in the history
Add `{{ captcha:selector }}` Tag
  • Loading branch information
aryehraber authored Nov 29, 2024
2 parents bc86b27 + 16b5aa2 commit 2614948
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Altcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function getResponseToken()
return request('altcha-payload');
}

public function getResponseSelector()
{
return '#altcha-widget input[name=altcha]';
}

public function getVerificationUrl()
{
return null;
Expand All @@ -37,7 +42,8 @@ public function renderHeadTag()

public function verify()
{
$payload = json_decode(base64_decode($this->getResponseToken()), true);
$responseToken = $this->getResponseToken() ?: request('captcha-response');
$payload = json_decode(base64_decode($responseToken), true);

if ($payload) {
$challenge = $this->createChallenge($payload['salt'], $payload['number']);
Expand Down
4 changes: 3 additions & 1 deletion src/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function __construct()

abstract public function getResponseToken();

abstract public function getResponseSelector();

abstract public function getVerificationUrl();

abstract public function getDefaultDisclaimer();
Expand All @@ -31,7 +33,7 @@ public function verify()
{
$params = [
'secret' => $this->getSecret(),
'response' => $this->getResponseToken(),
'response' => $this->getResponseToken() ?: request('captcha-response'),
'remoteip' => request()->ip(),
];

Expand Down
12 changes: 11 additions & 1 deletion src/CaptchaTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace AryehRaber\Captcha;

use Statamic\Tags\Tags;
use Statamic\Support\Html;
use Statamic\Tags\Tags;

class CaptchaTags extends Tags
{
Expand Down Expand Up @@ -36,6 +36,16 @@ public function head()
return $this->captcha->renderHeadTag();
}

/**
* The {{ captcha:selector }} tag
*
* @return string
*/
public function selector()
{
return $this->captcha->getResponseSelector();
}

/**
* The {{ captcha:disclaimer }} tag
*
Expand Down
5 changes: 5 additions & 0 deletions src/Hcaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function getResponseToken()
return request('h-captcha-response');
}

public function getResponseSelector()
{
return '.h-captcha textarea[name=h-captcha-response]';
}

public function getVerificationUrl()
{
return 'https://hcaptcha.com/siteverify';
Expand Down
5 changes: 5 additions & 0 deletions src/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function getResponseToken()
return request('g-recaptcha-response');
}

public function getResponseSelector()
{
return '.g-recaptcha textarea[name=g-recaptcha-response]';
}

public function getVerificationUrl()
{
return 'https://www.google.com/recaptcha/api/siteverify';
Expand Down
5 changes: 5 additions & 0 deletions src/Turnstile.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function getResponseToken()
return request('cf-turnstile-response');
}

public function getResponseSelector()
{
return '.cf-turnstile input[name=cf-turnstile-response]';
}

public function getVerificationUrl()
{
return 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
Expand Down

0 comments on commit 2614948

Please sign in to comment.