fix(kook): KOOK 机器人鉴权用 Authorization: Bot <token> 而非 Bearer
httpclient 硬编码 Bearer,导致 KOOK bot token 被当 OAuth2 用户令牌发,
/ gateway/index 返回 401『登录环境异常』。按 KOOK reference 鉴权规范,
给共享 httpclient 增加可配置 authScheme(默认 Bearer),KOOK 用 Bot。
- httpclient: 新增 WithAuthScheme 选项 + authScheme 字段,do() 按 scheme 组装
- kook/adapter: 装配时传 WithAuthScheme("Bot")
- 同步修正 config.go 注释与 send/identity 测试断言
This commit is contained in:
1 parent
e9df928072
commit
c1bd740f7c
5 files changed
+36
-18
No files matched your search
@@ -105,9 +105,13 @@ func New(cfg Config, opts ...Option) *Adapter {
|
||||
a.baseURL = defaultBaseURL
|
||||
}
|
||||
}
|
||||
// KOOK authenticates bots with `Authorization: Bot <token>` (not "Bearer",
|
||||
// which is reserved for OAuth2 user tokens). This is mandated by the KOOK
|
||||
// reference: /doc/reference §鉴权.
|
||||
a.http = httpclient.New(
|
||||
httpclient.WithBaseURL(a.baseURL),
|
||||
httpclient.WithToken(cfg.BotToken),
|
||||
httpclient.WithAuthScheme("Bot"),
|
||||
httpclient.WithHTTPClient(a.client),
|
||||
httpclient.WithLogger(a.log),
|
||||
)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// modes: a WebSocket-gateway mode (events over a gateway WebSocket with
|
||||
// hello/heartbeat/event/resume signaling) and a Webhook mode (KOOK POSTs event
|
||||
// callbacks to a Cyrene gateway route). Outbound messages are always sent over a
|
||||
// bearer-authenticated REST API.
|
||||
// bot-token-authenticated REST API (`Authorization: Bot <token>`).
|
||||
//
|
||||
// The adapter conforms to internal/channel/adapter.PlatformAdapter, translating
|
||||
// native KOOK events into the normalized adapter.UnifiedMessage model and
|
||||
|
||||
@@ -15,8 +15,8 @@ func TestFetchBotIdentity(t *testing.T) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if got := r.Header.Get("Authorization"); got != "Bearer testtoken" {
|
||||
t.Errorf("Authorization = %q, want Bearer testtoken", got)
|
||||
if got := r.Header.Get("Authorization"); got != "Bot testtoken" {
|
||||
t.Errorf("Authorization = %q, want Bot testtoken", got)
|
||||
}
|
||||
kookOK(w, map[string]any{"id": "bot-1", "username": "bot", "nickname": "小K"})
|
||||
}))
|
||||
|
||||
@@ -38,8 +38,8 @@ func TestSendMessage_GroupText(t *testing.T) {
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if auth != "Bearer testtoken" {
|
||||
t.Errorf("Authorization = %q, want Bearer testtoken", auth)
|
||||
if auth != "Bot testtoken" {
|
||||
t.Errorf("Authorization = %q, want Bot testtoken", auth)
|
||||
}
|
||||
if body["target_id"] != "chan-1" {
|
||||
t.Errorf("target_id = %v, want chan-1", body["target_id"])
|
||||
|
||||
Reference in new issue
Block a user