feat: enhance Authorization header handling with Header Override support

This commit is contained in:
Li-Xingyu
2026-01-25 04:28:11 +08:00
parent 51751c9101
commit 1e0ba95dc0
2 changed files with 40 additions and 14 deletions

View File

@@ -184,9 +184,25 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, header *http.Header, info *
header.Set("api-key", info.ApiKey)
return nil
}
// 自定义渠道类型完全跳过默认 Authorization 设置,由 Header Override 控制
if info.ChannelType == constant.ChannelTypeCustom {
// 自定义渠道不设置默认 Authorization完全由 Header Override 控制
return nil
}
if info.ChannelType == constant.ChannelTypeOpenAI && "" != info.Organization {
header.Set("OpenAI-Organization", info.Organization)
}
// 检查 Header Override 是否已设置 Authorization如果已设置则跳过默认设置
// 这样可以避免在 Header Override 应用时被覆盖(虽然 Header Override 会在之后应用,但这里作为额外保护)
hasAuthOverride := false
if len(info.HeadersOverride) > 0 {
for k := range info.HeadersOverride {
if strings.EqualFold(k, "Authorization") {
hasAuthOverride = true
break
}
}
}
if info.RelayMode == relayconstant.RelayModeRealtime {
swp := c.Request.Header.Get("Sec-WebSocket-Protocol")
if swp != "" {
@@ -201,10 +217,14 @@ func (a *Adaptor) SetupRequestHeader(c *gin.Context, header *http.Header, info *
//req.Header.Set("Sec-Websocket-Version", c.Request.Header.Get("Sec-Websocket-Version"))
} else {
header.Set("openai-beta", "realtime=v1")
header.Set("Authorization", "Bearer "+info.ApiKey)
if !hasAuthOverride {
header.Set("Authorization", "Bearer "+info.ApiKey)
}
}
} else {
header.Set("Authorization", "Bearer "+info.ApiKey)
if !hasAuthOverride {
header.Set("Authorization", "Bearer "+info.ApiKey)
}
}
if info.ChannelType == constant.ChannelTypeOpenRouter {
header.Set("HTTP-Referer", "https://www.newapi.ai")