package models import ( "git.sch9.ru/new_gate/ms-auth/pkg/utils" "golang.org/x/crypto/bcrypt" "time" ) type User struct { Id *int32 `db:"id"` Username *string `db:"username"` Password *string `db:"hashed_pwd"` Email *string `db:"email"` ExpiresAt *time.Time `db:"expires_at"` CreatedAt *time.Time `db:"created_at"` UpdatedAt *time.Time `db:"updated_at"` Role *Role `db:"role"` } func (user *User) ValidUsername() error { if user.Username == nil { return utils.ErrBadUsername } err := utils.ValidUsername(*user.Username) if err != nil { return err } return nil } func (user *User) ValidPassword() error { if user.Password == nil { return utils.ErrBadHandleOrPassword } err := utils.ValidPassword(*user.Password) if err != nil { return err } return nil } func (user *User) ValidEmail() error { if user.Email == nil { return utils.ErrBadEmail } return utils.ValidEmail(*user.Email) } func (user *User) ValidRole() error { if user.Role == nil { return utils.ErrBadRole } return user.Role.Valid() } func (user *User) HashPassword() error { if user.Password == nil { return utils.ErrBadHandleOrPassword } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*user.Password), bcrypt.DefaultCost) if err != nil { return utils.ErrInternal } user.Password = utils.AsStringP(string(hashedPassword)) return nil } func (user *User) ComparePassword(password string) error { if user.Password == nil { return utils.ErrBadHandleOrPassword } err := bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(password)) if err != nil { return utils.ErrBadHandleOrPassword } return nil }