From 82b335c8cbe3817a7e82cff44e8453f3978cde6e Mon Sep 17 00:00:00 2001 From: nikurasu Date: Sat, 18 Nov 2023 11:03:26 +0100 Subject: [PATCH] Method to createUsers, beginning og JWTMiddleware --- src/app/controller/user_controller.go | 37 +++++++++++++++++++++++++++ src/app/routes/private_routes.go | 3 +++ src/app/service/user.go | 14 ++++++++++ src/config/database.go | 2 +- src/config/middleware.go | 5 ++++ src/entities/environment.go | 1 + src/entities/user.go | 8 ++++++ src/go.mod | 23 +++++++++-------- 8 files changed, 82 insertions(+), 11 deletions(-) create mode 100644 src/app/controller/user_controller.go create mode 100644 src/app/service/user.go create mode 100644 src/entities/user.go diff --git a/src/app/controller/user_controller.go b/src/app/controller/user_controller.go new file mode 100644 index 0000000..2a8891b --- /dev/null +++ b/src/app/controller/user_controller.go @@ -0,0 +1,37 @@ +package controller + +import ( + "github.com/gofiber/fiber/v2" + "golang.org/x/crypto/bcrypt" + "ulmer-furs.de/pretix-proxy/v2/app/service" + "ulmer-furs.de/pretix-proxy/v2/app/util" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func CreateUser(c *fiber.Ctx) error { + user := new(entities.User) + if err := c.BodyParser(user); err != nil { + return c.Status(fiber.ErrBadRequest.Code).SendString("Bad Request") + } + if err, errMsg := util.Validate(user); err { + return &fiber.Error{ + Code: fiber.ErrBadRequest.Code, + Message: errMsg, + } + } + if hashedPassword, err := bcrypt.GenerateFromPassword([]byte(user.Password), bcrypt.DefaultCost); err != nil { + return &fiber.Error{ + Code: fiber.ErrInternalServerError.Code, + Message: "Internal Server Error", + } + } else { + user.HashedPassword = hashedPassword + } + if err := service.Create_User(*user); err != nil { + return &fiber.Error{ + Code: fiber.ErrInternalServerError.Code, + Message: "Internal Server Error", + } + } + return c.SendStatus(fiber.StatusOK) +} diff --git a/src/app/routes/private_routes.go b/src/app/routes/private_routes.go index 81740a4..032cdab 100644 --- a/src/app/routes/private_routes.go +++ b/src/app/routes/private_routes.go @@ -16,4 +16,7 @@ func PrivateRoutes(app *fiber.App) { event.Delete("/:id", controller.DeleteEventById) event.Put("/:id", controller.UpdateEventById) event.Put("", controller.CreateEvent) + user := apiv1.Group("/user") + user.Use(config.AuthMiddleware) + user.Put("", controller.CreateUser) } diff --git a/src/app/service/user.go b/src/app/service/user.go new file mode 100644 index 0000000..a9563b5 --- /dev/null +++ b/src/app/service/user.go @@ -0,0 +1,14 @@ +package service + +import ( + "ulmer-furs.de/pretix-proxy/v2/config" + "ulmer-furs.de/pretix-proxy/v2/entities" +) + +func Create_User(user entities.User) error { + result := config.Database.Create(&user) + if result.Error != nil { + return result.Error + } + return nil +} diff --git a/src/config/database.go b/src/config/database.go index 901271e..1096da1 100644 --- a/src/config/database.go +++ b/src/config/database.go @@ -16,7 +16,7 @@ func Connect() error { panic(err) } - Database.AutoMigrate(&entities.Db_Event{}) + Database.AutoMigrate(&entities.Db_Event{}, &entities.User{}) return nil } diff --git a/src/config/middleware.go b/src/config/middleware.go index 1bdc32a..7f2adf9 100644 --- a/src/config/middleware.go +++ b/src/config/middleware.go @@ -4,11 +4,13 @@ import ( "crypto/sha256" "crypto/subtle" + jwtware "github.com/gofiber/contrib/jwt" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/keyauth" ) var AuthMiddleware fiber.Handler +var JwtMiddleware fiber.Handler func SetupMiddlewares() { AuthMiddleware = keyauth.New(keyauth.Config{ @@ -21,4 +23,7 @@ func SetupMiddlewares() { return false, keyauth.ErrMissingOrMalformedAPIKey }, }) + JwtMiddleware = jwtware.New(jwtware.Config{ + SigningKey: jwtware.SigningKey{Key: []byte(Env.Secret)}, + }) } diff --git a/src/entities/environment.go b/src/entities/environment.go index 497c75e..52c8db1 100644 --- a/src/entities/environment.go +++ b/src/entities/environment.go @@ -4,6 +4,7 @@ type Environment struct { Domain string `env:"DOMAIN,required"` ApiKeyPretix string `env:"API_KEY_PRETIX,required"` ApiKey string `env:"API_KEY,required"` + Secret string `env:"SECRET,required"` DatabasePath string `env:"DATABASE_PATH,required"` Debug bool `env:"DEBUG"` } diff --git a/src/entities/user.go b/src/entities/user.go new file mode 100644 index 0000000..71f28b4 --- /dev/null +++ b/src/entities/user.go @@ -0,0 +1,8 @@ +package entities + +type User struct { + Id int `gorm:"column:id;unique;not null"` + Email string `gorm:"column:email;not null" validate:"required"` + Password string `gorm:"-" validate:"required"` + HashedPassword []byte `gorm:"column:hashed_password;not null"` +} diff --git a/src/go.mod b/src/go.mod index 0bcbd6e..97013c2 100644 --- a/src/go.mod +++ b/src/go.mod @@ -4,20 +4,23 @@ go 1.19 require ( github.com/go-playground/validator/v10 v10.15.4 - github.com/gofiber/fiber/v2 v2.48.0 + github.com/gofiber/fiber/v2 v2.51.0 github.com/joho/godotenv v1.5.1 gorm.io/driver/sqlite v1.5.3 gorm.io/gorm v1.25.4 ) require ( + github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/gofiber/contrib/jwt v1.0.7 // indirect + github.com/golang-jwt/jwt/v5 v5.1.0 // indirect github.com/leodido/go-urn v1.2.4 // indirect - golang.org/x/crypto v0.7.0 // indirect - golang.org/x/net v0.8.0 // indirect - golang.org/x/text v0.8.0 // indirect + golang.org/x/crypto v0.15.0 // indirect + golang.org/x/net v0.10.0 // indirect + golang.org/x/text v0.14.0 // indirect ) require ( @@ -26,19 +29,19 @@ require ( github.com/gofiber/template v1.8.2 // indirect github.com/gofiber/template/html/v2 v2.0.5 github.com/gofiber/utils v1.1.0 // indirect - github.com/google/uuid v1.3.0 // indirect + github.com/google/uuid v1.4.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - github.com/klauspost/compress v1.16.3 // indirect + github.com/klauspost/compress v1.16.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mattn/go-sqlite3 v1.14.17 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/stretchr/testify v1.8.4 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.48.0 // indirect + github.com/valyala/fasthttp v1.50.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63 - golang.org/x/sys v0.11.0 // indirect + golang.org/x/sys v0.14.0 // indirect )