説明なし

Revoke.php 2.1KB

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