From 636a367d3eb93215f99c81685b276cd3370ff24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20Jurmanovi=C4=87?= Date: Sat, 5 Jun 2021 00:10:23 +0200 Subject: [PATCH] fixed multipart form issue --- Taskfile.yml | 5 ++++- pkg/controllers/auth.go | 21 +++++++++++++++++-- pkg/controllers/transactionTypes.go | 2 +- pkg/controllers/transactions.go | 2 +- pkg/controllers/wallets.go | 2 +- pkg/middleware/auth.go | 32 ++++++++++++++++------------- pkg/models/auth.go | 8 ++++++-- pkg/models/register.go | 8 ++++---- pkg/models/transactionTypes.go | 4 ++-- pkg/models/transactions.go | 8 ++++---- pkg/models/wallets.go | 4 ++-- pkg/services/transactions.go | 5 ++++- 12 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Taskfile.yml b/Taskfile.yml index f1082e5..d74fa6c 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -6,10 +6,13 @@ tasks: - go build -o "./bin/api.exe" "./cmd/api/main.go" run: cmds: - - go run cmd/api/main.go + - ./bin/api migrate: cmds: - go run cmd/migrate/main.go build-run: cmds: - go build -o "./bin/api.exe" "./cmd/api/main.go" && ./bin/api.exe + start: + cmds: + - go run cmd/api/main.go \ No newline at end of file diff --git a/pkg/controllers/auth.go b/pkg/controllers/auth.go index eda0de8..fb52f90 100644 --- a/pkg/controllers/auth.go +++ b/pkg/controllers/auth.go @@ -20,13 +20,14 @@ func NewAuthController(rs *services.UsersService, s *gin.RouterGroup) *AuthContr s.POST("login", rc.PostLogin) s.POST("register", rc.PostRegister) s.DELETE("deactivate", middleware.Auth, rc.Delete) + s.GET("check-token", rc.CheckToken) return rc } func (rc *AuthController) PostLogin(c *gin.Context) { body := new(models.Login) - if err := c.ShouldBindJSON(&body); err != nil { + if err := c.ShouldBind(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } @@ -43,7 +44,7 @@ func (rc *AuthController) PostRegister(c *gin.Context) { body := new(models.User) body.Init() body.IsActive = true - if err := c.ShouldBindJSON(body); err != nil { + if err := c.ShouldBind(body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } @@ -68,3 +69,19 @@ func (rc *AuthController) Delete(c *gin.Context) { c.JSON(200, mr) } } + +func (rc *AuthController) CheckToken(c *gin.Context) { + token, _ := c.GetQuery("token") + re := new(models.CheckToken) + + valid, err := middleware.CheckToken(token) + + if err != nil && valid.Valid { + re.Valid = false + c.AbortWithStatusJSON(400, re) + } + + re.Valid = true + + c.JSON(200, re) +} diff --git a/pkg/controllers/transactionTypes.go b/pkg/controllers/transactionTypes.go index 22187f0..ce14351 100644 --- a/pkg/controllers/transactionTypes.go +++ b/pkg/controllers/transactionTypes.go @@ -24,7 +24,7 @@ func NewTransactionTypeController(as *services.TransactionTypeService, s *gin.Ro func (wc *TransactionTypeController) New(c *gin.Context) { body := new(models.NewTransactionTypeBody) - if err := c.ShouldBindJSON(body); err != nil { + if err := c.ShouldBind(body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } diff --git a/pkg/controllers/transactions.go b/pkg/controllers/transactions.go index c1971d8..122b1ae 100644 --- a/pkg/controllers/transactions.go +++ b/pkg/controllers/transactions.go @@ -24,7 +24,7 @@ func NewTransactionController(as *services.TransactionService, s *gin.RouterGrou func (wc *TransactionController) New(c *gin.Context) { body := new(models.NewTransactionBody) - if err := c.ShouldBindJSON(body); err != nil { + if err := c.ShouldBind(body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } diff --git a/pkg/controllers/wallets.go b/pkg/controllers/wallets.go index cb1d434..7ca4e53 100644 --- a/pkg/controllers/wallets.go +++ b/pkg/controllers/wallets.go @@ -25,7 +25,7 @@ func NewWalletsController(as *services.WalletService, s *gin.RouterGroup) *Walle func (wc *WalletsController) New(c *gin.Context) { body := new(models.NewWalletBody) - if err := c.ShouldBindJSON(body); err != nil { + if err := c.ShouldBind(body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } diff --git a/pkg/middleware/auth.go b/pkg/middleware/auth.go index 2eb0ef0..087b58b 100644 --- a/pkg/middleware/auth.go +++ b/pkg/middleware/auth.go @@ -1,6 +1,7 @@ package middleware import ( + "errors" "os" "strings" "wallet-api/pkg/models" @@ -14,20 +15,7 @@ import ( func Auth(c *gin.Context) { exceptionReturn := new(models.Exception) tokenString := ExtractToken(c) - secret := os.Getenv("ACCESS_SECRET") - if secret == "" { - secret = configs.Secret - } - token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { - _, ok := token.Method.(*jwt.SigningMethodHMAC) - if !ok { - exceptionReturn.ErrorCode = "401001" - exceptionReturn.StatusCode = 401 - exceptionReturn.Message = "Invalid token" - c.AbortWithStatusJSON(exceptionReturn.StatusCode, exceptionReturn) - } - return []byte(secret), nil - }) + token, err := CheckToken(tokenString) if err != nil { exceptionReturn.ErrorCode = "401001" exceptionReturn.StatusCode = 401 @@ -57,3 +45,19 @@ func ExtractToken(c *gin.Context) string { } return "" } + +func CheckToken(tokenString string) (*jwt.Token, error) { + secret := os.Getenv("ACCESS_SECRET") + if secret == "" { + secret = configs.Secret + } + token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { + _, ok := token.Method.(*jwt.SigningMethodHMAC) + var err error + if !ok { + err = errors.New("Invalid token") + } + return []byte(secret), err + }) + return token, err +} diff --git a/pkg/models/auth.go b/pkg/models/auth.go index 49ca4ee..2145764 100644 --- a/pkg/models/auth.go +++ b/pkg/models/auth.go @@ -5,10 +5,14 @@ type Token struct { } type Login struct { - Email string - Password string + Email string `form:"email"` + Password string `form:"password"` } type Auth struct { Id string } + +type CheckToken struct { + Valid bool `json:"valid"` +} diff --git a/pkg/models/register.go b/pkg/models/register.go index 01ab894..d612d71 100644 --- a/pkg/models/register.go +++ b/pkg/models/register.go @@ -3,10 +3,10 @@ package models type User struct { tableName struct{} `pg:"users,alias:users"` BaseModel - IsActive bool `json:"isActive" pg:"is_active"` - Username string `json:"username" pg:"username"` - Password string `json:"password" pg:"password"` - Email string `json:"email" pg:"email"` + IsActive bool `json:"isActive" pg:"is_active" form:"isActive"` + Username string `json:"username" pg:"username" form:"username"` + Password string `json:"password" pg:"password" form:"password"` + Email string `json:"email" pg:"email" form:"email"` } type UserReturnInfo struct { diff --git a/pkg/models/transactionTypes.go b/pkg/models/transactionTypes.go index f6e2932..db0a4b0 100644 --- a/pkg/models/transactionTypes.go +++ b/pkg/models/transactionTypes.go @@ -8,6 +8,6 @@ type TransactionType struct { } type NewTransactionTypeBody struct { - Name string `json:"name"` - Type string `json:"type"` + Name string `json:"name" form:"name"` + Type string `json:"type" form:"type"` } diff --git a/pkg/models/transactions.go b/pkg/models/transactions.go index dc86df9..9fb9b8a 100644 --- a/pkg/models/transactions.go +++ b/pkg/models/transactions.go @@ -15,8 +15,8 @@ type Transaction struct { } type NewTransactionBody struct { - WalletID string `json:"walletId"` - TransactionTypeID string `json:"transactionTypeId"` - TransactionDate time.Time `json:"transactionDate"` - Description string `json:"description"` + WalletID string `json:"walletId" form:"walletId"` + TransactionTypeID string `json:"transactionTypeId" form:"transactionTypeId"` + TransactionDate time.Time `json:"transactionDate" form:"transactionDate"` + Description string `json:"description" form:"description"` } diff --git a/pkg/models/wallets.go b/pkg/models/wallets.go index 83fa2f1..a58013a 100644 --- a/pkg/models/wallets.go +++ b/pkg/models/wallets.go @@ -9,6 +9,6 @@ type Wallet struct { } type NewWalletBody struct { - Name string `json:"name"` - UserID string `json:"userId"` + Name string `json:"name" form:"name"` + UserID string `json:"userId" form:"userId"` } diff --git a/pkg/services/transactions.go b/pkg/services/transactions.go index 2f9949f..30a4d93 100644 --- a/pkg/services/transactions.go +++ b/pkg/services/transactions.go @@ -27,6 +27,9 @@ func (as *TransactionService) New(body *models.NewTransactionBody) *models.Trans func (as *TransactionService) GetAll(walletId string, filtered *models.FilteredResponse) { wm := new([]models.Transaction) - query := as.Db.Model(wm).Where("? = ?", pg.Ident("wallet_id"), walletId) + query := as.Db.Model((wm)) + if walletId != "" { + query = query.Where("? = ?", pg.Ident("wallet_id"), walletId) + } FilteredResponse(query, wm, filtered) }