-
Notifications
You must be signed in to change notification settings - Fork 1
/
challenge.go
47 lines (39 loc) · 1005 Bytes
/
challenge.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
package larkhertz
import (
"context"
"encoding/json"
"net/http"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/go-lark/lark"
)
// LarkChallengeHandler Lark challenge handler
func (opt *LarkMiddleware) LarkChallengeHandler() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
defer ctx.Next(c)
if !opt.checkURL(ctx) {
return
}
inputBody, ok := opt.getBody(ctx)
if !ok {
handleGetBodyError(c)
return
}
var challenge lark.EventChallengeReq
err := json.Unmarshal(inputBody, &challenge)
if err != nil || challenge.Challenge == "" {
return
}
if !opt.checkToken(ctx, challenge.Token) {
handleCheckTokenError(c)
return
}
if challenge.Type == "url_verification" {
hlog.CtxDebugf(c, "Handling challenge: %v", challenge.Challenge)
ctx.AbortWithStatusJSON(http.StatusOK, utils.H{
"challenge": challenge.Challenge,
})
}
}
}