1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xmly; |
4
|
|
|
|
5
|
|
|
final class Auth |
6
|
|
|
{ |
7
|
|
|
private $appKey; |
8
|
|
|
private $appSecret; |
9
|
|
|
private $deviceID; |
10
|
|
|
|
11
|
|
|
public function __construct($appKey, $appSecret, $deviceID) |
12
|
|
|
{ |
13
|
|
|
$this->appKey = $appKey; |
14
|
|
|
$this->appSecret = $appSecret; |
15
|
|
|
$this->deviceID = $deviceID; |
16
|
|
|
} |
17
|
|
|
|
18
|
4 |
|
public function getAppKey() |
19
|
|
|
{ |
20
|
4 |
|
return $this->appKey; |
21
|
|
|
} |
22
|
|
|
|
23
|
4 |
|
public function getAppSecret() |
24
|
|
|
{ |
25
|
4 |
|
return $this->appSecret; |
26
|
|
|
} |
27
|
|
|
|
28
|
4 |
|
public function getdeviceID() |
29
|
|
|
{ |
30
|
4 |
|
return $this->deviceID; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 使用 hashKey 对 base64EncodedStr 进行HMAC-SHA1哈希得到字节数组 |
35
|
|
|
* 并对上面得到的sha1ResultBytes进行MD5得到32位字符串,即为sig |
36
|
|
|
* |
37
|
|
|
* @param string $data base64编码后的请求参数 |
38
|
|
|
* @param string $hashKey HashKey |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
4 |
|
public function sign($data, $hashKey) |
42
|
|
|
{ |
43
|
4 |
|
$hmac = hash_hmac('sha1', $data, $hashKey, true); |
44
|
4 |
|
return md5($hmac); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* 将除了sig以外的所有请求参数的原始值按照参数名的字典序排序,将排序后的参数键值对用&拼接 |
49
|
|
|
* 并对得到的字符串进行Base64编码 |
50
|
|
|
* |
51
|
|
|
* @param array $body 除sig以外的所有请求参数 |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
4 |
|
public function signWithData($body) |
55
|
|
|
{ |
56
|
4 |
|
$data = http_build_query($body); |
57
|
4 |
|
return base64_encode($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 将除了sig以外的所有请求参数的原始值按照参数名的字典序排序,将排序后的参数键值对用&拼接 |
62
|
|
|
* 并对得到的字符串进行Base64编码,如果传入 |
63
|
|
|
* |
64
|
|
|
* @param array $body 除sig以外的所有请求参数 |
65
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
4 |
|
public function signRequest($body, $serverAuthenticateStaticKey = null) |
69
|
|
|
{ |
70
|
4 |
|
$base64EncodedStr = $this->signWithData($body); |
71
|
4 |
|
$hashKey = $this->getAppSecret() . $serverAuthenticateStaticKey; |
72
|
4 |
|
return $this->sign($base64EncodedStr, $hashKey); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 通用签名生成 |
77
|
|
|
* |
78
|
|
|
* @param array $body 除sig以外的所有请求参数 |
79
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
80
|
|
|
* @return string |
81
|
|
|
* |
82
|
|
|
* @link https://open.ximalaya.com/doc/detailApi?categoryId=6&articleId=69#%E9%80%9A%E7%94%A8%E7%AD%BE%E5%90%8D%E7%94%9F%E6%88%90%E7%AE%97%E6%B3%95 |
83
|
|
|
*/ |
84
|
4 |
|
public function signature($body, $serverAuthenticateStaticKey = null) |
85
|
|
|
{ |
86
|
4 |
|
return '&sig=' . $this->signRequest($body, $serverAuthenticateStaticKey); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 拼接带 sig 参数完整的请求 URL |
91
|
|
|
* |
92
|
|
|
* @param array $body 除sig以外的所有请求参数 |
93
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
4 |
|
public function signatureURL($body, $serverAuthenticateStaticKey = null) |
97
|
|
|
{ |
98
|
4 |
|
ksort($body, SORT_STRING); |
99
|
4 |
|
$sig = $this->signature($body, $serverAuthenticateStaticKey); |
100
|
4 |
|
$requestURL = http_build_query($body); |
101
|
|
|
|
102
|
4 |
|
return $requestURL . $sig; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* 公共参数 |
107
|
|
|
* |
108
|
|
|
* @param array $body 其他请求参数 |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
4 |
|
public function commonParams(array $body = array()) |
112
|
|
|
{ |
113
|
4 |
|
$body['app_key'] = $this->getAppKey(); |
114
|
4 |
|
$body['client_os_type'] = 4; |
115
|
4 |
|
$body['nonce'] = Util::randomString(); |
116
|
4 |
|
$body['timestamp'] = Util::msecTime(); |
117
|
4 |
|
$body['device_id'] = $this->getdeviceID(); |
118
|
4 |
|
$body['server_api_version'] = '1.0.0'; |
119
|
|
|
|
120
|
4 |
|
return $body; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|