-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.go
More file actions
93 lines (83 loc) · 2.15 KB
/
base.go
File metadata and controls
93 lines (83 loc) · 2.15 KB
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
package httpsignature
import (
"fmt"
"net/http"
"strings"
)
type SigBase struct {
*SigParams
Method string
Path string
Query string
ContentDigest *ContentDigest
Headers http.Header
}
func NewSigBase(params *SigParams, method string, path string, query string, headers http.Header, body []byte) *SigBase {
content := NewContentDigest(body)
return &SigBase{
SigParams: params,
Method: method,
Path: path,
Query: query,
ContentDigest: content,
Headers: headers,
}
}
func SigbaseFrom(params *SigParams, method string, path string, query string, headers http.Header, digestHeader *ContentDigest) *SigBase {
return &SigBase{
SigParams: params,
Method: method,
Path: path,
Query: query,
Headers: headers,
ContentDigest: digestHeader,
}
}
func (s *SigBase) Serialize() (string, error) {
query := s.Query
if !strings.HasPrefix(query, "?") {
// should have leading ?
query = "?" + query
}
path := s.Path
if !strings.HasPrefix(path, "/") {
// should have leading /
path = "/" + path
}
signBase := ""
for _, component := range s.Components {
switch component {
case "@method":
signBase += fmt.Sprintf(`"@method": %s`, s.Method)
case "@path":
signBase += fmt.Sprintf(`"@path": %s`, s.Path)
case "@query":
signBase += fmt.Sprintf(`"@query": %s`, s.Query)
case "content-digest":
signBase += fmt.Sprintf(`"content-digest": %s`, s.ContentDigest.Base64())
case "@signature-params":
signBase += fmt.Sprintf(`"@signature-params": %s`, s.SigParams.Serialize())
default:
if strings.HasPrefix(component, "@") {
return "", fmt.Errorf("unsupported component: %s", component)
}
signBase += fmt.Sprintf(`"%s": %s`, component, s.Headers.Get(component))
}
// each line has '\n' newline
signBase += "\n"
}
// // each line has '\n' newline
// template := fmt.Sprintf(`"@method": %s
// "@path": %s
// "@query": %s
// content-digest: sha-256=:%s:
// "@signature-params": %s
// `,
// s.Method,
// path,
// query,
// s.ContentDigest.Base64(),
// s.SigParams.Serialize(),
// )
return signBase, nil
}