Skip to content

Commit 91564be

Browse files
committed
Add hostname to Response object
It can be used on server-side in order to check the domain name where the reCaptcha was solved to improve the security. This verication is mandatory when the domain name validation is turned off on Admin Console. See more: https://developers.google.com/recaptcha/docs/domain_validation
1 parent d3274db commit 91564be

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/ReCaptcha/Response.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Response
4343
*/
4444
private $errorCodes = array();
4545

46+
/**
47+
* The hostname of the site where the reCAPTCHA was solved.
48+
* @var string
49+
*/
50+
private $hostname = array();
51+
4652
/**
4753
* Build the response from the expected JSON returned by the service.
4854
*
@@ -57,27 +63,31 @@ public static function fromJson($json)
5763
return new Response(false, array('invalid-json'));
5864
}
5965

66+
$hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
67+
6068
if (isset($responseData['success']) && $responseData['success'] == true) {
61-
return new Response(true);
69+
return new Response(true, [], $hostname);
6270
}
6371

6472
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
65-
return new Response(false, $responseData['error-codes']);
73+
return new Response(false, $responseData['error-codes'], $hostname);
6674
}
6775

68-
return new Response(false);
76+
return new Response(false, array(), $hostname);
6977
}
7078

7179
/**
7280
* Constructor.
7381
*
7482
* @param boolean $success
7583
* @param array $errorCodes
84+
* @param string $hostname
7685
*/
77-
public function __construct($success, array $errorCodes = array())
86+
public function __construct($success, array $errorCodes = array(), $hostname = null)
7887
{
7988
$this->success = $success;
8089
$this->errorCodes = $errorCodes;
90+
$this->hostname = $hostname;
8191
}
8292

8393
/**
@@ -99,4 +109,14 @@ public function getErrorCodes()
99109
{
100110
return $this->errorCodes;
101111
}
112+
113+
/**
114+
* Get hostname.
115+
*
116+
* @return string
117+
*/
118+
public function getHostname()
119+
{
120+
return $this->hostname;
121+
}
102122
}

tests/ReCaptcha/ResponseTest.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,26 @@ class ResponseTest extends \PHPUnit_Framework_TestCase
3232
/**
3333
* @dataProvider provideJson
3434
*/
35-
public function testFromJson($json, $success, $errorCodes)
35+
public function testFromJson($json, $success, $errorCodes, $hostname)
3636
{
3737
$response = Response::fromJson($json);
3838
$this->assertEquals($success, $response->isSuccess());
3939
$this->assertEquals($errorCodes, $response->getErrorCodes());
40+
$this->assertEquals($hostname, $response->getHostname());
4041
}
4142

4243
public function provideJson()
4344
{
4445
return array(
45-
array('{"success": true}', true, array()),
46-
array('{"success": false, "error-codes": ["test"]}', false, array('test')),
47-
array('{"success": true, "error-codes": ["test"]}', true, array()),
48-
array('{"success": false}', false, array()),
49-
array('BAD JSON', false, array('invalid-json')),
46+
array('{"success": true}', true, array(), null),
47+
array('{"success": true, "hostname": "google.com"}', true, array(), 'google.com'),
48+
array('{"success": false, "error-codes": ["test"]}', false, array('test'), null),
49+
array('{"success": false, "error-codes": ["test"], "hostname": "google.com"}', false, array('test'), 'google.com'),
50+
array('{"success": true, "error-codes": ["test"]}', true, array(), null),
51+
array('{"success": true, "error-codes": ["test"], "hostname": "google.com"}', true, array(), 'google.com'),
52+
array('{"success": false}', false, array(), null),
53+
array('{"success": false, "hostname": "google.com"}', false, array(), 'google.com'),
54+
array('BAD JSON', false, array('invalid-json'), null),
5055
);
5156
}
5257

@@ -65,4 +70,12 @@ public function testGetErrorCodes()
6570
$response = new Response(true, $errorCodes);
6671
$this->assertEquals($errorCodes, $response->getErrorCodes());
6772
}
73+
74+
public function testGetHostname()
75+
{
76+
$hostname = 'google.com';
77+
$errorCodes = array();
78+
$response = new Response(true, $errorCodes, $hostname);
79+
$this->assertEquals($hostname, $response->getHostname());
80+
}
6881
}

0 commit comments

Comments
 (0)