-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebhook_events.go
More file actions
342 lines (297 loc) · 9.59 KB
/
webhook_events.go
File metadata and controls
342 lines (297 loc) · 9.59 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gokick
import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"net/http"
)
type Badge struct {
Text string `json:"text"`
Type string `json:"type"`
Count int `json:"count"`
}
type IdentityEvent struct {
UsernameColor string `json:"username_color"`
Badges []Badge `json:"badges"`
}
type UserEvent struct {
IsAnonymous bool `json:"is_anonymous"`
UserID int `json:"user_id"`
Username string `json:"username"`
IsVerified bool `json:"is_verified"`
ProfilePicture string `json:"profile_picture"`
ChannelSlug string `json:"channel_slug"`
Identity IdentityEvent `json:"identity"`
}
type ChatMessageEmotesEvent struct {
EmoteID int `json:"emote_id"`
Positions []struct {
Start int `json:"s"`
End int `json:"e"`
} `json:"positions"`
}
type ChatMessageEvent struct {
MessageID string `json:"message_id"`
RepliesTo struct {
MessageID string `json:"message_id"`
Sender UserEvent `json:"sender"`
Content string `json:"content"`
} `json:"replies_to"`
Broadcaster UserEvent `json:"broadcaster"`
Sender UserEvent `json:"sender"`
Content string `json:"content"`
Emotes []ChatMessageEmotesEvent `json:"emotes"`
CreatedAt string `json:"created_at"`
}
type ChannelFollowEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Follower UserEvent `json:"follower"`
}
type ChannelSubscriptionRenewalEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Subscriber UserEvent `json:"subscriber"`
Duration int `json:"duration"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
}
type ChannelSubscriptionGiftsEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Gifter UserEvent `json:"gifter"`
Giftees []UserEvent `json:"giftees"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
}
type ChannelSubscriptionCreatedEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Subscriber UserEvent `json:"subscriber"`
Duration int `json:"duration"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
}
type LivestreamStatusUpdatedEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
IsLive bool `json:"is_live"`
Title string `json:"title"`
StartedAt string `json:"started_at"`
EndedAt string `json:"ended_at"`
}
type LivestreamMetadataUpdatedEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Metadata struct {
Title string `json:"title"`
Language string `json:"language"`
HasMatureContent bool `json:"has_mature_content"`
Category struct {
ID string `json:"id"`
Name string `json:"name"`
Thumbnail string `json:"thumbnail"`
} `json:"category"`
} `json:"metadata"`
}
type ModerationBannedEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Moderator UserEvent `json:"moderator"`
BannedUser UserEvent `json:"banned_user"`
Metadata struct {
Reason string `json:"reason"`
CreatedAt string `json:"created_at"`
ExpiresAt string `json:"expires_at"`
} `json:"reason"`
}
type KicksGiftedEvent struct {
Broadcaster UserEvent `json:"broadcaster"`
Sender UserEvent `json:"sender"`
Gift struct {
Amount int `json:"amount"`
Name string `json:"name"`
Type string `json:"type"`
Tier string `json:"tier"`
Message string `json:"message"`
} `json:"gift"`
CreatedAt string `json:"created_at"`
}
type ChannelRewardRedemptionUpdatedEvent struct {
ID string `json:"id"`
UserInput string `json:"user_input"`
Status string `json:"status"`
RedeemedAt string `json:"redeemed_at"`
Reward struct {
ID string `json:"id"`
Title string `json:"title"`
Cost int `json:"cost"`
Description string `json:"description"`
} `json:"reward"`
Redeemer UserEvent `json:"redeemer"`
Broadcaster UserEvent `json:"broadcaster"`
}
// I set it as public to be able to change it in tests.
// It's not a good practice to do so, but it's the only way to do it for now.
var DefaultEventPublicKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq/+l1WnlRrGSolDMA+A8
6rAhMbQGmQ2SapVcGM3zq8ANXjnhDWocMqfWcTd95btDydITa10kDvHzw9WQOqp2
MZI7ZyrfzJuz5nhTPCiJwTwnEtWft7nV14BYRDHvlfqPUaZ+1KR4OCaO/wWIk/rQ
L/TjY0M70gse8rlBkbo2a8rKhu69RQTRsoaf4DVhDPEeSeI5jVrRDGAMGL3cGuyY
6CLKGdjVEM78g3JfYOvDU/RvfqD7L89TZ3iN94jrmWdGz34JNlEI5hqK8dd7C5EF
BEbZ5jgB8s8ReQV8H+MkuffjdAj3ajDDX3DOJMIut1lBrUVD1AaSrGCKHooWoL2e
twIDAQAB
-----END PUBLIC KEY-----`
// I set it to be able to tests without having to sign the events.
// It's not a good practice to do so, but it's the only way to do it for now.
// Do not override it in production !
var SkipSignatureValidation = false
func GetEventFromRequest(request *http.Request) (interface{}, error) {
if request == nil {
return nil, errors.New("request cannot be nil")
}
subscriptionName, err := NewSubscriptionName(request.Header.Get("X-Event-Subscription"))
if err != nil {
return nil, fmt.Errorf("failed to parse subscription name: %v", err)
}
version := request.Header.Get("X-Event-Version")
eventSignature := request.Header.Get("X-Event-Signature")
messageID := request.Header.Get("X-Event-Message-Id")
timestamp := request.Header.Get("X-Event-Timestamp")
body, err := io.ReadAll(request.Body)
if err != nil {
return nil, fmt.Errorf("failed to read body: %v", err)
}
return ValidateAndParseEvent(
subscriptionName,
version,
eventSignature,
messageID,
timestamp,
string(body),
)
}
func ValidateEvent(
header http.Header,
body []byte,
) bool {
if !SkipSignatureValidation {
payload := struct {
signature string
messageID string
timestamp string
}{
signature: header.Get("Kick-Event-Signature"),
messageID: header.Get("Kick-Event-Message-Id"),
timestamp: header.Get("Kick-Event-Message-Timestamp"),
}
signature := bytes.Join([][]byte{
[]byte(payload.messageID),
[]byte(payload.timestamp),
body,
}, []byte("."))
publicKey, _ := parsePublicKey([]byte(DefaultEventPublicKey))
err := verifyEventValidity(&publicKey, signature, []byte(payload.signature))
if err != nil {
return false
}
}
return true
}
func ValidateAndParseEvent(
subscriptionName SubscriptionName,
version string,
eventSignature string,
messageID string,
timestamp string,
body string,
) (interface{}, error) {
if !SkipSignatureValidation {
signature := []byte(fmt.Sprintf("%s.%s.%s", messageID, timestamp, body))
publicKey, err := parsePublicKey([]byte(DefaultEventPublicKey))
if err != nil {
return nil, fmt.Errorf("failed to parse public key: %v", err)
}
err = verifyEventValidity(&publicKey, signature, []byte(eventSignature))
if err != nil {
return nil, fmt.Errorf("failed to verify event validity: %v", err)
}
}
var event interface{}
if versionConstructor, ok := eventConstructors[subscriptionName]; ok {
if constructor, ok := versionConstructor[version]; ok {
event = constructor()
}
}
err := json.Unmarshal([]byte(body), &event)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal event: %v", err)
}
return event, nil
}
func parsePublicKey(key []byte) (rsa.PublicKey, error) {
block, _ := pem.Decode(key)
if block == nil {
return rsa.PublicKey{}, errors.New("failed to decode public key")
}
if block.Type != "PUBLIC KEY" {
return rsa.PublicKey{}, errors.New("not public key")
}
parsed, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return rsa.PublicKey{}, fmt.Errorf("failed to parse public key: %v", err)
}
publicKey, ok := parsed.(*rsa.PublicKey)
if !ok {
return rsa.PublicKey{}, errors.New("not expected public key interface")
}
return *publicKey, nil
}
func verifyEventValidity(publicKey *rsa.PublicKey, body []byte, signature []byte) error {
decoded := make([]byte, base64.StdEncoding.DecodedLen(len(signature)))
n, err := base64.StdEncoding.Decode(decoded, signature)
if err != nil {
return fmt.Errorf("failed to decode signature: %v", err)
}
signature = decoded[:n]
hashed := sha256.Sum256(body)
err = rsa.VerifyPKCS1v15(publicKey, crypto.SHA256, hashed[:], signature)
if err != nil {
return fmt.Errorf("failed to verify signature: %v", err)
}
return nil
}
type eventConstructor func() interface{}
var eventConstructors = map[SubscriptionName]map[string]eventConstructor{
SubscriptionNameChatMessage: {
"1": func() interface{} { return new(ChatMessageEvent) },
},
SubscriptionNameChannelFollow: {
"1": func() interface{} { return new(ChannelFollowEvent) },
},
SubscriptionNameChannelSubscriptionRenewal: {
"1": func() interface{} { return new(ChannelSubscriptionRenewalEvent) },
},
SubscriptionNameChannelSubscriptionGifts: {
"1": func() interface{} { return new(ChannelSubscriptionGiftsEvent) },
},
SubscriptionNameChannelSubscriptionCreated: {
"1": func() interface{} { return new(ChannelSubscriptionCreatedEvent) },
},
SubscriptionNameLivestreamStatusUpdated: {
"1": func() interface{} { return new(LivestreamStatusUpdatedEvent) },
},
SubscriptionNameLivestreamMetadataUpdated: {
"1": func() interface{} { return new(LivestreamMetadataUpdatedEvent) },
},
SubscriptionNameModerationBanned: {
"1": func() interface{} { return new(ModerationBannedEvent) },
},
SubscriptionNameKicksGifted: {
"1": func() interface{} { return new(KicksGiftedEvent) },
},
SubscriptionNameChannelRewardRedemptionUpdated: {
"1": func() interface{} { return new(ChannelRewardRedemptionUpdatedEvent) },
},
}