-
-
Save afiqiqmal/94f12aab4fff4fe1bf9a80cb51e96b5a to your computer and use it in GitHub Desktop.
ReCaptchaEnterpriseRule
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Rules; | |
| use Illuminate\Contracts\Validation\Rule; | |
| class ReCaptchaEnterpriseRule implements Rule | |
| { | |
| /** | |
| * Determine if the validation rule passes. | |
| * | |
| * @param string $attribute | |
| * @param mixed $value | |
| * @return bool | |
| */ | |
| public function passes($attribute, $value) | |
| { | |
| $response = \Http::asJson()->post($this->recaptchaEnterpriseVerificationUrl(), [ | |
| 'event' => [ | |
| 'token' => $value, | |
| 'site_key' => $this->getSiteKey(), | |
| ] | |
| ]); | |
| return $response->json('tokenProperties.valid'); | |
| } | |
| /** | |
| * Get the validation error message. | |
| * | |
| * @return string | |
| */ | |
| public function message() | |
| { | |
| return 'Unable to validate recaptcha token'; | |
| } | |
| private function recaptchaEnterpriseVerificationUrl(): string | |
| { | |
| return "https://recaptchaenterprise.googleapis.com/v1/projects/{$this->getProjectId()}/assessments?key={$this->getApiKey()}"; | |
| } | |
| private function getProjectId(): ?string | |
| { | |
| return config('services.recaptcha_ent.project_id'); | |
| } | |
| private function getApiKey(): ?string | |
| { | |
| return config('services.recaptcha_ent.api_key'); | |
| } | |
| public function getSiteKey(): ?string | |
| { | |
| return config('services.recaptcha_ent.site_key'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment