From b166ceaea72dc6db77467621ffc270fbdccb6566 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 29 Apr 2018 17:04:23 -0700 Subject: Avoid people to unlink their OAuth2 account without having a local password --- storage/user.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'storage') diff --git a/storage/user.go b/storage/user.go index fea59d4..de58c09 100644 --- a/storage/user.go +++ b/storage/user.go @@ -339,6 +339,24 @@ func (s *Storage) CheckPassword(username, password string) error { return nil } +// HasPassword returns true if the given user has a password defined. +func (s *Storage) HasPassword(userID int64) (bool, error) { + var result bool + query := `SELECT true FROM users WHERE id=$1 AND password <> ''` + + err := s.db.QueryRow(query, userID).Scan(&result) + if err == sql.ErrNoRows { + return false, nil + } else if err != nil { + return false, fmt.Errorf("unable to execute query: %v", err) + } + + if result { + return true, nil + } + return false, nil +} + func hashPassword(password string) (string, error) { bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) return string(bytes), err -- cgit v1.2.3