-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt_test.go
276 lines (252 loc) · 12.7 KB
/
jwt_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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package signature_go_test
import (
"context"
"crypto/rsa"
"encoding/json"
"errors"
hash "github.com/harryosmar/hash-go"
"testing"
"time"
)
const (
privateKeyBase64 = `LS0tLS1CRUdJTiBQUklWQVRFIEtFWS0tLS0tCk1JSUV2d0lCQURBTkJna3Foa2lHOXcwQkFRRUZBQVNDQktrd2dnU2xBZ0VBQW9JQkFRQzdWSlRVdDlVczhjS2oKTXpFZll5amlXQTRSNC9NMmJTMUdCNHQ3TlhwOThDM1NDNmRWTXZEdWljdEdldXJUOGpOYnZKWkh0Q1N1WUV2dQpOTW9TZm03Nm9xRnZBcDhHeTBpejVzeGpabVNuWHlDZFBFb3ZHaExhMFZ6TWFROHMrQ0xPeVM1Nll5Q0ZHZUpaCnFndHpKNkdSM2Vxb1lTVzliOVVNdmtCcFpP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mZmQXQwZ3VuVlRMdzdvbkxSbnJxMC9Jelc3eVdSN1Frcm1CTDdqVEtFbjV1CitxS2hid0tmQnN0SXMrYk1ZMlprcDE4Z25UeEtMeG9TMnRGY3pHa1BMUGdpenNrdWVtTWdoUm5pV2FvTGN5ZWgKa2QzcXFHRWx2Vy9WREw1QWFXVGcwbkxWa2pSbzl6KzQwUlF6dVZhRThBa0FGbXhaem93M3grVkpZS2RqeWtrSgowaVQ5d0NTMERSVFh1MjY5VjI2NFZmLzNqdnJlZFppS1JrZ3dsTDl4TkF3eFhGZzB4L1hGdzAwNVVXVlJJa2RnCmNLV1RqcEJQMmRQd1ZaNFdXQys5YUdWZCtHeW4xbzBDTGVsZjRyRWpHb1hiQUFFZ0FxZUdVeHJjSWxialhmYmMKbXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t`
)
func TestValidateJwtSignHS512Hmac(t *testing.T) {
type args struct {
secret string
token string
}
testData := []struct {
name string
args args
expectedResult string
isExpectErr bool
expectedErr error
}{
{
name: "1. Should be valid",
args: args{
secret: "thisisasecretkeyverysecret",
token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhbW91bnQiOjIwMDAwLCJjb3Vyc2VfY29kZSI6Im1lbWJ1MHVtenBmeDhqMyIsImNvdXJzZV9pZCI6NTU4MiwiY3JlYXRlZF9hdCI6MTY2MjAyNjQ5MSwiaW52b2ljZV9jb2RlIjoiaW52b2ljZS10cmFuc2FjdGlvbi10ZXN0MTExMDAyMiIsImludm9pY2VfdXJsIjoiaHR0cDovL3BsYXRmb3JtLmNvbS9pbnZvaWNlL0lOVjAwMDEiLCJwbGF0Zm9ybV9pZCI6Mywic2NoZWR1bGVfaWQiOjExLCJzdGF0dXMiOjAsInVzZXJfdWQiOjI1N30.twL4lZMBwkudun5k16JBFUkeC3REvbmiUWjR0paV6Al8SrSkLTw2ZoqUCxO96xjBsQOjmokk620PMGXgi2yDzQ",
},
expectedResult: `{"amount":20000,"course_code":"membu0umzpfx8j3","course_id":5582,"created_at":1662026491,"invoice_code":"invoice-transaction-test1110022","invoice_url":"http://platform.com/invoice/INV0001","platform_id":3,"schedule_id":11,"status":0,"user_ud":257}`,
},
{
name: "2. Given wrong secret Then Should be invalid",
args: args{
secret: "wrongsecret",
token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhbW91bnQiOjIwMDAwLCJjb3Vyc2VfY29kZSI6Im1lbWJ1MHVtenBmeDhqMyIsImNvdXJzZV9pZCI6NTU4MiwiY3JlYXRlZF9hdCI6MTY2MjAyNjQ5MSwiaW52b2ljZV9jb2RlIjoiaW52b2ljZS10cmFuc2FjdGlvbi10ZXN0MTExMDAyMiIsImludm9pY2VfdXJsIjoiaHR0cDovL3BsYXRmb3JtLmNvbS9pbnZvaWNlL0lOVjAwMDEiLCJwbGF0Zm9ybV9pZCI6Mywic2NoZWR1bGVfaWQiOjExLCJzdGF0dXMiOjAsInVzZXJfdWQiOjI1N30.twL4lZMBwkudun5k16JBFUkeC3REvbmiUWjR0paV6Al8SrSkLTw2ZoqUCxO96xjBsQOjmokk620PMGXgi2yDzQ",
},
isExpectErr: true,
expectedErr: errors.New("token signature is invalid: signature is invalid"),
},
{
name: "3. Given valid secret but token payload contained `exp` field with value expired Then Should be invalid",
args: args{
secret: "thisisasecretkeyverysecret",
token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhbW91bnQiOjIwMDAwLCJjb3Vyc2VfY29kZSI6Im1lbWJ1MHVtenBmeDhqMyIsImNvdXJzZV9pZCI6NTU4MiwiY3JlYXRlZF9hdCI6MTY2MjAyNjQ5MSwiaW52b2ljZV9jb2RlIjoiaW52b2ljZS10cmFuc2FjdGlvbi10ZXN0MTExMDAyMiIsImludm9pY2VfdXJsIjoiaHR0cDovL3BsYXRmb3JtLmNvbS9pbnZvaWNlL0lOVjAwMDEiLCJwbGF0Zm9ybV9pZCI6Mywic2NoZWR1bGVfaWQiOjExLCJzdGF0dXMiOjAsInVzZXJfdWQiOjI1NywiZXhwIjoxNTE2MjM5MDIyfQ.-wV2w0OIJNVeRlCYJkjKK6_92DPLFDVZSc2_-OYVLRtV4_Kwt5zo0HmhazO53jtN1m_t5uJbicZv6wSleHMVSA",
},
isExpectErr: true,
expectedErr: errors.New("token has invalid claims: token is expired"),
},
{
name: "4. Given valid secret and token payload contained `exp` field with value not expired Then Should be valid",
args: args{
secret: "thisisasecretkeyverysecret",
token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhbW91bnQiOjIwMDAwLCJjb3Vyc2VfY29kZSI6Im1lbWJ1MHVtenBmeDhqMyIsImNvdXJzZV9pZCI6NTU4MiwiY3JlYXRlZF9hdCI6MTY2MjAyNjQ5MSwiaW52b2ljZV9jb2RlIjoiaW52b2ljZS10cmFuc2FjdGlvbi10ZXN0MTExMDAyMiIsImludm9pY2VfdXJsIjoiaHR0cDovL3BsYXRmb3JtLmNvbS9pbnZvaWNlL0lOVjAwMDEiLCJwbGF0Zm9ybV9pZCI6Mywic2NoZWR1bGVfaWQiOjExLCJzdGF0dXMiOjAsInVzZXJfdWQiOjI1NywiZXhwIjoxMTUxNjIzOTAyMn0.rxHNMuXa2Hq7q6hMKgfheRdkil99AlE40PPNdFsUlsy98wX80jWCaBCqaBdAXg_iyWJV_SdtfCFtulmzzd_VKA",
},
expectedResult: `{"amount":20000,"course_code":"membu0umzpfx8j3","course_id":5582,"created_at":1662026491,"exp":11516239022,"invoice_code":"invoice-transaction-test1110022","invoice_url":"http://platform.com/invoice/INV0001","platform_id":3,"schedule_id":11,"status":0,"user_ud":257}`,
},
{
name: "5. valid jwt",
args: args{
secret: "k4RTU_pr4KERJA",
token: "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhbW91bnQiOjIwMDAwLCJjb3Vyc2VfY29kZSI6InRla25pbTQ3bW8zYXNsdSIsImNvdXJzZV9pZCI6MzA2LCJjcmVhdGVkX2F0IjoxNjYyNzA0ODA3LCJpbnZvaWNlX2NvZGUiOiJpbnZvaWNlLXRyYW5zYWN0aW9uLXRlc3QxMjM0NSIsInBsYXRmb3JtX2lkIjo1LCJzY2hlZHVsZV9pZCI6MjksInN0YXR1cyI6MCwidXJsX2ZpbGUiOiJodHRwOi8vcGxhdGZvcm0uY29tL2ludm9pY2UvSU5WMDAwMSIsInVzZXJfaWQiOjI1NX0.ZDzejHDRho46CyxC-LAU__g8c3lcDUOEjoWAZfVDE4et6U1V3ywmpHwtTkhaytJSlFPB-xTcPcyeto4QtAc4Gw",
},
expectedResult: `{"amount":20000,"course_code":"teknim47mo3aslu","course_id":306,"created_at":1662704807,"invoice_code":"invoice-transaction-test12345","platform_id":5,"schedule_id":29,"status":0,"url_file":"http://platform.com/invoice/INV0001","user_id":255}`,
},
}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
hmac := hash.NewJwtSignHS512Hmac([]byte(tt.args.secret))
actual, err := hmac.Validate(context.TODO(), tt.args.token)
if !tt.isExpectErr {
if err != nil {
t.Error(err)
return
}
b, _ := json.Marshal(actual)
actualStr := string(b)
if tt.expectedResult != actualStr {
t.Errorf("expected %s got %s", tt.expectedResult, actualStr)
return
}
} else {
if err == nil {
t.Error("expect err got nil")
return
}
if tt.expectedErr.Error() != err.Error() {
t.Errorf("expected %s got %s", tt.expectedErr.Error(), err.Error())
return
}
}
})
}
}
func TestValidateJwtSignRS256Hmac(t *testing.T) {
type args struct {
privateKey *rsa.PrivateKey
publicKey *rsa.PublicKey
token string
}
testData := []struct {
name string
args args
expectedResult string
isExpectErr bool
expectedErr error
}{
{
name: "1. Should be valid",
args: args{
privateKey: func() *rsa.PrivateKey {
key, _ := hash.LoadPrivateKeyFromBase64Encoded(privateKeyBase64)
return key
}(),
publicKey: func() *rsa.PublicKey {
key, _ := hash.LoadPublicKeyFromBase64Encoded(publicKeyBase64)
return key
}(),
token: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6dHJ1ZSwiaWF0IjoxNTE2MjM5MDIyLCJuYW1lIjoiSm9obiBEb2UiLCJzdWIiOiIxMjM0NTY3ODkwIn0.LTP39t13r4FAMKnZd1kgVR4389oNwYtBy-NBwwkFkCmpr8bwG7rFWkZMDBMJPvgyp1aHCeIEiyJWxb0lPM0Kq6o8JAdfFcORT0pkZ0OCuIfvpK92s88XPLuLbf0F2vgzrjMMnKj3DZRVf08YGpPlRBamRuJfL8Y4Y47j-IhqyJ2M_mWqiSrHLoS7AJwjLh3y229ooBWYcthuDAAHXT312hzjgp7ShE10HK1rRTLFKNTM6V9jlWFGa8NUb2WJsC7Fx3haKauwtYGH3qmfyHQsSTWmpARaEPaG4XumvfRlhaAzwMFGZAr0vXPRdMqPkPRX0-dPnLEC0in0YxocXYRKsw",
},
expectedResult: `{"admin":true,"iat":1516239022,"name":"John Doe","sub":"1234567890"}`,
},
}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
hmac := hash.NewJwtSignRS256Hmac(tt.args.privateKey, tt.args.publicKey)
actual, err := hmac.Validate(context.TODO(), tt.args.token)
if !tt.isExpectErr {
if err != nil {
t.Error(err)
return
}
b, _ := json.Marshal(actual)
actualStr := string(b)
if tt.expectedResult != actualStr {
t.Errorf("expected %s got %s", tt.expectedResult, actualStr)
return
}
} else {
if err == nil {
t.Error("expect err got nil")
return
}
if tt.expectedErr.Error() != err.Error() {
t.Errorf("expected %s got %s", tt.expectedErr.Error(), err.Error())
return
}
}
})
}
}
func TestGenerateJwtSignRS256Hmac(t *testing.T) {
type args struct {
payload []byte
}
testData := []struct {
name string
args args
expectedResult string
}{
{
name: "1. Should be valid",
args: args{
payload: []byte(`{"admin":true,"iat":1516239022,"name":"John Doe","sub":"1234567890"}`),
},
expectedResult: `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6dHJ1ZSwiaWF0IjoxNTE2MjM5MDIyLCJuYW1lIjoiSm9obiBEb2UiLCJzdWIiOiIxMjM0NTY3ODkwIn0.LTP39t13r4FAMKnZd1kgVR4389oNwYtBy-NBwwkFkCmpr8bwG7rFWkZMDBMJPvgyp1aHCeIEiyJWxb0lPM0Kq6o8JAdfFcORT0pkZ0OCuIfvpK92s88XPLuLbf0F2vgzrjMMnKj3DZRVf08YGpPlRBamRuJfL8Y4Y47j-IhqyJ2M_mWqiSrHLoS7AJwjLh3y229ooBWYcthuDAAHXT312hzjgp7ShE10HK1rRTLFKNTM6V9jlWFGa8NUb2WJsC7Fx3haKauwtYGH3qmfyHQsSTWmpARaEPaG4XumvfRlhaAzwMFGZAr0vXPRdMqPkPRX0-dPnLEC0in0YxocXYRKsw`,
},
}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
privateKey, err := hash.LoadPrivateKeyFromBase64Encoded(privateKeyBase64)
if err != nil {
t.Error(err)
return
}
publicKey, err := hash.LoadPublicKeyFromBase64Encoded(publicKeyBase64)
if err != nil {
t.Error(err)
return
}
hmac := hash.NewJwtSignRS256Hmac(privateKey, publicKey)
claims, err := hash.PayloadToJwtClaims(tt.args.payload)
if err != nil {
t.Error(err)
return
}
actualStr, err := hmac.Sign(context.TODO(), claims)
if err != nil {
t.Error(err)
return
}
if tt.expectedResult != actualStr {
t.Errorf("expected %s got %s", tt.expectedResult, actualStr)
return
}
})
}
}
func TestGenerateJwtSignSHA256Hmac(t *testing.T) {
type args struct {
payload []byte
secret string
}
testData := []struct {
name string
args args
expectedResult string
}{
{
name: "1. Should be valid",
args: args{
payload: func() []byte {
now := time.Unix(1516239022, 0)
m := map[string]any{
"appKey": "appId",
"mn": "00000",
"role": 0,
"iat": now.Unix(),
"exp": now.Add(time.Duration(48) * time.Hour).Unix(),
"tokenExp": now.Add(time.Duration(48) * time.Hour).Unix(),
}
bytes, _ := json.Marshal(m)
return bytes
}(),
secret: "appSecret",
},
expectedResult: `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBLZXkiOiJhcHBJZCIsImV4cCI6MTUxNjQxMTgyMiwiaWF0IjoxNTE2MjM5MDIyLCJtbiI6IjAwMDAwIiwicm9sZSI6MCwidG9rZW5FeHAiOjE1MTY0MTE4MjJ9.QPK9GTS3qTd5FedeLZ94izH-XAysjjVMh8pWL8FLjuM`,
},
}
for _, tt := range testData {
t.Run(tt.name, func(t *testing.T) {
hmac := hash.NewJwtSignSHA256Hmac(tt.args.secret)
claims, err := hash.PayloadToJwtClaims(tt.args.payload)
if err != nil {
t.Error(err)
return
}
actualStr, err := hmac.Sign(context.TODO(), claims)
if err != nil {
t.Error(err)
return
}
if tt.expectedResult != actualStr {
t.Errorf("expected %s got %s", tt.expectedResult, actualStr)
return
}
})
}
}