Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows users to pass broadcaster IDs into verify-subscription #297

Merged
merged 2 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func init() {
verifyCmd.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
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
verifyCmd.Flags().StringVarP(&version, "version", "v", "", "Chooses the EventSub version used for a specific event. Not required for most events.")
verifyCmd.Flags().StringVarP(&toUser, "broadcaster", "b", "", "User ID of the broadcaster for the verification event.")
verifyCmd.MarkFlagRequired("forward-address")

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

_, err := verify.VerifyWebhookSubscription(verify.VerifyParameters{
Event: args[0],
Transport: transport,
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
Event: args[0],
Transport: transport,
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
BroadcasterUserID: toUser,
Version: version,
})

Expand Down
3 changes: 2 additions & 1 deletion docs/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [Trigger](#trigger)
- [Retrigger](#retrigger)
- [Verify-Subscription](#verify-subscription)
- [Websocket](#websocket)
- [WebSocket](#websocket)

## Description

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

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|---------------------|-----------|----------------------------------------------------------------------------------------------------------------------|-----------------------------|-----------------|
| `--broadcaster` | `-b` | The broadcaster's user ID to be used for verification | `-b 1234` | N |
| `--forward-address` | `-F` | Web server address for where to send mock subscription. | `-F https://localhost:8080` | Y |
| `--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 |
| `--transport` | `-T` | The method used to send events. Default is `eventsub`. | `-T eventsub` | N |
Expand Down
25 changes: 15 additions & 10 deletions internal/events/verify/subscription_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (
)

type VerifyParameters struct {
Transport string
Timestamp string
Event string
ForwardAddress string
Secret string
EventID string
Version string
Transport string
Timestamp string
Event string
ForwardAddress string
Secret string
EventID string
Version string
BroadcasterUserID string
}

type VerifyResponse struct {
Expand Down Expand Up @@ -55,7 +56,11 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
p.EventID = util.RandomGUID()
}

body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), challenge, p.ForwardAddress)
if p.BroadcasterUserID == "" {
p.BroadcasterUserID = util.RandomUserID()
}

body, err := generateWebhookSubscriptionBody(p.Transport, p.EventID, event.GetTopic(p.Transport, p.Event), event.SubscriptionVersion(), p.BroadcasterUserID, challenge, p.ForwardAddress)
if err != nil {
return VerifyResponse{}, err
}
Expand Down Expand Up @@ -133,7 +138,7 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
return r, nil
}

func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, challenge string, callback string) (trigger.TriggerResponse, error) {
func generateWebhookSubscriptionBody(transport string, eventID string, event string, subscriptionVersion string, broadcaster string, challenge string, callback string) (trigger.TriggerResponse, error) {
var res []byte
var err error
ts := util.GetTimestamp().Format(time.RFC3339Nano)
Expand All @@ -147,7 +152,7 @@ func generateWebhookSubscriptionBody(transport string, eventID string, event str
Type: event,
Version: subscriptionVersion,
Condition: models.EventsubCondition{
BroadcasterUserID: util.RandomUserID(),
BroadcasterUserID: broadcaster,
},
Transport: models.EventsubTransport{
Method: "webhook",
Expand Down