Method to createUsers, beginning og JWTMiddleware
This commit is contained in:
parent
b6d19f9109
commit
82b335c8cb
8 changed files with 82 additions and 11 deletions
37
src/app/controller/user_controller.go
Normal file
37
src/app/controller/user_controller.go
Normal file
|
@ -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)
|
||||||
|
}
|
|
@ -16,4 +16,7 @@ func PrivateRoutes(app *fiber.App) {
|
||||||
event.Delete("/:id", controller.DeleteEventById)
|
event.Delete("/:id", controller.DeleteEventById)
|
||||||
event.Put("/:id", controller.UpdateEventById)
|
event.Put("/:id", controller.UpdateEventById)
|
||||||
event.Put("", controller.CreateEvent)
|
event.Put("", controller.CreateEvent)
|
||||||
|
user := apiv1.Group("/user")
|
||||||
|
user.Use(config.AuthMiddleware)
|
||||||
|
user.Put("", controller.CreateUser)
|
||||||
}
|
}
|
||||||
|
|
14
src/app/service/user.go
Normal file
14
src/app/service/user.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ func Connect() error {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Database.AutoMigrate(&entities.Db_Event{})
|
Database.AutoMigrate(&entities.Db_Event{}, &entities.User{})
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,13 @@ import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
|
|
||||||
|
jwtware "github.com/gofiber/contrib/jwt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AuthMiddleware fiber.Handler
|
var AuthMiddleware fiber.Handler
|
||||||
|
var JwtMiddleware fiber.Handler
|
||||||
|
|
||||||
func SetupMiddlewares() {
|
func SetupMiddlewares() {
|
||||||
AuthMiddleware = keyauth.New(keyauth.Config{
|
AuthMiddleware = keyauth.New(keyauth.Config{
|
||||||
|
@ -21,4 +23,7 @@ func SetupMiddlewares() {
|
||||||
return false, keyauth.ErrMissingOrMalformedAPIKey
|
return false, keyauth.ErrMissingOrMalformedAPIKey
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
JwtMiddleware = jwtware.New(jwtware.Config{
|
||||||
|
SigningKey: jwtware.SigningKey{Key: []byte(Env.Secret)},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ type Environment struct {
|
||||||
Domain string `env:"DOMAIN,required"`
|
Domain string `env:"DOMAIN,required"`
|
||||||
ApiKeyPretix string `env:"API_KEY_PRETIX,required"`
|
ApiKeyPretix string `env:"API_KEY_PRETIX,required"`
|
||||||
ApiKey string `env:"API_KEY,required"`
|
ApiKey string `env:"API_KEY,required"`
|
||||||
|
Secret string `env:"SECRET,required"`
|
||||||
DatabasePath string `env:"DATABASE_PATH,required"`
|
DatabasePath string `env:"DATABASE_PATH,required"`
|
||||||
Debug bool `env:"DEBUG"`
|
Debug bool `env:"DEBUG"`
|
||||||
}
|
}
|
||||||
|
|
8
src/entities/user.go
Normal file
8
src/entities/user.go
Normal file
|
@ -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"`
|
||||||
|
}
|
23
src/go.mod
23
src/go.mod
|
@ -4,20 +4,23 @@ go 1.19
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/go-playground/validator/v10 v10.15.4
|
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
|
github.com/joho/godotenv v1.5.1
|
||||||
gorm.io/driver/sqlite v1.5.3
|
gorm.io/driver/sqlite v1.5.3
|
||||||
gorm.io/gorm v1.25.4
|
gorm.io/gorm v1.25.4
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/MicahParks/keyfunc/v2 v2.1.0 // indirect
|
||||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||||
github.com/go-playground/locales v0.14.1 // indirect
|
github.com/go-playground/locales v0.14.1 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.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
|
github.com/leodido/go-urn v1.2.4 // indirect
|
||||||
golang.org/x/crypto v0.7.0 // indirect
|
golang.org/x/crypto v0.15.0 // indirect
|
||||||
golang.org/x/net v0.8.0 // indirect
|
golang.org/x/net v0.10.0 // indirect
|
||||||
golang.org/x/text v0.8.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
@ -26,19 +29,19 @@ require (
|
||||||
github.com/gofiber/template v1.8.2 // indirect
|
github.com/gofiber/template v1.8.2 // indirect
|
||||||
github.com/gofiber/template/html/v2 v2.0.5
|
github.com/gofiber/template/html/v2 v2.0.5
|
||||||
github.com/gofiber/utils v1.1.0 // indirect
|
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/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // 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-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.14 // indirect
|
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.17 // indirect
|
github.com/mattn/go-sqlite3 v1.14.17 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
github.com/stretchr/testify v1.8.4 // indirect
|
github.com/stretchr/testify v1.8.4 // indirect
|
||||||
github.com/valyala/bytebufferpool v1.0.0 // 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
|
github.com/valyala/tcplisten v1.0.0 // indirect
|
||||||
golang.org/x/exp v0.0.0-20230817173708-d852ddb80c63
|
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
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue