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 Closure; |
17
|
|
|
use Psr\Http\Message\RequestInterface; |
18
|
|
|
|
19
|
|
|
class HttpsAuthMiddleware |
20
|
|
|
{ |
21
|
|
|
const AUTH_NAME = 'gigya'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $apiKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $secret; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var null|string |
35
|
|
|
*/ |
36
|
|
|
private $userKey; |
37
|
|
|
/** |
38
|
|
|
* @var callable |
39
|
|
|
*/ |
40
|
|
|
private $nextHandler; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param callable $nextHandler |
44
|
|
|
* @param string $apiKey |
45
|
|
|
* @param string $secret |
46
|
|
|
* @param string|null $userKey |
47
|
|
|
*/ |
48
|
13 |
|
public function __construct(callable $nextHandler, $apiKey, $secret, $userKey = null) |
49
|
|
|
{ |
50
|
13 |
|
$this->nextHandler = $nextHandler; |
51
|
13 |
|
$this->apiKey = $apiKey; |
52
|
13 |
|
$this->secret = $secret; |
53
|
13 |
|
$this->userKey = $userKey; |
54
|
13 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Inject the https auth into the query string |
58
|
|
|
* |
59
|
|
|
* @param RequestInterface $request |
60
|
|
|
* @param array $options |
61
|
|
|
* |
62
|
|
|
* @return Closure |
63
|
|
|
*/ |
64
|
12 |
|
public function __invoke(RequestInterface $request, array $options) |
65
|
|
|
{ |
66
|
12 |
|
if ($request->getUri()->getScheme() == 'https' && $options['auth'] == static::AUTH_NAME) { |
67
|
10 |
|
$params = array_merge( |
68
|
10 |
|
\GuzzleHttp\Psr7\parse_query($request->getBody()), |
69
|
|
|
[ |
70
|
10 |
|
'apiKey' => $this->apiKey, |
71
|
10 |
|
'secret' => $this->secret, |
72
|
|
|
] |
73
|
|
|
); |
74
|
|
|
|
75
|
10 |
|
if ((bool)$this->userKey) { |
76
|
3 |
|
$params['userKey'] = $this->userKey; |
77
|
|
|
} |
78
|
|
|
$request = $request |
79
|
10 |
|
->withBody(\GuzzleHttp\Psr7\stream_for(http_build_query($params))); |
80
|
|
|
} |
81
|
12 |
|
$fn = $this->nextHandler; |
82
|
12 |
|
return $fn($request, $options); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
1 |
|
public function getApiKey() |
89
|
|
|
{ |
90
|
1 |
|
return $this->apiKey; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
1 |
|
public function getSecret() |
97
|
|
|
{ |
98
|
1 |
|
return $this->secret; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string|null |
103
|
|
|
*/ |
104
|
1 |
|
public function getUserKey() |
105
|
|
|
{ |
106
|
1 |
|
return $this->userKey; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return a middleware handler function for https Authentication |
111
|
|
|
* |
112
|
|
|
* @param string $apiKey |
113
|
|
|
* @param string $secret |
114
|
|
|
* @param string|null $userKey |
115
|
|
|
* |
116
|
|
|
* @return Closure |
117
|
|
|
*/ |
118
|
|
|
public static function middleware($apiKey, $secret, $userKey = null) |
119
|
|
|
{ |
120
|
54 |
|
return function (callable $handler) use ($apiKey, $secret, $userKey) { |
121
|
12 |
|
return new static($handler, $apiKey, $secret, $userKey); |
122
|
54 |
|
}; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|