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

oauth2: added shopify endpoint and request validation #404

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions shopify/shopify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package shopify provides constants for using OAuth2 to access Shopify.
package shopify // import "golang.org/x/oauth2/shopify"

import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"net/http"

"golang.org/x/oauth2"
)

// ShopEndpoint returns a new oauth2.Endpoint for the given shopify shop
// https://help.shopify.com/en/api/getting-started/authentication/oauth
func ShopEndpoint(shop string) oauth2.Endpoint {
return oauth2.Endpoint{
AuthURL: "https://" + shop + "/admin/oauth/authorize",
TokenURL: "https://" + shop + "/admin/oauth/access_token",
}
}

// VerifyRequest validates that the requested params come from shopify as signed
// with an hmac sha256 string against your api secret key - returns true/false
func VerifyRequest(r *http.Request, secret string) bool {
values := r.URL.Query()

// if there are no params sent or no hmac param sent at all, bad request
if len(values) == 0 || len(values["hmac"]) < 1 {
return false
}

hmacSig := values["hmac"][0]
values.Del("hmac") // we compare the query without hmac param

// the query string to generate an hmac compare on
compareQuery := values.Encode()

h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(compareQuery))

sha := hex.EncodeToString(h.Sum(nil))

return hmac.Equal([]byte(sha), []byte(hmacSig))
}