Skip to content

Commit

Permalink
[feat]custom jwt attr
Browse files Browse the repository at this point in the history
  • Loading branch information
piupuer committed Oct 15, 2024
1 parent a220bc0 commit c9c7989
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/go-cinch/common/i18n v1.0.6
github.com/go-cinch/common/id v1.0.4
github.com/go-cinch/common/idempotent v1.0.4
github.com/go-cinch/common/jwt v1.0.3
github.com/go-cinch/common/jwt v1.0.4
github.com/go-cinch/common/log v1.1.1
github.com/go-cinch/common/middleware/i18n v1.0.5
github.com/go-cinch/common/middleware/logging v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ github.com/go-cinch/common/id v1.0.4 h1:bMjcxMq5lL19o2a/ty6OnS8tIn7kc98FznAq2vyp
github.com/go-cinch/common/id v1.0.4/go.mod h1:ZeuOCZyx1ZKuCexzLRW0gBCnwDUKH8XDs1S0dNv4Z+k=
github.com/go-cinch/common/idempotent v1.0.4 h1:E/Ab+FHWjs1ym0U0mips6MyHEzucEdedmVWA22U3du8=
github.com/go-cinch/common/idempotent v1.0.4/go.mod h1:t4rBikz+RhpmF0tdGx+rm0Z1FzDy/6GtelgFB7RlaiY=
github.com/go-cinch/common/jwt v1.0.3 h1:/jznvjevW+2KsmWm666O1NxkHLSfVmrjoWwkuWAqewk=
github.com/go-cinch/common/jwt v1.0.3/go.mod h1:++p7kddWh9SyCYIYWEb5mfiR+ljKC3I5puiyjz7oGNs=
github.com/go-cinch/common/jwt v1.0.4 h1:PWLIQ/HaMhQSkyUafBCjQDNXwz1mzeCzsTCbgeKEeRI=
github.com/go-cinch/common/jwt v1.0.4/go.mod h1:++p7kddWh9SyCYIYWEb5mfiR+ljKC3I5puiyjz7oGNs=
github.com/go-cinch/common/log v1.1.1 h1:9ot3Qw4BKDfOZ7IC/3ZreuYJb953YD7pmM6ev6xT9FY=
github.com/go-cinch/common/log v1.1.1/go.mod h1:O4k/ArEdZS6c+YPKdESWJnVfhQ0QlwtIQ6mYxJZt5po=
github.com/go-cinch/common/middleware/i18n v1.0.5 h1:SmxEMljqYF8xraaaa3KZgvNbKIE06ShQAZ34w87wkvs=
Expand Down
8 changes: 5 additions & 3 deletions internal/biz/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (uc *UserUseCase) find(ctx context.Context, action string, condition *FindU

func (uc *UserUseCase) InfoFromCtx(ctx context.Context) (rp *UserInfo) {
user := jwt.FromServerContext(ctx)
return uc.Info(ctx, user.Code)
return uc.Info(ctx, user.Attrs["code"])
}

func (uc *UserUseCase) Info(ctx context.Context, code string) (rp *UserInfo) {
Expand Down Expand Up @@ -259,8 +259,10 @@ func (uc *UserUseCase) Login(ctx context.Context, item *Login) (rp *LoginToken,
return
}
authUser := jwt.User{
Code: status.Code,
Platform: status.Platform,
Attrs: map[string]string{
"code": status.Code,
"platform": status.Platform,
},
}
token, expireTime := authUser.CreateToken(uc.c.Server.Jwt.Key, uc.c.Server.Jwt.Expires)
rp.Token = token
Expand Down
2 changes: 1 addition & 1 deletion internal/server/middleware/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func parseJwt(ctx context.Context, c *conf.Bootstrap, client redis.UniversalClie
if err != nil {
return
}
ctx = jwtLocal.NewServerContext(ctx, info.Claims)
ctx = jwtLocal.NewServerContext(ctx, info.Claims, "code", "platform")
user = jwtLocal.FromServerContext(ctx)
client.Set(ctx, key, utils.Struct2Json(user), jwtTokenCacheExpire)
return
Expand Down
14 changes: 8 additions & 6 deletions internal/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (s *AuthService) Permission(ctx context.Context, req *auth.PermissionReques
rp = &emptypb.Empty{}
user := jwt.FromServerContext(ctx)
r := biz.CheckPermission{
UserCode: user.Code,
UserCode: user.Attrs["code"],
}
if req.Resource != nil {
r.Resource = *req.Resource
Expand All @@ -156,10 +156,12 @@ func (s *AuthService) Permission(ctx context.Context, req *auth.PermissionReques
err = biz.ErrNoPermission(ctx)
return
}
info := s.user.Info(ctx, user.Code)
info := s.user.Info(ctx, user.Attrs["code"])
jwt.AppendToReplyHeader(ctx, jwt.User{
Code: info.Code,
Platform: info.Platform,
Attrs: map[string]string{
"code": info.Code,
"platform": info.Platform,
},
})
return
}
Expand All @@ -171,8 +173,8 @@ func (s *AuthService) Info(ctx context.Context, _ *emptypb.Empty) (rp *auth.Info
rp = &auth.InfoReply{}
rp.Permission = &auth.Permission{}
user := jwt.FromServerContext(ctx)
res := s.user.Info(ctx, user.Code)
permission := s.permission.GetByUserCode(ctx, user.Code)
res := s.user.Info(ctx, user.Attrs["code"])
permission := s.permission.GetByUserCode(ctx, user.Attrs["code"])
copierx.Copy(&rp.Permission, permission)
copierx.Copy(&rp, res)
return
Expand Down

0 comments on commit c9c7989

Please sign in to comment.