media-cloud-sync
/
includes
/
sdk
/
google
/
google
/
auth
/
src
/
Middleware
/
AuthTokenMiddleware.php
AuthTokenMiddleware.php in Media Cloud Sync 1.1.1, at includes/sdk/google/google/auth/src/Middleware/AuthTokenMiddleware.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2015 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 | namespace Dudlewebs\WPMCS\Google\Auth\Middleware; |
| 19 | |
| 20 | use Dudlewebs\WPMCS\Google\Auth\FetchAuthTokenCache; |
| 21 | use Dudlewebs\WPMCS\Google\Auth\FetchAuthTokenInterface; |
| 22 | use Dudlewebs\WPMCS\Google\Auth\GetQuotaProjectInterface; |
| 23 | use Dudlewebs\WPMCS\Google\Auth\UpdateMetadataInterface; |
| 24 | use Dudlewebs\WPMCS\GuzzleHttp\Psr7\Utils; |
| 25 | use Dudlewebs\WPMCS\Psr\Http\Message\RequestInterface; |
| 26 | /** |
| 27 | * AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header |
| 28 | * provided by an object implementing FetchAuthTokenInterface. |
| 29 | * |
| 30 | * The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of |
| 31 | * the values value in that hash is added as the authorization header. |
| 32 | * |
| 33 | * Requests will be accessed with the authorization header: |
| 34 | * |
| 35 | * 'authorization' 'Bearer <value of auth_token>' |
| 36 | */ |
| 37 | class AuthTokenMiddleware |
| 38 | { |
| 39 | /** |
| 40 | * @var callable |
| 41 | */ |
| 42 | private $httpHandler; |
| 43 | /** |
| 44 | * It must be an implementation of FetchAuthTokenInterface. |
| 45 | * It may also implement UpdateMetadataInterface allowing direct |
| 46 | * retrieval of auth related headers |
| 47 | * @var FetchAuthTokenInterface |
| 48 | */ |
| 49 | private $fetcher; |
| 50 | /** |
| 51 | * @var ?callable |
| 52 | */ |
| 53 | private $tokenCallback; |
| 54 | /** |
| 55 | * Creates a new AuthTokenMiddleware. |
| 56 | * |
| 57 | * @param FetchAuthTokenInterface $fetcher is used to fetch the auth token |
| 58 | * @param callable $httpHandler (optional) callback which delivers psr7 request |
| 59 | * @param callable $tokenCallback (optional) function to be called when a new token is fetched. |
| 60 | */ |
| 61 | public function __construct(FetchAuthTokenInterface $fetcher, callable $httpHandler = null, callable $tokenCallback = null) |
| 62 | { |
| 63 | $this->fetcher = $fetcher; |
| 64 | $this->httpHandler = $httpHandler; |
| 65 | $this->tokenCallback = $tokenCallback; |
| 66 | } |
| 67 | /** |
| 68 | * Updates the request with an Authorization header when auth is 'google_auth'. |
| 69 | * |
| 70 | * use Google\Auth\Middleware\AuthTokenMiddleware; |
| 71 | * use Google\Auth\OAuth2; |
| 72 | * use GuzzleHttp\Client; |
| 73 | * use GuzzleHttp\HandlerStack; |
| 74 | * |
| 75 | * $config = [..<oauth config param>.]; |
| 76 | * $oauth2 = new OAuth2($config) |
| 77 | * $middleware = new AuthTokenMiddleware($oauth2); |
| 78 | * $stack = HandlerStack::create(); |
| 79 | * $stack->push($middleware); |
| 80 | * |
| 81 | * $client = new Client([ |
| 82 | * 'handler' => $stack, |
| 83 | * 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', |
| 84 | * 'auth' => 'google_auth' // authorize all requests |
| 85 | * ]); |
| 86 | * |
| 87 | * $res = $client->get('myproject/taskqueues/myqueue'); |
| 88 | * |
| 89 | * @param callable $handler |
| 90 | * @return \Closure |
| 91 | */ |
| 92 | public function __invoke(callable $handler) |
| 93 | { |
| 94 | return function (RequestInterface $request, array $options) use ($handler) { |
| 95 | // Requests using "auth"="google_auth" will be authorized. |
| 96 | if (!isset($options['auth']) || $options['auth'] !== 'google_auth') { |
| 97 | return $handler($request, $options); |
| 98 | } |
| 99 | $request = $this->addAuthHeaders($request); |
| 100 | if ($quotaProject = $this->getQuotaProject()) { |
| 101 | $request = $request->withHeader(GetQuotaProjectInterface::X_GOOG_USER_PROJECT_HEADER, $quotaProject); |
| 102 | } |
| 103 | return $handler($request, $options); |
| 104 | }; |
| 105 | } |
| 106 | /** |
| 107 | * Adds auth related headers to the request. |
| 108 | * |
| 109 | * @param RequestInterface $request |
| 110 | * @return RequestInterface |
| 111 | */ |
| 112 | private function addAuthHeaders(RequestInterface $request) |
| 113 | { |
| 114 | if (!$this->fetcher instanceof UpdateMetadataInterface || $this->fetcher instanceof FetchAuthTokenCache && !$this->fetcher->getFetcher() instanceof UpdateMetadataInterface) { |
| 115 | $token = $this->fetcher->fetchAuthToken(); |
| 116 | $request = $request->withHeader('authorization', 'Bearer ' . ($token['access_token'] ?? $token['id_token'] ?? '')); |
| 117 | } else { |
| 118 | $headers = $this->fetcher->updateMetadata($request->getHeaders(), null, $this->httpHandler); |
| 119 | $request = Utils::modifyRequest($request, ['set_headers' => $headers]); |
| 120 | } |
| 121 | if ($this->tokenCallback && $token = $this->fetcher->getLastReceivedToken()) { |
| 122 | if (array_key_exists('access_token', $token)) { |
| 123 | call_user_func($this->tokenCallback, $this->fetcher->getCacheKey(), $token['access_token']); |
| 124 | } |
| 125 | } |
| 126 | return $request; |
| 127 | } |
| 128 | /** |
| 129 | * @return string|null |
| 130 | */ |
| 131 | private function getQuotaProject() |
| 132 | { |
| 133 | if ($this->fetcher instanceof GetQuotaProjectInterface) { |
| 134 | return $this->fetcher->getQuotaProject(); |
| 135 | } |
| 136 | return null; |
| 137 | } |
| 138 | } |
| 139 |