FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
google-auth-library-php/src/ApplicationDefaultCredentials.php at main · nidcode/google-auth-library-php · GitHub
nidcode
/
google-auth-library-php
Public
forked from
googleapis/google-auth-library-php
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
google-auth-library-php
/
src
/
ApplicationDefaultCredentials.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
391 lines (358 loc) · 15 KB
Breadcrumbs
google-auth-library-php
/
src
/
ApplicationDefaultCredentials.php
Copy path
File metadata and controls
391 lines (358 loc) · 15 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
/*
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace
Google
\
Auth
;
use
DomainException
;
use
Google
\
Auth
\
Credentials
\
AppIdentityCredentials
;
use
Google
\
Auth
\
Credentials
\
GCECredentials
;
use
Google
\
Auth
\
Credentials
\
ImpersonatedServiceAccountCredentials
;
use
Google
\
Auth
\
Credentials
\
ServiceAccountCredentials
;
use
Google
\
Auth
\
Credentials
\
UserRefreshCredentials
;
use
Google
\
Auth
\
HttpHandler
\
HttpClientCache
;
use
Google
\
Auth
\
HttpHandler
\
HttpHandlerFactory
;
use
Google
\
Auth
\
Logging
\
StdOutLogger
;
use
Google
\
Auth
\
Middleware
\
AuthTokenMiddleware
;
use
Google
\
Auth
\
Middleware
\
ProxyAuthTokenMiddleware
;
use
Google
\
Auth
\
Subscriber
\
AuthTokenSubscriber
;
use
GuzzleHttp
\
Client
;
use
InvalidArgumentException
;
use
Psr
\
Cache
\
CacheItemPoolInterface
;
use
Psr
\
Log
\
LoggerInterface
;
/**
* ApplicationDefaultCredentials obtains the default credentials for
* authorizing a request to a Google service.
*
* Application Default Credentials are described here:
* https://developers.google.com/accounts/docs/application-default-credentials
*
* This class implements the search for the application default credentials as
* described in the link.
*
* It provides three factory methods:
* - #get returns the computed credentials object
* - #getSubscriber returns an AuthTokenSubscriber built from the credentials object
* - #getMiddleware returns an AuthTokenMiddleware built from the credentials object
*
* This allows it to be used as follows with GuzzleHttp\Client:
*
* ```
* use Google\Auth\ApplicationDefaultCredentials;
* use GuzzleHttp\Client;
* use GuzzleHttp\HandlerStack;
*
* $middleware = ApplicationDefaultCredentials::getMiddleware(
* 'https://www.googleapis.com/auth/taskqueue'
* );
* $stack = HandlerStack::create();
* $stack->push($middleware);
*
* $client = new Client([
* 'handler' => $stack,
* 'base_uri' => 'https://www.googleapis.com/taskqueue/v1beta2/projects/',
* 'auth' => 'google_auth' // authorize all requests
* ]);
*
* $res = $client->get('myproject/taskqueues/myqueue');
* ```
*/
class
ApplicationDefaultCredentials
{
private
const
SDK_DEBUG_ENV_VAR
=
'
GOOGLE_SDK_PHP_LOGGING
'
;
/**
* @deprecated
*
* Obtains an AuthTokenSubscriber that uses the default FetchAuthTokenInterface
* implementation to use in this environment.
*
* If supplied, $scope is used to in creating the credentials instance if
* this does not fallback to the compute engine defaults.
*
* @param string|string[] $scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @return AuthTokenSubscriber
* @throws DomainException if no implementation can be obtained.
*/
public
static
function
getSubscriber
(
// @phpstan-ignore-line
$
scope
=
null
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
) {
$
creds
=
self
::
getCredentials
(
$
scope
,
$
httpHandler
,
$
cacheConfig
,
$
cache
);
/** @phpstan-ignore-next-line */
return
new
AuthTokenSubscriber
(
$
creds
,
$
httpHandler
);
}
/**
* Obtains an AuthTokenMiddleware that uses the default FetchAuthTokenInterface
* implementation to use in this environment.
*
* If supplied, $scope is used to in creating the credentials instance if
* this does not fallback to the compute engine defaults.
*
* @param string|string[] $scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @param string $quotaProject specifies a project to bill for access
* charges associated with the request.
* @return AuthTokenMiddleware
* @throws DomainException if no implementation can be obtained.
*/
public
static
function
getMiddleware
(
$
scope
=
null
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
,
$
quotaProject
=
null
) {
$
creds
=
self
::
getCredentials
(
$
scope
,
$
httpHandler
,
$
cacheConfig
,
$
cache
,
$
quotaProject
);
return
new
AuthTokenMiddleware
(
$
creds
,
$
httpHandler
);
}
/**
* Obtains the default FetchAuthTokenInterface implementation to use
* in this environment.
*
* @param string|string[] $scope the scope of the access request, expressed
* either as an Array or as a space-delimited String.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @param string|null $quotaProject specifies a project to bill for access
* charges associated with the request.
* @param string|string[]|null $defaultScope The default scope to use if no
* user-defined scopes exist, expressed either as an Array or as a
* space-delimited string.
* @param string|null $universeDomain Specifies a universe domain to use for the
* calling client library.
* @param null|false|LoggerInterface $logger A PSR3 compliant LoggerInterface.
*
* @return FetchAuthTokenInterface
* @throws DomainException if no implementation can be obtained.
*/
public
static
function
getCredentials
(
$
scope
=
null
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
,
$
quotaProject
=
null
,
$
defaultScope
=
null
,
?
string
$
universeDomain
=
null
,
null
|
false
|
LoggerInterface
$
logger
=
null
,
) {
$
creds
=
null
;
$
jsonKey
= CredentialsLoader::
fromEnv
()
?: CredentialsLoader::
fromWellKnownFile
();
$
anyScope
=
$
scope
?:
$
defaultScope
;
if
(!
$
httpHandler
) {
if
(!(
$
client
= HttpClientCache::
getHttpClient
())) {
$
client
=
new
Client
();
HttpClientCache::
setHttpClient
(
$
client
);
}
$
httpHandler
= HttpHandlerFactory::
build
(
$
client
,
$
logger
);
}
if
(
is_null
(
$
quotaProject
)) {
// if a quota project isn't specified, try to get one from the env var
$
quotaProject
= CredentialsLoader::
quotaProjectFromEnv
();
}
if
(!
is_null
(
$
jsonKey
)) {
if
(
$
quotaProject
) {
$
jsonKey
[
'
quota_project_id
'
] =
$
quotaProject
;
}
if
(
$
universeDomain
) {
$
jsonKey
[
'
universe_domain
'
] =
$
universeDomain
;
}
$
creds
= CredentialsLoader::
makeCredentials
(
$
scope
,
$
jsonKey
,
$
defaultScope
);
}
elseif
(AppIdentityCredentials::
onAppEngine
() && !GCECredentials::
onAppEngineFlexible
()) {
$
creds
=
new
AppIdentityCredentials
(
$
anyScope
);
}
elseif
(
self
::
onGce
(
$
httpHandler
,
$
cacheConfig
,
$
cache
)) {
$
creds
=
new
GCECredentials
(
null
,
$
anyScope
,
null
,
$
quotaProject
,
null
,
$
universeDomain
);
$
creds
->
setIsOnGce
(
true
);
// save the credentials a trip to the metadata server
}
if
(
is_null
(
$
creds
)) {
throw
new
DomainException
(
self
::
notFound
());
}
if
(!
is_null
(
$
cache
)) {
$
creds
=
new
FetchAuthTokenCache
(
$
creds
,
$
cacheConfig
,
$
cache
);
}
return
$
creds
;
}
/**
* Obtains an AuthTokenMiddleware which will fetch an ID token to use in the
* Authorization header. The middleware is configured with the default
* FetchAuthTokenInterface implementation to use in this environment.
*
* If supplied, $targetAudience is used to set the "aud" on the resulting
* ID token.
*
* @param string $targetAudience The audience for the ID token.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @return AuthTokenMiddleware
* @throws DomainException if no implementation can be obtained.
*/
public
static
function
getIdTokenMiddleware
(
$
targetAudience
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
) {
$
creds
=
self
::
getIdTokenCredentials
(
$
targetAudience
,
$
httpHandler
,
$
cacheConfig
,
$
cache
);
return
new
AuthTokenMiddleware
(
$
creds
,
$
httpHandler
);
}
/**
* Obtains an ProxyAuthTokenMiddleware which will fetch an ID token to use in the
* Authorization header. The middleware is configured with the default
* FetchAuthTokenInterface implementation to use in this environment.
*
* If supplied, $targetAudience is used to set the "aud" on the resulting
* ID token.
*
* @param string $targetAudience The audience for the ID token.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @return ProxyAuthTokenMiddleware
* @throws DomainException if no implementation can be obtained.
*/
public
static
function
getProxyIdTokenMiddleware
(
$
targetAudience
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
) {
$
creds
=
self
::
getIdTokenCredentials
(
$
targetAudience
,
$
httpHandler
,
$
cacheConfig
,
$
cache
);
return
new
ProxyAuthTokenMiddleware
(
$
creds
,
$
httpHandler
);
}
/**
* Obtains the default FetchAuthTokenInterface implementation to use
* in this environment, configured with a $targetAudience for fetching an ID
* token.
*
* @param string $targetAudience The audience for the ID token.
* @param callable|null $httpHandler callback which delivers psr7 request
* @param array<mixed>|null $cacheConfig configuration for the cache when it's present
* @param CacheItemPoolInterface|null $cache A cache implementation, may be
* provided if you have one already available for use.
* @return FetchAuthTokenInterface
* @throws DomainException if no implementation can be obtained.
* @throws InvalidArgumentException if JSON "type" key is invalid
*/
public
static
function
getIdTokenCredentials
(
$
targetAudience
,
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
) {
$
creds
=
null
;
$
jsonKey
= CredentialsLoader::
fromEnv
()
?: CredentialsLoader::
fromWellKnownFile
();
if
(!
$
httpHandler
) {
if
(!(
$
client
= HttpClientCache::
getHttpClient
())) {
$
client
=
new
Client
();
HttpClientCache::
setHttpClient
(
$
client
);
}
$
httpHandler
= HttpHandlerFactory::
build
(
$
client
);
}
if
(!
is_null
(
$
jsonKey
)) {
if
(!
array_key_exists
(
'
type
'
,
$
jsonKey
)) {
throw
new
\
InvalidArgumentException
(
'
json key is missing the type field
'
);
}
$
creds
=
match
(
$
jsonKey
[
'
type
'
]) {
'
authorized_user
'
=>
new
UserRefreshCredentials
(
null
,
$
jsonKey
,
$
targetAudience
),
'
impersonated_service_account
'
=>
new
ImpersonatedServiceAccountCredentials
(
null
,
$
jsonKey
,
$
targetAudience
),
'
service_account
'
=>
new
ServiceAccountCredentials
(
null
,
$
jsonKey
,
null
,
$
targetAudience
),
default
=>
throw
new
InvalidArgumentException
(
'
invalid value in the type field
'
)
};
}
elseif
(
self
::
onGce
(
$
httpHandler
,
$
cacheConfig
,
$
cache
)) {
$
creds
=
new
GCECredentials
(
null
,
null
,
$
targetAudience
);
$
creds
->
setIsOnGce
(
true
);
// save the credentials a trip to the metadata server
}
if
(
is_null
(
$
creds
)) {
throw
new
DomainException
(
self
::
notFound
());
}
if
(!
is_null
(
$
cache
)) {
$
creds
=
new
FetchAuthTokenCache
(
$
creds
,
$
cacheConfig
,
$
cache
);
}
return
$
creds
;
}
/**
* Returns a StdOutLogger instance
*
* @internal
*
* @return null|LoggerInterface
*/
public
static
function
getDefaultLogger
():
null
|
LoggerInterface
{
$
loggingFlag
=
getenv
(
self
::
SDK_DEBUG_ENV_VAR
);
// Env var is not set
if
(
empty
(
$
loggingFlag
)) {
return
null
;
}
$
loggingFlag
=
strtolower
(
$
loggingFlag
);
// Env Var is not true
if
(
$
loggingFlag
!==
'
true
'
) {
if
(
$
loggingFlag
!==
'
false
'
) {
trigger_error
(
'
The
'
.
self
::
SDK_DEBUG_ENV_VAR
.
'
is set, but it is set to another value than false or true. Logging is disabled
'
);
}
return
null
;
}
return
new
StdOutLogger
();
}
/**
* @return string
*/
private
static
function
notFound
()
{
$
msg
=
'
Your default credentials were not found. To set up
'
;
$
msg
.=
'
Application Default Credentials, see
'
;
$
msg
.=
'
https://cloud.google.com/docs/authentication/external/set-up-adc
'
;
return
$
msg
;
}
/**
* @param callable|null $httpHandler
* @param array<mixed>|null $cacheConfig
* @param CacheItemPoolInterface|null $cache
* @return bool
*/
private
static
function
onGce
(
?
callable
$
httpHandler
=
null
,
?
array
$
cacheConfig
=
null
,
?
CacheItemPoolInterface
$
cache
=
null
) {
$
gceCacheConfig
= [];
foreach
([
'
lifetime
'
,
'
prefix
'
]
as
$
key
) {
if
(
isset
(
$
cacheConfig
[
'
gce_
'
.
$
key
])) {
$
gceCacheConfig
[
$
key
] =
$
cacheConfig
[
'
gce_
'
.
$
key
];
}
}
return
(
new
GCECache
(
$
gceCacheConfig
,
$
cache
))->
onGce
(
$
httpHandler
);
}
}
Back
|
FazBrowse Home
|
New Git URL