forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
73 lines (60 loc) · 1.87 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package goshopify
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"net/url"
)
// Returns a Shopify oauth authorization url for the given shopname and state.
//
// State is a unique value that can be used to check the authenticity during a
// callback from Shopify.
func (app App) AuthorizeUrl(shopName string, state string) string {
shopUrl, _ := url.Parse(ShopBaseUrl(shopName))
shopUrl.Path = "/admin/oauth/authorize"
query := shopUrl.Query()
query.Set("client_id", app.ApiKey)
query.Set("redirect_uri", app.RedirectUrl)
query.Set("scope", app.Scope)
query.Set("state", state)
shopUrl.RawQuery = query.Encode()
return shopUrl.String()
}
func (app App) GetAccessToken(shopName string, code string) (string, error) {
type Token struct {
Token string `json:"access_token"`
}
data := struct {
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
Code string `json:"code"`
}{
ClientId: app.ApiKey,
ClientSecret: app.ApiSecret,
Code: code,
}
client := NewClient(app, shopName, "")
req, err := client.NewRequest("POST", "admin/oauth/access_token", data, nil)
token := new(Token)
err = client.Do(req, token)
return token.Token, err
}
// Verify a message against a message HMAC
func (app App) VerifyMessage(message, messageMAC string) bool {
mac := hmac.New(sha256.New, []byte(app.ApiSecret))
mac.Write([]byte(message))
expectedMAC := mac.Sum(nil)
// shopify HMAC is in hex so it needs to be decoded
actualMac, _ := hex.DecodeString(messageMAC)
return hmac.Equal(actualMac, expectedMAC)
}
// Verifying URL callback parameters.
func (app App) VerifyAuthorizationURL(u *url.URL) bool {
q := u.Query()
messageMAC := q.Get("hmac")
// Remove hmac and signature and leave the rest of the parameters alone.
q.Del("hmac")
q.Del("signature")
message := q.Encode()
return app.VerifyMessage(message, messageMAC)
}