| | |
| | | return "std" |
| | | return mode if mode in {"std", "pro"} else "pro" |
| | | |
| | | # ─── JWT 鉴权 ─── |
| | | # ─── 鉴权 ─── |
| | | |
| | | def _generate_token(self) -> str: |
| | | """ |
| | | 使用 Access Key / Secret Key 生成 JWT Token |
| | | 算法: HS256 |
| | | Payload: |
| | | - iss: Access Key |
| | | - iat: 签发时间 |
| | | - exp: 过期时间 |
| | | - nbf: 生效时间 |
| | | 使用 Access Key / Secret Key 生成 JWT Token (老版本鉴权方式) |
| | | """ |
| | | now = int(time.time()) |
| | | payload = { |
| | | "iss": self.access_key, |
| | | "iat": now, |
| | | "exp": now + self.token_ttl, |
| | | "nbf": now - 5, # 允许 5 秒时钟偏差 |
| | | "nbf": now - 5, |
| | | } |
| | | token = jwt.encode(payload, self.secret_key, algorithm="HS256") |
| | | return token |
| | | |
| | | def _auth_headers(self) -> dict: |
| | | """构建带 JWT 鉴权的请求头""" |
| | | token = self._generate_token() |
| | | """构建带鉴权的请求头 (优先使用新版 API Key,向下兼容老版 JWT)""" |
| | | # 如果没有配置 secret_key,说明提供的是新版的 API_KEY(直接填在 access_key 字段里) |
| | | if not self.secret_key: |
| | | token = self.access_key |
| | | else: |
| | | token = self._generate_token() |
| | | |
| | | return { |
| | | "Content-Type": "application/json", |
| | | "Authorization": f"Bearer {token}", |