Revoke.php 2.3 KB

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