11package jwtauth_test
22
33import (
4- "context"
54 "crypto/x509"
65 "encoding/pem"
76 "fmt"
@@ -17,8 +16,9 @@ import (
1716
1817 "github.com/go-chi/chi/v5"
1918 "github.com/go-chi/jwtauth/v5"
20- "github.com/lestrrat-go/jwx/v2/jwa"
21- "github.com/lestrrat-go/jwx/v2/jwt"
19+ "github.com/lestrrat-go/jwx/v3/jwa"
20+ "github.com/lestrrat-go/jwx/v3/jwt"
21+ "github.com/lestrrat-go/jwx/v3/transform"
2222)
2323
2424var (
@@ -28,19 +28,27 @@ var (
2828 TokenAuthRS256 * jwtauth.JWTAuth
2929
3030 PrivateKeyRS256String = `-----BEGIN RSA PRIVATE KEY-----
31- MIIBOwIBAAJBALxo3PCjFw4QjgOX06QCJIJBnXXNiEYwDLxxa5/7QyH6y77nCRQy
32- J3x3UwF9rUD0RCsp4sNdX5kOQ9PUyHyOtCUCAwEAAQJARjFLHtuj2zmPrwcBcjja
33- IS0Q3LKV8pA0LoCS+CdD+4QwCxeKFq0yEMZtMvcQOfqo9x9oAywFClMSlLRyl7ng
34- gQIhAOyerGbcdQxxwjwGpLS61Mprf4n2HzjwISg20cEEH1tfAiEAy9dXmgQpDPir
35- C6Q9QdLXpNgSB+o5CDqfor7TTyTCovsCIQDNCfpu795luDYN+dvD2JoIBfrwu9v2
36- ZO72f/pm/YGGlQIgUdRXyW9kH13wJFNBeBwxD27iBiVj0cbe8NFUONBUBmMCIQCN
37- jVK4eujt1lm/m60TlEhaWBC3p+3aPT2TqFPUigJ3RQ==
31+ MIICXgIBAAKBgQDsH6T+WrdRLKHEhbhbnItRo7X5tj0xssOSCJUiZbCHr52XftIr
32+ hBD6HxbGaKUEzuaCDYGEcQZZRJ1KHfYmJtXPCz4Zp3qlhjNugvTaZoFtQ8RqiWVY
33+ cHqCY6cmI+3cq2mVrd7MstpXKhC8dZ2MZnzx/zqaeiV21SiwxHed8LmWmQIDAQAB
34+ AoGAff9I0L1hkrxJOg/M133KTe8Y3L4lG07z0wonYmp274CDjGKNDdF0KbPLOGaA
35+ n/czw3Qnh5+0LpBRikpAng0dC06z0YnyzrkoPPawC4s2zJeY3NnajK9IfRAAVlby
36+ cIJVmEL/xF3FFHhCfrJNWd+zthcHxCATJOBpH2pwhb4WLfECQQD/geZ/B6p8WlGb
37+ amHFhBd/hQN6cq63RGujf3ecz5H+h4RqFyycaVr3t8QZBBd3O3jRB9FCcan2IxRa
38+ UoYNGNB9AkEA7JQtfmb0p8cTHiDyV6qb8aNJFWipwQVVMmpaXvfC6Aue5uJiyHnx
39+ iScLsj1ozewCgTvzL7MAsfj0k6qX3c01TQJBAPL2JCdhM8XB4N4Hf+dhHzMcWd1j
40+ Fi6hOjWjrSsI2owNc2Wqmbo2GNF8BlW/ZUz02YLzixJCoVqzqtPkqyHjGcUCQQDk
41+ msrbOeFvvo5arrt+uv21oXMdnOVr/xs0fFCXNBLC53fE4z1RO4SKY5CJy41abpR9
42+ DNERZodlcovjpRTa31CBAkEAw8geqJ1+cfEDZYfJxJigFSwbwoLw6BH+GD4KAEdX
43+ G1u9SGGYP19eC2mpQei4V5MqAYEbb82bqcebhwg8kAReNQ==
3844-----END RSA PRIVATE KEY-----
3945`
4046
4147 PublicKeyRS256String = `-----BEGIN PUBLIC KEY-----
42- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALxo3PCjFw4QjgOX06QCJIJBnXXNiEYw
43- DLxxa5/7QyH6y77nCRQyJ3x3UwF9rUD0RCsp4sNdX5kOQ9PUyHyOtCUCAwEAAQ==
48+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDsH6T+WrdRLKHEhbhbnItRo7X5
49+ tj0xssOSCJUiZbCHr52XftIrhBD6HxbGaKUEzuaCDYGEcQZZRJ1KHfYmJtXPCz4Z
50+ p3qlhjNugvTaZoFtQ8RqiWVYcHqCY6cmI+3cq2mVrd7MstpXKhC8dZ2MZnzx/zqa
51+ eiV21SiwxHed8LmWmQIDAQAB
4452-----END PUBLIC KEY-----
4553`
4654
@@ -67,7 +75,7 @@ DLxxa5/7QyH6y77nCRQyJ3x3UwF9rUD0RCsp4sNdX5kOQ9PUyHyOtCUCAwEAAQ==
6775)
6876
6977func init () {
70- TokenAuthHS256 = jwtauth .New (jwa .HS256 .String (), TokenSecret , nil , jwt .WithAcceptableSkew (30 * time .Second ))
78+ TokenAuthHS256 = jwtauth .New (jwa .HS256 () .String (), TokenSecret , nil , jwt .WithAcceptableSkew (30 * time .Second ))
7179}
7280
7381//
@@ -181,17 +189,17 @@ func TestSimpleRSA(t *testing.T) {
181189
182190 privateKey , err := x509 .ParsePKCS1PrivateKey (privateKeyBlock .Bytes )
183191 if err != nil {
184- t .Fatalf (err .Error ())
192+ t .Fatal (err .Error ())
185193 }
186194
187195 publicKeyBlock , _ := pem .Decode ([]byte (PublicKeyRS256String ))
188196
189197 publicKey , err := x509 .ParsePKIXPublicKey (publicKeyBlock .Bytes )
190198 if err != nil {
191- t .Fatalf (err .Error ())
199+ t .Fatal (err .Error ())
192200 }
193201
194- TokenAuthRS256 = jwtauth .New (jwa .RS256 .String (), privateKey , publicKey )
202+ TokenAuthRS256 = jwtauth .New (jwa .RS256 () .String (), privateKey , publicKey )
195203
196204 claims := map [string ]interface {}{
197205 "key" : "val" ,
@@ -209,9 +217,9 @@ func TestSimpleRSA(t *testing.T) {
209217 t .Fatalf ("Failed to decode token string %s\n " , err .Error ())
210218 }
211219
212- tokenClaims , err := token . AsMap ( context . Background ())
213- if err != nil {
214- t .Fatal ( err .Error ())
220+ tokenClaims := map [ string ] interface {}{}
221+ if err := transform . AsMap ( token , tokenClaims ); err != nil {
222+ t .Fatalf ( "Failed to get claims %s \n " , err .Error ())
215223 }
216224
217225 if ! reflect .DeepEqual (claims , tokenClaims ) {
@@ -220,7 +228,7 @@ func TestSimpleRSA(t *testing.T) {
220228}
221229
222230func TestSimpleRSAVerifyOnly (t * testing.T ) {
223- tokenString := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWwiLCJrZXkyIjoidmFsMiIsImtleTMiOiJ2YWwzIn0.kLEK3FZZPsAlQNKR5yHyjRyrlCJFhvKmrh7o-GqDT_zaGQgvb0Dufp8uNSMeOFAlLGK5FbKX7BckjJqfvEyrTQ "
231+ tokenString := "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJrZXkiOiJ2YWwiLCJrZXkyIjoidmFsMiIsImtleTMiOiJ2YWwzIn0.IK0G0Qi_c6N6uHRokHMSHQEeYxoi_T73A4RdEzJIfnbs5kA5hF0UhApSWUMZfsTYFNC2buYvWqbyj2kDdXcStpqTUPENGTKvJi66puwhN16BqEOS-jb7kVyf3vWif7XabY0_5S8H_aeqazaj4FemHvWnywJznuMWJRXWw83edpA "
224232 claims := map [string ]interface {}{
225233 "key" : "val" ,
226234 "key2" : "val2" ,
@@ -230,10 +238,10 @@ func TestSimpleRSAVerifyOnly(t *testing.T) {
230238 publicKeyBlock , _ := pem .Decode ([]byte (PublicKeyRS256String ))
231239 publicKey , err := x509 .ParsePKIXPublicKey (publicKeyBlock .Bytes )
232240 if err != nil {
233- t .Fatalf (err .Error ())
241+ t .Fatal (err .Error ())
234242 }
235243
236- TokenAuthRS256 = jwtauth .New (jwa .RS256 .String (), nil , publicKey )
244+ TokenAuthRS256 = jwtauth .New (jwa .RS256 () .String (), nil , publicKey )
237245
238246 _ , _ , err = TokenAuthRS256 .Encode (claims )
239247 if err == nil {
@@ -245,9 +253,9 @@ func TestSimpleRSAVerifyOnly(t *testing.T) {
245253 t .Fatalf ("Failed to decode token string %s\n " , err .Error ())
246254 }
247255
248- tokenClaims , err := token . AsMap ( context . Background ())
249- if err != nil {
250- t .Fatal ( err .Error ())
256+ tokenClaims := map [ string ] interface {}{}
257+ if err := transform . AsMap ( token , tokenClaims ); err != nil {
258+ t .Fatalf ( "Failed to get claims %s \n " , err .Error ())
251259 }
252260
253261 if ! reflect .DeepEqual (claims , tokenClaims ) {
@@ -306,42 +314,42 @@ func TestMore(t *testing.T) {
306314
307315 // sending unauthorized requests
308316 if status , resp := testRequest (t , ts , "GET" , "/admin" , nil , nil ); status != 401 || resp != "token is unauthorized\n " {
309- t .Fatalf (resp )
317+ t .Fatal (resp )
310318 }
311319
312320 h := http.Header {}
313321 h .Set ("Authorization" , "BEARER " + newJwtToken ([]byte ("wrong" ), map [string ]interface {}{}))
314322 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
315- t .Fatalf (resp )
323+ t .Fatal (resp )
316324 }
317325 h .Set ("Authorization" , "BEARER asdf" )
318326 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
319- t .Fatalf (resp )
327+ t .Fatal (resp )
320328 }
321329 // wrong token secret and wrong alg
322330 h .Set ("Authorization" , "BEARER " + newJwt512Token ([]byte ("wrong" ), map [string ]interface {}{}))
323331 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
324- t .Fatalf (resp )
332+ t .Fatal (resp )
325333 }
326334 // correct token secret but wrong alg
327335 h .Set ("Authorization" , "BEARER " + newJwt512Token (TokenSecret , map [string ]interface {}{}))
328336 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 401 || resp != "token is unauthorized\n " {
329- t .Fatalf (resp )
337+ t .Fatal (resp )
330338 }
331339
332340 h = newAuthHeader (map [string ]interface {}{"exp" : jwtauth .EpochNow () - 1000 })
333341 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 401 || resp != "token is expired\n " {
334- t .Fatalf (resp )
342+ t .Fatal (resp )
335343 }
336344
337345 // sending authorized requests
338346 if status , resp := testRequest (t , ts , "GET" , "/" , nil , nil ); status != 200 || resp != "welcome" {
339- t .Fatalf (resp )
347+ t .Fatal (resp )
340348 }
341349
342350 h = newAuthHeader ((map [string ]interface {}{"user_id" : 31337 , "exp" : jwtauth .ExpireIn (5 * time .Minute )}))
343351 if status , resp := testRequest (t , ts , "GET" , "/admin" , h , nil ); status != 200 || resp != "protected, user:31337" {
344- t .Fatalf (resp )
352+ t .Fatal (resp )
345353 }
346354}
347355
@@ -499,7 +507,7 @@ func newJwtToken(secret []byte, claims ...map[string]interface{}) string {
499507 }
500508 }
501509
502- tokenPayload , err := jwt .Sign (token , jwt .WithKey (jwa .HS256 , secret ))
510+ tokenPayload , err := jwt .Sign (token , jwt .WithKey (jwa .HS256 () , secret ))
503511 if err != nil {
504512 log .Fatal (err )
505513 }
@@ -514,7 +522,7 @@ func newJwt512Token(secret []byte, claims ...map[string]interface{}) string {
514522 token .Set (k , v )
515523 }
516524 }
517- tokenPayload , err := jwt .Sign (token , jwt .WithKey (jwa .HS512 , secret ))
525+ tokenPayload , err := jwt .Sign (token , jwt .WithKey (jwa .HS512 () , secret ))
518526 if err != nil {
519527 log .Fatal (err )
520528 }
0 commit comments