Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User related enhance #162

Merged
merged 4 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ run_rproxy_local.sh
run_server_local.sh
run_user_local.sh
cmd/csghub-server/__debug_*
run_dataviewer_local.sh

*/*/_api_test/
1 change: 0 additions & 1 deletion common/types/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

type CreateJWTReq struct {
UUID string `json:"uuid" binding:"required"`
CurrentUser string `json:"current_user" binding:"required"`
Organizations []string `json:"-"`
}

Expand Down
3 changes: 0 additions & 3 deletions user/component/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ func (c *JwtComponent) GenerateToken(ctx context.Context, req types.CreateJWTReq
if err != nil {
return nil, "", fmt.Errorf("failed to find user by uuid '%s',error: %w", req.UUID, err)
}
if u.Username != req.CurrentUser {
return nil, "", fmt.Errorf("user uuid '%s' does not match current user '%s'", req.UUID, req.CurrentUser)
}
expireAt := jwt.NewNumericDate(time.Now().Add(c.ValidTime))
claims = &types.JWTClaims{
UUID: u.UUID,
Expand Down
24 changes: 22 additions & 2 deletions user/component/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,23 @@ func (c *UserComponent) CanAdmin(ctx context.Context, username string) (bool, er
return user.CanAdmin(), nil
}

// GetInternal get *full* user info by username or uuid
//
// should only be called by other *internal* services
func (c *UserComponent) GetInternal(ctx context.Context, userNameOrUUID string, useUUID bool) (*types.User, error) {
var dbuser = new(database.User)
var err error
if useUUID {
dbuser, err = c.us.FindByUUID(ctx, userNameOrUUID)
} else {
*dbuser, err = c.us.FindByUsername(ctx, userNameOrUUID)
}
if err != nil {
return nil, fmt.Errorf("failed to find user by name or uuid '%s' in db,error:%w", userNameOrUUID, err)
}
return c.buildUserInfo(ctx, dbuser, false)
}

func (c *UserComponent) Get(ctx context.Context, userNameOrUUID, visitorName string, useUUID bool) (*types.User, error) {
var dbuser = new(database.User)
var err error
Expand Down Expand Up @@ -371,6 +388,10 @@ func (c *UserComponent) Get(ctx context.Context, userNameOrUUID, visitorName str
}
}

return c.buildUserInfo(ctx, dbuser, onlyBasicInfo)
}

func (c *UserComponent) buildUserInfo(ctx context.Context, dbuser *database.User, onlyBasicInfo bool) (*types.User, error) {
u := types.User{
Username: dbuser.Username,
Nickname: dbuser.NickName,
Expand Down Expand Up @@ -503,8 +524,7 @@ func (c *UserComponent) Signin(ctx context.Context, code, state string) (*types.
}()
}
hubToken, signed, err := c.jwtc.GenerateToken(ctx, types.CreateJWTReq{
UUID: dbu.UUID,
CurrentUser: dbu.Username,
UUID: dbu.UUID,
})
if err != nil {
return nil, "", fmt.Errorf("failed to generate jwt token,error:%w", err)
Expand Down
9 changes: 8 additions & 1 deletion user/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,16 @@ func (h *UserHandler) Delete(ctx *gin.Context) {
// @Router /user/{username} [get]
func (h *UserHandler) Get(ctx *gin.Context) {
visitorName := httpbase.GetCurrentUser(ctx)
authType := httpbase.GetAuthType(ctx)
userNameOrUUID := ctx.Param("username")
useUUID := ctx.Query("type") == "uuid"
user, err := h.c.Get(ctx, userNameOrUUID, visitorName, useUUID)
var user *types.User
var err error
if authType == httpbase.AuthTypeApiKey {
user, err = h.c.GetInternal(ctx, userNameOrUUID, useUUID)
} else {
user, err = h.c.Get(ctx, userNameOrUUID, visitorName, useUUID)
}
if err != nil {
slog.Error("Failed to get user", slog.Any("error", err))
httpbase.ServerError(ctx, err)
Expand Down