| 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; |
| 19 |
|
| 20 |
use Google\Auth\Credentials\AppIdentityCredentials; |
| 21 |
use Google\Auth\Credentials\GCECredentials; |
| 22 |
use Google\Auth\Middleware\AuthTokenMiddleware; |
| 23 |
use Google\Auth\Subscriber\AuthTokenSubscriber; |
| 24 |
|
| 25 |
/** |
| 26 |
* ApplicationDefaultCredentials obtains the default credentials for |
| 27 |
* authorizing a request to a Google service. |
| 28 |
* |
| 29 |
* Application Default Credentials are described here: |
| 30 |
* https://developers.google.com/accounts/docs/application-default-credentials |
| 31 |
* |
| 32 |
* This class implements the search for the application default credentials as |
| 33 |
* described in the link. |
| 34 |
* |
| 35 |
* It provides three factory methods: |
| 36 |
* - #get returns the computed credentials object |
| 37 |
* - #getSubscriber returns an AuthTokenSubscriber built from the credentials object |
| 38 |
* - #getMiddleware returns an AuthTokenMiddleware built from the credentials object |
| 39 |
* |
| 40 |
* This allows it to be used as follows with GuzzleHttp\Client: |
| 41 |
* |
| 42 |
* use Google\Auth\ApplicationDefaultCredentials; |
| 43 |
* use GuzzleHttp\Client; |
| 44 |
* use GuzzleHttp\HandlerStack; |
| 45 |
* |
| 46 |
* $middleware = ApplicationDefaultCredentials::getMiddleware( |
| 47 |
* 'https://www.googleapis.com/auth/taskqueue' |
| 48 |
* ); |
| 49 |
* $stack = HandlerStack::create(); |
| 50 |
* $stack->push($middleware); |
| 51 |
* |
| 52 |
* $client = new Client([ |
| 53 |
* 'handler' => $stack, |
| 54 |
* 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/', |
| 55 |
* 'auth' => 'google_auth' // authorize all requests |
| 56 |
* ]); |
| 57 |
* |
| 58 |
* $res = $client->get('myproject/taskqueues/myqueue'); |
| 59 |
*/ |
| 60 |
class ApplicationDefaultCredentials |
| 61 |
{ |
| 62 |
/** |
| 63 |
* Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface |
| 64 |
* implementation to use in this environment. |
| 65 |
* |
| 66 |
* If supplied, $scope is used to in creating the credentials instance if |
| 67 |
* this does not fallback to the compute engine defaults. |
| 68 |
* |
| 69 |
* @param string|array scope the scope of the access request, expressed |
| 70 |
* either as an Array or as a space-delimited String. |
| 71 |
* @param callable $httpHandler callback which delivers psr7 request |
| 72 |
* @param array $cacheConfig configuration for the cache when it's present |
| 73 |
* @param object $cache an implementation of CacheInterface |
| 74 |
* |
| 75 |
* @throws DomainException if no implementation can be obtained. |
| 76 |
*/ |
| 77 |
public static function getSubscriber( |
| 78 |
$scope = null, |
| 79 |
callable $httpHandler = null, |
| 80 |
array $cacheConfig = null, |
| 81 |
CacheInterface $cache = null |
| 82 |
) { |
| 83 |
$creds = self::getCredentials($scope, $httpHandler); |
| 84 |
|
| 85 |
return new AuthTokenSubscriber($creds, $cacheConfig, $cache, $httpHandler); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface |
| 90 |
* implementation to use in this environment. |
| 91 |
* |
| 92 |
* If supplied, $scope is used to in creating the credentials instance if |
| 93 |
* this does not fallback to the compute engine defaults. |
| 94 |
* |
| 95 |
* @param string|array scope the scope of the access request, expressed |
| 96 |
* either as an Array or as a space-delimited String. |
| 97 |
* @param callable $httpHandler callback which delivers psr7 request |
| 98 |
* @param cacheConfig configuration for the cache when it's present |
| 99 |
* @param object $cache an implementation of CacheInterface |
| 100 |
* |
| 101 |
* @throws DomainException if no implementation can be obtained. |
| 102 |
*/ |
| 103 |
public static function getMiddleware( |
| 104 |
$scope = null, |
| 105 |
callable $httpHandler = null, |
| 106 |
array $cacheConfig = null, |
| 107 |
CacheInterface $cache = null |
| 108 |
) { |
| 109 |
$creds = self::getCredentials($scope, $httpHandler); |
| 110 |
|
| 111 |
return new AuthTokenMiddleware($creds, $cacheConfig, $cache, $httpHandler); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Obtains the default FetchAuthTokenInterface implementation to use |
| 116 |
* in this environment. |
| 117 |
* |
| 118 |
* If supplied, $scope is used to in creating the credentials instance if |
| 119 |
* this does not fallback to the Compute Engine defaults. |
| 120 |
* |
| 121 |
* @param string|array scope the scope of the access request, expressed |
| 122 |
* either as an Array or as a space-delimited String. |
| 123 |
* |
| 124 |
* @param callable $httpHandler callback which delivers psr7 request |
| 125 |
* @throws DomainException if no implementation can be obtained. |
| 126 |
*/ |
| 127 |
public static function getCredentials($scope = null, callable $httpHandler = null) |
| 128 |
{ |
| 129 |
$creds = CredentialsLoader::fromEnv($scope); |
| 130 |
if (!is_null($creds)) { |
| 131 |
return $creds; |
| 132 |
} |
| 133 |
$creds = CredentialsLoader::fromWellKnownFile($scope); |
| 134 |
if (!is_null($creds)) { |
| 135 |
return $creds; |
| 136 |
} |
| 137 |
if (AppIdentityCredentials::onAppEngine()) { |
| 138 |
return new AppIdentityCredentials($scope); |
| 139 |
} |
| 140 |
if (GCECredentials::onGce($httpHandler)) { |
| 141 |
return new GCECredentials(); |
| 142 |
} |
| 143 |
throw new \DomainException(self::notFound()); |
| 144 |
} |
| 145 |
|
| 146 |
private static function notFound() |
| 147 |
{ |
| 148 |
$msg = 'Could not load the default credentials. Browse to '; |
| 149 |
$msg .= 'https://developers.google.com'; |
| 150 |
$msg .= '/accounts/docs/application-default-credentials'; |
| 151 |
$msg .= ' for more information' ; |
| 152 |
return $msg; |
| 153 |
} |
| 154 |
} |
| 155 |
|