-
Notifications
You must be signed in to change notification settings - Fork 769
/
OAuth2TokenBuilder.php
292 lines (258 loc) · 7.82 KB
/
OAuth2TokenBuilder.php
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
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* 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\AdsApi\Common;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Auth\Credentials\UserRefreshCredentials;
use InvalidArgumentException;
/**
* Builds OAuth2 access token fetchers.
*
* @see FetchAuthTokenInterface
*/
final class OAuth2TokenBuilder implements AdsBuilder
{
private $configurationLoader;
private $jsonKeyFilePath;
private $scopes;
private $impersonatedEmail;
private $clientId;
private $clientSecret;
private $refreshToken;
public function __construct()
{
$this->configurationLoader = new ConfigurationLoader();
}
/**
* Reads configuration settings from the specified filepath. The filepath is
* optional, and if omitted, it will look for the default configuration
* filename in the home directory of the user running PHP.
*
* @see AdsBuilder::DEFAULT_CONFIGURATION_FILENAME
*
* @param string $path the filepath
* @return OAuth2TokenBuilder this builder populated from the configuration
* @throws InvalidArgumentException if the configuration file could not be
* found
*/
public function fromFile($path = null)
{
if ($path === null) {
$path = self::DEFAULT_CONFIGURATION_FILENAME;
}
return $this->from($this->configurationLoader->fromFile($path));
}
/**
* @see AdsBuilder::from()
*/
public function from(Configuration $configuration)
{
$this->jsonKeyFilePath = $configuration->getConfiguration('jsonKeyFilePath', 'OAUTH2');
$this->scopes = $configuration->getConfiguration('scopes', 'OAUTH2');
$this->impersonatedEmail = $configuration->getConfiguration('impersonatedEmail', 'OAUTH2');
$this->clientId = $configuration->getConfiguration('clientId', 'OAUTH2');
$this->clientSecret = $configuration->getConfiguration('clientSecret', 'OAUTH2');
$this->refreshToken = $configuration->getConfiguration('refreshToken', 'OAUTH2');
return $this;
}
/**
* Includes an absolute path to an OAuth2 JSON key file when using service
* account flow. Required and only applicable when using service account flow.
*
* @param string|null $jsonKeyFilePath
* @return OAuth2TokenBuilder this builder
*/
public function withJsonKeyFilePath($jsonKeyFilePath)
{
$this->jsonKeyFilePath = $jsonKeyFilePath;
return $this;
}
/**
* Includes OAuth2 scopes. Required and only applicable when using service
* account flow.
*
* @param string|null $scopes a space-delimited list of scopes
* @return OAuth2TokenBuilder this builder
*/
public function withScopes($scopes)
{
$this->scopes = $scopes;
return $this;
}
/**
* Includes an email of account to impersonate when using service account
* flow. Optional and only applicable when using service account flow.
*
* @param string|null $impersonatedEmail
* @return OAuth2TokenBuilder this builder
*/
public function withImpersonatedEmail($impersonatedEmail)
{
$this->impersonatedEmail = $impersonatedEmail;
return $this;
}
/**
* Includes an OAuth2 client ID. Required when using installed application or
* web application flow.
*
* @param string|null $clientId
* @return OAuth2TokenBuilder this builder
*/
public function withClientId($clientId)
{
$this->clientId = $clientId;
return $this;
}
/**
* Includes an OAuth2 client secret. Required when using installed application
* or web application flow.
*
* @param string|null $clientSecret
* @return OAuth2TokenBuilder this builder
*/
public function withClientSecret($clientSecret)
{
$this->clientSecret = $clientSecret;
return $this;
}
/**
* Includes an OAuth2 refresh token. Required when using installed application
* or web application flow.
*
* @param string|null $refreshToken
* @return OAuth2TokenBuilder this builder
*/
public function withRefreshToken($refreshToken)
{
$this->refreshToken = $refreshToken;
return $this;
}
/**
* @see AdsBuilder::build()
*/
public function build()
{
$this->defaultOptionals();
$this->validate();
if ($this->jsonKeyFilePath !== null) {
return new ServiceAccountCredentials(
$this->scopes,
$this->jsonKeyFilePath,
$this->impersonatedEmail
);
} else {
return new UserRefreshCredentials(
null,
[
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'refresh_token' => $this->refreshToken
]
);
}
}
/**
* @see AdsBuilder::defaultOptionals()
*/
public function defaultOptionals()
{
// Nothing to default for this builder.
}
/**
* @see AdsBuilder::validate()
*/
public function validate()
{
if (($this->jsonKeyFilePath !== null || $this->scopes !== null)
&& ($this->clientId !== null || $this->clientSecret !== null
|| $this->refreshToken !== null)) {
throw new InvalidArgumentException(
'Cannot have both service account '
. 'flow and installed/web application flow credential values set.'
);
}
if ($this->jsonKeyFilePath !== null || $this->scopes !== null) {
if ($this->jsonKeyFilePath === null || $this->scopes === null) {
throw new InvalidArgumentException(
'Both \'jsonKeyFilePath\' and '
. '\'scopes\' must be set when using service account flow.'
);
}
} elseif ($this->clientId === null
|| $this->clientSecret === null
|| $this->refreshToken === null) {
throw new InvalidArgumentException(
'All of \'clientId\', '
. '\'clientSecret\', and \'refreshToken\' must be set when using '
. 'installed/web application flow.'
);
}
}
/**
* Gets the JSON key file path.
*
* @return string|null
*/
public function getJsonKeyFilePath()
{
return $this->jsonKeyFilePath;
}
/**
* Gets the scopes.
*
* @return string|null
*/
public function getScopes()
{
return $this->scopes;
}
/**
* Gets the impersonated email.
*
* @return string|null
*/
public function getImpersonatedEmail()
{
return $this->impersonatedEmail;
}
/**
* Gets the client ID.
*
* @return string|null
*/
public function getClientId()
{
return $this->clientId;
}
/**
* Gets the client secret.
*
* @return string|null
*/
public function getClientSecret()
{
return $this->clientSecret;
}
/**
* Gets the refresh token.
*
* @return string|null
*/
public function getRefreshToken()
{
return $this->refreshToken;
}
}