暂无描述

AuthTokenMiddleware.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Middleware;
  18. use Google\Auth\FetchAuthTokenInterface;
  19. use Psr\Http\Message\RequestInterface;
  20. /**
  21. * AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header
  22. * provided by an object implementing FetchAuthTokenInterface.
  23. *
  24. * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of
  25. * the values value in that hash is added as the authorization header.
  26. *
  27. * Requests will be accessed with the authorization header:
  28. *
  29. * 'authorization' 'Bearer <value of auth_token>'
  30. */
  31. class AuthTokenMiddleware
  32. {
  33. /**
  34. * @var callback
  35. */
  36. private $httpHandler;
  37. /**
  38. * @var FetchAuthTokenInterface
  39. */
  40. private $fetcher;
  41. /**
  42. * @var callable
  43. */
  44. private $tokenCallback;
  45. /**
  46. * Creates a new AuthTokenMiddleware.
  47. *
  48. * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token
  49. * @param callable $httpHandler (optional) callback which delivers psr7 request
  50. * @param callable $tokenCallback (optional) function to be called when a new token is fetched.
  51. */
  52. public function __construct(
  53. FetchAuthTokenInterface $fetcher,
  54. callable $httpHandler = null,
  55. callable $tokenCallback = null
  56. ) {
  57. $this->fetcher = $fetcher;
  58. $this->httpHandler = $httpHandler;
  59. $this->tokenCallback = $tokenCallback;
  60. }
  61. /**
  62. * Updates the request with an Authorization header when auth is 'google_auth'.
  63. *
  64. * use Google\Auth\Middleware\AuthTokenMiddleware;
  65. * use Google\Auth\OAuth2;
  66. * use GuzzleHttp\Client;
  67. * use GuzzleHttp\HandlerStack;
  68. *
  69. * $config = [..<oauth config param>.];
  70. * $oauth2 = new OAuth2($config)
  71. * $middleware = new AuthTokenMiddleware($oauth2);
  72. * $stack = HandlerStack::create();
  73. * $stack->push($middleware);
  74. *
  75. * $client = new Client([
  76. * 'handler' => $stack,
  77. * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
  78. * 'auth' => 'google_auth' // authorize all requests
  79. * ]);
  80. *
  81. * $res = $client->get('myproject/taskqueues/myqueue');
  82. *
  83. * @param callable $handler
  84. *
  85. * @return \Closure
  86. */
  87. public function __invoke(callable $handler)
  88. {
  89. return function (RequestInterface $request, array $options) use ($handler) {
  90. // Requests using "auth"="google_auth" will be authorized.
  91. if (!isset($options['auth']) || $options['auth'] !== 'google_auth') {
  92. return $handler($request, $options);
  93. }
  94. $request = $request->withHeader('authorization', 'Bearer ' . $this->fetchToken());
  95. return $handler($request, $options);
  96. };
  97. }
  98. /**
  99. * Call fetcher to fetch the token.
  100. *
  101. * @return string
  102. */
  103. private function fetchToken()
  104. {
  105. $auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler);
  106. if (array_key_exists('access_token', $auth_tokens)) {
  107. // notify the callback if applicable
  108. if ($this->tokenCallback) {
  109. call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $auth_tokens['access_token']);
  110. }
  111. return $auth_tokens['access_token'];
  112. }
  113. }
  114. }