|
1
|
|
|
<?php |
|
2
|
|
|
namespace mikemix\Wiziq\API; |
|
3
|
|
|
|
|
4
|
|
|
class Auth |
|
5
|
|
|
{ |
|
6
|
|
|
/** @var string */ |
|
7
|
|
|
protected $secretAcessKey; |
|
8
|
|
|
|
|
9
|
|
|
/** @var string */ |
|
10
|
|
|
protected $accessKey; |
|
11
|
|
|
|
|
12
|
|
|
/** @var int */ |
|
13
|
|
|
protected $currentTime; |
|
14
|
|
|
|
|
15
|
3 |
|
public function __construct($secretAcessKey, $accessKey, $currentTime = null) |
|
16
|
|
|
{ |
|
17
|
3 |
|
$this->secretAcessKey = $secretAcessKey; |
|
18
|
3 |
|
$this->accessKey = $accessKey; |
|
19
|
3 |
|
$this->currentTime = $currentTime ? (int)$currentTime : time(); |
|
20
|
3 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $methodName Method name |
|
24
|
|
|
* @param array $data Method payload |
|
25
|
|
|
* @return array Send ready request payload |
|
26
|
|
|
*/ |
|
27
|
3 |
|
public function preparePayload($methodName, array $data) |
|
28
|
|
|
{ |
|
29
|
3 |
|
$requestParameters = []; |
|
30
|
3 |
|
$requestParameters['access_key'] = $this->accessKey; |
|
31
|
3 |
|
$requestParameters['timestamp'] = $this->currentTime; |
|
32
|
3 |
|
$requestParameters['method'] = $methodName; |
|
33
|
|
|
|
|
34
|
3 |
|
$signatureBase = ''; |
|
35
|
3 |
|
foreach ($requestParameters as $key => $value) { |
|
36
|
3 |
|
if ($signatureBase) { |
|
37
|
3 |
|
$signatureBase .= '&'; |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
$signatureBase .= "$key=$value"; |
|
41
|
3 |
|
} |
|
42
|
|
|
|
|
43
|
3 |
|
foreach ($data as $key => $val) { |
|
44
|
3 |
|
if (is_bool($data[$key])) { |
|
45
|
1 |
|
$data[$key] = (int)$val; |
|
46
|
3 |
|
} elseif (null === $val) { |
|
47
|
1 |
|
$data[$key] = ''; |
|
48
|
1 |
|
} |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
return array_merge( |
|
52
|
3 |
|
$requestParameters, |
|
53
|
3 |
|
['signature' => base64_encode($this->hmacsha1($signatureBase))], |
|
54
|
|
|
$data |
|
55
|
3 |
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Based on code from api.wiziq.com |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $data |
|
62
|
|
|
* @param int $blocksize |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
3 |
|
protected function hmacsha1($data, $blocksize = 64) |
|
66
|
|
|
{ |
|
67
|
3 |
|
$key = urlencode($this->secretAcessKey); |
|
68
|
|
|
|
|
69
|
3 |
|
if (strlen($key) > $blocksize) { |
|
70
|
|
|
$key = pack('H*', sha1($key)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
$key = str_pad($key, $blocksize, chr(0x00)); |
|
74
|
3 |
|
$ipad = str_repeat(chr(0x36), $blocksize); |
|
75
|
3 |
|
$opad = str_repeat(chr(0x5c), $blocksize); |
|
76
|
3 |
|
return pack('H*', sha1(($key^$opad).pack('H*', sha1(($key^$ipad).$data)))); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|