forked from pascaldekloe/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsign_test.go
More file actions
219 lines (193 loc) · 5.4 KB
/
sign_test.go
File metadata and controls
219 lines (193 loc) · 5.4 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package jwt
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"errors"
"math"
"math/big"
"reflect"
"testing"
)
func TestFormatWithoutSign(t *testing.T) {
var claims Claims
token, err := claims.FormatWithoutSign("none")
if err != nil {
t.Fatal("sign error:", err)
}
const want = "eyJhbGciOiJub25lIn0.e30"
if string(token) != want {
t.Errorf("got token %q, want %q", token, want)
}
claims.Set = map[string]interface{}{
"notAllowedInJSON": math.NaN(),
}
_, err = claims.FormatWithoutSign("X")
var wantErr error = new(json.UnsupportedValueError)
if !errors.As(err, &wantErr) {
t.Errorf("got error %#v, want a %T", err, wantErr)
}
}
func TestECDSASign(t *testing.T) {
const want = "sweet-44 tender-9 hot-juicy porkchops"
var c Claims
c.KeyID = want
token, err := c.ECDSASign("ES384", testKeyEC384)
if err != nil {
t.Fatal("sign error:", err)
}
got, err := ECDSACheck(token, &testKeyEC384.PublicKey)
if err != nil {
t.Fatal("check error:", err)
}
if got.KeyID != want {
t.Errorf("got key ID %q, want %q", got.KeyID, want)
}
}
func TestEdDSASign(t *testing.T) {
want := []string{"The Idiots"}
var c Claims
c.Audiences = want
token, err := c.EdDSASign(testKeyEd25519Private)
if err != nil {
t.Fatal("sign error:", err)
}
got, err := EdDSACheck(token, testKeyEd25519Public)
if err != nil {
t.Fatal("check error:", err)
}
if !reflect.DeepEqual(got.Audiences, want) {
t.Errorf("got audience %q, want %q", got.Audiences, want)
}
}
func TestHMACSign(t *testing.T) {
var c Claims
c.Subject = "the world's greatest secret agent"
got, err := c.HMACSign("HS512", []byte("guest"))
if err != nil {
t.Fatal(err)
}
want := "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0aGUgd29ybGQncyBncmVhdGVzdCBzZWNyZXQgYWdlbnQifQ.6shd8lGY9wOn9NghWeVAwRFtTE9Y-HtYy3PFxPc2ulahSq2HMOR5b8T0OhUCnZzM0svC6VH3hgh8fACD_30ubQ"
if s := string(got); s != want {
t.Errorf("got %q, want %q", s, want)
}
}
// Full-cycle happy flow.
func TestRSA(t *testing.T) {
var c Claims
c.Issuer = "Malory"
c.Set = map[string]interface{}{"dossier": nil}
for alg := range RSAAlgs {
token, err := c.RSASign(alg, testKeyRSA2048)
if err != nil {
t.Errorf("sign %q error: %s", alg, err)
continue
}
got, err := RSACheck(token, &testKeyRSA2048.PublicKey)
if err != nil {
t.Errorf("check %q error: %s", alg, err)
continue
}
if got.Issuer != "Malory" {
t.Errorf(`%q: got issuer %q, want "Malory"`, alg, got.Issuer)
}
if v, ok := got.Set["dossier"]; !ok {
t.Error("no dossier claim")
} else if v != nil {
t.Errorf("got dossier %#v, want nil", v)
}
}
}
func TestSignNoSecret(t *testing.T) {
_, err := new(Claims).HMACSign(HS512, []byte{})
if err != errNoSecret {
t.Errorf("got error %v, want %v", err, errNoSecret)
}
}
func TestSignHashNotLinked(t *testing.T) {
alg := "HB2b256"
if _, ok := HMACAlgs[alg]; ok {
t.Fatalf("non-standard alg %q present", alg)
}
HMACAlgs[alg] = crypto.BLAKE2b_256
defer delete(HMACAlgs, alg)
_, err := new(Claims).HMACSign(alg, []byte("guest"))
if err != errHashLink {
t.Errorf("got error %v, want %v", err, errHashLink)
}
}
func TestSignAlgError(t *testing.T) {
unknownAlg := "doesntexist"
want := AlgError("doesntexist")
c := new(Claims)
if _, err := c.ECDSASign(unknownAlg, testKeyEC256); err != want {
t.Errorf("ECDSA got error %v, want %v", err, want)
}
if _, err := c.HMACSign(unknownAlg, []byte("guest")); err != want {
t.Errorf("HMAC got error %v, want %v", err, want)
}
if _, err := c.RSASign(unknownAlg, testKeyRSA1024); err != want {
t.Errorf("RSA got error %v, want %v", err, want)
}
}
func TestECDSAKeyBroken(t *testing.T) {
key, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
if err != nil {
t.Fatal(err)
}
key.Params().N = new(big.Int)
_, err = new(Claims).ECDSASign(ES512, key)
if err == nil || err.Error() != "zero parameter" {
t.Errorf("got error %q, want zero parameter", err)
}
}
func TestRSASignKeyTooSmall(t *testing.T) {
// can't sign 512 bits with a 512-bit RSA key
key := mustParseRSAKey(`-----BEGIN RSA PRIVATE KEY-----
MIIBPAIBAAJBAIRyPiLmS7ta5bS6eEqUb1IZhxYJ/sB+Hq/uV3xIEcu075uE0mr9
xSUHcztAcHwEYE/JF0Zc5HS++ALadTK2qZUCAwEAAQJARsqdRaAcOG70Oi4034AJ
JDO6zV/YR2Dh3B0jq60FvgAVLYKDJ7klDpeqmLB64q2IXfnoVtJDjXSTpA6qyNvG
/QIhAOWfFSLq07Ock4gjGy7qeT3Tpa/uYmRuqk90jEfn2/oDAiEAk6lYDZ1DdXmY
4cnQu3Q8A/ZHW52uFR76mLi8FihzRocCIQCWGgT+G1WibvM+JfzKEXqKAQWpWQK2
tmTcpcph4t44swIhAIzdu8PZKHbUlvWnqzp5S5vYAgEzrtQ1Zon1inF1C2vXAiEA
uRVZaJLTfpQ+n88IcdG4WPKnRZqxGnrq3DjtIvFrBlM=
-----END RSA PRIVATE KEY-----`)
_, err := new(Claims).RSASign(RS512, key)
if err != rsa.ErrMessageTooLong {
t.Errorf("got error %q, want %q", err, rsa.ErrMessageTooLong)
}
}
func TestFormatHeader(t *testing.T) {
/// test all standard algorithms
algs := map[string]struct{}{
EdDSA: struct{}{},
}
for alg := range ECDSAAlgs {
algs[alg] = struct{}{}
}
for alg := range HMACAlgs {
algs[alg] = struct{}{}
}
for alg := range RSAAlgs {
algs[alg] = struct{}{}
}
for alg := range algs {
header := new(Claims).formatHeader(alg)
headerJSON, err := encoding.DecodeString(header)
if err != nil {
t.Errorf("malformed header for %q: %s", alg, err)
continue
}
m := make(map[string]interface{})
if err := json.Unmarshal(headerJSON, &m); err != nil {
t.Errorf("malformed header for %q: %s", alg, err)
continue
}
if s, ok := m["alg"].(string); !ok || s != alg {
t.Errorf("got alg %#v for %q", m["alg"], alg)
}
}
}