4
4
package webhook
5
5
6
6
import (
7
- "bytes"
8
7
"context"
9
8
"crypto/hmac"
10
9
"crypto/sha256"
11
10
"encoding/base64"
12
11
"fmt"
13
12
"net/http"
14
- "strconv"
15
13
"strings"
16
14
"time"
17
15
18
16
webhook_model "code.gitea.io/gitea/models/webhook"
19
17
"code.gitea.io/gitea/modules/git"
20
- "code.gitea.io/gitea/modules/json"
21
18
api "code.gitea.io/gitea/modules/structs"
22
19
webhook_module "code.gitea.io/gitea/modules/webhook"
23
20
)
@@ -193,27 +190,17 @@ func (feishuConvertor) WorkflowJob(p *api.WorkflowJobPayload) (FeishuPayload, er
193
190
return newFeishuTextPayload (text ), nil
194
191
}
195
192
196
- // GenSign generates a signature for Feishu webhook
193
+ // feishuGenSign generates a signature for Feishu webhook
197
194
// 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
200
197
stringToSign := fmt .Sprintf ("%d\n %s" , timestamp , secret )
201
-
202
198
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 ))
210
200
}
211
201
212
202
func 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 )
217
204
if err != nil {
218
205
return nil , nil , err
219
206
}
@@ -222,35 +209,10 @@ func newFeishuRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_mo
222
209
if w .Secret != "" {
223
210
timestamp := time .Now ().Unix ()
224
211
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 )
249
213
}
250
- req .Header .Set ("Content-Type" , "application/json" )
251
214
252
- // Add default headers
253
- return req , body , addDefaultHeaders (req , []byte (w .Secret ), w , t , body )
215
+ return sendHTTPRequest (payload , w , t , false )
254
216
}
255
217
256
218
func init () {
0 commit comments