From d68e8aa6b1ead2d7674dd887b1aebfe67d19bbfc Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Fri, 17 Dec 2021 01:45:09 +0800 Subject: [PATCH] update --- auth/api_auth.go | 1 + auth/jwt.go | 3 +++ server/middleware.go | 3 +++ 3 files changed, 7 insertions(+) diff --git a/auth/api_auth.go b/auth/api_auth.go index 2c42e1f..a8bf8ec 100644 --- a/auth/api_auth.go +++ b/auth/api_auth.go @@ -54,6 +54,7 @@ func ApiLogin(c *gin.Context) { } err = SetClaims(c, &Claims{ Uid: usr.ID, + User: usr.Username, Groups: grps, }) if err != nil { diff --git a/auth/jwt.go b/auth/jwt.go index 8ae4601..38adacd 100644 --- a/auth/jwt.go +++ b/auth/jwt.go @@ -15,9 +15,11 @@ import ( ) const GinClaimKey = "claim" +const GinUserKey = "user" type Claims struct { Uid string `json:"uid,omitempty"` + User string `json:"usr,omitempty"` Groups []string `json:"grp,omitempty"` Endpoint string `json:"ept,omitempty"` IP string `json:"ip,omitempty"` @@ -155,6 +157,7 @@ func (srv Auth) Middleware(c *gin.Context) { claims, err := srv.GetClaims(c) if err == nil { c.Set(GinClaimKey, claims) + c.Set(GinUserKey, claims.User) if srv.AutoRenew && claims.ExpiresAt != 0 { claims.ExpiresAt = time.Now().Unix() + srv.TokenExpire tok, err := srv.NewToken(*claims) diff --git a/server/middleware.go b/server/middleware.go index e8de7bc..c8b5ead 100644 --- a/server/middleware.go +++ b/server/middleware.go @@ -39,6 +39,9 @@ func (srv *Service) Default(c *gin.Context) { if srv.SkipLog != nil && srv.SkipLog(c) { return } + if usr, ok := c.Get("user"); ok { + cl.Str("user", usr.(string)) + } cl. Str("method", c.Request.Method). Str("ip", c.ClientIP()).