Skip to content

Commit

Permalink
v1.2.8 webhook hmacHeader fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
fareskato committed Mar 4, 2024
1 parent ea81658 commit c7335dc
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
2 changes: 0 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ type Shopify struct {
storeName string
storeApiVersion string
storeWebHookKey string
hmacHeader string
}

// init the store with store credentials so U can interact
Expand All @@ -22,7 +21,6 @@ func New(su, sp, sn, apiV, swhk string) Shopify {
storeName: sn,
storeApiVersion: apiV,
storeWebHookKey: swhk,
hmacHeader: "X-Shopify-Hmac-SHA256",
}
}

Expand Down
10 changes: 5 additions & 5 deletions webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import (
"github.com/fareskato/fkshopify/fkutils"
)

func (s Shopify) verifyWebHook(data []byte) bool {
func (s Shopify) verifyWebHook(data []byte, hmacHeader string) bool {
secret := []byte(s.storeWebHookKey)
computedHMAC := hmac.New(sha256.New, secret)
computedHMAC.Write(data)
expectedHMAC := computedHMAC.Sum(nil)
decodedHMAC, err := base64.StdEncoding.DecodeString(s.hmacHeader)
decodedHMAC, err := base64.StdEncoding.DecodeString(hmacHeader)
if err != nil {
return false
}
return hmac.Equal(expectedHMAC, decodedHMAC)
}
func WebHookCreateEntity[T any](s Shopify, t T, reqBody []byte) (*T, error) {
func WebHookCreateEntity[T any](s Shopify, t T, hmacHeader string, reqBody []byte) (*T, error) {
if s.storeWebHookKey == "" {
return nil, fkutils.ErrWebHookMissed
}
if s.hmacHeader == "" {
if hmacHeader == "" {
return nil, fkutils.ErrHmacHeaderMissed
}

if !s.verifyWebHook(reqBody) {
if !s.verifyWebHook(reqBody, hmacHeader) {
return nil, fkutils.ErrInvalidWebHookKey
}
err := json.Unmarshal(reqBody, &t)
Expand Down

0 comments on commit c7335dc

Please sign in to comment.