-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathheader.go
51 lines (45 loc) · 1.19 KB
/
header.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
package aliacm
import (
"net/http"
"strconv"
"strings"
)
const (
longPullingTimeoutHeader = "longPullingTimeout"
spssAccessKeyHeader = "Spas-AccessKey"
timeStampHeader = "timeStamp"
spasSignatureHeader = "Spas-Signature"
)
type headerSetter func(header http.Header) error
func (d *Diamond) withLongPollingTimeout() headerSetter {
return func(header http.Header) error {
if header == nil {
header = make(http.Header)
}
header.Set(longPullingTimeoutHeader, "30000")
return nil
}
}
func (d *Diamond) withSignature(tenant, group string) headerSetter {
return func(header http.Header) error {
if header == nil {
header = make(http.Header)
}
now := timeInMilli()
var toSignList []string
toSignList = append(toSignList, tenant)
if group != "" {
toSignList = append(toSignList, group)
}
toSignList = append(toSignList, strconv.FormatInt(now, 10))
str := strings.Join(toSignList, "+")
signature, err := HMACSHA1Encrypt(str, d.option.secretKey)
if err != nil {
return err
}
header.Set(spssAccessKeyHeader, d.option.accessKey)
header.Set(timeStampHeader, strconv.FormatInt(now, 10))
header.Set(spasSignatureHeader, signature)
return nil
}
}