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