44package webhook
55
66import (
7- "bytes"
87 "context"
98 "crypto/hmac"
109 "crypto/sha256"
1110 "encoding/base64"
1211 "fmt"
1312 "net/http"
14- "strconv"
1513 "strings"
1614 "time"
1715
1816 webhook_model "code.gitea.io/gitea/models/webhook"
1917 "code.gitea.io/gitea/modules/git"
20- "code.gitea.io/gitea/modules/json"
2118 api "code.gitea.io/gitea/modules/structs"
2219 webhook_module "code.gitea.io/gitea/modules/webhook"
2320)
@@ -193,27 +190,17 @@ func (feishuConvertor) WorkflowJob(p *api.WorkflowJobPayload) (FeishuPayload, er
193190 return newFeishuTextPayload (text ), nil
194191}
195192
196- // GenSign generates a signature for Feishu webhook
193+ // feishuGenSign generates a signature for Feishu webhook
197194// https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot
198- func GenSign (secret string , timestamp int64 ) ( string , error ) {
199- // timestamp + key do sha256, then base64 encode
195+ func feishuGenSign (secret string , timestamp int64 ) string {
196+ // key="{ timestamp}\n{secret}", then hmac- sha256, then base64 encode
200197 stringToSign := fmt .Sprintf ("%d\n %s" , timestamp , secret )
201-
202198 h := hmac .New (sha256 .New , []byte (stringToSign ))
203- _ , err := h .Write ([]byte {})
204- if err != nil {
205- return "" , err
206- }
207-
208- signature := base64 .StdEncoding .EncodeToString (h .Sum (nil ))
209- return signature , nil
199+ return base64 .StdEncoding .EncodeToString (h .Sum (nil ))
210200}
211201
212202func newFeishuRequest (_ context.Context , w * webhook_model.Webhook , t * webhook_model.HookTask ) (* http.Request , []byte , error ) {
213- var pc payloadConvertor [FeishuPayload ] = feishuConvertor {}
214-
215- // Get the payload first
216- payload , err := newPayload (pc , []byte (t .PayloadContent ), t .EventType )
203+ payload , err := newPayload (feishuConvertor {}, []byte (t .PayloadContent ), t .EventType )
217204 if err != nil {
218205 return nil , nil , err
219206 }
@@ -222,35 +209,10 @@ func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
222209 if w .Secret != "" {
223210 timestamp := time .Now ().Unix ()
224211 payload .Timestamp = timestamp
225-
226- // Generate signature
227- sign , err := GenSign (w .Secret , timestamp )
228- if err != nil {
229- return nil , nil , err
230- }
231- payload .Sign = sign
232- }
233-
234- // Marshal the payload
235- body , err := json .MarshalIndent (payload , "" , " " )
236- if err != nil {
237- return nil , nil , err
238- }
239-
240- // Create the request
241- method := w .HTTPMethod
242- if method == "" {
243- method = http .MethodPost
244- }
245-
246- req , err := http .NewRequest (method , w .URL , bytes .NewReader (body ))
247- if err != nil {
248- return nil , nil , err
212+ payload .Sign = feishuGenSign (w .Secret , timestamp )
249213 }
250- req .Header .Set ("Content-Type" , "application/json" )
251214
252- // Add default headers
253- return req , body , addDefaultHeaders (req , []byte (w .Secret ), w , t , body )
215+ return sendHTTPRequest (payload , w , t , false )
254216}
255217
256218func init () {
0 commit comments