From e05bd1c7431877943021469bec497f2fc2ea5c8c Mon Sep 17 00:00:00 2001 From: Jeffrey Duroyon Date: Wed, 17 Nov 2021 23:44:20 +0100 Subject: [PATCH] wip --- internal/account/database.go | 9 ++++++ internal/account/handler.go | 53 ++++++++++++++++++++++++++++++++++-- internal/account/service.go | 4 +++ middleware/oauth_token.go | 3 +- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/internal/account/database.go b/internal/account/database.go index c2c5097..1f70bcd 100644 --- a/internal/account/database.go +++ b/internal/account/database.go @@ -82,6 +82,15 @@ func (db *Database) CreateAccount(account *Account) error { } +func (db *Database) DeleteAccountOfAnUser(userId, accountId string) error { + query := ` + DELETE FROM account + WHERE user_id = $1 + AND id = $2;` + _, err := db.Session.Exec(query, userId, accountId) + return err +} + func NewDatabase(db *postgresql.DatabasePostgreSQL) *Database { return &Database{db} } diff --git a/internal/account/handler.go b/internal/account/handler.go index c9c8cd0..2f0df67 100644 --- a/internal/account/handler.go +++ b/internal/account/handler.go @@ -63,7 +63,6 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) { utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err)) return } - utils.GetLogger().Info(userId) _, err = c.userService.GetUserById(userId) if e, ok := err.(*model.APIError); ok { utils.GetLogger().Info(err) @@ -111,7 +110,57 @@ func (c *Context) CreateAccountOfUser(gc *gin.Context) { return } -func (c *Context) DeleteAccountOfUser(context *gin.Context) { +func (c *Context) DeleteAccountOfUser(gc *gin.Context) { + userId := gc.Param("userId") + err := c.Validator.VarCtx(gc, userId, "uuid4") + if err != nil { + utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err)) + return + } + + accountId := gc.Param("accountId") + err = c.Validator.VarCtx(gc, userId, "uuid4") + if err != nil { + utils2.JSONError(gc.Writer, validators.NewDataValidationAPIError(err)) + return + } + accountParam, err := c.userService.GetUserById(userId) + if e, ok := err.(*model.APIError); ok { + utils.GetLogger().Info(err) + utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUser: get user error") + utils.JSONErrorWithMessage(gc.Writer, *e, e.Description) + return + } else if err != nil { + utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user") + utils.JSONError(gc.Writer, model.ErrInternalServer) + return + } + + googleUserId, exists := gc.Get("googleUserId") + if exists == false { + utils.GetLoggerFromCtx(gc).Error("error while getting google user id") + utils.JSONError(gc.Writer, model.ErrInternalServer) + return + } + + acc, err := c.userService.GetUserFromGoogleID(googleUserId.(string)) + if e, ok := err.(*model.APIError); ok { + utils.GetLogger().Info(err) + utils.GetLoggerFromCtx(gc).WithError(err).WithField("type", e.Type).Error("error GetUserFromGoogleID: get user from google user id") + utils.JSONErrorWithMessage(gc.Writer, *e, e.Description) + return + } else if err != nil { + utils.GetLoggerFromCtx(gc).WithError(err).Error("error while get user from google user id") + utils.JSONError(gc.Writer, model.ErrInternalServer) + return + } + if acc == nil || acc.ID != accountParam.ID { + utils.GetLoggerFromCtx(gc).WithError(err).Error("User in path doesn't match authenticated user") + utils.JSONError(gc.Writer, model.ErrBadRequestFormat) + return + } + + c.service.DeleteAccountOfUser(userId, accountId) } diff --git a/internal/account/service.go b/internal/account/service.go index ccf0ee7..a65b540 100644 --- a/internal/account/service.go +++ b/internal/account/service.go @@ -48,6 +48,10 @@ func (s *Service) CreateAccount(account Account) (*Account, error) { return &account, err } +func (s *Service) DeleteAccountOfUser(userId, accountId string) error { + return s.db.DeleteAccountOfAnUser(userId, accountId) +} + func NewService(database *Database) *Service { return &Service{db: database} } diff --git a/middleware/oauth_token.go b/middleware/oauth_token.go index 6a2bc61..d22a1dc 100644 --- a/middleware/oauth_token.go +++ b/middleware/oauth_token.go @@ -27,10 +27,11 @@ func ValidateOAuthToken(c *gin.Context) { } tokenInfoCall := oauth2Service.Tokeninfo() tokenInfoCall.IdToken(authorizationHeaderSplitted[1]) - _, err = tokenInfoCall.Do() + token, err := tokenInfoCall.Do() if err != nil { utils.GetLogger().WithError(err).Error(err) utils.JSONError(c.Writer, model.ErrBadRequestFormat) return } + c.Set("googleUserId", token.UserId) }