Commit e60101b7 authored by Julien Schröter's avatar Julien Schröter Committed by Julien Schröter

Extend gin validator by password validation and translations

parent ea670b3d
......@@ -70,7 +70,7 @@ func RegisterUser(repository UserQuery, tokenFactory otp.TokenFactory, verifyEma
}
// check password rules
if ok := checkPasswordOK(signUpForm.Password); !ok {
if ok := CheckPasswordOK(signUpForm.Password); !ok {
ctx.String(http.StatusBadRequest, "Password violates the password rules")
return
}
......@@ -296,7 +296,7 @@ func patchPassword(repository UserQuery) gin.HandlerFunc {
}
// check password rules
if ok := checkPasswordOK(requestData.Password); !ok {
if ok := CheckPasswordOK(requestData.Password); !ok {
ctx.String(http.StatusBadRequest, "Password violates the password rules")
return
}
......
......@@ -12,7 +12,7 @@ const minLength = 8
var emailPasslist = [...]string{"@stud.tu-darmstadt.de", "@akamu.de"}
func checkPasswordOK(pw string) bool {
func CheckPasswordOK(pw string) bool {
if len(pw) < minLength {
return false
}
......
......@@ -5,6 +5,9 @@ import (
"net/http"
"github.com/getsentry/raven-go"
"github.com/go-playground/locales"
"github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
"github.com/gin-gonic/gin"
"gitlab.akamu.de/akamu/game-server-go/schemas"
......@@ -14,6 +17,9 @@ var ErrFetchingSessionPayload = errors.New("failed to fetch session payload")
var errFetchingUserID = errors.New("failed fetching user id")
var errFetchingUsername = errors.New("failed fetching username")
var transEngl locales.Translator
var translator ut.Translator
// GetSessionPayload returns the payload of the current session.
// Returns error if unable to receive the session payload.
func GetSessionPayload(ctx *gin.Context) (*schemas.TokenPayload, error) {
......@@ -56,3 +62,12 @@ func GetUsername(ctx *gin.Context) (string, error) {
tokenPayload := identity.(schemas.TokenPayload)
return tokenPayload.Username, nil
}
func GetTranslator() ut.Translator {
if transEngl == nil {
transEngl = en.New()
translator, _ = ut.New(transEngl).GetTranslator("en")
}
return translator
}
......@@ -9,6 +9,7 @@ import (
"net/mail"
"time"
"gitlab.akamu.de/akamu/game-server-go/endpoint"
"gitlab.akamu.de/akamu/game-server-go/sendmail"
"gitlab.akamu.de/akamu/game-server-go/token/otp"
"gitlab.akamu.de/akamu/game-server-go/version"
......@@ -16,8 +17,11 @@ import (
"gitlab.akamu.de/akamu/game-server-go/schemas"
"github.com/getsentry/raven-go"
ut "github.com/go-playground/universal-translator"
validator "github.com/go-playground/validator/v10"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
jwt "gitlab.akamu.de/akamu/gin-jwt"
"gitlab.akamu.de/akamu/game-server-go/config"
......@@ -121,6 +125,44 @@ func SetupRoutes(courseRepository course.CourseQuery, flashcardRepository flashc
//gin.SetMode(gin.ReleaseMode)
router := gin.Default()
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
// Register Password Validator
v.RegisterValidation("password", func(fl validator.FieldLevel) bool {
password, ok := fl.Field().Interface().(string)
if ok {
return user.CheckPasswordOK(password)
}
return false
})
t := endpoint.GetTranslator()
// Register custom error messages
v.RegisterTranslation("required", t, func(ut ut.Translator) error {
return ut.Add("required", "{0} cannot be empty.", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
m, _ := ut.T("required", fe.Field())
return m
})
v.RegisterTranslation("eqfield", t, func(ut ut.Translator) error {
return ut.Add("eqfield", "{0} must match the value of {1}.", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
m, _ := ut.T("eqfield", fe.Field(), fe.Param())
return m
})
v.RegisterTranslation("password", t, func(ut ut.Translator) error {
return ut.Add("password", "A valid password must be at least 8 characters long and must contain at least one uppercase and one lowercase letter.", true)
}, func(ut ut.Translator, fe validator.FieldError) string {
m, _ := ut.T("password")
return m
})
} else {
panic("failed setting up password validator")
}
router.GET("/", getRoot)
s := "secret"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment