|
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\Auth; |
|
15
|
|
|
|
|
16
|
|
|
use GuzzleHttp\Event\BeforeEvent; |
|
17
|
|
|
use GuzzleHttp\Event\RequestEvents; |
|
18
|
|
|
use GuzzleHttp\Event\SubscriberInterface; |
|
19
|
|
|
|
|
20
|
|
|
class HttpsAuth implements SubscriberInterface |
|
21
|
|
|
{ |
|
22
|
|
|
const AUTH_NAME = 'gigya'; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $apiKey; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $secret; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var null|string |
|
36
|
|
|
*/ |
|
37
|
|
|
private $userKey; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $apiKey |
|
41
|
|
|
* @param string $secret |
|
42
|
|
|
* @param string|null $userKey |
|
43
|
|
|
*/ |
|
44
|
52 |
|
public function __construct($apiKey, $secret, $userKey = null) |
|
45
|
|
|
{ |
|
46
|
52 |
|
$this->apiKey = $apiKey; |
|
47
|
52 |
|
$this->secret = $secret; |
|
48
|
52 |
|
$this->userKey = $userKey; |
|
49
|
52 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
8 |
|
public function getEvents() |
|
57
|
|
|
{ |
|
58
|
8 |
|
return ['before' => ['sign', RequestEvents::SIGN_REQUEST]]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Add the authentication params to the request. |
|
63
|
|
|
* |
|
64
|
|
|
* @param BeforeEvent $event |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function sign(BeforeEvent $event) |
|
67
|
|
|
{ |
|
68
|
11 |
|
$request = $event->getRequest(); |
|
69
|
11 |
|
if ($request->getScheme() == 'https' && $request->getConfig()->get('auth') == static::AUTH_NAME) { |
|
70
|
9 |
|
$query = $request->getQuery(); |
|
71
|
9 |
|
$query['apiKey'] = $this->apiKey; |
|
72
|
9 |
|
$query['secret'] = $this->secret; |
|
73
|
9 |
|
if ((bool) $this->userKey) { |
|
74
|
2 |
|
$query['userKey'] = $this->userKey; |
|
75
|
2 |
|
} |
|
76
|
9 |
|
} |
|
77
|
11 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
39 |
|
public function getApiKey() |
|
83
|
|
|
{ |
|
84
|
39 |
|
return $this->apiKey; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
39 |
|
public function getSecret() |
|
91
|
|
|
{ |
|
92
|
39 |
|
return $this->secret; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string|null |
|
97
|
|
|
*/ |
|
98
|
39 |
|
public function getUserKey() |
|
99
|
|
|
{ |
|
100
|
39 |
|
return $this->userKey; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|