From 0822d2a8ae66ab394ed61a1e6b80ccf8794c4493 Mon Sep 17 00:00:00 2001 From: summer Date: Fri, 13 Aug 2021 11:08:19 +0800 Subject: [PATCH] add authorization header to agent --- api.go | 18 ++++++++++----- examples/ActionHandler/action_handler.go | 8 +++---- examples/EventHandler/event_handler.go | 8 +++---- .../RowMessageHandler/row_message_handler.go | 11 +++++----- session.go | 6 ++--- whitedew.go | 15 +++++++------ whitedew_test.go | 22 +++++++++++++++++++ 7 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 whitedew_test.go diff --git a/api.go b/api.go index c420a89..571697a 100644 --- a/api.go +++ b/api.go @@ -3,16 +3,18 @@ package whitedew import ( "encoding/json" "fmt" - "github.com/parnurzeal/gorequest" "log" + + "github.com/parnurzeal/gorequest" ) type Agent struct { - URL string + URL string + AccessToken string } -func NewAgent(URL string) *Agent { - return &Agent{URL: URL} +func NewAgent(URL string, accessToken string) *Agent { + return &Agent{URL: URL, AccessToken: accessToken} } type MessageTemplate struct { @@ -37,7 +39,13 @@ func (a *Agent) PostMessage(action string, param map[string]interface{}, autoEsc } uri := fmt.Sprintf("%s/%s", a.URL, action) data, _ := json.Marshal(param) - _, body, errs := gorequest.New().Post(uri).Set("Content-Type", "application/json").Send(string(data)).EndBytes() + _, body, errs := gorequest. + New(). + Post(uri). + Set("Content-Type", "application/json"). + Set("Authorization", a.AccessToken). + Send(string(data)). + EndBytes() if errs != nil { log.Fatalln(errs) } diff --git a/examples/ActionHandler/action_handler.go b/examples/ActionHandler/action_handler.go index f8dc374..4052f12 100644 --- a/examples/ActionHandler/action_handler.go +++ b/examples/ActionHandler/action_handler.go @@ -4,9 +4,9 @@ import ( "github.com/m1dsummer/whitedew" ) -type PluginPing struct {} +type PluginPing struct{} -func (p PluginPing)Init(w *whitedew.WhiteDew) { +func (p PluginPing) Init(w *whitedew.WhiteDew) { w.SetActionHandler("/ping", Callback) } @@ -16,7 +16,7 @@ func Callback(session *whitedew.Session) { func main() { w := whitedew.New() - w.SetCQServer("http://localhost:60001") + w.SetCQServer("http://localhost:60001", "access-key") w.AddPlugin(PluginPing{}) w.Run("/event", 60000) -} \ No newline at end of file +} diff --git a/examples/EventHandler/event_handler.go b/examples/EventHandler/event_handler.go index 5d31a6b..e9e20b2 100644 --- a/examples/EventHandler/event_handler.go +++ b/examples/EventHandler/event_handler.go @@ -2,9 +2,9 @@ package main import "github.com/m1dsummer/whitedew" -type Plugin struct {} +type Plugin struct{} -func (p Plugin)Init(w *whitedew.WhiteDew) { +func (p Plugin) Init(w *whitedew.WhiteDew) { w.SetEventHandler("poke", Handler) } @@ -17,7 +17,7 @@ func Handler(agent *whitedew.Agent, event whitedew.Event) { func main() { w := whitedew.New() - w.SetCQServer("http://localhost:60001") + w.SetCQServer("http://localhost:60001", "access-key") w.AddPlugin(Plugin{}) w.Run("/event", 60000) -} \ No newline at end of file +} diff --git a/examples/RowMessageHandler/row_message_handler.go b/examples/RowMessageHandler/row_message_handler.go index ff355a4..f6b6eb6 100644 --- a/examples/RowMessageHandler/row_message_handler.go +++ b/examples/RowMessageHandler/row_message_handler.go @@ -1,13 +1,14 @@ package main import ( - "github.com/m1dsummer/whitedew" "strings" + + "github.com/m1dsummer/whitedew" ) -type Plugin struct {} +type Plugin struct{} -func (p Plugin)Init(w *whitedew.WhiteDew) { +func (p Plugin) Init(w *whitedew.WhiteDew) { w.SetRowMsgHandler(Handler) } @@ -28,7 +29,7 @@ func Handler(session *whitedew.Session) { func main() { w := whitedew.New() - w.SetCQServer("http://localhost:60001") + w.SetCQServer("http://localhost:60001", "access-key") w.AddPlugin(Plugin{}) w.Run("/event", 60000) -} \ No newline at end of file +} diff --git a/session.go b/session.go index aeb9364..2b1102a 100644 --- a/session.go +++ b/session.go @@ -10,7 +10,7 @@ type Session struct { Sender Sender StartTime time.Time Message Message - Env map[string]interface{} + Env map[string]interface{} IsFirstRun bool Action string Agent *Agent @@ -47,7 +47,7 @@ func (s SessionManager) Destroy(uid int64) { delete(s.Pool, uid) } -func (s SessionManager) NewSession(url string, msg Message) *Session { +func (s SessionManager) NewSession(url string, accessToken string, msg Message) *Session { oldSession := s.Get(msg.GetSender().GetId()) if oldSession != nil { oldSession.IsFirstRun = false @@ -59,7 +59,7 @@ func (s SessionManager) NewSession(url string, msg Message) *Session { session.Message = msg session.IsFirstRun = true session.Env = make(map[string]interface{}) - session.Agent = NewAgent(url) + session.Agent = NewAgent(url, accessToken) session.Sender = msg.GetSender() session.Action = ParseAction(msg.GetContent()) diff --git a/whitedew.go b/whitedew.go index 9a9ed19..d08d046 100644 --- a/whitedew.go +++ b/whitedew.go @@ -35,9 +35,10 @@ func NewServer() *Server { } type Config struct { - CQHost string - Secret string - CacheDir string + CQHost string + Secret string + CQAccessToken string + CacheDir string } type WhiteDew struct { @@ -59,8 +60,9 @@ func New() *WhiteDew { return &w } -func (w *WhiteDew) SetCQServer(url string) { +func (w *WhiteDew) SetCQServer(url string, accessToken string) { w.Config.CQHost = url + w.Config.CQAccessToken = accessToken } func (w *WhiteDew) SetAuth(secret string) { @@ -117,7 +119,7 @@ func (w *WhiteDew) universalEventHandler(msgStr []byte) { handlers := w.eventPool[noticeType] if handlers != nil { for _, handler := range handlers { - agent := NewAgent(w.Config.CQHost) + agent := NewAgent(w.Config.CQHost, w.Config.CQAccessToken) handler.Handle(agent, rowEvent.(Event)) } } @@ -141,7 +143,7 @@ func (w *WhiteDew) dispatchEvent(msgStr []byte) { var session *Session switch postType { case "message": - session = w.sessionManager.NewSession(w.Config.CQHost, msg) + session = w.sessionManager.NewSession(w.Config.CQHost, w.Config.CQAccessToken, msg) w.messageEventHandler(msgStr, session) case "notice", "request": w.universalEventHandler(msgStr) @@ -182,7 +184,6 @@ func (w *WhiteDew) eventHandler(c *gin.Context) { } } - log.Println(jsonData) go w.dispatchEvent(jsonData) } diff --git a/whitedew_test.go b/whitedew_test.go new file mode 100644 index 0000000..af42fa7 --- /dev/null +++ b/whitedew_test.go @@ -0,0 +1,22 @@ +package whitedew + +import ( + "testing" +) + +type PluginPing struct{} + +func (p PluginPing) Init(w *WhiteDew) { + w.SetActionHandler("/ping", Callback) +} + +func Callback(session *Session) { + session.PostPrivateMessage(session.Sender.GetId(), "pong!") +} + +func TestRun(t *testing.T) { + w := New() + w.SetCQServer("http://127.0.0.1:60001", "tQwUiHbnJEEZ7aHb9F8B2BvujWMciyyu") + w.AddPlugin(PluginPing{}) + w.Run("/event", 60000) +}