-
Notifications
You must be signed in to change notification settings - Fork 1
/
card.go
66 lines (58 loc) · 1.6 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
66
package larkhertz
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"strings"
"github.com/cloudwego/hertz/pkg/app"
"github.com/go-lark/lark"
)
// GetCardCallback from gin context
func (opt LarkMiddleware) GetCardCallback(c *app.RequestContext) (*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() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
defer ctx.Next(c)
inputBody, ok := opt.getBody(ctx)
if !ok {
handleGetBodyError(c)
return
}
var event lark.EventCardCallback
err := json.Unmarshal(inputBody, &event)
if err != nil {
handleJSONUnmarshalError(c, err)
return
}
if opt.enableTokenVerification {
nonce := ctx.Request.Header.Get("X-Lark-Request-Nonce")
timestamp := ctx.Request.Header.Get("X-Lark-Request-Timestamp")
signature := ctx.Request.Header.Get("X-Lark-Signature")
token := opt.cardSignature(nonce, timestamp, string(inputBody), opt.verificationToken)
if signature != token {
return
}
}
ctx.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)
}