-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaccept.go
More file actions
68 lines (57 loc) · 1.81 KB
/
Copy pathaccept.go
File metadata and controls
68 lines (57 loc) · 1.81 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
package httpsig
import (
"fmt"
sfv "github.com/dunglas/httpsfv"
)
type AcceptSignature struct {
Profile SigningProfile
MetaNonce string // 'nonce'
MetaKeyID string // 'keyid'
MetaTag string // 'tag' - No default. A value must be provided if the parameter is in Metadata.
}
func ParseAcceptSignature(acceptHeader string) (AcceptSignature, error) {
as := AcceptSignature{}
acceptDict, err := sfv.UnmarshalDictionary([]string{acceptHeader})
if err != nil {
return as, newError(ErrInvalidAcceptSignature, "Unable to parse Accept-Signature value", err)
}
profiles := acceptDict.Names()
if len(profiles) == 0 {
return as, newError(ErrMissingAcceptSignature, "No Accept-Signature value")
}
label := profiles[0]
profileItems, _ := acceptDict.Get(label)
profileList, isList := profileItems.(sfv.InnerList)
if !isList {
return as, newError(ErrInvalidAcceptSignature, "Unable to parse Accept-Signature value. Accept-Signature must be a dictionary.")
}
fields := []string{}
for _, componentItem := range profileList.Items {
field, ok := componentItem.Value.(string)
if !ok {
return as, newError(ErrInvalidAcceptSignature, fmt.Sprintf("Invalid signature component '%v', Components must be strings", componentItem.Value))
}
fields = append(fields, field)
}
as.Profile = SigningProfile{
Fields: Fields(fields...),
Label: label,
Metadata: []Metadata{},
}
md := metadataProviderFromParams{profileList.Params}
for _, meta := range profileList.Params.Names() {
as.Profile.Metadata = append(as.Profile.Metadata, Metadata(meta))
switch Metadata(meta) {
case MetaNonce:
as.MetaNonce, _ = md.Nonce()
case MetaAlgorithm:
alg, _ := md.Alg()
as.Profile.Algorithm = Algorithm(alg)
case MetaKeyID:
as.MetaKeyID, _ = md.KeyID()
case MetaTag:
as.MetaTag, _ = md.Tag()
}
}
return as, nil
}