Skip to content

Commit fabe1cf

Browse files
committed
UPDATED
1 parent f4960a6 commit fabe1cf

File tree

4 files changed

+185
-6
lines changed

4 files changed

+185
-6
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This package requires the following dependencies:
2828
Via Composer
2929

3030
``` sh
31-
$ composer require ryandeng/googlerecaptcha
31+
$ composer require ryandeng/googlerecaptcha "^0.1.2"
3232
```
3333

3434
If your Laravel framework version <= 5.4, please register the service provider in your config file: /config/app.php, otherwise please go to step 3.
@@ -52,7 +52,7 @@ If you want to change the default template, please check Advanced Usage.
5252

5353
Please register all details on host_name, site_key, secret_key and site_verify_url.
5454

55-
Specify your Score threshold in score_setting, e.g.
55+
Specify your Score threshold and action in 'setting', e.g.
5656

5757
[
5858
'action' => 'contact_us', // Google reCAPTCHA required paramater
@@ -67,6 +67,29 @@ Remember to turn on the service by enable is_service_enabled to be true.
6767

6868
For more details please check comments in config file.
6969

70+
{!! \RyanDeng\GoogleReCaptcha\Facades\GoogleReCaptchaV3::render($action1,$action2) !!}
71+
72+
<form method="POST" action="/verify1">
73+
@csrf
74+
<input type="hidden" id="your_id_1" name="g-recaptcha-response">
75+
<input type="submit" class="g-recaptcha" value="submit">
76+
</form>
77+
78+
<form method="POST" action="/verify2">
79+
@csrf
80+
<input type="hidden" id="your_id_2" name="g-recaptcha-response">
81+
<input type="submit" class="g-recaptcha" value="submit">
82+
</form>
83+
84+
- You can pass multiple $action in render(...)
85+
- Please specify your id for the input below:
86+
87+
``` html
88+
<input type="hidden" id="your_id" name="g-recaptcha-response">
89+
```
90+
Note: all values should be registered in googlerecaptchav3 config file in 'setting' section
91+
92+
7093
#### Validation Class
7194

7295
You can use provided Validation object to verify your reCAPTCHA.

config/googlerecaptchav3.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,16 @@
6161
'is_score_enabled' => true,
6262
/*
6363
|--------------------------------------------------------------------------
64-
| Score Setting
64+
| Setting
6565
|--------------------------------------------------------------------------
6666
| Type: array
67-
| Define your score threshold
67+
| Define your score threshold, define your action
6868
| action: Google reCAPTCHA required paramater
6969
| id: <input> id
7070
| threshold: score threshold
7171
| is_enabled: true/false, if this is true, the system will do score comparsion against your threshold for the action
7272
*/
73-
'score_setting' => [
73+
'setting' => [
7474
[
7575
'action' => 'contact_us',
7676
'id' => 'contact_us_id',

src/Configurations/ReCaptchaConfigV3.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ public function getOptions()
9595
*/
9696
public function getScoreSetting()
9797
{
98-
return config('googlerecaptchav3.score_setting');
98+
return config('googlerecaptchav3.setting');
9999
}
100100
}

src/GoogleReCaptchaV3.php

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: rayndeng
5+
* Date: 6/8/18
6+
* Time: 5:29 PM
7+
*/
8+
9+
namespace RyanDeng\GoogleReCaptcha;
10+
11+
use RyanDeng\GoogleReCaptcha\Interfaces\ReCaptchaConfigV3Interface;
12+
use RyanDeng\GoogleReCaptcha\Interfaces\RequestClientInterface;
13+
use RyanDeng\GoogleReCaptcha\Core\GoogleReCaptchaV3Response;
14+
15+
class GoogleReCaptchaV3
16+
{
17+
private $config;
18+
private $requestClient;
19+
private $action;
20+
private $defaultView = 'GoogleReCaptchaV3::googlerecaptcha.googlerecaptchav3';
21+
22+
public function __construct(ReCaptchaConfigV3Interface $config, RequestClientInterface $requestClient)
23+
{
24+
$this->config = $config;
25+
$this->requestClient = $requestClient;
26+
}
27+
28+
/**
29+
* @param mixed ...$actions
30+
* @return mixed
31+
*/
32+
public function render(...$actions)
33+
{
34+
if ($this->config->isServiceEnabled() === false) {
35+
return null;
36+
}
37+
38+
$mapping = collect($this->config->getScoreSetting())
39+
->whereIn('action', $actions)
40+
->pluck('id', 'action')
41+
->toArray();
42+
43+
$data = [
44+
'publicKey' => value($this->config->getSiteKey()),
45+
'rows' => $mapping
46+
];
47+
48+
$view = $this->getView();
49+
50+
return app('view')->make($view, $data);
51+
}
52+
53+
/**
54+
* @return mixed|string
55+
*/
56+
protected function getView()
57+
{
58+
$configTemplate = $this->config->getTemplate();
59+
60+
if (!empty($configTemplate)) {
61+
$this->defaultView = $configTemplate;
62+
}
63+
return $this->defaultView;
64+
}
65+
66+
/**
67+
* @param $response
68+
* @param null $ip
69+
* @return GoogleReCaptchaV3Response
70+
*/
71+
public function verifyResponse($response, $ip = null)
72+
{
73+
74+
if (!$this->config->isServiceEnabled()) {
75+
$res = new GoogleReCaptchaV3Response([], $ip);
76+
$res->setSuccess(true);
77+
return $res;
78+
}
79+
80+
if (empty($response)) {
81+
return new GoogleReCaptchaV3Response([], $ip, 'Missing input response.');
82+
}
83+
84+
$verifiedResponse = $this->requestClient->post(
85+
$this->config->getSiteVerifyUrl(),
86+
[
87+
'secret' => $this->config->getSecretKey(),
88+
'remoteip' => $ip,
89+
'response' => $response,
90+
],
91+
[
92+
'curl_timeout' => $this->config->getCurlTimeout(),
93+
'curl_verify' => $this->config->getCurlVerify(),
94+
]
95+
);
96+
97+
if (is_null($verifiedResponse) || empty($verifiedResponse)) {
98+
return new GoogleReCaptchaV3Response([], $ip, 'Unable to verify.');
99+
}
100+
101+
$decodedResponse = json_decode($verifiedResponse, true);
102+
$rawResponse = new GoogleReCaptchaV3Response($decodedResponse, $ip);
103+
104+
if ($rawResponse->isSuccess() === false) {
105+
return $rawResponse;
106+
}
107+
108+
if (strcasecmp($this->config->getHostName(), $rawResponse->getHostname()) !== 0) {
109+
$rawResponse->setMessage('Hostname does not match.');
110+
$rawResponse->setSuccess(false);
111+
return $rawResponse;
112+
}
113+
114+
if (isset($this->action) && strcasecmp($this->action, $rawResponse->getAction()) !== 0) {
115+
$rawResponse->setMessage('Action does not match.');
116+
$rawResponse->setSuccess(false);
117+
return $rawResponse;
118+
}
119+
120+
if ($this->getConfig()->isScoreEnabled()) {
121+
$count = collect($this->getConfig()->getScoreSetting())
122+
->where('action', '=', $rawResponse->getAction())
123+
->where('is_enabled', '=', true)
124+
->where('threshold', '>', $rawResponse->getScore())
125+
->count();
126+
127+
128+
if ($count > 0) {
129+
$rawResponse->setSuccess(false);
130+
$rawResponse->setMessage('Score does not meet threshold.');
131+
return $rawResponse;
132+
}
133+
}
134+
$rawResponse->setSuccess(true);
135+
$rawResponse->setMessage('Successfully passed.');
136+
return $rawResponse;
137+
}
138+
139+
140+
public function getConfig()
141+
{
142+
return $this->config;
143+
144+
}
145+
146+
/**
147+
* @param string|null $value
148+
* @return $this
149+
*/
150+
public function setAction(string $value = null)
151+
{
152+
$this->action = $value;
153+
return $this;
154+
}
155+
156+
}

0 commit comments

Comments
 (0)