forked from o1egl/paseto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
246 lines (224 loc) · 8.19 KB
/
parser_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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package paseto
import (
"crypto"
"crypto/ed25519"
"encoding/hex"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEncrypt(t *testing.T) {
key := []byte("YELLOW SUBMARINE, BLACK WIZARDRY")
payload := []byte("payload")
footer := []byte("footer")
token, err := Encrypt(key, payload, footer)
if assert.NoError(t, err) {
var (
decryptedPayload []byte
decryptedFooter []byte
)
if err = NewV2().Decrypt(token, key, &decryptedPayload, &decryptedFooter); assert.NoError(t, err) {
assert.Equal(t, payload, decryptedPayload)
assert.Equal(t, footer, decryptedFooter)
}
}
}
func TestDecrypt(t *testing.T) {
key := []byte("YELLOW SUBMARINE, BLACK WIZARDRY")
payload := []byte("payload")
footer := []byte("footer")
token, err := NewV2().Encrypt(key, payload, footer)
if assert.NoError(t, err) {
var (
decryptedPayload []byte
decryptedFooter []byte
)
if err = Decrypt(token, key, &decryptedPayload, &decryptedFooter); assert.NoError(t, err) {
assert.Equal(t, payload, decryptedPayload)
assert.Equal(t, footer, decryptedFooter)
}
}
}
func TestSign(t *testing.T) {
b, _ := hex.DecodeString("b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")
privateKey := ed25519.PrivateKey(b)
payload := []byte("payload")
footer := []byte("footer")
token, err := Sign(privateKey, payload, footer)
if assert.NoError(t, err) {
var (
decryptedPayload []byte
decryptedFooter []byte
)
if err := NewV2().Verify(token, privateKey.Public(), &decryptedPayload, &decryptedFooter); assert.NoError(t, err) {
assert.Equal(t, payload, decryptedPayload)
assert.Equal(t, footer, decryptedFooter)
}
}
}
func TestVerify(t *testing.T) {
b, _ := hex.DecodeString("b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a37741eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")
privateKey := ed25519.PrivateKey(b)
payload := []byte("payload")
footer := []byte("footer")
token, err := NewV2().Sign(privateKey, payload, footer)
if assert.NoError(t, err) {
var (
decryptedPayload []byte
decryptedFooter []byte
)
if err := Verify(token, privateKey.Public(), &decryptedPayload, &decryptedFooter); assert.NoError(t, err) {
assert.Equal(t, payload, decryptedPayload)
assert.Equal(t, footer, decryptedFooter)
}
}
}
func TestParse(t *testing.T) {
symmetricKey, _ := hex.DecodeString("707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f")
cases := map[string]struct {
token string
version Version
payload interface{}
footer interface{}
}{
"v1.local": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
version: VersionV1,
payload: []byte("Love is stronger than hate or fear"),
footer: []byte("Cuon Alpinus"),
},
"v1.public": {
token: "v1.public.TG9yZW0gSXBzdW1684wbBiSvpwhED_5bdFnF2ithKoKDyzEyTOLUlFnz83IibTKCOw3LPOEp8xKM67EYOw1xU6OBBOdLQT-XO5mKMg51JJ4J91IBDwDazDex0D2UQphr7i8gPGP_5FyjlNincP_rToVbYOOzfk9cmnH1-iLmOxxbrsa7-v08Gx12ib-Z-KxKBXBHbxI8uvauVWUVS6A7rl0eAlb6SecSPPQpxQnD1zakA-nGFUbWq5Zx8XqgVZ-VidcGcd7kmhZ-bMy4Z1uGOWmAXHC793v8sbXuRdroZM8kmO0pqQMoE_wmlriIxflFABCa1PPWi5YB87aVF3oIWHYXawZXxRwxevgK.Zm9vdGVy",
version: VersionV1,
payload: []byte("Lorem Ipsum"),
footer: []byte("footer"),
},
"v2.local": {
token: "v2.local.FGVEQLywggpvH0AzKtLXz0QRmGYuC6yvl05z9GIX0cnol6UK94cfV77AXnShlUcNgpDR12FrQiurS8jxBRmvoIKmeMWC5wY9Y6w.Q3VvbiBBbHBpbnVz",
version: VersionV2,
payload: []byte("Love is stronger than hate or fear"),
footer: []byte("Cuon Alpinus"),
},
"v2.public": {
token: "v2.public.RnJhbmsgRGVuaXMgcm9ja3O7MPuu90WKNyvBUUhAGFmi4PiPOr2bN2ytUSU-QWlj8eNefki2MubssfN1b8figynnY0WusRPwIQ-o0HSZOS0F.Q3VvbiBBbHBpbnVz",
version: VersionV2,
payload: []byte("Frank Denis rocks"),
footer: []byte("Cuon Alpinus"),
},
}
b, _ := hex.DecodeString("1eb9dbbbbc047c03fd70604e0071f0987e16b28b757225c11f00415d0e20b1a2")
v2PublicKey := ed25519.PublicKey(b)
for name, test := range cases {
t.Run(name, func(t *testing.T) {
var payload []byte
var footer []byte
if ver, err := Parse(test.token, &payload, &footer, symmetricKey, map[Version]crypto.PublicKey{VersionV1: rsaPublicKey, VersionV2: v2PublicKey}); assert.NoError(t, err) {
assert.Equal(t, test.version, ver)
assert.Equal(t, test.payload, payload)
assert.Equal(t, test.footer, footer)
}
})
}
}
func TestParse_Err(t *testing.T) {
cases := map[string]struct {
token string
error error
}{
"Incorrect token format": {
token: "v1.publiceqreqqereqrqerq",
error: ErrIncorrectTokenFormat,
},
"Unsupported token version": {
token: "v0.local.rElw-WywOuwAqKC9Yao3YokSp7vx",
error: ErrUnsupportedTokenVersion,
},
"Unsupported token type": {
token: "v1.private.rElw",
error: ErrUnsupportedTokenType,
},
"Public key not found": {
token: "v1.public.rElw",
error: ErrPublicKeyNotFound,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
_, err := Parse(test.token, nil, nil, nil, nil)
assert.Equal(t, test.error, err)
})
}
}
func TestParseFooter(t *testing.T) {
cases := map[string]struct {
token string
footer []byte
version Version
err error
}{
"Non empty footer": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
footer: []byte("Cuon Alpinus"),
version: VersionV1,
},
"Empty footer": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx",
version: VersionV1,
},
"Incorrect token format": {
token: "v1.rElw-WywOuwAqK",
err: ErrIncorrectTokenFormat,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
var footer []byte
err := ParseFooter(test.token, &footer)
assert.Equal(t, test.err, err)
assert.Equal(t, test.footer, footer)
})
}
}
func TestGetTokenInfo(t *testing.T) {
cases := map[string]struct {
token string
version Version
purpose Purpose
err error
}{
"v1.local": {
token: "v1.local.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVz",
version: VersionV1,
purpose: PurposeLocal,
},
"v2.local": {
token: "v2.local.driRNhM20GQPvlWfJCepzh6HdijAq-yNUtKpdy5KXjKfpSKrOlqQvQ",
version: VersionV2,
purpose: PurposeLocal,
},
"v1.public": {
token: "v1.public.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
version: VersionV1,
purpose: PurposePublic,
},
"Unsupported token version": {
token: "v0.public.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
err: ErrUnsupportedTokenVersion,
},
"Unsupported token type": {
token: "v1.private.rElw-WywOuwAqKC9Yao3YokSp7vx0YiUB9hLTnsVOYYTojmVaYumJSQt8aggtCaFKWyaodw5k-CUWhYKATopiabAl4OAmTxHCfm2E4NSPvrmMcmi8n-JcZ93HpcxC6rx_ps22vutv7iP7wf8QcSD1Mwx.Q3VvbiBBbHBpbnVh",
err: ErrUnsupportedTokenType,
},
"Incorrect token format": {
token: "v1.private",
err: ErrIncorrectTokenFormat,
},
}
for name, test := range cases {
t.Run(name, func(t *testing.T) {
version, purpose, err := GetTokenInfo(test.token)
assert.Equal(t, test.err, err)
assert.Equal(t, test.version, version)
assert.Equal(t, test.purpose, purpose)
})
}
}