From 6b7dc2f5bb57c77059f7e75c961e275e17597600 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Fri, 9 Apr 2021 17:16:37 +0200 Subject: [PATCH] fix: missing json fields --- service/user.go | 6 +++--- types/client.go | 2 +- types/template.go | 2 +- types/user.go | 3 ++- types/verification.go | 8 ++++---- web/routes/api/user.go | 6 +++--- 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/service/user.go b/service/user.go index 5a9c65b..8583908 100644 --- a/service/user.go +++ b/service/user.go @@ -41,7 +41,7 @@ func (s *UserService) GetAll() (users []*types.User, err error) { func (s *UserService) GetById(id string) (*types.User, error) { var user types.User err := s.store.Get(id, &user) - return user.Sanitize(), err + return &user, err } func (s *UserService) Create(signup *dto.Signup) (*types.User, error) { @@ -59,7 +59,7 @@ func (s *UserService) Create(signup *dto.Signup) (*types.User, error) { if s.config.Security.VerifyUsers { go s.verifyUser(user) } - return user.Sanitize(), nil + return user, nil } func (s *UserService) Update(user *types.User, update *types.User) (*types.User, error) { @@ -83,7 +83,7 @@ func (s *UserService) Update(user *types.User, update *types.User) (*types.User, if err := s.store.Update(user.ID, user); err != nil { return nil, err } - return user.Sanitize(), nil + return user, nil } func (s *UserService) Delete(id string) error { diff --git a/types/client.go b/types/client.go index 61a040d..e40ea66 100644 --- a/types/client.go +++ b/types/client.go @@ -29,7 +29,7 @@ func AllPermissions() []string { type Client struct { ID string `json:"id" boltholdKey:"ID"` Description string `json:"description"` - UserId string `json:"-" boltholdIndex:"UserId"` + UserId string `json:"user_id" boltholdIndex:"UserId"` Permissions []string `json:"permissions"` Sender MailAddress `json:"sender"` ApiKey *string `json:"api_key"` // caution: usually you want to hide this! diff --git a/types/template.go b/types/template.go index 346a673..52caa51 100644 --- a/types/template.go +++ b/types/template.go @@ -8,7 +8,7 @@ import ( type Template struct { ID string `json:"id"` Name string `json:"name"` - UserId string `json:"-" boltholdIndex:"UserId"` + UserId string `json:"user_id" boltholdIndex:"UserId"` Content string `json:"content"` } diff --git a/types/user.go b/types/user.go index f8ea42c..7e83720 100644 --- a/types/user.go +++ b/types/user.go @@ -4,7 +4,7 @@ import "github.com/muety/mailwhale/util" type User struct { ID string `json:"id" boltholdKey:"ID"` - Password string `json:"-"` + Password string `json:"password"` // clear before returning in a response ! Senders []SenderAddress `json:"senders"` Verified bool `json:"verified"` } @@ -26,6 +26,7 @@ func (u *User) Sanitize() *User { if u.Senders == nil { u.Senders = []SenderAddress{} } + u.Password = "" return u } diff --git a/types/verification.go b/types/verification.go index 9fc6a79..fd6d900 100644 --- a/types/verification.go +++ b/types/verification.go @@ -8,10 +8,10 @@ const ( ) type Verification struct { - Token string `boltholdKey:"Token"` - UserId string - Scope string - Subject string + Token string `json:"token" boltholdKey:"Token"` + UserId string `json:"user_id"` + Scope string `json:"scope"` + Subject string `json:"subject"` } func NewVerification(user *User, scope, subject string) *Verification { diff --git a/web/routes/api/user.go b/web/routes/api/user.go index e2506e4..ed6ad7f 100644 --- a/web/routes/api/user.go +++ b/web/routes/api/user.go @@ -54,7 +54,7 @@ func (h *UserHandler) getMe(w http.ResponseWriter, r *http.Request) { util.RespondError(w, r, http.StatusNotFound, errors.New("user not found")) return } - util.RespondJson(w, http.StatusOK, user) + util.RespondJson(w, http.StatusOK, user.Sanitize()) } func (h *UserHandler) post(w http.ResponseWriter, r *http.Request) { @@ -76,7 +76,7 @@ func (h *UserHandler) post(w http.ResponseWriter, r *http.Request) { } logbuch.Info("created user '%s'", user.ID) - util.RespondJson(w, http.StatusCreated, user) + util.RespondJson(w, http.StatusCreated, user.Sanitize()) } func (h *UserHandler) updateMe(w http.ResponseWriter, r *http.Request) { @@ -106,7 +106,7 @@ func (h *UserHandler) updateMe(w http.ResponseWriter, r *http.Request) { } logbuch.Info("updated user '%s'", user.ID) - util.RespondJson(w, http.StatusOK, user) + util.RespondJson(w, http.StatusOK, user.Sanitize()) } func (h *UserHandler) verify(w http.ResponseWriter, r *http.Request) {