Repository navigation
Expand file tree
/
Copy pathsign.go
More file actions
145 lines (126 loc) · 4.26 KB
/
Copy pathsign.go
File metadata and controls
145 lines (126 loc) · 4.26 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2026 Micah Hausler
// SPDX-License-Identifier: Apache-2.0
package httpsig
import (
"net/http"
"strings"
"time"
"github.com/micahhausler/sfv"
)
// SignOptions control how a request signature is created.
type SignOptions struct {
// Components are the message components to cover. If nil, the
// signature covers @method and @target-uri. An empty, non-nil slice
// covers no components, which [RFC 9421 Section 7.2.1] discourages.
//
// [RFC 9421 Section 7.2.1]: https://datatracker.ietf.org/doc/html/rfc9421#section-7.2.1
Components []Component
// Label identifies the signature within the message. It defaults to
// "sig1".
Label string
// Created is the signature creation time, truncated to seconds. The
// zero value means the current time.
Created time.Time
// Expires is the signature expiration time, truncated to seconds.
// The zero value omits the expires parameter.
Expires time.Time
// KeyID sets the keyid parameter. Empty omits it.
KeyID string
// Nonce sets the nonce parameter. Empty omits it.
Nonce string
// Tag sets the application-specific tag parameter. Empty omits it.
Tag string
// IncludeAlg includes the signer's algorithm as the alg parameter.
IncludeAlg bool
// Scheme and Authority override the values derived from the request,
// for use with derived components such as @target-uri.
Scheme string
Authority string
// StructuredFields maps lowercase field names to their structured
// field types, required for components with the SF flag.
StructuredFields map[string]FieldType
}
// Sign signs req and adds the signature to its Signature-Input and Signature
// header fields, merging with any signatures already present.
func Sign(req *http.Request, key Signer, opts SignOptions) error {
comps := opts.Components
if comps == nil {
comps = []Component{{Name: "@method"}, {Name: "@target-uri"}}
}
ids := make([]covered, len(comps))
items := make([]sfv.Item, len(comps))
for i, c := range comps {
c.Name = strings.ToLower(c.Name)
if err := c.validate(); err != nil {
return err
}
ids[i] = covered{id: c.identifier(), c: c}
items[i] = ids[i].id
}
created := opts.Created
if created.IsZero() {
created = time.Now()
}
params := sfv.Parameters{{Key: "created", Value: sfv.Integer(created.Unix())}}
if !opts.Expires.IsZero() {
params = append(params, sfv.Parameter{Key: "expires", Value: sfv.Integer(opts.Expires.Unix())})
}
if opts.Nonce != "" {
params = append(params, sfv.Parameter{Key: "nonce", Value: sfv.String(opts.Nonce)})
}
if opts.IncludeAlg {
params = append(params, sfv.Parameter{Key: "alg", Value: sfv.String(key.Algorithm())})
}
if opts.KeyID != "" {
params = append(params, sfv.Parameter{Key: "keyid", Value: sfv.String(opts.KeyID)})
}
if opts.Tag != "" {
params = append(params, sfv.Parameter{Key: "tag", Value: sfv.String(opts.Tag)})
}
input := sfv.InnerList{Items: items, Params: params}
t := newTarget(req, opts.Scheme, opts.Authority, opts.StructuredFields)
base, err := signatureBase(t, ids, input)
if err != nil {
return err
}
sig, err := key.Sign(base)
if err != nil {
return err
}
label := opts.Label
if label == "" {
label = "sig1"
}
return addSignature(req.Header, label, input, sig)
}
// addSignature merges a labeled signature into the Signature-Input and
// Signature dictionaries of h.
func addSignature(h http.Header, label string, input sfv.InnerList, sig []byte) error {
inputs, err := sfv.ParseDictionary(h.Values("Signature-Input")...)
if err != nil {
return syntaxErrorf("Signature-Input: %v", err)
}
sigs, err := sfv.ParseDictionary(h.Values("Signature")...)
if err != nil {
return syntaxErrorf("Signature: %v", err)
}
if _, ok := inputs.Get(label); ok {
return syntaxErrorf("signature label %q already present", label)
}
if _, ok := sigs.Get(label); ok {
return syntaxErrorf("signature label %q already present", label)
}
inputs = append(inputs, sfv.DictMember{Key: label, Value: input})
sigs = append(sigs, sfv.DictMember{Key: label, Value: sfv.Item{Value: sfv.Bytes(sig)}})
b, err := inputs.MarshalText()
if err != nil {
return syntaxErrorf("Signature-Input: %v", err)
}
h.Set("Signature-Input", string(b))
b, err = sigs.MarshalText()
if err != nil {
return syntaxErrorf("Signature: %v", err)
}
h.Set("Signature", string(b))
return nil
}