Skip to content

Commit

Permalink
cmd/httphandler: log user activity when updating user info (#139)
Browse files Browse the repository at this point in the history
What
Log the user activity when updating users' info (updating roles, creating users through CLI).

Why
Security review.
  • Loading branch information
CaioTeixeira95 authored Jan 8, 2024
1 parent 68032ad commit bf66812
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 34 deletions.
30 changes: 27 additions & 3 deletions internal/serve/httphandler/user_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ func (h UserHandler) UserActivation(rw http.ResponseWriter, req *http.Request) {

var activationErr error
if *reqBody.IsActive {
log.Ctx(ctx).Infof("[ActivateUserAccount] - Activating user with account ID %s", reqBody.UserID)
log.Ctx(ctx).Infof("[ActivateUserAccount] - User ID %s activating user with account ID %s", userID, reqBody.UserID)
activationErr = h.AuthManager.ActivateUser(ctx, token, reqBody.UserID)
} else {
log.Ctx(ctx).Infof("[DeactivateUserAccount] - Deactivating user with account ID %s", reqBody.UserID)
log.Ctx(ctx).Infof("[DeactivateUserAccount] - User ID %s deactivating user with account ID %s", userID, reqBody.UserID)
activationErr = h.AuthManager.DeactivateUser(ctx, token, reqBody.UserID)
}

Expand All @@ -173,6 +173,13 @@ func (h UserHandler) UserActivation(rw http.ResponseWriter, req *http.Request) {
func (h UserHandler) CreateUser(rw http.ResponseWriter, req *http.Request) {
ctx := req.Context()

token, ok := ctx.Value(middleware.TokenContextKey).(string)
if !ok {
log.Ctx(ctx).Warn("token not found when updating user activation")
httperror.Unauthorized("", nil, nil).Render(rw)
return
}

var reqBody CreateUserRequest
if err := httpdecode.DecodeJSON(req, &reqBody); err != nil {
err = fmt.Errorf("decoding the request body: %w", err)
Expand All @@ -186,6 +193,14 @@ func (h UserHandler) CreateUser(rw http.ResponseWriter, req *http.Request) {
return
}

authenticatedUserID, err := h.AuthManager.GetUserID(ctx, token)
if err != nil {
err = fmt.Errorf("getting request authenticated user ID: %w", err)
log.Ctx(ctx).Error(err)
httperror.Unauthorized("", err, nil).Render(rw)
return
}

newUser := &auth.User{
FirstName: reqBody.FirstName,
LastName: reqBody.LastName,
Expand Down Expand Up @@ -241,7 +256,7 @@ func (h UserHandler) CreateUser(rw http.ResponseWriter, req *http.Request) {
return
}

log.Ctx(ctx).Infof("[CreateUserAccount] - Created user with account ID %s", u.ID)
log.Ctx(ctx).Infof("[CreateUserAccount] - User ID %s created user with account ID %s", authenticatedUserID, u.ID)
httpjson.RenderStatus(rw, http.StatusCreated, u, httpjson.JSON)
}

Expand All @@ -268,6 +283,14 @@ func (h UserHandler) UpdateUserRoles(rw http.ResponseWriter, req *http.Request)
return
}

authenticatedUserID, err := h.AuthManager.GetUserID(ctx, token)
if err != nil {
err = fmt.Errorf("getting request authenticated user ID: %w", err)
log.Ctx(ctx).Error(err)
httperror.Unauthorized("", err, nil).Render(rw)
return
}

updateUserRolesErr := h.AuthManager.UpdateUserRoles(ctx, token, reqBody.UserID, data.FromUserRoleArrayToStringArray(reqBody.Roles))
if updateUserRolesErr != nil {
if errors.Is(updateUserRolesErr, auth.ErrInvalidToken) {
Expand All @@ -284,6 +307,7 @@ func (h UserHandler) UpdateUserRoles(rw http.ResponseWriter, req *http.Request)
return
}

log.Ctx(ctx).Infof("[UpdateUserRoles] - User ID %s updated user with account ID %s roles to %v", authenticatedUserID, reqBody.UserID, reqBody.Roles)
httpjson.RenderStatus(rw, http.StatusOK, map[string]string{"message": "user roles were updated successfully"}, httpjson.JSON)
}

Expand Down
Loading

0 comments on commit bf66812

Please sign in to comment.