Skip to content

Commit cd85e26

Browse files
authored
Merge pull request #9 from yaronf/remove-keyid
Breaking change: keyid is optional, now in SignConfig/VerifyConfig
2 parents 5e2c5d8 + 542e0a2 commit cd85e26

13 files changed

+216
-297
lines changed

client_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestClient_Get(t *testing.T) {
3636
fields: fields{
3737
sigName: "sig1",
3838
signer: func() *Signer {
39-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(), Headers("@method"))
39+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"), Headers("@method"))
4040
return signer
4141
}(),
4242
verifier: nil,
@@ -54,7 +54,7 @@ func TestClient_Get(t *testing.T) {
5454
fields: fields{
5555
sigName: "sig1",
5656
signer: func() *Signer {
57-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(), Headers("@method"))
57+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"), Headers("@method"))
5858
return signer
5959
}(),
6060
verifier: nil,
@@ -72,7 +72,7 @@ func TestClient_Get(t *testing.T) {
7272
fields: fields{
7373
sigName: "",
7474
signer: func() *Signer {
75-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(), Headers("@method"))
75+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"), Headers("@method"))
7676
return signer
7777
}(),
7878
verifier: nil,
@@ -90,7 +90,7 @@ func TestClient_Get(t *testing.T) {
9090
fields: fields{
9191
sigName: "sig1",
9292
signer: func() *Signer {
93-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(), Headers("@method"))
93+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"), Headers("@method"))
9494
return signer
9595
}(),
9696
verifier: nil,
@@ -110,12 +110,12 @@ func TestClient_Get(t *testing.T) {
110110
fields: fields{
111111
sigName: "sig1",
112112
signer: func() *Signer {
113-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(), Headers("@method"))
113+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"), Headers("@method"))
114114
return signer
115115
}(),
116116
verifier: nil,
117117
fetchVerifier: func(res *http.Response, req *http.Request) (sigName string, verifier *Verifier) {
118-
verifier, _ = NewHMACSHA256Verifier("key1", bytes.Repeat([]byte{2}, 64), NewVerifyConfig(), Headers("@method"))
118+
verifier, _ = NewHMACSHA256Verifier(bytes.Repeat([]byte{2}, 64), NewVerifyConfig(), Headers("@method"))
119119
return "name", verifier
120120
},
121121
Client: *http.DefaultClient,
@@ -196,7 +196,7 @@ func TestClient_Head(t *testing.T) {
196196
fields: fields{
197197
sigName: "sig1",
198198
signer: func() *Signer {
199-
signer, _ := NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64), NewSignConfig(),
199+
signer, _ := NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64), NewSignConfig().SetKeyID("key1"),
200200
Headers("@method"))
201201
return signer
202202
}(),

clientex_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func ExampleClient_Get() {
3030
// Client code starts here
3131
// Create a signer and a wrapped HTTP client (we set SignCreated to false to make the response deterministic,
3232
// don't do that in production.)
33-
signer, _ := httpsign.NewHMACSHA256Signer("key1", bytes.Repeat([]byte{1}, 64),
34-
httpsign.NewSignConfig().SignCreated(false), httpsign.Headers("@method"))
33+
signer, _ := httpsign.NewHMACSHA256Signer(bytes.Repeat([]byte{1}, 64),
34+
httpsign.NewSignConfig().SignCreated(false).SetKeyID("key1"), httpsign.Headers("@method"))
3535
client := httpsign.NewDefaultClient(httpsign.NewClientConfig().SetSignatureName("sig22").SetSigner(signer)) // sign, don't verify
3636

3737
// Send an HTTP GET, get response -- signing and verification happen behind the scenes
@@ -107,8 +107,8 @@ func TestClientUsage(t *testing.T) {
107107

108108
// Client code starts here
109109
// Create a signer and a wrapped HTTP client
110-
signer, _ := httpsign.NewRSAPSSSigner("key1", *prvKey,
111-
httpsign.NewSignConfig(),
110+
signer, _ := httpsign.NewRSAPSSSigner(*prvKey,
111+
httpsign.NewSignConfig().SetKeyID("key1"),
112112
httpsign.Headers("@request-target", "content-digest")) // The Content-Digest header will be auto-generated
113113
client := httpsign.NewDefaultClient(httpsign.NewClientConfig().SetSignatureName("sig1").SetSigner(signer)) // sign requests, don't verify responses
114114

config.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type SignConfig struct {
1616
expires int64
1717
nonce string
1818
tag string
19+
keyID *string
1920
}
2021

2122
// NewSignConfig generates a default configuration.
@@ -27,6 +28,7 @@ func NewSignConfig() *SignConfig {
2728
expires: 0,
2829
nonce: "",
2930
tag: "", // we disallow an empty tag
31+
keyID: nil,
3032
}
3133
}
3234

@@ -70,14 +72,20 @@ func (c *SignConfig) SetTag(tag string) *SignConfig {
7072
return c
7173
}
7274

75+
// SetKeyID configures a keyid value that will be included as a signature parameter.
76+
func (c *SignConfig) SetKeyID(keyID string) *SignConfig {
77+
c.keyID = &keyID
78+
return c
79+
}
80+
7381
// VerifyConfig contains additional configuration for the verifier.
7482
type VerifyConfig struct {
7583
verifyCreated bool
7684
notNewerThan time.Duration
7785
notOlderThan time.Duration
7886
allowedAlgs []string
7987
rejectExpired bool
80-
verifyKeyID bool
88+
keyID *string
8189
dateWithin time.Duration
8290
allowedTags []string
8391
}
@@ -118,11 +126,11 @@ func (v *VerifyConfig) SetAllowedAlgs(allowedAlgs []string) *VerifyConfig {
118126
return v
119127
}
120128

121-
// SetVerifyKeyID defines how to verify the keyid parameter, if one exists. If this value is set,
122-
// the signature verifies only if the value is the same as was specified in the Verifier structure.
123-
// Default: true.
124-
func (v *VerifyConfig) SetVerifyKeyID(verify bool) *VerifyConfig {
125-
v.verifyKeyID = verify
129+
// SetKeyID defines how to verify the keyid parameter, if one exists. If this value is a non-nil string,
130+
// the signature verifies only if the value is the same as was specified here.
131+
// Default: nil.
132+
func (v *VerifyConfig) SetKeyID(keyID string) *VerifyConfig {
133+
v.keyID = &keyID
126134
return v
127135
}
128136

@@ -150,7 +158,7 @@ func NewVerifyConfig() *VerifyConfig {
150158
notOlderThan: 10 * time.Second,
151159
rejectExpired: true,
152160
allowedAlgs: []string{},
153-
verifyKeyID: true,
161+
keyID: nil,
154162
dateWithin: 0, // meaning no constraint
155163
allowedTags: nil, // no constraint
156164
}

0 commit comments

Comments
 (0)