-
Notifications
You must be signed in to change notification settings - Fork 8
/
signatureheader_test.go
162 lines (154 loc) · 6.47 KB
/
signatureheader_test.go
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package httpsign
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
sampleBodyContent = "hello world"
)
func newAuthorizationHeader(s string) http.Header {
return http.Header{
`Authorization`: []string{s},
}
}
func newSignatureHeader(s string) http.Header {
return http.Header{
`Signature`: []string{s},
}
}
func TestFromSignatureString(t *testing.T) {
tests := []struct {
name string
header http.Header
keyID string
algorithm string
headers []string
signature string
err error
}{
{
name: `empty headers`,
header: http.Header{},
err: ErrNoSignature,
},
{
name: `Authorization Signature invalid begin`,
header: newAuthorizationHeader(`notASignature keyId="sample_key_id",algorithm="hmac-sha512",headers="(request-target) date digest",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: ErrInvalidAuthorizationHeader,
},
{
name: `Authorization Signature invalid key pair format`,
header: newAuthorizationHeader(`Signature xxx`),
err: ErrUnterminatedParameter,
},
{
name: `Authorization Signature missing keyId`,
header: newAuthorizationHeader(`Signature algorithm="hmac-sha512",headers="",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: ErrMissingKeyID,
},
{
name: `Authorization Signature missing signature`,
header: newAuthorizationHeader(`Signature keyId="sample_key_id",algorithm="hmac-sha512",headers=""`),
err: ErrMissingSignature,
},
{
name: `Authorization Signature header empty`,
header: newAuthorizationHeader(`Signature keyId="sample_key_id",algorithm="hmac-sha512",headers="",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"date"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Authorization Signature missing headers`,
header: newAuthorizationHeader(`Signature keyId="sample_key_id",algorithm="hmac-sha512",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"date"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Authorization Normal case`,
header: newAuthorizationHeader(`Signature keyId="sample_key_id",algorithm="hmac-sha512",headers="(request-target) date digest",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"(request-target)", "date", "digest"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Authorization Repeated params`,
header: newAuthorizationHeader(`Signature keyId="sample_key_id",algorithm="hmac-sha512",headers="(request-target) date digest",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",keyId="sample_key_id_2"`),
err: nil,
keyID: "sample_key_id_2",
algorithm: "hmac-sha512",
headers: []string{"(request-target)", "date", "digest"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Signature missing keyId`,
header: newSignatureHeader(`algorithm="hmac-sha512",headers="",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: ErrMissingKeyID,
},
{
name: `Signature missing signature`,
header: newSignatureHeader(`keyId="sample_key_id",algorithm="hmac-sha512",headers=""`),
err: ErrMissingSignature,
},
{
name: `Signature header empty`,
header: newSignatureHeader(`keyId="sample_key_id",algorithm="hmac-sha512",headers="",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"date"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Signature missing headers`,
header: newSignatureHeader(`keyId="sample_key_id",algorithm="hmac-sha512",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"date"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Normal case`,
header: newSignatureHeader(`keyId="sample_key_id",algorithm="hmac-sha512",headers="(request-target) date digest",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ=="`),
err: nil,
keyID: "sample_key_id",
algorithm: "hmac-sha512",
headers: []string{"(request-target)", "date", "digest"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
{
name: `Repeated params`,
header: newSignatureHeader(`keyId="sample_key_id",algorithm="hmac-sha512",headers="(request-target) date digest",signature="70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",keyId="sample_key_id_2"`),
err: nil,
keyID: "sample_key_id_2",
algorithm: "hmac-sha512",
headers: []string{"(request-target)", "date", "digest"},
signature: "70AaN3BDO0XC9QbtgksgCy2jJvmOvshq8VmjSthdXC+sgcgrKrl9WME4DbZv4W7UZKElvCemhDLHQ1Nln9GMkQ==",
},
}
for _, tc := range tests {
r, err := http.NewRequestWithContext(context.Background(), "GET", "/", nil)
require.NoError(t, err, tc.name)
r.Header = tc.header
s, err := NewSignatureHeader(r)
require.Equal(t, tc.err, err, tc.name)
if err != nil {
continue
}
assert.Equal(t, KeyID(tc.keyID), s.keyID, tc.name)
assert.Equal(t, tc.algorithm, s.algorithm, tc.name)
assert.Equal(t, tc.headers, s.headers, tc.name)
assert.Equal(t, tc.signature, s.signature, tc.name)
}
}