-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsigntag.go
More file actions
104 lines (95 loc) · 3.46 KB
/
Copy pathsigntag.go
File metadata and controls
104 lines (95 loc) · 3.46 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
94
95
96
97
98
99
100
101
102
103
104
package httpsign
import (
"errors"
"fmt"
"net/http"
)
// ErrSignatureTagNotFound is returned by *DetailsByTag when no signature
// carries the requested tag parameter.
var ErrSignatureTagNotFound = errors.New("no signature with the given tag")
// ErrSignatureTagAmbiguous is returned by *DetailsByTag when more than one
// signature carries the requested tag parameter.
var ErrSignatureTagAmbiguous = errors.New("multiple signatures with the given tag")
// RequestDetailsByTag returns MessageDetails for the unique Signature whose
// Signature-Input tag equals tag. Matching uses parsed headers only (not trailers)
// and does not cryptographically verify. Returns ErrSignatureTagNotFound or
// ErrSignatureTagAmbiguous when the match count is not exactly one.
// Use details.Label with VerifyRequest after obtaining a Verifier (e.g. via details.KeyID).
func RequestDetailsByTag(req *http.Request, tag string) (*MessageDetails, error) {
if req == nil {
return nil, fmt.Errorf("nil request")
}
all, err := signatureDetailsListFromHeaders(req.Header)
if err != nil {
return nil, err
}
return detailsByTag(all, tag)
}
// ResponseDetailsByTag returns MessageDetails for the unique Signature whose
// Signature-Input tag equals tag. Matching uses parsed headers only (not trailers)
// and does not cryptographically verify. Returns ErrSignatureTagNotFound or
// ErrSignatureTagAmbiguous when the match count is not exactly one.
// Use details.Label with VerifyResponse after obtaining a Verifier (e.g. via details.KeyID).
func ResponseDetailsByTag(res *http.Response, tag string) (*MessageDetails, error) {
if res == nil {
return nil, fmt.Errorf("nil response")
}
all, err := signatureDetailsListFromHeaders(res.Header)
if err != nil {
return nil, err
}
return detailsByTag(all, tag)
}
// RequestDetailsListByTag returns MessageDetails for every Signature whose
// Signature-Input tag equals tag (possibly empty). Matching uses parsed headers
// only (not trailers) and does not cryptographically verify. Each result has Label set.
func RequestDetailsListByTag(req *http.Request, tag string) ([]*MessageDetails, error) {
if req == nil {
return nil, fmt.Errorf("nil request")
}
all, err := signatureDetailsListFromHeaders(req.Header)
if err != nil {
return nil, err
}
return detailsListByTag(all, tag), nil
}
// ResponseDetailsListByTag returns MessageDetails for every Signature whose
// Signature-Input tag equals tag (possibly empty). Matching uses parsed headers
// only (not trailers) and does not cryptographically verify. Each result has Label set.
func ResponseDetailsListByTag(res *http.Response, tag string) ([]*MessageDetails, error) {
if res == nil {
return nil, fmt.Errorf("nil response")
}
all, err := signatureDetailsListFromHeaders(res.Header)
if err != nil {
return nil, err
}
return detailsListByTag(all, tag), nil
}
func detailsListByTag(all []*MessageDetails, tag string) []*MessageDetails {
var found []*MessageDetails
for _, details := range all {
if details.Tag != nil && *details.Tag == tag {
found = append(found, details)
}
}
return found
}
func detailsByTag(all []*MessageDetails, tag string) (*MessageDetails, error) {
var match *MessageDetails
n := 0
for _, details := range all {
if details.Tag == nil || *details.Tag != tag {
continue
}
n++
if n > 1 {
return nil, fmt.Errorf("%w: %q", ErrSignatureTagAmbiguous, tag)
}
match = details
}
if n == 0 {
return nil, fmt.Errorf("%w: %q", ErrSignatureTagNotFound, tag)
}
return match, nil
}