gin-jwt icon indicating copy to clipboard operation
gin-jwt copied to clipboard

Exception after changing IdentityKey

Open superarts opened this issue 7 years ago • 3 comments

Suppose I set my auth IdentityKey to "uid", call login, and get a token and it works for the auth group. Now I change my auth IdentityKey to "userid", restart server, and try to call auth group again.

Expected behavior should be invalid token or some other 4xx errors, but instead I got 500:

interface conversion: interface {} is nil, not string
/usr/local/Cellar/go/1.11/libexec/src/runtime/panic.go:513 (0x102bbc8)
	gopanic: reflectcall(nil, unsafe.Pointer(d.fn), deferArgs(d), uint32(d.siz), uint32(d.siz))
/usr/local/Cellar/go/1.11/libexec/src/runtime/iface.go:248 (0x100ac45)
	panicdottypeE: panic(&TypeAssertionError{iface, have, want, ""})
/Users/leo/prj/web/go/src/hindsight/auth/auth.go:31 (0x15cd078)
	GetMiddleware.func2: username := claims[user.IdentityKey].(string)
/Users/leo/prj/web/go/src/github.com/appleboy/gin-jwt/auth_jwt.go:340 (0x1545378)
	(*GinJWTMiddleware).middlewareImpl: identity := mw.IdentityHandler(c)
/Users/leo/prj/web/go/src/github.com/appleboy/gin-jwt/auth_jwt.go:328 (0x15480c3)
	(*GinJWTMiddleware).MiddlewareFunc.func1: mw.middlewareImpl(c)
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/context.go:108 (0x152b7a2)
	(*Context).Next: c.handlers[c.index](c)
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/recovery.go:48 (0x153cb89)
	RecoveryWithWriter.func1: c.Next()
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/context.go:108 (0x152b7a2)
	(*Context).Next: c.handlers[c.index](c)
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/logger.go:84 (0x153be91)
	LoggerWithWriter.func1: c.Next()
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/context.go:108 (0x152b7a2)
	(*Context).Next: c.handlers[c.index](c)
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/gin.go:367 (0x153415a)
	(*Engine).handleHTTPRequest: c.Next()
/Users/leo/prj/web/go/src/github.com/gin-gonic/gin/gin.go:332 (0x1533981)
	(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.11/libexec/src/net/http/server.go:2741 (0x127fefa)
	serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.11/libexec/src/net/http/server.go:1847 (0x127c165)
	(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.11/libexec/src/runtime/asm_amd64.s:1333 (0x1058c80)
	goexit: BYTE	$0x90	// NOP

superarts avatar Sep 18 '18 03:09 superarts

Could you post the example code and what is your commit version of gin-jwt?

claims[user.IdentityKey].(string)

Please check the interface to string in go

if _, ok := claims[user.IdentityKey].(string); ok {
....
}

appleboy avatar Sep 18 '18 03:09 appleboy

Here's my code, whereas user.IdentityKey is "uid":

		IdentityHandler: func(c *gin.Context) interface{} {
			claims := jwt.ExtractClaims(c)
			username := claims[user.IdentityKey].(string)
			return &user.User{
				Username: username,
			}
		},

The whole file can be found here: https://github.com/hindsight-inc/hindsight-api/blob/master/auth/auth.go

From my debugging, claims["xxx"] is nil so it cannot be converted to String. I guess that's because the IdentityKey has been changed to something else. Maybe there's a non-null checking is missing here?

superarts avatar Sep 18 '18 16:09 superarts

Just restart your server and generate new tokens.

On Mon, Oct 22, 2018, 1:57 AM potsables [email protected] wrote:

@superarts https://github.com/superarts did you ever find a solution? I think I'm running into this issue right now where my claims["id"] is alwasy returning nil

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/appleboy/gin-jwt/issues/170#issuecomment-431746044, or mute the thread https://github.com/notifications/unsubscribe-auth/AAd--b8gxykWrzm9Wep_CO0MzAapXX7Dks5unV5CgaJpZM4WtFM1 .

superarts avatar Oct 22 '18 17:10 superarts