From eb13faf54ff745a036d68985d1b3163d138e4ad4 Mon Sep 17 00:00:00 2001 From: Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com> Date: Sat, 31 Jul 2021 13:57:23 +0200 Subject: Password change (#123) * add password change command * document cli commands * go fmt --- internal/cliactions/admin/account/account.go | 48 ++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'internal') diff --git a/internal/cliactions/admin/account/account.go b/internal/cliactions/admin/account/account.go index 123913327..732527aa4 100644 --- a/internal/cliactions/admin/account/account.go +++ b/internal/cliactions/admin/account/account.go @@ -31,6 +31,7 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/db/pg" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/util" + "golang.org/x/crypto/bcrypt" ) // Create creates a new account in the database using the provided flags. @@ -208,3 +209,50 @@ var Suspend cliactions.GTSAction = func(ctx context.Context, c *config.Config, l // TODO return nil } + +// Password sets the password of target account. +var Password cliactions.GTSAction = func(ctx context.Context, c *config.Config, log *logrus.Logger) error { + dbConn, err := pg.NewPostgresService(ctx, c, log) + if err != nil { + return fmt.Errorf("error creating dbservice: %s", err) + } + + username, ok := c.AccountCLIFlags[config.UsernameFlag] + if !ok { + return errors.New("no username set") + } + if err := util.ValidateUsername(username); err != nil { + return err + } + + password, ok := c.AccountCLIFlags[config.PasswordFlag] + if !ok { + return errors.New("no password set") + } + if err := util.ValidateNewPassword(password); err != nil { + return err + } + + a := >smodel.Account{} + if err := dbConn.GetLocalAccountByUsername(username, a); err != nil { + return err + } + + u := >smodel.User{} + if err := dbConn.GetWhere([]db.Where{{Key: "account_id", Value: a.ID}}, u); err != nil { + return err + } + + pw, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + return fmt.Errorf("error hashing password: %s", err) + } + + u.EncryptedPassword = string(pw) + + if err := dbConn.UpdateByID(u.ID, u); err != nil { + return err + } + + return nil +} -- cgit v1.2.3