[BUG] jwt Claims - Invalid for individual custom struct fields
Closed this issue · 2 comments
baagod commented
main.go
:
app := iris.New()
app.UseRouter(accesslog.New(app.Logger().Printer).Handler)
app.UseRouter(recover2.New())
app.Get("kf/auth", api.Auth)
app.Use(api.VerifyMiddleware)
app.Get("/test", func(c iris.Context) {
admin := jwt.Get(c).(*tables.AdminUser)
// test - Is inconsistent with the AdminUser given in auth,types.NullString All defaults。
// &{1 { false} { false} { false} { false} { false} { false} { false} 1 3 { false} 2010-08-15 00:37:37 { false} 0 { false} { false} { false} 0}
fmt.Println("test:", admin)
})
_ = app.Listen(":188")
api.Auth
:
func Auth(c iris.Context) {
// ...
var user tables.AdminUser
if err = db.Where(squirrel.Eq{"username": splits[0], "password": splits[1]}).
First(&user).Error; err != nil {
c.StopWithError(iris.StatusInternalServerError, err)
return
}
// auth:
// {1 {admin true} {jdw123 true} {|0| true} {符之云 true} { true} {20200414093409 true} {test pid str true} 1 3 {1 true} 2010-08-15 00:37:37 { true} 0 { true} { true} { true} 0}
fmt.Println("auth:", user)
token, err := signer.Sign(user)
if err != nil {
c.StopWithError(iris.StatusInternalServerError, err)
return
}
_, _ = c.Write(token)
}
AdminUser
:
type AdminUser struct {
ID int `gorm:"column:ID;type:int;primaryKey" json:"ID"`
Username types.NullString `gorm:"column:username;type:varchar(50)" json:"username"`
Password types.NullString `gorm:"column:password;type:varchar(50)" json:"password"`
Role types.NullString `gorm:"column:role;type:varchar(255)" json:"role"`
People types.NullString `gorm:"column:people;type:varchar(60)" json:"people"`
Area types.NullString `gorm:"column:area;type:varchar(50)" json:"area"`
EditTime types.NullString `gorm:"column:edit_time;type:varchar(50)" json:"edit_time"`
PIDSTR types.NullString `gorm:"column:PIDSTR;type:varchar(200)" json:"PIDSTR"`
Bmid int `gorm:"column:bmid;type:int;default:0" json:"bmid"`
Bmgid int `gorm:"column:bmgid;type:int;default:0" json:"bmgid"`
Kind types.NullString `gorm:"column:kind;type:varchar(100)" json:"kind"`
InsertTime types.Time `gorm:"column:insert_time;type:datetime;default:getdate()" json:"insert_time"`
ProvStr types.NullString `gorm:"column:prov_str;type:varchar(200)" json:"prov_str"`
CheckPID int `gorm:"column:CheckPID;type:int;default:0" json:"CheckPID"`
Sname types.NullString `gorm:"column:sname;type:varchar(20)" json:"sname"`
UserQQ types.NullString `gorm:"column:user_qq;type:varchar(20)" json:"user_qq"`
UserMobile types.NullString `gorm:"column:user_mobile;type:varchar(20)" json:"user_mobile"`
Issyn int `gorm:"column:issyn;type:int;default:0" json:"issyn"`
}
But I found that there is a value for types.Time
(2010-08-15 00:37:37),At the time of request, I found that it was assigned by UnmarshalJSON
, but the output types.NullString
is always ""
。
type NullString struct {
sql.NullString
}
func (v *NullString) Scan(value interface{}) error {
v.Valid = value == nil
if s, ok := value.(string); ok && v.Valid {
v.String = s
}
return nil
}
func (v NullString) Value() (driver.Value, error) {
if !v.Valid {
return nil, nil
}
return v.String, nil
}
func (v NullString) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String)
}
func (v NullString) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
if v.Valid = len(s) > 0; v.Valid {
v.String = s // s = ""
}
return nil
}
kataras commented
@hjzCy why is that an iris or jwt issue? Can you please provide me a simpler code snippet that I can directly run and reproduce your issue?