1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2015-2016 WePay. |
4
|
|
|
* |
5
|
|
|
* http://opensource.org/licenses/Apache2.0 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace WePay\Signer; |
9
|
|
|
|
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
11
|
|
|
use Psr\Log\LoggerAwareTrait; |
12
|
|
|
use Skyzyx\Signer\Signer as WrappedSigner; |
13
|
|
|
use Skyzyx\Signer\SignerInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @see Skyzyx\Signer\Signer |
17
|
|
|
*/ |
18
|
|
|
class Signer implements SignerInterface, LoggerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use LoggerAwareTrait; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/**************************************************************************/ |
24
|
|
|
// PROPERTIES |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $self_key; |
28
|
|
|
|
29
|
|
|
/** @var string */ |
30
|
|
|
private $client_id; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
private $client_secret; |
34
|
|
|
|
35
|
|
|
/** @var string */ |
36
|
|
|
private $hash_algo; |
37
|
|
|
|
38
|
|
|
/** @var \Skyzyx\Signer\Signer */ |
39
|
|
|
private $signer; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/**************************************************************************/ |
43
|
|
|
// PUBLIC METHODS |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructs a new instance of this class. |
47
|
|
|
* |
48
|
|
|
* @param string $client_id A string which is the public portion of the keypair identifying the client party. |
49
|
|
|
* The pairing of the public and private portions of the keypair should only be known |
50
|
|
|
* to the client party and the signing party. |
51
|
|
|
* @param string $client_secret A string which is the private portion of the keypair identifying the client party. |
52
|
|
|
* The pairing of the public and private portions of the keypair should only be known |
53
|
|
|
* to the client party and the signing party. |
54
|
|
|
* @see http://php.net/hash_algos |
55
|
|
|
*/ |
56
|
7 |
|
public function __construct($client_id, $client_secret) |
57
|
|
|
{ |
58
|
7 |
|
$this->self_key = 'WePay'; |
59
|
7 |
|
$this->client_id = $client_id; |
60
|
7 |
|
$this->client_secret = $client_secret; |
61
|
7 |
|
$this->hash_algo = 'sha512'; |
62
|
|
|
|
63
|
7 |
|
$this->signer = new WrappedSigner( |
64
|
7 |
|
$this->self_key, |
65
|
7 |
|
$this->client_id, |
66
|
7 |
|
$this->client_secret, |
67
|
7 |
|
$this->hash_algo |
68
|
7 |
|
); |
69
|
7 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
1 |
|
public function getSelfKey() |
75
|
|
|
{ |
76
|
|
|
/** @var string */ |
77
|
1 |
|
return $this->signer->getSelfKey(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
4 |
|
public function getClientId() |
84
|
|
|
{ |
85
|
|
|
/** @var string */ |
86
|
4 |
|
return $this->signer->getClientId(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
4 |
|
public function getClientSecret() |
93
|
|
|
{ |
94
|
|
|
/** @var string */ |
95
|
4 |
|
return $this->signer->getClientSecret(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
3 |
|
public function sign(array $payload) |
102
|
|
|
{ |
103
|
3 |
|
$payload = array_merge($payload, [ |
104
|
3 |
|
'client_id' => $this->getClientId(), |
105
|
3 |
|
'client_secret' => $this->getClientSecret(), |
106
|
3 |
|
]); |
107
|
|
|
|
108
|
3 |
|
return $this->signer->sign($payload); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Signs and generates the query string URL parameters to use when making a request. |
113
|
|
|
* |
114
|
|
|
* If the `client_secret` key is provided, then it will be automatically excluded from the result. |
115
|
|
|
* |
116
|
|
|
* @param array $payload The data to generate a signature for. |
117
|
|
|
* @return string The query string parameters to append to the end of a URL. |
118
|
|
|
*/ |
119
|
2 |
|
public function generateQueryStringParams(array $payload) |
120
|
|
|
{ |
121
|
2 |
|
$token = $this->sign($payload); |
122
|
|
|
|
123
|
|
|
// Explicitly remove the client_secret if they accidentally passed it. |
124
|
2 |
|
if (isset($payload['client_secret'])) { |
125
|
1 |
|
unset($payload['client_secret']); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
2 |
|
$payload['client_id'] = $this->getClientId(); |
129
|
2 |
|
$payload['stoken'] = $token; |
130
|
|
|
|
131
|
2 |
|
ksort($payload); |
132
|
|
|
|
133
|
2 |
|
return http_build_query($payload, '', '&'); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|