-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge.go
47 lines (42 loc) · 1.05 KB
/
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 larkgin
import (
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-lark/lark"
)
// LarkChallengeHandler Lark challenge handler
func (opt LarkMiddleware) LarkChallengeHandler() gin.HandlerFunc {
return func(c *gin.Context) {
defer c.Next()
if opt.enableURLBinding && c.Request.URL.String() != opt.urlPrefix {
// url not match just pass
return
}
body, err := fetchBody(c)
if err != nil {
return
}
var inputBody = body
if opt.enableEncryption {
decryptedData, err := opt.decodeEncryptedJSON(body)
if err != nil {
opt.logger.Log(c, lark.LogLevelError, fmt.Sprintf("Decrypt failed: %v", err))
return
}
inputBody = decryptedData
}
var challenge lark.EventChallenge
err = json.Unmarshal(inputBody, &challenge)
if err != nil {
return
}
if challenge.Type == "url_verification" {
opt.logger.Log(c, lark.LogLevelInfo, fmt.Sprintf("Handling challenge: %s", challenge.Challenge))
c.AbortWithStatusJSON(http.StatusOK, gin.H{
"challenge": challenge.Challenge,
})
}
}
}