Skip to content

Commit

Permalink
add authorization header to agent
Browse files Browse the repository at this point in the history
  • Loading branch information
summer committed Aug 13, 2021
1 parent a4ef42e commit 0822d2a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 28 deletions.
18 changes: 13 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions examples/ActionHandler/action_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
8 changes: 4 additions & 4 deletions examples/EventHandler/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
}
11 changes: 6 additions & 5 deletions examples/RowMessageHandler/row_message_handler.go
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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)
}
}
6 changes: 3 additions & 3 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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())

Expand Down
15 changes: 8 additions & 7 deletions whitedew.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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))
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -182,7 +184,6 @@ func (w *WhiteDew) eventHandler(c *gin.Context) {
}
}

log.Println(jsonData)
go w.dispatchEvent(jsonData)
}

Expand Down
22 changes: 22 additions & 0 deletions whitedew_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0822d2a

Please sign in to comment.