-
Notifications
You must be signed in to change notification settings - Fork 15
/
signature_test.go
137 lines (109 loc) · 3.51 KB
/
signature_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
package httpsignatures
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
const (
TEST_SIGNATURE = `keyId="Test",algorithm="hmac-sha256",signature="JldXnt8W9t643M2Sce10gqCh/+E7QIYLiI+bSjnFBGCti7s+mPPvOjVb72sbd1FjeOUwPTDpKbrQQORrm+xBYfAwCxF3LBSSzORvyJ5nRFCFxfJ3nlQD6Kdxhw8wrVZX5nSem4A/W3C8qH5uhFTRwF4ruRjh+ENHWuovPgO/HGQ="`
TEST_HASH = `JldXnt8W9t643M2Sce10gqCh/+E7QIYLiI+bSjnFBGCti7s+mPPvOjVb72sbd1FjeOUwPTDpKbrQQORrm+xBYfAwCxF3LBSSzORvyJ5nRFCFxfJ3nlQD6Kdxhw8wrVZX5nSem4A/W3C8qH5uhFTRwF4ruRjh+ENHWuovPgO/HGQ=`
TEST_KEY = "SomethingRandom"
TEST_DATE = "Thu, 05 Jan 2012 21:31:40 GMT"
TEST_KEY_ID = "Test"
)
func TestCreateSignatureFromAuthorizationHeader(t *testing.T) {
r := http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
headerAuthorization: []string{authScheme + TEST_SIGNATURE},
},
}
s, err := FromRequest(&r)
assert.Nil(t, err)
assert.Equal(t, "Test", s.KeyID)
assert.Equal(t, AlgorithmHmacSha256, s.Algorithm)
assert.Equal(t, TEST_HASH, s.Signature)
assert.Equal(t, s.String(), TEST_SIGNATURE)
}
func TestCreateSignatureFromSignatureHeaderHeader(t *testing.T) {
r := http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
headerSignature: []string{TEST_SIGNATURE},
},
}
s, err := FromRequest(&r)
assert.Nil(t, err)
assert.Equal(t, "Test", s.KeyID)
assert.Equal(t, AlgorithmHmacSha256, s.Algorithm)
assert.Equal(t, TEST_HASH, s.Signature)
assert.Equal(t, s.String(), TEST_SIGNATURE)
}
func TestCreateSignatureWithNoSignature(t *testing.T) {
r := http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
},
}
s, err := FromRequest(&r)
assert.Equal(t, ErrorNoSignatureHeader, err)
assert.Nil(t, s)
}
func TestCreateWithMissingSignature(t *testing.T) {
s, err := FromString(`keyId="Test",algorithm="hmac-sha256"`)
assert.Equal(t, "Missing signature", err.Error())
assert.Nil(t, s)
}
func TestCreateWithMissingAlgorithm(t *testing.T) {
s, err := FromString(`keyId="Test",signature="fffff"`)
assert.Equal(t, "Missing algorithm", err.Error())
assert.Nil(t, s)
}
func TestCreateWithMissingKeyId(t *testing.T) {
s, err := FromString(`algorithm="hmac-sha256",signature="fffff"`)
assert.Equal(t, "Missing keyId", err.Error())
assert.Nil(t, s)
}
func TestCreateWithInvalidKey(t *testing.T) {
s, err := FromString(`keyId="Test",algorithm="hmac-sha256",signature="fffff",garbage="bob"`)
assert.Equal(t, "Unexpected key in signature 'garbage'", err.Error())
assert.Nil(t, s)
}
func TestValidRequestIsValid(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
},
}
err := DefaultSha256Signer.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
sig, err := FromRequest(r)
assert.Nil(t, err)
assert.True(t, sig.IsValid(TEST_KEY, r))
}
func TestNotValidIfRequestHeadersChange(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
},
}
err := DefaultSha256Signer.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
r.Header.Set("Date", "Thu, 05 Jan 2012 21:31:41 GMT")
sig, err := FromRequest(r)
assert.Nil(t, err)
assert.False(t, sig.IsValid(TEST_KEY, r))
}
func TestNotValidIfRequestIsMissingDate(t *testing.T) {
r := &http.Request{
Header: http.Header{
"Date": []string{TEST_DATE},
},
}
s := Signer{AlgorithmHmacSha1, HeaderList{RequestTarget}}
err := s.SignRequest(TEST_KEY_ID, TEST_KEY, r)
assert.Nil(t, err)
sig, err := FromRequest(r)
assert.Nil(t, err)
assert.False(t, sig.IsValid(TEST_KEY, r))
}