refactor(voice): 语音配置改用新版 API Key 鉴权(弃旧版 appid+token)

用户提醒:火山新版走 API Key 鉴权,不用旧版 appid+access_token。新版 WS 握手只带两个
header——Authorization(Bearer <APIKey>) + X-Api-Resource-Id。

配置从 {appid, access_token, 2×resource-id, voice} 收敛为 {api_key, 2×resource-id,
voice}:APIKey 走 secrets AES 加密入库;ASREnabled/TTSEnabled 改为只看 api_key+对应
resource-id。admin 配置页两个字段并一个 API Key 字段,清单同步改为新版口径。build+tsc 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-21 17:06:22 +08:00
parent 618184689e
commit a28ae49b6a
4 changed files with 36 additions and 50 deletions
@@ -28,12 +28,11 @@ func (h *Handler) loadVoiceConfig(ctx context.Context) voice.Config {
return c.DecryptFromStore()
}
// AdminGetVoiceConfig: GET /api/v1/admin/voice —— 回显语音配置(token 明文,RequireAdmin 已拦)。
// AdminGetVoiceConfig: GET /api/v1/admin/voice —— 回显语音配置(api_key 明文,RequireAdmin 已拦)。
func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
cfg := h.loadVoiceConfig(c.Request.Context())
c.JSON(http.StatusOK, gin.H{
"appid": cfg.AppID,
"access_token": cfg.AccessToken,
"api_key": cfg.APIKey,
"asr_resource_id": cfg.ASRResourceID,
"tts_resource_id": cfg.TTSResourceID,
"tts_voice_type": cfg.TTSVoiceType,
@@ -42,11 +41,10 @@ func (h *Handler) AdminGetVoiceConfig(c *gin.Context) {
})
}
// AdminSaveVoiceConfig: PUT /api/v1/admin/voice —— 保存语音配置(token 空串=沿用已存)。
// AdminSaveVoiceConfig: PUT /api/v1/admin/voice —— 保存语音配置(api_key 空串=沿用已存)。
func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
var b struct {
AppID string `json:"appid"`
AccessToken string `json:"access_token"`
APIKey string `json:"api_key"`
ASRResourceID string `json:"asr_resource_id"`
TTSResourceID string `json:"tts_resource_id"`
TTSVoiceType string `json:"tts_voice_type"`
@@ -56,13 +54,12 @@ func (h *Handler) AdminSaveVoiceConfig(c *gin.Context) {
return
}
ctx := c.Request.Context()
token := strings.TrimSpace(b.AccessToken)
if token == "" {
token = h.loadVoiceConfig(ctx).AccessToken // 留空=沿用已存
key := strings.TrimSpace(b.APIKey)
if key == "" {
key = h.loadVoiceConfig(ctx).APIKey // 留空=沿用已存
}
cfg := voice.Config{
AppID: strings.TrimSpace(b.AppID),
AccessToken: token,
APIKey: key,
ASRResourceID: strings.TrimSpace(b.ASRResourceID),
TTSResourceID: strings.TrimSpace(b.TTSResourceID),
TTSVoiceType: strings.TrimSpace(b.TTSVoiceType),
+15 -18
View File
@@ -2,48 +2,45 @@ package voice
import "github.com/sundynix/sundynix-shared/secrets"
// Config 是语音交互(火山引擎豆包语音)所需配置。AccessToken 加密入库、后台明文回显(同微信配置)。
// Config 是语音交互(火山引擎豆包语音)所需配置。APIKey 加密入库、后台明文回显(同微信配置)。
//
// 火山鉴权走 WS 握手 headerX-Api-App-Key(AppID) / X-Api-Access-Key(AccessToken) /
// X-Api-Resource-Id(区分服务,ASR 与双向 TTS 各一个)。AppID/Token 通常同一应用共用。
// 用**新版 API Key 鉴权**(非旧版 appid+access_token):WS 握手只带两个 header——
// AuthorizationBearer <APIKey>+ X-Api-Resource-Id(区分服务,ASR 与双向 TTS 各一个)。
// 端点/音频格式(PCM 16k 单声道等)由代码固定,不入用户配置。
type Config struct {
AppID string `json:"appid"` // 火山应用 AppIDX-Api-App-Key
AccessToken string `json:"access_token"` // 火山 Access TokenX-Api-Access-Key,密文入库)
APIKey string `json:"api_key"` // 新版 API KeyAuthorization: Bearer <APIKey>,密文入库
ASRResourceID string `json:"asr_resource_id"` // 流式语音识别 X-Api-Resource-Id
TTSResourceID string `json:"tts_resource_id"` // 双向流式 TTS X-Api-Resource-Id
TTSVoiceType string `json:"tts_voice_type"` // 音色(如 zh_male_… / BV700_streaming
}
// ASREnabled / TTSEnabled 分别报告耳朵、嘴是否配齐(可各自独立启用)。
func (c Config) ASREnabled() bool {
return c.AppID != "" && c.AccessToken != "" && c.ASRResourceID != ""
}
// ASREnabled / TTSEnabled 分别报告耳朵、嘴是否配齐(共用同一 API Key,各自还需对应 resource-id)。
func (c Config) ASREnabled() bool { return c.APIKey != "" && c.ASRResourceID != "" }
func (c Config) TTSEnabled() bool {
return c.AppID != "" && c.AccessToken != "" && c.TTSResourceID != "" && c.TTSVoiceType != ""
return c.APIKey != "" && c.TTSResourceID != "" && c.TTSVoiceType != ""
}
// Enabled 报告语音整体是否可用(耳朵 + 嘴都配齐)。
func (c Config) Enabled() bool { return c.ASREnabled() && c.TTSEnabled() }
// EncryptedForStore 返回 AccessToken 已加密的副本,用于落库。
// EncryptedForStore 返回 APIKey 已加密的副本,用于落库。
func (c Config) EncryptedForStore() (Config, error) {
if c.AccessToken == "" {
if c.APIKey == "" {
return c, nil
}
enc, err := secrets.Encrypt(c.AccessToken)
enc, err := secrets.Encrypt(c.APIKey)
if err != nil {
return c, err
}
c.AccessToken = enc
c.APIKey = enc
return c, nil
}
// DecryptFromStore 把库内密文 AccessToken 还原为明文。
// DecryptFromStore 把库内密文 APIKey 还原为明文。
func (c Config) DecryptFromStore() Config {
if c.AccessToken != "" {
if plain, err := secrets.Decrypt(c.AccessToken); err == nil {
c.AccessToken = plain
if c.APIKey != "" {
if plain, err := secrets.Decrypt(c.APIKey); err == nil {
c.APIKey = plain
}
}
return c