Skip to content

Commit ba87aaf

Browse files
authored
Merge branch 'main' into goals-event-sub
2 parents 1602fbf + be0c569 commit ba87aaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+293
-928
lines changed

cmd/events.go

+39-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/twitchdev/twitch-cli/internal/events/verify"
1313
)
1414

15+
const websubDeprecationNotice = "Halt! It appears you are trying to use WebSub, which has been deprecated. For more information, see: https://discuss.dev.twitch.tv/t/deprecation-of-websub-based-webhooks/32152"
16+
1517
var (
1618
isAnonymous bool
1719
forwardAddress string
@@ -33,7 +35,7 @@ var (
3335

3436
var eventCmd = &cobra.Command{
3537
Use: "event",
36-
Short: "Used to interface with Event services, such as Eventsub and Websub.",
38+
Short: "Used to interface with EventSub topics.",
3739
}
3840

3941
var triggerCmd = &cobra.Command{
@@ -61,6 +63,9 @@ var verifyCmd = &cobra.Command{
6163
ValidArgs: events.ValidTriggers(),
6264
Run: verifyCmdRun,
6365
Example: `twitch event verify-subscription subscribe`,
66+
Aliases: []string{
67+
"verify",
68+
},
6469
}
6570

6671
var retriggerCmd = &cobra.Command{
@@ -78,7 +83,7 @@ func init() {
7883
// flags for forwarding functionality/changing payloads
7984
triggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
8085
triggerCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to /EventSub.\nSupported values: %s", events.ValidTransports()))
81-
triggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")
86+
triggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length.")
8287

8388
// per-topic flags
8489
triggerCmd.Flags().StringVarP(&toUser, "to-user", "t", "", "User ID of the receiver of the event. For example, the user that receives a follow. In most contexts, this is the broadcaster.")
@@ -96,13 +101,13 @@ func init() {
96101
// retrigger flags
97102
retriggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
98103
retriggerCmd.Flags().StringVarP(&eventID, "id", "i", "", "ID of the event to be refired.")
99-
retriggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")
104+
retriggerCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length.")
100105
retriggerCmd.MarkFlagRequired("id")
101106

102107
// verify-subscription flags
103108
verifyCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
104109
verifyCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to EventSub.\nSupported values: %s", events.ValidTransports()))
105-
verifyCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC.")
110+
verifyCmd.Flags().StringVarP(&secret, "secret", "s", "", "Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length.")
106111
verifyCmd.MarkFlagRequired("forward-address")
107112
}
108113

@@ -112,6 +117,16 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
112117
return
113118
}
114119

120+
if transport == "websub" {
121+
fmt.Println(websubDeprecationNotice)
122+
return
123+
}
124+
125+
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
126+
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
127+
return
128+
}
129+
115130
// Validate that the forward address is actually a URL
116131
if len(forwardAddress) > 0 {
117132
_, err := url.ParseRequestURI(forwardAddress)
@@ -149,6 +164,16 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
149164
}
150165

151166
func retriggerCmdRun(cmd *cobra.Command, args []string) {
167+
if transport == "websub" {
168+
fmt.Println(websubDeprecationNotice)
169+
return
170+
}
171+
172+
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
173+
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
174+
return
175+
}
176+
152177
res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{
153178
ForwardAddress: forwardAddress,
154179
Secret: secret,
@@ -167,6 +192,16 @@ func verifyCmdRun(cmd *cobra.Command, args []string) {
167192
return
168193
}
169194

195+
if transport == "websub" {
196+
fmt.Println(websubDeprecationNotice)
197+
return
198+
}
199+
200+
if secret != "" && (len(secret) < 10 || len(secret) > 100) {
201+
fmt.Println("Invalid secret provided. Secrets must be between 10-100 characters")
202+
return
203+
}
204+
170205
// Validate that the forward address is actually a URL
171206
if len(forwardAddress) > 0 {
172207
_, err := url.ParseRequestURI(forwardAddress)

docs/event.md

+28-29
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,24 @@ Used to either create or send mock events for use with local webhooks testing.
6060

6161
**Flags**
6262

63-
Flag | Shorthand | Description | Example | Required? (Y/N)
64-
---------------------|-----------|---------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------|-----------------
65-
`--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N
66-
`--transport` | `-T` | The method used to send events. Default is `eventsub`, but can send using `websub`. | `-T websub` | N
67-
`--to-user` | `-t` | Denotes the receiver's TUID of the event, usually the broadcaster. | `-t 44635596` | N
68-
`--from-user` | `-f` | Denotes the sender's TUID of the event, for example the user that follows another user or the subscriber to a broadcaster. | `-f 44635596` | N
69-
`--gift-user` | `-g` | Used only for subcription-based events, denotes the gifting user ID | `-g 44635596` | N
70-
`--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N
71-
`--count` | `-c` | Count of events to fire. This can be used to simulate an influx of events. | `-c 100` | N
72-
`--anonymous` | `-a` | If the event is anonymous. Only applies to `gift` and `cheer` events. | `-a` | N
73-
`--status` | `-S` | Status of the event object, currently applies to channel points redemptions. | `-S fulfilled` | N
74-
`--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events or game in stream events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N
75-
`--item-name` | `-n` | Manually set the name of the event payload item (for example the reward ID in redemption events or game name in stream events). | `-n "Science & Technology"` | N
76-
`--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N
77-
`--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | N
78-
`--game-id` | `-G` | Game ID for Drop or other relevant events. | `-G 1234` | N
63+
| Flag | Shorthand | Description | Example | Required? (Y/N) |
64+
|---------------------|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------|-----------------|
65+
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
66+
| `--transport` | `-T` | The method used to send events. Default is `eventsub`. | `-T eventsub` | N |
67+
| `--to-user` | `-t` | Denotes the receiver's TUID of the event, usually the broadcaster. | `-t 44635596` | N |
68+
| `--from-user` | `-f` | Denotes the sender's TUID of the event, for example the user that follows another user or the subscriber to a broadcaster. | `-f 44635596` | N |
69+
| `--gift-user` | `-g` | Used only for subcription-based events, denotes the gifting user ID | `-g 44635596` | N |
70+
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length. | `-s testsecret` | N |
71+
| `--count` | `-c` | Count of events to fire. This can be used to simulate an influx of events. | `-c 100` | N |
72+
| `--anonymous` | `-a` | If the event is anonymous. Only applies to `gift` and `cheer` events. | `-a` | N |
73+
| `--status` | `-S` | Status of the event object, currently applies to channel points redemptions. | `-S fulfilled` | N |
74+
| `--item-id` | `-i` | Manually set the ID of the event payload item (for example the reward ID in redemption events or game in stream events). | `-i 032e4a6c-4aef-11eb-a9f5-1f703d1f0b92` | N |
75+
| `--item-name` | `-n` | Manually set the name of the event payload item (for example the reward ID in redemption events or game name in stream events). | `-n "Science & Technology"` | N |
76+
| `--cost` | `-C` | Amount of bits or channel points redeemed/used in the event. | `-C 250` | N |
77+
| `--description` | `-d` | Title the stream should be updated/started with. | `-d Awesome new title!` | N |
78+
| `--game-id` | `-G` | Game ID for Drop or other relevant events. | `-G 1234` | N |
7979

8080

81-
82-
**Examples**
83-
8481
```sh
8582
twitch event trigger subscribe -F https://localhost:8080/ // triggers a randomly generated subscribe event and forwards to the localhost:8080 server
8683
twitch event trigger cheer -f 1234 -t 4567 // generates JSON for a cheer event from user 1234 to user 4567
@@ -109,11 +106,11 @@ None
109106

110107
**Flags**
111108

112-
| Flag | Shorthand | Description | Example | Required? (Y/N) |
113-
| ------------------- | --------- | ---------------------------------------------------------------------------- | --------------------------- | --------------- |
114-
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
115-
| `--id` | `-i` | The ID of the event to refire. | `-i <id>` | Y |
116-
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
109+
| Flag | Shorthand | Description | Example | Required? (Y/N) |
110+
|---------------------|-----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
111+
| `--forward-address` | `-F` | Web server address for where to send mock events. | `-F https://localhost:8080` | N |
112+
| `--id` | `-i` | The ID of the event to refire. | `-i <id>` | Y |
113+
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length. | `-s testsecret` | N |
117114

118115
**Examples**
119116

@@ -169,11 +166,13 @@ Allows you to test if your webserver responds to subscription requests properly.
169166

170167
**Flags**
171168

172-
| Flag | Shorthand | Description | Example | Required? (Y/N) |
173-
| ------------------- | --------- | ------------------------------------------------------------------------------- | --------------------------- | --------------- |
174-
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
175-
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC. | `-s testsecret` | N |
176-
| `--transport` | `-T` | The method used to send events. Default is eventsub, but can send using websub. | `-T websub` | N |
169+
| Flag | Shorthand | Description | Example | Required? (Y/N) |
170+
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
171+
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
172+
| `--secret` | `-s` | Webhook secret. If defined, signs all forwarded events with the SHA256 HMAC and must be 10-100 characters in length. | `-s testsecret` | N |
173+
| `--transport` | `-T` | The method used to send events. Default is `eventsub`. | `-T eventsub` | N |
174+
175+
177176

178177
**Examples**
179178

internal/events/event.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ type MockEventResponse struct {
3131
Timestamp string
3232
}
3333

34-
// MockEvent represents an event to be triggered using the `twitch event trigger <event>` command. Given that
35-
// both WebSub and EventSub need to be supported, it's required to have logic for both currently.
34+
// MockEvent represents an event to be triggered using the `twitch event trigger <event>` command.
3635
type MockEvent interface {
37-
3836
// Returns the Mock Response for the given transport
3937
GenerateEvent(p MockEventParameters) (MockEventResponse, error)
4038

@@ -46,4 +44,7 @@ type MockEvent interface {
4644

4745
// Returns the string of the topic
4846
GetTopic(transport string, trigger string) string
47+
48+
// Returns back the correct "trigger" if using the eventsub topic
49+
GetEventSubAlias(trigger string) string
4950
}

internal/events/models.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var triggerSupported = map[string]bool{
2727
}
2828

2929
var transportSupported = map[string]bool{
30-
"websub": true,
30+
"websub": false,
3131
"eventsub": true,
3232
"websockets": false,
3333
}

internal/events/trigger/forward_event.go

-15
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ var notificationHeaders = map[string][]header{
4747
HeaderValue: `test`,
4848
},
4949
},
50-
models.TransportWebSub: {
51-
{
52-
HeaderName: `Twitch-Notification-Timestamp`,
53-
HeaderValue: util.GetTimestamp().Format(time.RFC3339Nano),
54-
},
55-
{
56-
HeaderName: `Twitch-Notification-Retry`,
57-
HeaderValue: `0`,
58-
},
59-
},
6050
}
6151

6252
func ForwardEvent(p ForwardParamters) (*http.Response, error) {
@@ -85,8 +75,6 @@ func ForwardEvent(p ForwardParamters) (*http.Response, error) {
8575
case EventSubMessageTypeVerification:
8676
req.Header.Add("Twitch-Eventsub-Message-Type", EventSubMessageTypeVerification)
8777
}
88-
case models.TransportWebSub:
89-
req.Header.Set("Twitch-Notification-Id", p.ID)
9078
}
9179

9280
if p.Secret != "" {
@@ -117,8 +105,5 @@ func getSignatureHeader(req *http.Request, id string, secret string, transport s
117105
mac.Write(prefix)
118106
mac.Write(payload)
119107
req.Header.Set("Twitch-Eventsub-Message-Signature", fmt.Sprintf("sha256=%x", mac.Sum(nil)))
120-
case models.TransportWebSub:
121-
mac.Write(payload)
122-
req.Header.Set("X-Hub-Signature", fmt.Sprintf("sha256=%x", mac.Sum(nil)))
123108
}
124109
}

internal/events/trigger/forward_event_test.go

-25
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,3 @@ func TestForwardEventEventsub(t *testing.T) {
4949

5050
// TODO update test
5151
}
52-
53-
func TestForwardEventWebsub(t *testing.T) {
54-
a := test_setup.SetupTestEnv(t)
55-
56-
secret := "potaytoes"
57-
58-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
59-
w.WriteHeader(http.StatusAccepted)
60-
61-
body, err := ioutil.ReadAll(r.Body)
62-
a.Nil(err)
63-
a.NotNil(body)
64-
65-
mac := hmac.New(sha256.New, []byte(secret))
66-
67-
mac.Write(body)
68-
69-
hash := fmt.Sprintf("sha256=%x", mac.Sum(nil))
70-
a.Equal(hash, r.Header.Get("X-Hub-Signature"))
71-
}))
72-
defer ts.Close()
73-
74-
// TODO update test
75-
76-
}

0 commit comments

Comments
 (0)