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 namespace Google\AccessToken;
20 
21 use Google\Auth\HttpHandler\HttpHandlerFactory;
22 use Google\Client;
23 use GuzzleHttp\ClientInterface;
24 use GuzzleHttp\Psr7;
25 use GuzzleHttp\Psr7\Request;
26 
27 /**
28  * Wrapper around Google Access Tokens which provides convenience functions
29  *
30  */
31 class Revoke
32 {
33   /**
34    * @var ClientInterface The http client
35    */
36   private $http;
37 
38   /**
39    * Instantiates the class, but does not initiate the login flow, leaving it
40    * to the discretion of the caller.
41    */
42   public function __construct(ClientInterface $http = null)
43   {
44     $this->http = $http;
45   }
46 
47   /**
48    * Revoke an OAuth2 access token or refresh token. This method will revoke the current access
49    * token, if a token isn't provided.
50    *
51    * @param string|array $token The token (access token or a refresh token) that should be revoked.
52    * @return boolean Returns True if the revocation was successful, otherwise False.
53    */
54   public function revokeToken($token)
55   {
56     if (is_array($token)) {
57       if (isset($token['refresh_token'])) {
58         $token = $token['refresh_token'];
59       } else {
60         $token = $token['access_token'];
61       }
62     }
63 
64     $body = Psr7\Utils::streamFor(http_build_query(array('token' => $token)));
65     $request = new Request(
66         'POST',
67         Client::OAUTH2_REVOKE_URI,
68         [
69           'Cache-Control' => 'no-store',
70           'Content-Type'  => 'application/x-www-form-urlencoded',
71         ],
72         $body
73     );
74 
75     $httpHandler = HttpHandlerFactory::build($this->http);
76 
77     $response = $httpHandler($request);
78 
79     return $response->getStatusCode() == 200;
80   }
81 }
82