Skip to content

Commit 1c5aafa

Browse files
authored
Add files via upload
1 parent 4323daf commit 1c5aafa

1 file changed

Lines changed: 266 additions & 0 deletions

File tree

apidoc.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# 阿里云ESA A方案鉴权URL生成器 API 文档
2+
3+
## 概述
4+
5+
本API用于生成符合阿里云边缘安全加速(ESA) A方案规范的鉴权密钥(auth_key),用于保护CDN资源访问安全。
6+
7+
**鉴权算法**:MD5(Path-timestamp-rand-uid-密钥)
8+
9+
---
10+
11+
## 基础信息
12+
13+
- **协议**:HTTPS
14+
- **基础URL**`https://auth-api.mxzysoa.com`
15+
- **端口**:443 (默认)
16+
17+
---
18+
19+
## API端点
20+
21+
### 生成鉴权密钥
22+
23+
#### 端点
24+
`GET /generate-auth-url`
25+
`POST /generate-auth-url`
26+
27+
#### 请求参数
28+
29+
| 参数名 | 类型 | 必填 | 默认值 | 说明 |
30+
|--------|------|------|--------|------|
31+
| filePath | string || - | 资源文件路径,需以 `/` 开头,例如:`/index.json` |
32+
| expireTime | integer || 3600 | 鉴权URL有效时长,单位:秒 |
33+
| uid | string || 0 | 用户ID,根据业务需求设置 |
34+
| rawKey | boolean || true (GET) / false (POST) | 是否直接返回auth_key字符串,而不返回JSON格式 |
35+
36+
#### 响应格式
37+
38+
##### 1. rawKey=true (默认GET请求)
39+
40+
**Content-Type**: text/plain
41+
42+
直接返回auth_key字符串,格式:`{timestamp}-{rand}-{uid}-{md5hash}`
43+
44+
**示例响应**
45+
```
46+
1776791568-dH1VMcHAcH7XgXh9nlEQXIkNVohmD5iv-0-c670c5f30c9650ac20d70f621cb82df9
47+
```
48+
49+
##### 2. rawKey=false (默认POST请求)
50+
51+
**Content-Type**: application/json
52+
53+
返回包含完整信息的JSON对象。
54+
55+
**示例响应**
56+
```json
57+
{
58+
"authKey": "1776791568-dH1VMcHAcH7XgXh9nlEQXIkNVohmD5iv-0-c670c5f30c9650ac20d70f621cb82df9",
59+
"timestamp": 1776791568,
60+
"rand": "dH1VMcHAcH7XgXh9nlEQXIkNVohmD5iv",
61+
"uid": "0",
62+
"md5hash": "c670c5f30c9650ac20d70f621cb82df9"
63+
}
64+
```
65+
66+
---
67+
68+
## 调用示例
69+
70+
### 1. GET请求 (推荐,简单快速)
71+
72+
#### cURL示例
73+
```bash
74+
curl "https://auth-api.mxzysoa.com/generate-auth-url?filePath=/index.json"
75+
```
76+
77+
#### JavaScript (Fetch API)
78+
```javascript
79+
async function getAuthKey() {
80+
const response = await fetch('https://auth-api.mxzysoa.com/generate-auth-url?filePath=/index.json');
81+
const authKey = await response.text();
82+
console.log('生成的auth_key:', authKey);
83+
return authKey;
84+
}
85+
86+
// 使用生成的auth_key构建完整URL
87+
async function buildAuthUrl() {
88+
const authKey = await getAuthKey();
89+
const fullUrl = `https://dl1.mxzysoa.com/index.json?auth_key=${authKey}`;
90+
console.log('完整鉴权URL:', fullUrl);
91+
return fullUrl;
92+
}
93+
```
94+
95+
#### Python (requests)
96+
```python
97+
import requests
98+
99+
response = requests.get('https://auth-api.mxzysoa.com/generate-auth-url', params={
100+
'filePath': '/index.json'
101+
})
102+
auth_key = response.text
103+
print('生成的auth_key:', auth_key)
104+
```
105+
106+
### 2. POST请求 (适合需要更多控制的场景)
107+
108+
#### cURL示例
109+
```bash
110+
curl -X POST "https://auth-api.mxzysoa.com/generate-auth-url" \
111+
-H "Content-Type: application/json" \
112+
-d '{
113+
"filePath": "/index.json",
114+
"expireTime": 3600,
115+
"uid": "0",
116+
"rawKey": true
117+
}'
118+
```
119+
120+
#### JavaScript (Fetch API)
121+
```javascript
122+
async function getAuthKey() {
123+
const response = await fetch('https://auth-api.mxzysoa.com/generate-auth-url', {
124+
method: 'POST',
125+
headers: {
126+
'Content-Type': 'application/json'
127+
},
128+
body: JSON.stringify({
129+
filePath: '/index.json',
130+
expireTime: 3600,
131+
uid: '0',
132+
rawKey: true
133+
})
134+
});
135+
136+
const authKey = await response.text();
137+
console.log('生成的auth_key:', authKey);
138+
return authKey;
139+
}
140+
```
141+
142+
#### Python (requests)
143+
```python
144+
import requests
145+
146+
response = requests.post('https://auth-api.mxzysoa.com/generate-auth-url', json={
147+
'filePath': '/index.json',
148+
'expireTime': 3600,
149+
'uid': '0',
150+
'rawKey': true
151+
})
152+
auth_key = response.text
153+
print('生成的auth_key:', auth_key)
154+
```
155+
156+
---
157+
158+
## 响应字段说明
159+
160+
### auth_key结构
161+
`{timestamp}-{rand}-{uid}-{md5hash}`
162+
163+
| 字段 | 类型 | 说明 |
164+
|------|------|------|
165+
| timestamp | integer | 十进制Unix时间戳,表示auth_key失效时间 |
166+
| rand | string | 32位随机字符串,由大小写字母和数字组成 |
167+
| uid | string | 用户ID,默认为0 |
168+
| md5hash | string | 32位MD5哈希值,小写字母 |
169+
170+
### JSON响应字段
171+
| 字段 | 类型 | 说明 |
172+
|------|------|------|
173+
| authKey | string | 完整的auth_key字符串 |
174+
| timestamp | integer | 时间戳 |
175+
| rand | string | 随机字符串 |
176+
| uid | string | 用户ID |
177+
| md5hash | string | MD5哈希值 |
178+
179+
---
180+
181+
## 错误处理
182+
183+
| 状态码 | 说明 | 响应 |
184+
|--------|------|------|
185+
| 400 | 缺少必要参数 | `请提供文件路径` (rawKey=true) 或 `{"error": "请提供文件路径"}` |
186+
| 500 | 服务器内部错误 | 错误信息 (rawKey=true) 或 `{"error": "错误信息"}` |
187+
188+
---
189+
190+
## 使用示例
191+
192+
### 完整示例:生成并使用鉴权URL
193+
194+
```javascript
195+
async function generateAndUseAuthUrl() {
196+
try {
197+
// 1. 调用API获取auth_key
198+
const response = await fetch('https://auth-api.mxzysoa.com/generate-auth-url?filePath=/video/test.mp4&expireTime=7200');
199+
const authKey = await response.text();
200+
201+
console.log('生成的auth_key:', authKey);
202+
203+
// 2. 构建完整的鉴权URL
204+
const authUrl = `https://dl1.mxzysoa.com/video/test.mp4?auth_key=${authKey}`;
205+
206+
console.log('完整鉴权URL:', authUrl);
207+
208+
// 3. 使用鉴权URL访问资源
209+
const resourceResponse = await fetch(authUrl);
210+
if (resourceResponse.ok) {
211+
const data = await resourceResponse.json();
212+
console.log('资源访问成功:', data);
213+
} else {
214+
console.error('资源访问失败:', resourceResponse.status);
215+
}
216+
} catch (error) {
217+
console.error('错误:', error);
218+
}
219+
}
220+
221+
// 调用函数
222+
generateAndUseAuthUrl();
223+
```
224+
225+
---
226+
227+
## 技术细节
228+
229+
### 签名算法
230+
231+
```
232+
md5hash = MD5(Path - timestamp - rand - uid - secretKey)
233+
```
234+
235+
**参数**
236+
- `Path`:资源路径,以`/`开头
237+
- `timestamp`:当前时间戳 + 过期时间(秒)
238+
- `rand`:32位随机字符串
239+
- `uid`:用户ID
240+
- `secretKey`:预设密钥(`w8Fp2Lk9vR5mN7zT4wY6bJ1cH3gX5qP0`
241+
242+
### 安全说明
243+
244+
1. **密钥保密**:请勿在前端代码中硬编码密钥
245+
2. **时效性**:设置合适的expireTime,避免auth_key长期有效
246+
3. **HTTPS**:生产环境建议使用HTTPS协议
247+
4. **服务端验证**:API应部署在受信任的服务器上
248+
249+
---
250+
251+
## 注意事项
252+
253+
1. **filePath必须以/开头**:例如 `/index.json`,不要写成 `index.json`
254+
2. **时间戳格式**:使用十进制Unix时间戳(秒)
255+
3. **随机字符串**:不包含`-`符号
256+
4. **uid**:根据业务需求设置,默认为0
257+
5. **expireTime**:根据实际需求设置,默认为3600秒(1小时)
258+
259+
---
260+
261+
## 更新日志
262+
263+
### v1.0.0
264+
- 初始版本
265+
- 支持阿里云ESA A方案鉴权
266+
- 支持GET和POST请求

0 commit comments

Comments
 (0)