refactor/fix: now using pgx errors for postgres error checking instead of trying to look up the error code;
feat: implemented working custom validation; fix: authservice begin/complete registration
This commit is contained in:
@@ -19,9 +19,12 @@ package middleware
|
||||
|
||||
import (
|
||||
"easywish/internal/utils/enums"
|
||||
"easywish/internal/validation"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
@@ -87,6 +90,15 @@ func RequestMiddleware[T any](role enums.Role) gin.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
validate := validation.NewValidator()
|
||||
|
||||
if err := validate.Struct(body); err != nil {
|
||||
errorList := err.(validator.ValidationErrors)
|
||||
c.String(http.StatusBadRequest, fmt.Sprintf("Validation error: %s", errorList))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
request := Request[T]{
|
||||
User: *userInfo,
|
||||
Body: body,
|
||||
|
||||
@@ -23,13 +23,13 @@ type Tokens struct {
|
||||
}
|
||||
|
||||
type RegistrationBeginRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=20"`
|
||||
Username string `json:"username" binding:"required,min=3,max=20" validate:"username"`
|
||||
Email *string `json:"email" binding:"email"`
|
||||
Password string `json:"password" binding:"required"` // TODO: password checking
|
||||
}
|
||||
|
||||
type RegistrationCompleteRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=20"`
|
||||
Username string `json:"username" binding:"required,min=3,max=20" validate:"username"`
|
||||
VerificationCode string `json:"verification_code" binding:"required"`
|
||||
Name string `json:"name" binding:"required,max=75"`
|
||||
Birthday *string `json:"birthday"`
|
||||
@@ -41,7 +41,7 @@ type RegistrationCompleteResponse struct {
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=20"`
|
||||
Username string `json:"username" binding:"required,min=3,max=20" validate:"username"`
|
||||
Password string `json:"password" binding:"required,max=100"`
|
||||
TOTP *string `json:"totp"`
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ import (
|
||||
"easywish/internal/models"
|
||||
"easywish/internal/utils"
|
||||
"easywish/internal/utils/enums"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
@@ -57,7 +57,7 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
|
||||
|
||||
if user, err = db.TXQueries.CreateUser(db.CTX, request.Username); err != nil {
|
||||
|
||||
if errs.IsPgErr(err, pgerrcode.UniqueViolation) {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
a.log.Warn(
|
||||
"Attempted registration for a taken username",
|
||||
zap.String("username", request.Username),
|
||||
@@ -69,13 +69,13 @@ func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequ
|
||||
return false, errs.ErrServerError
|
||||
}
|
||||
|
||||
if _, err := db.TXQueries.GetUserByEmail(db.CTX, *request.Email); err == nil {
|
||||
if _, emailerr := db.TXQueries.GetUserByEmail(db.CTX, *request.Email); emailerr == nil {
|
||||
a.log.Warn(
|
||||
"Attempted registration for a taken email",
|
||||
zap.String("email", *request.Email))
|
||||
return false, errs.ErrEmailTaken
|
||||
|
||||
} else if !errs.IsPgErr(err, pgerrcode.NoData) {
|
||||
} else if !errors.Is(emailerr, pgx.ErrNoRows) {
|
||||
a.log.Error(
|
||||
"Failed to check if email is not taken",
|
||||
zap.String("email", *request.Email),
|
||||
@@ -139,7 +139,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
|
||||
user, err = db.TXQueries.GetUserByUsername(db.CTX, request.Username)
|
||||
|
||||
if err != nil {
|
||||
if errs.IsPgErr(err, pgerrcode.NoData) {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
a.log.Warn(
|
||||
"Could not find user attempting to complete registration with given username",
|
||||
zap.String("username", request.Username),
|
||||
@@ -161,7 +161,7 @@ func (a *authServiceImpl) RegistrationComplete(request models.RegistrationComple
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errs.IsPgErr(err, pgerrcode.NoData) {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
a.log.Warn(
|
||||
"User supplied unexistent confirmation code for completing registration",
|
||||
zap.String("username", user.Username),
|
||||
|
||||
52
backend/internal/validation/custom.go
Normal file
52
backend/internal/validation/custom.go
Normal file
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type CustomValidatorHandler struct {
|
||||
Function func(fl validator.FieldLevel) bool
|
||||
FieldName string
|
||||
}
|
||||
|
||||
func GetCustomHandlers() []CustomValidatorHandler {
|
||||
|
||||
handlers := []CustomValidatorHandler{
|
||||
|
||||
{
|
||||
FieldName: "username",
|
||||
Function: func(fl validator.FieldLevel) bool {
|
||||
username := fl.Field().String()
|
||||
return regexp.MustCompile(`^[a-zA-Z0-9_]{3,20}$`).MatchString(username)
|
||||
}},
|
||||
|
||||
{
|
||||
FieldName: "name",
|
||||
Function: func(fl validator.FieldLevel) bool {
|
||||
username := fl.Field().String()
|
||||
return regexp.MustCompile(`^[.]{1,75}$`).MatchString(username)
|
||||
}},
|
||||
|
||||
}
|
||||
|
||||
return handlers
|
||||
}
|
||||
@@ -1,34 +1,28 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package errors
|
||||
package validation
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
func IsPgErr(err error, code string) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
if pgErr.Code == code {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
var Module = fx.Module("validation",
|
||||
fx.Provide(
|
||||
NewValidator,
|
||||
),
|
||||
)
|
||||
32
backend/internal/validation/validator.go
Normal file
32
backend/internal/validation/validator.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Copyright (c) 2025 Nikolai Papin
|
||||
//
|
||||
// This file is part of Easywish
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
|
||||
// the GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
func NewValidator() *validator.Validate {
|
||||
v := validator.New()
|
||||
|
||||
for _, handler := range GetCustomHandlers() {
|
||||
v.RegisterValidation(handler.FieldName, handler.Function)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user