@@ -1094,3 +1094,49 @@ func TestBadTokenOnRefreshHandler(t *testing.T) {
1094
1094
assert .Equal (t , http .StatusUnauthorized , r .Code )
1095
1095
})
1096
1096
}
1097
+
1098
+ func TestExpiredField (t * testing.T ) {
1099
+ // the middleware to test
1100
+ authMiddleware , _ := New (& GinJWTMiddleware {
1101
+ Realm : "test zone" ,
1102
+ Key : key ,
1103
+ Timeout : time .Hour ,
1104
+ Authenticator : defaultAuthenticator ,
1105
+ })
1106
+
1107
+ handler := ginHandler (authMiddleware )
1108
+
1109
+ r := gofight .New ()
1110
+
1111
+ token := jwt .New (jwt .GetSigningMethod ("HS256" ))
1112
+ claims := token .Claims .(jwt.MapClaims )
1113
+ claims ["identity" ] = "admin"
1114
+ claims ["orig_iat" ] = 0
1115
+ tokenString , _ := token .SignedString (key )
1116
+
1117
+ r .GET ("/auth/hello" ).
1118
+ SetHeader (gofight.H {
1119
+ "Authorization" : "Bearer " + tokenString ,
1120
+ }).
1121
+ Run (handler , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
1122
+ message := gjson .Get (r .Body .String (), "message" )
1123
+
1124
+ assert .Equal (t , ErrMissingExpField .Error (), message .String ())
1125
+ assert .Equal (t , http .StatusBadRequest , r .Code )
1126
+ })
1127
+
1128
+ // wrong format
1129
+ claims ["exp" ] = "test"
1130
+ tokenString , _ = token .SignedString (key )
1131
+
1132
+ r .GET ("/auth/hello" ).
1133
+ SetHeader (gofight.H {
1134
+ "Authorization" : "Bearer " + tokenString ,
1135
+ }).
1136
+ Run (handler , func (r gofight.HTTPResponse , rq gofight.HTTPRequest ) {
1137
+ message := gjson .Get (r .Body .String (), "message" )
1138
+
1139
+ assert .Equal (t , ErrWrongFormatOfExp .Error (), message .String ())
1140
+ assert .Equal (t , http .StatusBadRequest , r .Code )
1141
+ })
1142
+ }
0 commit comments