-
Notifications
You must be signed in to change notification settings - Fork 2
/
signature.go
117 lines (93 loc) · 3.08 KB
/
signature.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package appserver
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type SignatureVerificationError struct {
err error
}
func (e SignatureVerificationError) Error() string {
return "invalid signature"
}
func (e SignatureVerificationError) Unwrap() error {
return e.err
}
func (srv *Server) verifyPayloadSignature(req *http.Request) error {
body, err := extractBody(req)
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("extract request body: %w", err)}
}
if len(body) == 0 {
return SignatureVerificationError{err: errors.New("empty payload")}
}
// copy body back to the request
req.Body = io.NopCloser(bytes.NewBuffer(body))
appReq := AppRequest{}
if err := json.Unmarshal(body, &appReq); err != nil {
return SignatureVerificationError{err: fmt.Errorf("parse body: %w", err)}
}
credentials, err := srv.credentialStore.Get(req.Context(), appReq.Source.ShopID)
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("get shop credentials: %w", err)}
}
signature, err := hex.DecodeString(req.Header.Get(ShopSignatureKey))
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("decode signature: %w", err)}
}
if err := verifySignature(body, signature, credentials.ShopSecret); err != nil {
return SignatureVerificationError{err: err}
}
return nil
}
func (srv *Server) verifyQuerySignature(req *http.Request) error {
shopID := req.URL.Query().Get("shop-id")
if shopID == "" {
return SignatureVerificationError{err: errors.New("missing query parameter: shop-id")}
}
signature, err := hex.DecodeString(req.URL.Query().Get(ShopSignatureKey))
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("decode signature: %w", err)}
}
// Go sorts the query internally by key, so it's hard to replicate Shopware's behaviour here.
// The signature needs be calculated on the raw query string, because that's what Shopware does and expects. The
// tests in Shopware indicate, that the signature key/value should just be replaced with an empty string. It works.
query := strings.ReplaceAll(req.URL.RawQuery, fmt.Sprintf("&%s=%s", ShopSignatureKey, req.URL.Query().Get(ShopSignatureKey)), "")
query, err = url.QueryUnescape(query)
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("encode query: %w", err)}
}
credentials, err := srv.credentialStore.Get(req.Context(), shopID)
if err != nil {
return SignatureVerificationError{err: fmt.Errorf("get shop credentials: %w", err)}
}
if err := verifySignature([]byte(query), signature, credentials.ShopSecret); err != nil {
return SignatureVerificationError{err: err}
}
return nil
}
func verifySignature(data []byte, signature []byte, key string) error {
if len(data) == 0 {
return errors.New("empty data")
}
if len(signature) == 0 {
return errors.New("empty signature")
}
if key == "" {
return errors.New("empty key")
}
h := hmac.New(sha256.New, []byte(key))
h.Write(data)
if !hmac.Equal(signature, h.Sum(nil)) {
return errors.New("signature mismatch")
}
return nil
}