summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-07-31 13:57:23 +0200
committerLibravatar GitHub <noreply@github.com>2021-07-31 13:57:23 +0200
commiteb13faf54ff745a036d68985d1b3163d138e4ad4 (patch)
tree3ae74a9d7f8ec3b8ebd0f355fba62aea7751a8a5 /internal
parentLink hashtag bug (#121) (diff)
downloadgotosocial-eb13faf54ff745a036d68985d1b3163d138e4ad4.tar.xz
Password change (#123)
* add password change command * document cli commands * go fmt
Diffstat (limited to 'internal')
-rw-r--r--internal/cliactions/admin/account/account.go48
1 files changed, 48 insertions, 0 deletions
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 := &gtsmodel.Account{}
+ if err := dbConn.GetLocalAccountByUsername(username, a); err != nil {
+ return err
+ }
+
+ u := &gtsmodel.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
+}