mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-11-01 16:02:25 -05:00
Password change (#123)
* add password change command * document cli commands * go fmt
This commit is contained in:
parent
a940a520d3
commit
eb13faf54f
3 changed files with 280 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue