package models import ( "git.sch9.ru/new_gate/ms-auth/internal/lib" "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 lib.ErrBadUsername } err := lib.ValidUsername(*user.Username) if err != nil { return err } return nil } func (user *User) ValidPassword() error { if user.Password == nil { return lib.ErrBadHandleOrPassword } err := lib.ValidPassword(*user.Password) if err != nil { return err } return nil } func (user *User) ValidEmail() error { if user.Email == nil { return lib.ErrBadEmail } return lib.ValidEmail(*user.Email) } func (user *User) ValidRole() error { if user.Role == nil { return lib.ErrBadRole } return user.Role.Valid() } func (user *User) HashPassword() error { if user.Password == nil { return lib.ErrBadHandleOrPassword } hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*user.Password), bcrypt.DefaultCost) if err != nil { return lib.ErrInternal } user.Password = lib.AsStringP(string(hashedPassword)) return nil } func (user *User) ComparePassword(password string) error { if user.Password == nil { return lib.ErrBadHandleOrPassword } err := bcrypt.CompareHashAndPassword([]byte(*user.Password), []byte(password)) if err != nil { return lib.ErrBadHandleOrPassword } return nil }