Skip to content

Commit 10482e3

Browse files
authored
Merge pull request #297 from twitchdev/fix/103
allows users to pass broadcaster IDs into verify-subscription
2 parents 11a468d + 3f840ac commit 10482e3

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

cmd/events.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ func init() {
176176
verifyCmd.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
177177
verifyCmd.Flags().StringVarP(&eventID, "subscription-id", "u", "", "Manually set the subscription/event ID of the event itself.") // TODO: This description will need to change with https://github.com/twitchdev/twitch-cli/issues/184
178178
verifyCmd.Flags().StringVarP(&version, "version", "v", "", "Chooses the EventSub version used for a specific event. Not required for most events.")
179+
verifyCmd.Flags().StringVarP(&toUser, "broadcaster", "b", "", "User ID of the broadcaster for the verification event.")
179180
verifyCmd.MarkFlagRequired("forward-address")
180181

181182
// websocket flags
@@ -313,12 +314,13 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
313314
}
314315

315316
_, err := verify.VerifyWebhookSubscription(verify.VerifyParameters{
316-
Event: args[0],
317-
Transport: transport,
318-
ForwardAddress: forwardAddress,
319-
Secret: secret,
320-
Timestamp: timestamp,
321-
EventID: eventID,
317+
Event: args[0],
318+
Transport: transport,
319+
ForwardAddress: forwardAddress,
320+
Secret: secret,
321+
Timestamp: timestamp,
322+
EventID: eventID,
323+
BroadcasterUserID: toUser,
322324
Version: version,
323325
})
324326

docs/event.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [Trigger](#trigger)
66
- [Retrigger](#retrigger)
77
- [Verify-Subscription](#verify-subscription)
8-
- [Websocket](#websocket)
8+
- [WebSocket](#websocket)
99

1010
## Description
1111

@@ -154,6 +154,7 @@ This command takes the same arguments as [Trigger](#trigger).
154154

155155
| Flag | Shorthand | Description | Example | Required? (Y/N) |
156156
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
157+
| `--broadcaster` | `-b` | The broadcaster's user ID to be used for verification | `-b 1234` | N |
157158
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
158159
| `--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 |
159160
| `--transport` | `-T` | The method used to send events. Default is `eventsub`. | `-T eventsub` | N |

internal/events/verify/subscription_verify.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ import (
1919
)
2020

2121
type VerifyParameters struct {
22-
Transport string
23-
Timestamp string
24-
Event string
25-
ForwardAddress string
26-
Secret string
27-
EventID string
28-
Version string
22+
Transport string
23+
Timestamp string
24+
Event string
25+
ForwardAddress string
26+
Secret string
27+
EventID string
28+
Version string
29+
BroadcasterUserID string
2930
}
3031

3132
type VerifyResponse struct {
@@ -55,7 +56,11 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
5556
p.EventID = util.RandomGUID()
5657
}
5758

58-
body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), challenge, p.ForwardAddress)
59+
if p.BroadcasterUserID == "" {
60+
p.BroadcasterUserID = util.RandomUserID()
61+
}
62+
63+
body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), p.BroadcasterUserID, challenge, p.ForwardAddress)
5964
if err != nil {
6065
return VerifyResponse{}, err
6166
}
@@ -133,7 +138,7 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
133138
return r, nil
134139
}
135140

136-
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, challenge string, callback string) (trigger.TriggerResponse, error) {
141+
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, broadcaster string, challenge string, callback string) (trigger.TriggerResponse, error) {
137142
var res []byte
138143
var err error
139144
ts := util.GetTimestamp().Format(time.RFC3339Nano)
@@ -147,7 +152,7 @@ func generateWebhookSubscriptionBody(transport string, eventID string, event str
147152
Type: event,
148153
Version: subscriptionVersion,
149154
Condition: models.EventsubCondition{
150-
BroadcasterUserID: util.RandomUserID(),
155+
BroadcasterUserID: broadcaster,
151156
},
152157
Transport: models.EventsubTransport{
153158
Method: "webhook",

0 commit comments

Comments
 (0)