-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
55 lines (44 loc) · 1.26 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
package url
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"net/http"
"strconv"
"time"
)
// SecretKey represents a key for hash function.
var secretKey []byte
func SetSecretKey(key []byte) {
secretKey = key
}
// HasValidURL determines if the given request has a valid signature.
func HasValidURL(request *http.Request) bool {
return hasCorrectSignature(request) && signatureHasNotExpired(request)
}
// hasCorrectSignature determines if the signature from the given request matches the URL.
func hasCorrectSignature(request *http.Request) bool {
url := request.URL
values := url.Query()
signature := values.Get("signature")
values.Del("signature")
url.RawQuery = values.Encode()
return signature == hash([]byte(url.String()))
}
// signatureHasNotExpired determine if the expires timestamp from the given request is not from the past.
func signatureHasNotExpired(request *http.Request) bool {
url := request.URL
expires := url.Query().Get("expires")
unix, _ := strconv.Atoi(expires)
if unix == 0 {
return true
}
return int(time.Now().Unix()) < unix
}
// hash for generate signature from given url
func hash(url []byte) string {
mac := hmac.New(sha256.New, secretKey)
mac.Write(url)
byteArray := mac.Sum(nil)
return hex.EncodeToString(byteArray)
}