diff --git a/go.mod b/go.mod index 673198f..cfe01dc 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module git.lumeweb.com/LumeWeb/portal go 1.18 require ( + github.com/go-playground/validator/v10 v10.13.0 github.com/iris-contrib/swagger v0.0.0-20230311205341-32127a753a68 github.com/joomcode/errorx v1.1.0 github.com/kataras/iris/v12 v12.2.0 @@ -49,6 +50,8 @@ require ( github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/spec v0.20.9 // indirect github.com/go-openapi/swag v0.22.3 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/gobwas/httphead v0.1.0 // indirect github.com/gobwas/pool v0.2.1 // indirect @@ -76,6 +79,7 @@ require ( github.com/klauspost/compress v1.16.5 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/klauspost/reedsolomon v1.11.7 // indirect + github.com/leodido/go-urn v1.2.3 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mailgun/raymond/v2 v2.0.48 // indirect github.com/mailru/easyjson v0.7.7 // indirect diff --git a/go.sum b/go.sum index 397a1ce..5b0aab9 100644 --- a/go.sum +++ b/go.sum @@ -150,6 +150,13 @@ github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.13.0 h1:cFRQdfaSMCOSfGCCLB20MHvuoHb/s5G8L5pu2ppK5AQ= +github.com/go-playground/validator/v10 v10.13.0/go.mod h1:dwu7+CG8/CtBiJFZDz4e+5Upb6OLw04gtBYw0mcG/z4= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= @@ -316,6 +323,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= +github.com/leodido/go-urn v1.2.3 h1:6BE2vPT0lqoz3fmOesHZiaiFh7889ssCo2GMvLCfiuA= +github.com/leodido/go-urn v1.2.3/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= @@ -420,6 +429,7 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= +github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8= github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0= github.com/swaggo/swag v1.16.1 h1:fTNRhKstPKxcnoKsytm4sahr8FaYzUcT7i1/3nd/fBg= diff --git a/main.go b/main.go index e277440..5e5f178 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "git.lumeweb.com/LumeWeb/portal/db" _ "git.lumeweb.com/LumeWeb/portal/docs" "git.lumeweb.com/LumeWeb/portal/service" + "git.lumeweb.com/LumeWeb/portal/validator" "github.com/iris-contrib/swagger" "github.com/iris-contrib/swagger/swaggerFiles" "github.com/kataras/iris/v12" @@ -40,6 +41,8 @@ func main() { // Create a new Iris app instance app := iris.New() + app.Validator = validator.Get() + // Enable Gzip compression for responses app.Use(iris.Compression) diff --git a/service/account_service.go b/service/account_service.go index 012015a..75d6df4 100644 --- a/service/account_service.go +++ b/service/account_service.go @@ -4,9 +4,12 @@ import ( "errors" "git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/model" + _validator "git.lumeweb.com/LumeWeb/portal/validator" + "github.com/go-playground/validator/v10" "github.com/kataras/iris/v12" "golang.org/x/crypto/bcrypt" "gorm.io/gorm" + "reflect" ) type AccountService struct { @@ -14,11 +17,27 @@ type AccountService struct { } type RegisterRequest struct { - Email string `json:"email"` + Email string `json:"email" validate:"required"` Password string `json:"password"` Pubkey []byte `json:"pubkey"` } +func init() { + jsonValidator := _validator.Get() + + jsonValidator.RegisterStructValidation(ValidateRegisterRequest, RegisterRequest{}) +} + +func ValidateRegisterRequest(structLevel validator.StructLevel) { + + request := structLevel.Current().Interface().(RegisterRequest) + + if len(request.Pubkey) == 0 && len(request.Password) == 0 { + structLevel.ReportError(reflect.ValueOf(request.Email), "Email", "email", "emailorpubkey", "") + structLevel.ReportError(reflect.ValueOf(request.Pubkey), "Pubkey", "pubkey", "emailorpubkey", "") + } +} + func hashPassword(password string) (string, error) { // Generate a new bcrypt hash from the provided password. diff --git a/validator/validator.go b/validator/validator.go new file mode 100644 index 0000000..e75acdc --- /dev/null +++ b/validator/validator.go @@ -0,0 +1,15 @@ +package validator + +import ( + "github.com/go-playground/validator/v10" +) + +var v *validator.Validate + +func init() { + v = validator.New() +} + +func Get() *validator.Validate { + return v +}