| 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 |
|
| 18 |
namespace Google\Auth\Middleware; |
| 19 |
|
| 20 |
use Google\Auth\CacheInterface; |
| 21 |
use Google\Auth\CacheTrait; |
| 22 |
use Google\Auth\FetchAuthTokenInterface; |
| 23 |
use Psr\Http\Message\RequestInterface; |
| 24 |
|
| 25 |
/** |
| 26 |
* AuthTokenMiddleware is a Guzzle Middleware that adds an Authorization header |
| 27 |
* provided by an object implementing FetchAuthTokenInterface. |
| 28 |
* |
| 29 |
* The FetchAuthTokenInterface#fetchAuthToken is used to obtain a hash; one of |
| 30 |
* the values value in that hash is added as the authorization header. |
| 31 |
* |
| 32 |
* Requests will be accessed with the authorization header: |
| 33 |
* |
| 34 |
* 'Authorization' 'Bearer <value of auth_token>' |
| 35 |
*/ |
| 36 |
class AuthTokenMiddleware |
| 37 |
{ |
| 38 |
use CacheTrait; |
| 39 |
|
| 40 |
const DEFAULT_CACHE_LIFETIME = 1500; |
| 41 |
|
| 42 |
/** @var An implementation of CacheInterface */ |
| 43 |
private $cache; |
| 44 |
|
| 45 |
/** @var callback */ |
| 46 |
private $httpHandler; |
| 47 |
|
| 48 |
/** @var An implementation of FetchAuthTokenInterface */ |
| 49 |
private $fetcher; |
| 50 |
|
| 51 |
/** @var cache configuration */ |
| 52 |
private $cacheConfig; |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a new AuthTokenMiddleware. |
| 56 |
* |
| 57 |
* @param FetchAuthTokenInterface $fetcher is used to fetch the auth token |
| 58 |
* @param array $cacheConfig configures the cache |
| 59 |
* @param CacheInterface $cache (optional) caches the token. |
| 60 |
* @param callable $httpHandler (optional) callback which delivers psr7 request |
| 61 |
*/ |
| 62 |
public function __construct( |
| 63 |
FetchAuthTokenInterface $fetcher, |
| 64 |
array $cacheConfig = null, |
| 65 |
CacheInterface $cache = null, |
| 66 |
callable $httpHandler = null |
| 67 |
) { |
| 68 |
$this->fetcher = $fetcher; |
| 69 |
$this->httpHandler = $httpHandler; |
| 70 |
if (!is_null($cache)) { |
| 71 |
$this->cache = $cache; |
| 72 |
$this->cacheConfig = array_merge([ |
| 73 |
'lifetime' => self::DEFAULT_CACHE_LIFETIME, |
| 74 |
'prefix' => '' |
| 75 |
], $cacheConfig); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Updates the request with an Authorization header when auth is 'google_auth'. |
| 81 |
* |
| 82 |
* use Google\Auth\Middleware\AuthTokenMiddleware; |
| 83 |
* use Google\Auth\OAuth2; |
| 84 |
* use GuzzleHttp\Client; |
| 85 |
* use GuzzleHttp\HandlerStack; |
| 86 |
* |
| 87 |
* $config = [..<oauth config param>.]; |
| 88 |
* $oauth2 = new OAuth2($config) |
| 89 |
* $middleware = new AuthTokenMiddleware( |
| 90 |
* $oauth2, |
| 91 |
* ['prefix' => 'OAuth2::'], |
| 92 |
* $cache = new Memcache() |
| 93 |
* ); |
| 94 |
* $stack = HandlerStack::create(); |
| 95 |
* $stack->push($middleware); |
| 96 |
* |
| 97 |
* $client = new Client([ |
| 98 |
* 'handler' => $stack, |
| 99 |
* 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', |
| 100 |
* 'auth' => 'google_auth' // authorize all requests |
| 101 |
* ]); |
| 102 |
* |
| 103 |
* $res = $client->get('myproject/taskqueues/myqueue'); |
| 104 |
*/ |
| 105 |
public function __invoke(callable $handler) |
| 106 |
{ |
| 107 |
return function (RequestInterface $request, array $options) use ($handler) { |
| 108 |
// Requests using "auth"="google_auth" will be authorized. |
| 109 |
if (!isset($options['auth']) || $options['auth'] !== 'google_auth') { |
| 110 |
return $handler($request, $options); |
| 111 |
} |
| 112 |
|
| 113 |
$request = $request->withHeader('Authorization', 'Bearer ' . $this->fetchToken()); |
| 114 |
return $handler($request, $options); |
| 115 |
}; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Determine if token is available in the cache, if not call fetcher to |
| 120 |
* fetch it. |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
private function fetchToken() |
| 125 |
{ |
| 126 |
// TODO: correct caching; update the call to setCachedValue to set the expiry |
| 127 |
// to the value returned with the auth token. |
| 128 |
// |
| 129 |
// TODO: correct caching; enable the cache to be cleared. |
| 130 |
$cached = $this->getCachedValue(); |
| 131 |
if (!empty($cached)) { |
| 132 |
return $cached; |
| 133 |
} |
| 134 |
|
| 135 |
$auth_tokens = $this->fetcher->fetchAuthToken($this->httpHandler); |
| 136 |
|
| 137 |
if (array_key_exists('access_token', $auth_tokens)) { |
| 138 |
$this->setCachedValue($auth_tokens['access_token']); |
| 139 |
return $auth_tokens['access_token']; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|