Skip to content

Commit

Permalink
DRY webhook signing key call
Browse files Browse the repository at this point in the history
  • Loading branch information
drkgrntt committed Nov 28, 2024
1 parent f10029a commit 446778f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
6 changes: 5 additions & 1 deletion mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,11 @@ func (mg *MailgunImpl) SetClient(c *http.Client) {

// WebhookSigningKey returns the webhook signing key configured for this client
func (mg *MailgunImpl) WebhookSigningKey() string {
return mg.webhookSigningKey
key := mg.webhookSigningKey
if key == "" {
return mg.APIKey()
}
return key
}

// SetWebhookSigningKey updates the webhook signing key for this client
Expand Down
14 changes: 2 additions & 12 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ type WebhookPayload struct {

// Use this method to parse the webhook signature given as JSON in the webhook response
func (mg *MailgunImpl) VerifyWebhookSignature(sig Signature) (verified bool, err error) {
key := mg.WebhookSigningKey()
// For backwards compatibility, fall back to the api key
if key == "" {
key = mg.APIKey()
}
h := hmac.New(sha256.New, []byte(key))
h := hmac.New(sha256.New, []byte(mg.WebhookSigningKey()))

_, err = io.WriteString(h, sig.TimeStamp)
if err != nil {
Expand All @@ -154,12 +149,7 @@ func (mg *MailgunImpl) VerifyWebhookSignature(sig Signature) (verified bool, err
// Deprecated: Please use the VerifyWebhookSignature() to parse the latest
// version of WebHooks from mailgun
func (mg *MailgunImpl) VerifyWebhookRequest(req *http.Request) (verified bool, err error) {
key := mg.WebhookSigningKey()
// For backwards compatibility, fall back to the api key
if key == "" {
key = mg.APIKey()
}
h := hmac.New(sha256.New, []byte(key))
h := hmac.New(sha256.New, []byte(mg.WebhookSigningKey()))

_, err = io.WriteString(h, req.FormValue("timestamp"))
if err != nil {
Expand Down

0 comments on commit 446778f

Please sign in to comment.