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
|
|
View Code Duplication |
class CredentialsAuth implements SubscriberInterface |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
const AUTH_NAME = 'credentials'; |
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
|
55 |
|
public function __construct($apiKey, $secret, $userKey = null) |
45
|
|
|
{ |
46
|
55 |
|
$this->apiKey = $apiKey; |
47
|
55 |
|
$this->secret = $secret; |
48
|
55 |
|
$this->userKey = $userKey; |
49
|
55 |
|
} |
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
|
2 |
|
$query = $request->getQuery(); |
71
|
2 |
|
$query['client_id'] = $this->userKey ?: $this->apiKey; |
72
|
2 |
|
$query['client_secret'] = $this->secret; |
73
|
2 |
|
} |
74
|
11 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
1 |
|
public function getApiKey() |
80
|
|
|
{ |
81
|
1 |
|
return $this->apiKey; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getSecret() |
88
|
|
|
{ |
89
|
1 |
|
return $this->secret; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
1 |
|
public function getUserKey() |
96
|
|
|
{ |
97
|
1 |
|
return $this->userKey; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.