refactor: add validation for all auth request structs

This commit is contained in:
Derrick Hammer 2023-06-06 23:28:33 -04:00
parent dd8e5704c8
commit f3e43f522f
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 0 deletions

View File

@ -8,6 +8,8 @@ import (
"git.lumeweb.com/LumeWeb/portal/db" "git.lumeweb.com/LumeWeb/portal/db"
"git.lumeweb.com/LumeWeb/portal/logger" "git.lumeweb.com/LumeWeb/portal/logger"
"git.lumeweb.com/LumeWeb/portal/model" "git.lumeweb.com/LumeWeb/portal/model"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/joomcode/errorx" "github.com/joomcode/errorx"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"github.com/kataras/jwt" "github.com/kataras/jwt"
@ -34,6 +36,13 @@ type LoginRequest struct {
Password string `json:"password"` Password string `json:"password"`
} }
func (r LoginRequest) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(&r.Email, is.EmailFormat, validation.Required),
validation.Field(&r.Password, validation.Required),
)
}
type LoginResponse struct { type LoginResponse struct {
Token string `json:"token"` Token string `json:"token"`
} }
@ -46,6 +55,12 @@ type ChallengeRequest struct {
Pubkey string `json:"pubkey"` Pubkey string `json:"pubkey"`
} }
func (r ChallengeRequest) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(&r.Pubkey, validation.Required, validation.By(CheckPubkeyValidator)),
)
}
type ChallengeResponse struct { type ChallengeResponse struct {
Challenge string `json:"challenge"` Challenge string `json:"challenge"`
} }
@ -56,6 +71,14 @@ type PubkeyLoginRequest struct {
Signature string `json:"signature"` Signature string `json:"signature"`
} }
func (r PubkeyLoginRequest) Validate() error {
return validation.ValidateStruct(&r,
validation.Field(&r.Pubkey, validation.Required, validation.By(CheckPubkeyValidator)),
validation.Field(&r.Challenge, validation.Required),
validation.Field(&r.Signature, validation.Required, validation.Length(128, 128)),
)
}
// verifyPassword compares the provided plaintext password with a hashed password and returns an error if they don't match. // verifyPassword compares the provided plaintext password with a hashed password and returns an error if they don't match.
func verifyPassword(hashedPassword, password string) error { func verifyPassword(hashedPassword, password string) error {
err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)) err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))