Skip to content

Commit 66a7a2a

Browse files
committed
Add apk_package_name and challenge_ts
1 parent 6a69deb commit 66a7a2a

File tree

2 files changed

+116
-25
lines changed

2 files changed

+116
-25
lines changed

src/ReCaptcha/Response.php

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ class Response
4949
*/
5050
private $hostname;
5151

52+
/**
53+
* Timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
54+
* @var string
55+
*/
56+
private $challengeTs;
57+
58+
/**
59+
* APK package name
60+
* @var string
61+
*/
62+
private $apkPackageName;
63+
5264
/**
5365
* Score assigned to the request
5466
* @var float
@@ -72,40 +84,46 @@ public static function fromJson($json)
7284
$responseData = json_decode($json, true);
7385

7486
if (!$responseData) {
75-
return new Response(false, array('invalid-json'));
87+
return new Response(false, array(ReCaptcha::E_INVALID_JSON));
7688
}
7789

7890
$hostname = isset($responseData['hostname']) ? $responseData['hostname'] : null;
91+
$challengeTs = isset($responseData['challenge_ts']) ? $responseData['challenge_ts'] : null;
92+
$apkPackageName = isset($responseData['apk_package_name']) ? $responseData['apk_package_name'] : null;
7993
$score = isset($responseData['score']) ? floatval($responseData['score']) : null;
8094
$action = isset($responseData['action']) ? $responseData['action'] : null;
8195

8296
if (isset($responseData['success']) && $responseData['success'] == true) {
83-
return new Response(true, array(), $hostname, $score, $action);
97+
return new Response(true, array(), $hostname, $challengeTs, $apkPackageName, $score, $action);
8498
}
8599

86100
if (isset($responseData['error-codes']) && is_array($responseData['error-codes'])) {
87-
return new Response(false, $responseData['error-codes'], $hostname);
101+
return new Response(false, $responseData['error-codes'], $hostname, $challengeTs, $apkPackageName, $score, $action);
88102
}
89103

90-
return new Response(false, array(), $hostname);
104+
return new Response(false, array(ReCaptcha::E_UNKNOWN_ERROR), $hostname, $challengeTs, $apkPackageName, $score, $action);
91105
}
92106

93107
/**
94108
* Constructor.
95109
*
96110
* @param boolean $success
97-
* @param array $errorCodes
98111
* @param string $hostname
112+
* @param string $challengeTs
113+
* @param string $apkPackageName
99114
* @param float $score
100115
* @param strong $action
116+
* @param array $errorCodes
101117
*/
102-
public function __construct($success, array $errorCodes = array(), $hostname = null, $score = null, $action = null)
118+
public function __construct($success, array $errorCodes = array(), $hostname = null, $challengeTs = null, $apkPackageName = null, $score = null, $action = null)
103119
{
104120
$this->success = $success;
105-
$this->errorCodes = $errorCodes;
106121
$this->hostname = $hostname;
122+
$this->challengeTs = $challengeTs;
123+
$this->apkPackageName = $apkPackageName;
107124
$this->score = $score;
108125
$this->action = $action;
126+
$this->errorCodes = $errorCodes;
109127
}
110128

111129
/**
@@ -138,6 +156,25 @@ public function getHostname()
138156
return $this->hostname;
139157
}
140158

159+
/**
160+
* Get challenge timestamp
161+
*
162+
* @return string
163+
*/
164+
public function getChallengeTs()
165+
{
166+
return $this->challengeTs;
167+
}
168+
169+
/**
170+
* Get APK package name
171+
*
172+
* @return string
173+
*/
174+
public function getApkPackageName()
175+
{
176+
return $this->apkPackageName;
177+
}
141178
/**
142179
* Get score
143180
*
@@ -161,11 +198,13 @@ public function getAction()
161198
public function toArray()
162199
{
163200
return array(
164-
'success' => $this->success,
165-
'error-codes' => $this->errorCodes,
166-
'hostname' => $this->hostname,
167-
'score' => $this->score,
168-
'action' => $this->action,
201+
'success' => $this->isSuccess(),
202+
'hostname' => $this->getHostname(),
203+
'challenge_ts' => $this->getChallengeTs(),
204+
'apk_package_name' => $this->getApkPackageName(),
205+
'score' => $this->getScore(),
206+
'action' => $this->getAction(),
207+
'error-codes' => $this->getErrorCodes(),
169208
);
170209
}
171210
}

tests/ReCaptcha/ResponseTest.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,61 @@ class ResponseTest extends TestCase
3434
/**
3535
* @dataProvider provideJson
3636
*/
37-
public function testFromJson($json, $success, $errorCodes, $hostname)
37+
public function testFromJson($json, $success, $errorCodes, $hostname, $challengeTs, $apkPackageName, $score, $action)
3838
{
3939
$response = Response::fromJson($json);
4040
$this->assertEquals($success, $response->isSuccess());
4141
$this->assertEquals($errorCodes, $response->getErrorCodes());
4242
$this->assertEquals($hostname, $response->getHostname());
43+
$this->assertEquals($challengeTs, $response->getChallengeTs());
44+
$this->assertEquals($apkPackageName, $response->getApkPackageName());
45+
$this->assertEquals($score, $response->getScore());
46+
$this->assertEquals($action, $response->getAction());
4347
}
4448

4549
public function provideJson()
4650
{
4751
return array(
48-
array('{"success": true}', true, array(), null),
49-
array('{"success": true, "hostname": "google.com"}', true, array(), 'google.com'),
50-
array('{"success": false, "error-codes": ["test"]}', false, array('test'), null),
51-
array('{"success": false, "error-codes": ["test"], "hostname": "google.com"}', false, array('test'), 'google.com'),
52-
array('{"success": true, "error-codes": ["test"]}', true, array(), null),
53-
array('{"success": true, "error-codes": ["test"], "hostname": "google.com"}', true, array(), 'google.com'),
54-
array('{"success": false}', false, array(), null),
55-
array('{"success": false, "hostname": "google.com"}', false, array(), 'google.com'),
56-
array('BAD JSON', false, array('invalid-json'), null),
52+
array(
53+
'{"success": true}',
54+
true, array(), null, null, null, null, null,
55+
),
56+
array(
57+
'{"success": true, "hostname": "google.com"}',
58+
true, array(), 'google.com', null, null, null, null,
59+
),
60+
array(
61+
'{"success": false, "error-codes": ["test"]}',
62+
false, array('test'), null, null, null, null, null,
63+
),
64+
array(
65+
'{"success": false, "error-codes": ["test"], "hostname": "google.com"}',
66+
false, array('test'), 'google.com', null, null, null, null,
67+
),
68+
array(
69+
'{"success": false, "error-codes": ["test"], "hostname": "google.com", "challenge_ts": "timestamp", "apk_package_name": "apk", "score": "0.5", "action": "action"}',
70+
false, array('test'), 'google.com', 'timestamp', 'apk', 0.5, 'action',
71+
),
72+
array(
73+
'{"success": true, "error-codes": ["test"]}',
74+
true, array(), null, null, null, null, null,
75+
),
76+
array(
77+
'{"success": true, "error-codes": ["test"], "hostname": "google.com"}',
78+
true, array(), 'google.com', null, null, null, null,
79+
),
80+
array(
81+
'{"success": false}',
82+
false, array(ReCaptcha::E_UNKNOWN_ERROR), null, null, null, null, null,
83+
),
84+
array(
85+
'{"success": false, "hostname": "google.com"}',
86+
false, array(ReCaptcha::E_UNKNOWN_ERROR), 'google.com', null, null, null, null,
87+
),
88+
array(
89+
'BAD JSON',
90+
false, array(ReCaptcha::E_INVALID_JSON), null, null, null, null, null,
91+
),
5792
);
5893
}
5994

@@ -84,27 +119,44 @@ public function testGetHostname()
84119
$this->assertEquals($hostname, $response->getHostname());
85120
}
86121

122+
public function testGetChallengeTs()
123+
{
124+
$timestamp = 'timestamp';
125+
$errorCodes = array();
126+
$response = new Response(true, array(), 'hostname', $timestamp);
127+
$this->assertEquals($timestamp, $response->getChallengeTs());
128+
}
129+
130+
public function TestGetApkPackageName()
131+
{
132+
$apk = 'apk';
133+
$response = new Response(true, array(), 'hostname', 'timestamp', 'apk');
134+
$this->assertEquals($apk, $response->getApkPackageName());
135+
}
136+
87137
public function testGetScore()
88138
{
89139
$score = 0.5;
90-
$response = new Response(true, array(), '', $score);
140+
$response = new Response(true, array(), 'hostname', 'timestamp', 'apk', $score);
91141
$this->assertEquals($score, $response->getScore());
92142
}
93143

94144
public function testGetAction()
95145
{
96146
$action = 'homepage';
97-
$response = new Response(true, array(), '', 0.5, $action);
147+
$response = new Response(true, array(), 'hostname', 'timestamp', 'apk', '0.5', 'homepage');
98148
$this->assertEquals($action, $response->getAction());
99149
}
100150

101151
public function testToArray()
102152
{
103-
$response = new Response(true, array(), 'hostname', 0.5, 'homepage');
153+
$response = new Response(true, array(), 'hostname', 'timestamp', 'apk', '0.5', 'homepage');
104154
$expected = array(
105155
'success' => true,
106156
'error-codes' => array(),
107157
'hostname' => 'hostname',
158+
'challenge_ts' => 'timestamp',
159+
'apk_package_name' => 'apk',
108160
'score' => 0.5,
109161
'action' => 'homepage',
110162
);

0 commit comments

Comments
 (0)