Skip to content

Commit

Permalink
Merge pull request #188 from twitchdev/pr-187-additions
Browse files Browse the repository at this point in the history
Pr 187 additions
  • Loading branch information
Xemdo authored Oct 29, 2022
2 parents b13f092 + 74d9517 commit fca40a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
21 changes: 20 additions & 1 deletion cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"fmt"
"log"
"net/url"
"time"

"github.com/spf13/cobra"
"github.com/twitchdev/twitch-cli/internal/events"
"github.com/twitchdev/twitch-cli/internal/events/mock_wss_server"
"github.com/twitchdev/twitch-cli/internal/events/trigger"
"github.com/twitchdev/twitch-cli/internal/events/verify"
"github.com/twitchdev/twitch-cli/internal/util"
)

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"
Expand Down Expand Up @@ -118,7 +120,7 @@ func init() {
triggerCmd.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")
triggerCmd.Flags().StringVarP(&gameID, "game-id", "G", "", "Sets the game/category ID for applicable events.")
triggerCmd.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
triggerCmd.Flags().StringVarP(&timestamp, "timestamp", "Z", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")
triggerCmd.Flags().StringVar(&timestamp, "timestamp", "", "Sets the timestamp to be used in payloads and headers. Must be in RFC3339Nano format.")

// retrigger flags
retriggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
Expand All @@ -130,6 +132,7 @@ func init() {
verifyCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
verifyCmd.Flags().StringVarP(&transport, "transport", "T", "eventsub", fmt.Sprintf("Preferred transport method for event. Defaults to EventSub.\nSupported values: %s", events.ValidTransports()))
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.")
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.MarkFlagRequired("forward-address")

Expand Down Expand Up @@ -209,6 +212,7 @@ func retriggerCmdRun(cmd *cobra.Command, args []string) {
res, err := trigger.RefireEvent(eventID, trigger.TriggerParameters{
ForwardAddress: forwardAddress,
Secret: secret,
Timestamp: util.GetTimestamp().Format(time.RFC3339Nano),
})
if err != nil {
fmt.Printf("Error refiring event: %s", err)
Expand Down Expand Up @@ -243,11 +247,26 @@ func verifyCmdRun(cmd *cobra.Command, args []string) {
}
}

if timestamp == "" {
timestamp = util.GetTimestamp().Format(time.RFC3339Nano)
} else {
// Verify custom timestamp
_, err := time.Parse(time.RFC3339Nano, timestamp)
if err != nil {
fmt.Println(
`Discarding verify: Invalid timestamp provided.
Please follow RFC3339Nano, which is used by Twitch as seen here:
https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`)
return
}
}

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

Expand Down
6 changes: 3 additions & 3 deletions internal/events/trigger/forward_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/twitchdev/twitch-cli/internal/models"
"github.com/twitchdev/twitch-cli/internal/request"
"github.com/twitchdev/twitch-cli/internal/util"
)

type ForwardParamters struct {
Expand Down Expand Up @@ -99,11 +98,12 @@ func ForwardEvent(p ForwardParamters) (*http.Response, error) {

func getSignatureHeader(req *http.Request, id string, secret string, transport string, timestamp string, payload []byte) {
mac := hmac.New(sha256.New, []byte(secret))
ts := util.GetTimestamp()
ts, _ := time.Parse(time.RFC3339Nano, timestamp)

switch transport {
case models.TransportEventSub:
req.Header.Set("Twitch-Eventsub-Message-Timestamp", timestamp)
prefix := ts.AppendFormat([]byte(id), timestamp)
prefix := ts.AppendFormat([]byte(id), time.RFC3339Nano)
mac.Write(prefix)
mac.Write(payload)
req.Header.Set("Twitch-Eventsub-Message-Signature", fmt.Sprintf("sha256=%x", mac.Sum(nil)))
Expand Down
1 change: 1 addition & 0 deletions internal/events/trigger/retrigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func RefireEvent(id string, p TriggerParameters) (string, error) {
resp, err := ForwardEvent(ForwardParamters{
ID: id,
Transport: res.Transport,
Timestamp: p.Timestamp,
ForwardAddress: p.ForwardAddress,
Secret: p.Secret,
JSON: []byte(res.JSON),
Expand Down
2 changes: 2 additions & 0 deletions internal/events/verify/subscription_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

type VerifyParameters struct {
Transport string
Timestamp string
Event string
ForwardAddress string
Secret string
Expand Down Expand Up @@ -72,6 +73,7 @@ func VerifyWebhookSubscription(p VerifyParameters) (VerifyResponse, error) {
Event: event.GetTopic(p.Transport, p.Event),
JSON: body.JSON,
Transport: p.Transport,
Timestamp: p.Timestamp,
Secret: p.Secret,
Method: requestMethod,
ForwardAddress: u.String(),
Expand Down

0 comments on commit fca40a4

Please sign in to comment.