1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xmly; |
4
|
|
|
|
5
|
|
|
final class Auth |
6
|
|
|
{ |
7
|
|
|
private $appKey; |
8
|
|
|
private $appSecret; |
9
|
|
|
private $deviceID; |
10
|
|
|
|
11
|
1 |
|
public function __construct($appKey, $appSecret, $deviceID) |
12
|
|
|
{ |
13
|
1 |
|
$this->appKey = $appKey; |
14
|
1 |
|
$this->appSecret = $appSecret; |
15
|
1 |
|
$this->deviceID = $deviceID; |
16
|
1 |
|
} |
17
|
|
|
|
18
|
10 |
|
public function getAppKey() |
19
|
|
|
{ |
20
|
10 |
|
return $this->appKey; |
21
|
|
|
} |
22
|
|
|
|
23
|
9 |
|
public function getAppSecret() |
24
|
|
|
{ |
25
|
9 |
|
return $this->appSecret; |
26
|
|
|
} |
27
|
|
|
|
28
|
9 |
|
public function getdeviceID() |
29
|
|
|
{ |
30
|
9 |
|
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
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
6 |
|
public function sign($data, $hashKey) |
43
|
|
|
{ |
44
|
6 |
|
$hmac = hash_hmac('sha1', $data, $hashKey, true); |
45
|
6 |
|
return md5($hmac); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 将除了sig以外的所有请求参数的原始值按照参数名的字典序排序,将排序后的参数键值对用&拼接 |
50
|
|
|
* 并对得到的字符串进行Base64编码 |
51
|
|
|
* |
52
|
|
|
* @param array $body 除sig以外的所有请求参数 |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
6 |
|
public function signWithData($body) |
57
|
|
|
{ |
58
|
|
|
// 重要:由于参数中会有特殊字符,比如, % +,http_build_query 会进行URL编码,所以需要URL解码下 |
59
|
6 |
|
$data = urldecode(http_build_query($body)); |
60
|
6 |
|
return base64_encode($data); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* 将除了sig以外的所有请求参数的原始值按照参数名的字典序排序,将排序后的参数键值对用&拼接 |
65
|
|
|
* 并对得到的字符串进行Base64编码,如果传入 |
66
|
|
|
* |
67
|
|
|
* @param array $body 除sig以外的所有请求参数 |
68
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
6 |
|
public function signRequest($body, $serverAuthenticateStaticKey = null) |
73
|
|
|
{ |
74
|
6 |
|
$base64EncodedStr = $this->signWithData($body); |
75
|
6 |
|
$hashKey = $this->getAppSecret() . $serverAuthenticateStaticKey; |
76
|
6 |
|
return $this->sign($base64EncodedStr, $hashKey); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 通用签名生成 |
81
|
|
|
* |
82
|
|
|
* @param array $body 除sig以外的所有请求参数 |
83
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
* |
87
|
|
|
* @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 |
88
|
|
|
*/ |
89
|
6 |
|
public function signature($body, $serverAuthenticateStaticKey = null) |
90
|
|
|
{ |
91
|
6 |
|
return '&sig=' . $this->signRequest($body, $serverAuthenticateStaticKey); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* 拼接带 sig 参数完整的请求 URL |
96
|
|
|
* |
97
|
|
|
* @param array $body 除sig以外的所有请求参数 |
98
|
|
|
* @param string $serverAuthenticateStaticKey 服务端密钥 |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
6 |
|
public function signatureURL($body, $serverAuthenticateStaticKey = null) |
103
|
|
|
{ |
104
|
6 |
|
ksort($body, SORT_STRING); |
105
|
6 |
|
$sig = $this->signature($body, $serverAuthenticateStaticKey); |
106
|
6 |
|
$requestURL = http_build_query($body); |
107
|
6 |
|
return $requestURL . $sig; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 公共参数 |
112
|
|
|
* |
113
|
|
|
* |
114
|
|
|
* @param array $body 其他请求参数 |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
6 |
|
public function commonParams(array $body = array()) |
119
|
|
|
{ |
120
|
6 |
|
$body['app_key'] = $this->getAppKey(); |
121
|
6 |
|
$body['client_os_type'] = 4; |
122
|
6 |
|
$body['nonce'] = Util::randomString(); |
123
|
6 |
|
$body['timestamp'] = Util::msecTime(); |
124
|
6 |
|
$body['device_id'] = $this->getdeviceID(); |
125
|
6 |
|
$body['server_api_version'] = '1.0.0'; |
126
|
|
|
|
127
|
6 |
|
return $body; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|