/library/Google/vendor/google/auth/src/Subscriber/AuthTokenSubscriber.php

https://gitlab.com/vangtrangbac123/zinkaiuit.tk · PHP · 118 lines · 39 code · 10 blank · 69 comment · 4 complexity · d75d8343b2af539516e15b6591f2865e MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright 2015 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\Auth\Subscriber;
  18. use Google\Auth\FetchAuthTokenInterface;
  19. use GuzzleHttp\Event\BeforeEvent;
  20. use GuzzleHttp\Event\RequestEvents;
  21. use GuzzleHttp\Event\SubscriberInterface;
  22. /**
  23. * AuthTokenSubscriber is a Guzzle Subscriber that adds an Authorization header
  24. * provided by an object implementing FetchAuthTokenInterface.
  25. *
  26. * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of
  27. * the values value in that hash is added as the authorization header.
  28. *
  29. * Requests will be accessed with the authorization header:
  30. *
  31. * 'authorization' 'Bearer <value of auth_token>'
  32. */
  33. class AuthTokenSubscriber implements SubscriberInterface
  34. {
  35. /**
  36. * @var callable
  37. */
  38. private $httpHandler;
  39. /**
  40. * @var FetchAuthTokenInterface
  41. */
  42. private $fetcher;
  43. /**
  44. * @var callable
  45. */
  46. private $tokenCallback;
  47. /**
  48. * Creates a new AuthTokenSubscriber.
  49. *
  50. * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
  51. * @param callable $httpHandler (optional) http client to fetch the token.
  52. * @param callable $tokenCallback (optional) function to be called when a new token is fetched.
  53. */
  54. public function __construct(
  55. FetchAuthTokenInterface $fetcher,
  56. callable $httpHandler = null,
  57. callable $tokenCallback = null
  58. ) {
  59. $this->fetcher = $fetcher;
  60. $this->httpHandler = $httpHandler;
  61. $this->tokenCallback = $tokenCallback;
  62. }
  63. /**
  64. * @return array
  65. */
  66. public function getEvents()
  67. {
  68. return ['before' => ['onBefore', RequestEvents::SIGN_REQUEST]];
  69. }
  70. /**
  71. * Updates the request with an Authorization header when auth is 'fetched_auth_token'.
  72. *
  73. * use GuzzleHttp\Client;
  74. * use Google\Auth\OAuth2;
  75. * use Google\Auth\Subscriber\AuthTokenSubscriber;
  76. *
  77. * $config = [..<oauth config param>.];
  78. * $oauth2 = new OAuth2($config)
  79. * $subscriber = new AuthTokenSubscriber($oauth2);
  80. *
  81. * $client = new Client([
  82. * 'base_url' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
  83. * 'defaults' => ['auth' => 'google_auth']
  84. * ]);
  85. * $client->getEmitter()->attach($subscriber);
  86. *
  87. * $res = $client->get('myproject/taskqueues/myqueue');
  88. *
  89. * @param BeforeEvent $event
  90. */
  91. public function onBefore(BeforeEvent $event)
  92. {
  93. // Requests using "auth"="google_auth" will be authorized.
  94. $request = $event->getRequest();
  95. if ($request->getConfig()['auth'] != 'google_auth') {
  96. return;
  97. }
  98. // Fetch the auth token.
  99. $auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler);
  100. if (array_key_exists('access_token', $auth_tokens)) {
  101. $request->setHeader('authorization', 'Bearer ' . $auth_tokens['access_token']);
  102. // notify the callback if applicable
  103. if ($this->tokenCallback) {
  104. call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']);
  105. }
  106. }
  107. }
  108. }