Skip to content

Commit e0f0590

Browse files
committed
Merge pull request googleapis#138 from ianbarber/master
Fixing token revoking and change URI template quoting
2 parents 3a6a6ec + 9bce45a commit e0f0590

File tree

5 files changed

+108
-27
lines changed

5 files changed

+108
-27
lines changed

src/Google/Auth/OAuth2.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,17 +328,21 @@ private function refreshTokenRequest($params)
328328
}
329329
}
330330

331-
/**
332-
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
333-
* token, if a token isn't provided.
334-
* @throws Google_Auth_Exception
335-
* @param string|null $token The token (access token or a refresh token) that should be revoked.
336-
* @return boolean Returns True if the revocation was successful, otherwise False.
337-
*/
331+
/**
332+
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
333+
* token, if a token isn't provided.
334+
* @throws Google_Auth_Exception
335+
* @param string|null $token The token (access token or a refresh token) that should be revoked.
336+
* @return boolean Returns True if the revocation was successful, otherwise False.
337+
*/
338338
public function revokeToken($token = null)
339339
{
340340
if (!$token) {
341-
$token = $this->token['access_token'];
341+
if (array_key_exists('refresh_token', $this->token)) {
342+
$token = $this->token['refresh_token'];
343+
} else {
344+
$token = $this->token['access_token'];
345+
}
342346
}
343347
$request = new Google_Http_Request(
344348
self::OAUTH2_REVOKE_URI,

src/Google/Collection.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ class Google_Collection extends Google_Model implements Iterator, Countable
1313

1414
public function rewind()
1515
{
16-
if (is_array($this->data[$this->collection_key])) {
17-
reset($this->data[$this->collection_key]);
16+
if (is_array($this->modelData[$this->collection_key])) {
17+
reset($this->modelData[$this->collection_key]);
1818
}
1919
}
2020

2121
public function current()
2222
{
2323
$this->coerceType($this->key());
24-
if (is_array($this->data[$this->collection_key])) {
25-
return current($this->data[$this->collection_key]);
24+
if (is_array($this->modelData[$this->collection_key])) {
25+
return current($this->modelData[$this->collection_key]);
2626
}
2727
}
2828

2929
public function key()
3030
{
31-
if (is_array($this->data[$this->collection_key])) {
32-
return key($this->data[$this->collection_key]);
31+
if (is_array($this->modelData[$this->collection_key])) {
32+
return key($this->modelData[$this->collection_key]);
3333
}
3434
}
3535

3636
public function next()
3737
{
38-
return next($this->data[$this->collection_key]);
38+
return next($this->modelData[$this->collection_key]);
3939
}
4040

4141
public function valid()
@@ -46,15 +46,15 @@ public function valid()
4646

4747
public function count()
4848
{
49-
return count($this->data[$this->collection_key]);
49+
return count($this->modelData[$this->collection_key]);
5050
}
5151

5252
public function offsetExists ($offset)
5353
{
5454
if (!is_numeric($offset)) {
5555
return parent::offsetExists($offset);
5656
}
57-
return isset($this->data[$this->collection_key][$offset]);
57+
return isset($this->modelData[$this->collection_key][$offset]);
5858
}
5959

6060
public function offsetGet($offset)
@@ -63,32 +63,32 @@ public function offsetGet($offset)
6363
return parent::offsetGet($offset);
6464
}
6565
$this->coerceType($offset);
66-
return $this->data[$this->collection_key][$offset];
66+
return $this->modelData[$this->collection_key][$offset];
6767
}
6868

6969
public function offsetSet($offset, $value)
7070
{
7171
if (!is_numeric($offset)) {
7272
return parent::offsetSet($offset, $value);
7373
}
74-
$this->data[$this->collection_key][$offset] = $value;
74+
$this->modelData[$this->collection_key][$offset] = $value;
7575
}
7676

7777
public function offsetUnset($offset)
7878
{
7979
if (!is_numeric($offset)) {
8080
return parent::offsetUnset($offset);
8181
}
82-
unset($this->data[$this->collection_key][$offset]);
82+
unset($this->modelData[$this->collection_key][$offset]);
8383
}
8484

8585
private function coerceType($offset)
8686
{
8787
$typeKey = $this->keyType($this->collection_key);
88-
if (isset($this->$typeKey) && !is_object($this->data[$this->collection_key][$offset])) {
88+
if (isset($this->$typeKey) && !is_object($this->modelData[$this->collection_key][$offset])) {
8989
$type = $this->$typeKey;
90-
$this->data[$this->collection_key][$offset] =
91-
new $type($this->data[$this->collection_key][$offset]);
90+
$this->modelData[$this->collection_key][$offset] =
91+
new $type($this->modelData[$this->collection_key][$offset]);
9292
}
9393
}
9494
}

src/Google/Utils/URITemplate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Google_Utils_URITemplate
4848
*/
4949
private $reserved = array(
5050
"=", ",", "!", "@", "|", ":", "/", "?", "#",
51-
"[", "]","$", "&", "'", "(", ")", "*", "+", ";"
51+
"[", "]",'$', "&", "'", "(", ")", "*", "+", ";"
5252
);
5353
private $reservedEncoded = array(
5454
"%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F",

tests/general/ApiModelTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,29 @@ public function testUnsetPropertyOnModel()
119119
unset($model->foo);
120120
$this->assertFalse(isset($model->foo));
121121
}
122+
123+
public function testCollection()
124+
{
125+
$data = json_decode(
126+
'{
127+
"kind": "calendar#events",
128+
"id": "1234566",
129+
"etag": "abcdef",
130+
"totalItems": 4,
131+
"items": [
132+
{"id": 1},
133+
{"id": 2},
134+
{"id": 3},
135+
{"id": 4}
136+
]
137+
}', true);
138+
$collection = new Google_Service_Calendar_Events($data);
139+
$this->assertEquals(4, count($collection));
140+
$count = 0;
141+
foreach ($collection as $col) {
142+
$count++;
143+
}
144+
$this->assertEquals(4, $count);
145+
$this->assertEquals(1, $collection[0]->id);
146+
}
122147
}

tests/general/ApiOAuth2Test.php

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
class ApiOAuth2Test extends BaseTest {
2626

27-
public function testSign() {
27+
public function testSign()
28+
{
2829
$client = $this->getClient();
2930
$oauth = new Google_Auth_OAuth2($client);
3031

@@ -53,7 +54,57 @@ public function testSign() {
5354
$this->assertEquals('Bearer ACCESS_TOKEN', $auth);
5455
}
5556

56-
public function testCreateAuthUrl() {
57+
public function testRevokeAccess()
58+
{
59+
$accessToken = "ACCESS_TOKEN";
60+
$refreshToken = "REFRESH_TOKEN";
61+
$accessToken2 = "ACCESS_TOKEN_2";
62+
$token = "";
63+
64+
$client = $this->getClient();
65+
$response = $this->getMock("Google_Http_Request", array(), array(''));
66+
$response->expects($this->any())
67+
->method('getResponseHttpCode')
68+
->will($this->returnValue(200));
69+
$io = $this->getMock("Google_IO_Stream", array(), array($client));
70+
$io->expects($this->any())
71+
->method('makeRequest')
72+
->will($this->returnCallback(function($request) use (&$token, $response) {
73+
$elements = array();
74+
parse_str($request->getPostBody(), $elements);
75+
$token = isset($elements['token']) ? $elements['token'] : null;
76+
return $response;
77+
}));
78+
$client->setIo($io);
79+
80+
// Test with access token.
81+
$oauth = new Google_Auth_OAuth2($client);
82+
$oauth->setAccessToken(json_encode(array(
83+
'access_token' => $accessToken,
84+
'created' => time(),
85+
'expires_in' => '3600'
86+
)));
87+
$this->assertTrue($oauth->revokeToken());
88+
$this->assertEquals($accessToken, $token);
89+
90+
// Test with refresh token.
91+
$oauth = new Google_Auth_OAuth2($client);
92+
$oauth->setAccessToken(json_encode(array(
93+
'access_token' => $accessToken,
94+
'refresh_token' => $refreshToken,
95+
'created' => time(),
96+
'expires_in' => '3600'
97+
)));
98+
$this->assertTrue($oauth->revokeToken());
99+
$this->assertEquals($refreshToken, $token);
100+
101+
// Test with passed in token.
102+
$this->assertTrue($oauth->revokeToken($accessToken2));
103+
$this->assertEquals($accessToken2, $token);
104+
}
105+
106+
public function testCreateAuthUrl()
107+
{
57108
$client = $this->getClient();
58109
$oauth = new Google_Auth_OAuth2($client);
59110

@@ -81,7 +132,8 @@ public function testCreateAuthUrl() {
81132
* this is just a general check to ensure we verify a valid
82133
* id token if one exists.
83134
*/
84-
public function testValidateIdToken() {
135+
public function testValidateIdToken()
136+
{
85137
if (!$this->checkToken()) {
86138
return;
87139
}

0 commit comments

Comments
 (0)