ValidateAPIKey 验证API密钥是否有效
(key string)
| 187 | |
| 188 | // ValidateAPIKey 验证API密钥是否有效 |
| 189 | func ValidateAPIKey(key string) (*APIKey, error) { |
| 190 | // 验证密钥前缀 |
| 191 | if !strings.HasPrefix(key, APIKeyPrefix) { |
| 192 | return nil, errors.New("无效的API密钥格式") |
| 193 | } |
| 194 | |
| 195 | // 获取API密钥 |
| 196 | apiKey, err := GetAPIKeyByKey(key) |
| 197 | if err != nil { |
| 198 | return nil, errors.New("无效的API密钥") |
| 199 | } |
| 200 | |
| 201 | // 检查状态 |
| 202 | if apiKey.Status != APIKeyStatusActive { |
| 203 | return nil, errors.New("API密钥已被禁用") |
| 204 | } |
| 205 | |
| 206 | // 检查过期时间 |
| 207 | if apiKey.ExpiresAt != nil && time.Now().After(*apiKey.ExpiresAt) { |
| 208 | return nil, errors.New("API密钥已过期") |
| 209 | } |
| 210 | |
| 211 | return apiKey, nil |
| 212 | } |
| 213 | |
| 214 | // GenerateAPIKey 生成新的API密钥 |
| 215 | func GenerateAPIKey(eid int64, creatorID int64) string { |
nothing calls this directly
no test coverage detected