-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
65 lines (58 loc) · 1.64 KB
/
card.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package larkgin
import (
"crypto/sha1"
"encoding/json"
"fmt"
"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 {
opt.logger.Log(c, lark.LogLevelWarn, fmt.Sprintf("Unmarshal JSON error: %v", 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)
if signature != token {
opt.logger.Log(c, lark.LogLevelError, "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)
}