You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							89 lines
						
					
					
						
							3.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							89 lines
						
					
					
						
							3.2 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Wrappers;
							 | 
						|
								
							 | 
						|
								require_once __DIR__ . "/../../vendor/autoload.php";
							 | 
						|
								
							 | 
						|
								// Include Google Cloud dependencies using Composer
							 | 
						|
								use App\Configuration;
							 | 
						|
								use App\Enumerators\SessionElement;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\Assessment;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\Event;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\Key;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\TokenProperties\InvalidReason;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings;
							 | 
						|
								use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType;
							 | 
						|
								use Grpc\Gcp\Config;
							 | 
						|
								
							 | 
						|
								class CAPTCHAWrapper
							 | 
						|
								{
							 | 
						|
								    public static function CreateAssessment(
							 | 
						|
								        string $token,
							 | 
						|
								        string $action
							 | 
						|
								    ): ?float
							 | 
						|
								    {
							 | 
						|
								
							 | 
						|
								        $credFilePath = __DIR__ . "/../../Configuration/GCCServiceAccountCreds.json";
							 | 
						|
								        putenv("GOOGLE_APPLICATION_CREDENTIALS=$credFilePath");
							 | 
						|
								
							 | 
						|
								        // Create the reCAPTCHA client.
							 | 
						|
								        // TODO: Cache the client generation code (recommended) or call client.close() before exiting the method.
							 | 
						|
								        $client = new RecaptchaEnterpriseServiceClient();
							 | 
						|
								
							 | 
						|
								        $project = Configuration::GetConfig("reCAPTCHA", "ProjectID");
							 | 
						|
								
							 | 
						|
								        $projectName = $client->projectName($project);
							 | 
						|
								
							 | 
						|
								        // Set the properties of the event to be tracked.
							 | 
						|
								        $event = (new Event())
							 | 
						|
								            ->setSiteKey(Configuration::GetConfig("reCAPTCHA", "KeyID"))
							 | 
						|
								            ->setToken($token);
							 | 
						|
								
							 | 
						|
								        // Build the assessment request.
							 | 
						|
								        $assessment = (new Assessment())->setEvent($event);
							 | 
						|
								
							 | 
						|
								        $response = $client->createAssessment(
							 | 
						|
								            $projectName,
							 | 
						|
								            $assessment
							 | 
						|
								        );
							 | 
						|
								
							 | 
						|
								        // Check if the token is valid.
							 | 
						|
								        if ($response->getTokenProperties()->getValid() == false) {
							 | 
						|
								            // printf('The CreateAssessment() call failed because the token was invalid for the following reason: ');
							 | 
						|
								            // printf(InvalidReason::name($response->getTokenProperties()->getInvalidReason()));
							 | 
						|
								            return floatval(-1);
							 | 
						|
								        }
							 | 
						|
								
							 | 
						|
								        // Check if the expected action was executed.
							 | 
						|
								        if ($response->getTokenProperties()->getAction() == $action) {
							 | 
						|
								            // Get the risk score and the reason(s).
							 | 
						|
								            // For more information on interpreting the assessment, see:
							 | 
						|
								            // https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
							 | 
						|
								            // printf('The score for the protection action is:');
							 | 
						|
								            return $response->getRiskAnalysis()->getScore();
							 | 
						|
								        } else {
							 | 
						|
								            // printf('The action attribute in your reCAPTCHA tag does not match the action you are expecting to score');
							 | 
						|
								        }
							 | 
						|
								        return null;
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    public static function HandleCaptchaResponse(string $captchaResponse): void
							 | 
						|
								    {
							 | 
						|
								        $assessmentResponse = CAPTCHAWrapper::CreateAssessment(
							 | 
						|
								            token: $captchaResponse,
							 | 
						|
								            action: 'submit'
							 | 
						|
								        );
							 | 
						|
								
							 | 
						|
								        if ($assessmentResponse == null)
							 | 
						|
								        {
							 | 
						|
								            die("captcha assessment is null");
							 | 
						|
								        }
							 | 
						|
								        if ($assessmentResponse <= floatval(Configuration::GetConfig("reCAPTCHA", "AcceptableLowerBoundsForLogin")))
							 | 
						|
								        {
							 | 
						|
								            die("go away robot!");
							 | 
						|
								        }
							 | 
						|
								
							 | 
						|
								        SessionWrapper::Set(SessionElement::LAST_ASSESSMENT_RESULT, $assessmentResponse);
							 | 
						|
								    }
							 | 
						|
								}
							 | 
						|
								
							 |