1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of graze/gigya-client |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license https://github.com/graze/gigya-client/blob/master/LICENSE.md |
11
|
|
|
* @link https://github.com/graze/gigya-client |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Graze\Gigya; |
15
|
|
|
|
16
|
|
|
use BadMethodCallException; |
17
|
|
|
use Graze\Gigya\Auth\CredentialsAuthMiddleware; |
18
|
|
|
use Graze\Gigya\Auth\HttpsAuthMiddleware; |
19
|
|
|
use Graze\Gigya\Auth\OAuth2\GigyaGrant; |
20
|
|
|
use Graze\Gigya\Auth\OAuth2\OAuth2Middleware; |
21
|
|
|
use Graze\Gigya\Endpoint\Accounts; |
22
|
|
|
use Graze\Gigya\Endpoint\Audit; |
23
|
|
|
use Graze\Gigya\Endpoint\Client; |
24
|
|
|
use Graze\Gigya\Endpoint\Comments; |
25
|
|
|
use Graze\Gigya\Endpoint\DataStore; |
26
|
|
|
use Graze\Gigya\Endpoint\GameMechanics; |
27
|
|
|
use Graze\Gigya\Endpoint\IdentityStorage; |
28
|
|
|
use Graze\Gigya\Endpoint\Reports; |
29
|
|
|
use Graze\Gigya\Endpoint\Saml; |
30
|
|
|
use Graze\Gigya\Endpoint\Socialize; |
31
|
|
|
use Graze\Gigya\Response\ResponseFactoryInterface; |
32
|
|
|
use Graze\Gigya\Validation\ResponseValidatorInterface; |
33
|
|
|
use Graze\Gigya\Validation\Signature; |
34
|
|
|
use Graze\Gigya\Validation\UidSignatureValidator; |
35
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
36
|
|
|
use GuzzleHttp\ClientInterface; |
37
|
|
|
use GuzzleHttp\Handler\CurlHandler; |
38
|
|
|
use GuzzleHttp\HandlerStack; |
39
|
|
|
|
40
|
|
|
class Gigya |
41
|
|
|
{ |
42
|
|
|
const DC_EU = 'eu1'; |
43
|
|
|
const DC_US = 'us1'; |
44
|
|
|
const DC_AU = 'au1'; |
45
|
|
|
|
46
|
|
|
const NAMESPACE_AUDIT = 'audit'; |
47
|
|
|
const NAMESPACE_ACCOUNTS = 'accounts'; |
48
|
|
|
const NAMESPACE_ACCOUNTS_TFA = 'accounts.tfa'; |
49
|
|
|
const NAMESPACE_SOCIALIZE = 'socialize'; |
50
|
|
|
const NAMESPACE_COMMENTS = 'comments'; |
51
|
|
|
const NAMESPACE_GAME_MECHANICS = 'gm'; |
52
|
|
|
const NAMESPACE_REPORTS = 'reports'; |
53
|
|
|
const NAMESPACE_DATA_STORE = 'ds'; |
54
|
|
|
const NAMESPACE_IDENTITY_STORAGE = 'ids'; |
55
|
|
|
const NAMESPACE_FIDM = 'fidm'; |
56
|
|
|
const NAMESPACE_FIDM_SAML = 'fidm.saml'; |
57
|
|
|
const NAMESPACE_FIDM_SAML_IDP = 'fidm.saml.idp'; |
58
|
|
|
|
59
|
|
|
const CERTIFICATE_FILE = 'cacert.pem'; |
60
|
|
|
|
61
|
|
|
const DATE_TIME_FORMAT = 'Y-m-d\TH:i:s.uP'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Data Center ID to use. |
65
|
|
|
* |
66
|
|
|
* - us1 - for the US datacenter |
67
|
|
|
* - eu1 - for the European datacenter |
68
|
|
|
* - au1 - for the Australian datacenter |
69
|
|
|
* |
70
|
|
|
* @var string (Default: eu1) |
71
|
|
|
*/ |
72
|
|
|
protected $dataCenter; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Collection of core options to be passed to each api request. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $options = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Configuration to pass to the constructor of guzzle. |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
protected $config = []; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var ResponseValidatorInterface[] |
90
|
|
|
*/ |
91
|
|
|
protected $validators = []; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var ResponseFactoryInterface |
95
|
|
|
*/ |
96
|
|
|
protected $factory = null; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @var ClientInterface |
100
|
|
|
*/ |
101
|
|
|
private $guzzle; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @var HandlerStack |
105
|
|
|
*/ |
106
|
|
|
private $handlerStack; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $apiKey |
110
|
|
|
* @param string $secretKey |
111
|
|
|
* @param string|null $dataCenter |
112
|
|
|
* @param string|null $userKey |
113
|
|
|
* @param array $config Gigya configuration: |
114
|
|
|
* - auth <string> (Default: gigya) Type of authentication, gigya |
115
|
|
|
* (HttpsAuthMiddleware) is the default. 'credentials' provides |
116
|
|
|
* `client_id,client_secret` params, 'gigya-oauth2' uses an oauth2 access token |
117
|
|
|
* - uidValidator <bool> (Default: true) Include Uid Signature Validation |
118
|
|
|
* - factory <object> (Default: null) A ResponseFactoryInterface to use, if none is |
119
|
|
|
* provided ResponseFactory will be used |
120
|
|
|
* - guzzle <array> (Default: []) A configuration to pass to guzzle if required |
121
|
|
|
* - options <array> (Default: []) A set of options to pass to each request |
122
|
|
|
*/ |
123
|
50 |
|
public function __construct($apiKey, $secretKey, $dataCenter = null, $userKey = null, array $config = []) |
124
|
|
|
{ |
125
|
50 |
|
$guzzleConfig = (isset($config['guzzle'])) ? $config['guzzle'] : []; |
126
|
|
|
|
127
|
50 |
|
if (!isset($guzzleConfig['handler'])) { |
128
|
1 |
|
$guzzleConfig['handler'] = new HandlerStack(new CurlHandler()); |
129
|
|
|
} |
130
|
50 |
|
$this->handlerStack = $guzzleConfig['handler']; |
131
|
|
|
|
132
|
50 |
|
$this->guzzle = new GuzzleClient($guzzleConfig); |
133
|
|
|
|
134
|
50 |
|
if (isset($config['options'])) { |
135
|
3 |
|
$this->addOptions($config['options']); |
136
|
|
|
} |
137
|
50 |
|
$this->addOption('verify', __DIR__ . '/' . static::CERTIFICATE_FILE); |
138
|
|
|
|
139
|
50 |
|
$this->addHandler(HttpsAuthMiddleware::middleware($apiKey, $secretKey, $userKey)); |
140
|
50 |
|
$this->addHandler(CredentialsAuthMiddleware::middleware($apiKey, $secretKey, $userKey)); |
141
|
|
|
|
142
|
50 |
|
$auth = isset($config['auth']) ? $config['auth'] : 'gigya'; |
143
|
|
|
switch ($auth) { |
144
|
50 |
|
case 'gigya-oauth2': |
145
|
1 |
|
$this->addOption('auth', 'gigya-oauth2'); |
146
|
1 |
|
$this->addHandler(OAuth2Middleware::middleware(new GigyaGrant($this))); |
147
|
1 |
|
break; |
148
|
|
|
default: |
149
|
49 |
|
$this->addOption('auth', $auth); |
150
|
49 |
|
break; |
151
|
|
|
} |
152
|
|
|
|
153
|
50 |
|
if (!isset($config['uidValidator']) || $config['uidValidator'] === true) { |
154
|
9 |
|
$this->addValidator(new UidSignatureValidator(new Signature(), $secretKey)); |
155
|
|
|
} |
156
|
|
|
|
157
|
50 |
|
if (isset($config['factory'])) { |
158
|
41 |
|
$this->setFactory($config['factory']); |
159
|
|
|
} |
160
|
50 |
|
$this->dataCenter = $dataCenter ?: static::DC_EU; |
161
|
50 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Add an option to be passed through to Guzzle for the request. |
165
|
|
|
* |
166
|
|
|
* N.B. This will overwrite any existing options apart from query and verify |
167
|
|
|
* |
168
|
|
|
* @param string $option |
169
|
|
|
* @param mixed $value |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
50 |
|
public function addOption($option, $value) |
174
|
|
|
{ |
175
|
50 |
|
$this->options[$option] = $value; |
176
|
|
|
|
177
|
50 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add a set of options as key value pairs. These will be passed to the Guzzle request. |
182
|
|
|
* |
183
|
|
|
* N.B. This will overwrite any existing options apart from query and verify |
184
|
|
|
* |
185
|
|
|
* @param array $options |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
5 |
|
public function addOptions(array $options) |
190
|
|
|
{ |
191
|
5 |
|
foreach ($options as $option => $value) { |
192
|
5 |
|
$this->addOption($option, $value); |
193
|
|
|
} |
194
|
|
|
|
195
|
5 |
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param ResponseValidatorInterface $validator |
200
|
|
|
* |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
12 |
|
public function addValidator(ResponseValidatorInterface $validator) |
204
|
|
|
{ |
205
|
12 |
|
$this->validators[] = $validator; |
206
|
|
|
|
207
|
12 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param callable $handler |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
50 |
|
public function addHandler(callable $handler) |
216
|
|
|
{ |
217
|
50 |
|
$this->handlerStack->push($handler); |
218
|
|
|
|
219
|
50 |
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param callable $handler |
224
|
|
|
* |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
2 |
|
public function removeHandler(callable $handler) |
228
|
|
|
{ |
229
|
2 |
|
$this->handlerStack->remove($handler); |
230
|
|
|
|
231
|
2 |
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param ResponseFactoryInterface $factory |
236
|
|
|
* |
237
|
|
|
* @return $this |
238
|
|
|
*/ |
239
|
41 |
|
public function setFactory(ResponseFactoryInterface $factory) |
240
|
|
|
{ |
241
|
41 |
|
$this->factory = $factory; |
242
|
|
|
|
243
|
41 |
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param string $method |
248
|
|
|
* @param array $arguments |
249
|
|
|
* |
250
|
|
|
* @return Client |
251
|
|
|
*/ |
252
|
7 |
|
public function __call($method, array $arguments) |
253
|
|
|
{ |
254
|
7 |
|
if (count($arguments) > 0) { |
255
|
1 |
|
throw new BadMethodCallException('No Arguments should be supplied for Gigya call'); |
256
|
|
|
} |
257
|
|
|
|
258
|
6 |
|
return $this->endpointFactory($method); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $namespace |
263
|
|
|
* @param string $className |
264
|
|
|
* |
265
|
|
|
* @return Client |
266
|
|
|
*/ |
267
|
48 |
|
private function endpointFactory($namespace, $className = Client::class) |
268
|
|
|
{ |
269
|
48 |
|
return new $className( |
270
|
48 |
|
$this->guzzle, |
271
|
48 |
|
$namespace, |
272
|
48 |
|
$this->dataCenter, |
273
|
48 |
|
$this->config, |
274
|
48 |
|
$this->options, |
275
|
48 |
|
$this->validators, |
276
|
48 |
|
$this->factory |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return Accounts |
282
|
|
|
*/ |
283
|
31 |
|
public function accounts() |
284
|
|
|
{ |
285
|
31 |
|
return $this->endpointFactory(static::NAMESPACE_ACCOUNTS, Accounts::class); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return Audit |
290
|
|
|
*/ |
291
|
1 |
|
public function audit() |
292
|
|
|
{ |
293
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_AUDIT, Audit::class); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* @return Socialize |
298
|
|
|
*/ |
299
|
2 |
|
public function socialize() |
300
|
|
|
{ |
301
|
2 |
|
return $this->endpointFactory(static::NAMESPACE_SOCIALIZE, Socialize::class); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return Comments |
306
|
|
|
*/ |
307
|
1 |
|
public function comments() |
308
|
|
|
{ |
309
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_COMMENTS, Comments::class); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return GameMechanics |
314
|
|
|
*/ |
315
|
1 |
|
public function gameMechanics() |
316
|
|
|
{ |
317
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_GAME_MECHANICS, GameMechanics::class); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return Reports |
322
|
|
|
*/ |
323
|
1 |
|
public function reports() |
324
|
|
|
{ |
325
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_REPORTS, Reports::class); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return DataStore |
330
|
|
|
*/ |
331
|
1 |
|
public function dataStore() |
332
|
|
|
{ |
333
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_DATA_STORE, DataStore::class); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @return IdentityStorage |
338
|
|
|
*/ |
339
|
1 |
|
public function identityStorage() |
340
|
|
|
{ |
341
|
1 |
|
return $this->endpointFactory(static::NAMESPACE_IDENTITY_STORAGE, IdentityStorage::class); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @return Saml |
346
|
|
|
*/ |
347
|
3 |
|
public function saml() |
348
|
|
|
{ |
349
|
3 |
|
return $this->endpointFactory(static::NAMESPACE_FIDM, Saml::class); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|