103 |
- |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* Copyright 2008 Google Inc.
|
|
|
5 |
*
|
|
|
6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
7 |
* you may not use this file except in compliance with the License.
|
|
|
8 |
* You may obtain a copy of the License at
|
|
|
9 |
*
|
|
|
10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
11 |
*
|
|
|
12 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
15 |
* See the License for the specific language governing permissions and
|
|
|
16 |
* limitations under the License.
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
use Google\Auth\HttpHandler\HttpHandlerFactory;
|
|
|
20 |
use GuzzleHttp\ClientInterface;
|
|
|
21 |
use GuzzleHttp\Psr7;
|
|
|
22 |
use GuzzleHttp\Psr7\Request;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Wrapper around Google Access Tokens which provides convenience functions
|
|
|
26 |
*
|
|
|
27 |
*/
|
|
|
28 |
class Google_AccessToken_Revoke
|
|
|
29 |
{
|
|
|
30 |
/**
|
|
|
31 |
* @var GuzzleHttp\ClientInterface The http client
|
|
|
32 |
*/
|
|
|
33 |
private $http;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Instantiates the class, but does not initiate the login flow, leaving it
|
|
|
37 |
* to the discretion of the caller.
|
|
|
38 |
*/
|
|
|
39 |
public function __construct(ClientInterface $http = null)
|
|
|
40 |
{
|
|
|
41 |
$this->http = $http;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
|
|
|
46 |
* token, if a token isn't provided.
|
|
|
47 |
*
|
|
|
48 |
* @param string|array $token The token (access token or a refresh token) that should be revoked.
|
|
|
49 |
* @return boolean Returns True if the revocation was successful, otherwise False.
|
|
|
50 |
*/
|
|
|
51 |
public function revokeToken($token)
|
|
|
52 |
{
|
|
|
53 |
if (is_array($token)) {
|
|
|
54 |
if (isset($token['refresh_token'])) {
|
|
|
55 |
$token = $token['refresh_token'];
|
|
|
56 |
} else {
|
|
|
57 |
$token = $token['access_token'];
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$body = Psr7\stream_for(http_build_query(array('token' => $token)));
|
|
|
62 |
$request = new Request(
|
|
|
63 |
'POST',
|
|
|
64 |
Google_Client::OAUTH2_REVOKE_URI,
|
|
|
65 |
[
|
|
|
66 |
'Cache-Control' => 'no-store',
|
|
|
67 |
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
|
68 |
],
|
|
|
69 |
$body
|
|
|
70 |
);
|
|
|
71 |
|
|
|
72 |
$httpHandler = HttpHandlerFactory::build($this->http);
|
|
|
73 |
|
|
|
74 |
$response = $httpHandler($request);
|
|
|
75 |
|
|
|
76 |
return $response->getStatusCode() == 200;
|
|
|
77 |
}
|
|
|
78 |
}
|