-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package larkgin | ||
|
||
import ( | ||
"crypto/sha1" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-lark/lark" | ||
) | ||
|
||
// GetCardCallback from gin context | ||
func (opt LarkMiddleware) GetCardCallback(c *gin.Context) (*lark.EventCardCallback, bool) { | ||
if card, ok := c.Get(opt.cardKey); ok { | ||
msg, ok := card.(lark.EventCardCallback) | ||
return &msg, ok | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
// LarkCardHandler card callback handler | ||
// Encryption is automatically ignored, because it's not supported officially | ||
func (opt LarkMiddleware) LarkCardHandler() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
defer c.Next() | ||
body, err := fetchBody(c) | ||
if err != nil { | ||
return | ||
} | ||
var inputBody = body | ||
|
||
var event lark.EventCardCallback | ||
err = json.Unmarshal(inputBody, &event) | ||
if err != nil { | ||
log.Println(err) | ||
return | ||
} | ||
if opt.enableTokenVerification { | ||
nonce := c.Request.Header.Get("X-Lark-Request-Nonce") | ||
timestamp := c.Request.Header.Get("X-Lark-Request-Timestamp") | ||
signature := c.Request.Header.Get("X-Lark-Signature") | ||
token := opt.cardSignature(nonce, timestamp, string(body), opt.verificationToken) | ||
log.Println(token, signature) | ||
if signature != token { | ||
log.Println("Token verification failed") | ||
return | ||
} | ||
} | ||
c.Set(opt.cardKey, event) | ||
} | ||
} | ||
|
||
func (opt LarkMiddleware) cardSignature(nonce string, timestamp string, body string, token string) string { | ||
var b strings.Builder | ||
b.WriteString(timestamp) | ||
b.WriteString(nonce) | ||
b.WriteString(token) | ||
b.WriteString(body) | ||
bs := []byte(b.String()) | ||
h := sha1.New() | ||
h.Write(bs) | ||
bs = h.Sum(nil) | ||
return fmt.Sprintf("%x", bs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package larkgin | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-lark/lark" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCardCallback(t *testing.T) { | ||
var ( | ||
r = gin.Default() | ||
middleware = NewLarkMiddleware() | ||
|
||
ok bool | ||
event *lark.EventCardCallback | ||
) | ||
r.Use(middleware.LarkCardHandler()) | ||
|
||
card := map[string]interface{}{ | ||
"app_id": "fake_app_id", | ||
"open_id": "fake_open_id", | ||
"user_id": "f123f456", | ||
"open_message_id": "om_8169c75fbae56c6bebb7e914b92253b4", | ||
"open_chat_id": "fake_oc_id", | ||
"tenant_key": "1068767a888dd740", | ||
"token": "c-2fa8cd831bc83e6350b5be32eb24d2863be4bc5b", | ||
"action": map[string]interface{}{ | ||
"tag": "button", | ||
"value": map[string]interface{}{"action": "1"}, | ||
}, | ||
} | ||
r.POST("/", func(c *gin.Context) { | ||
event, ok = middleware.GetCardCallback(c) | ||
t.Log(event, ok) | ||
}) | ||
performRequest(r, "POST", "/", card) | ||
assert.True(t, ok) | ||
if assert.NotNil(t, event) { | ||
assert.Equal(t, "button", event.Action.Tag) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters