From 9016f8704157729829ffb51a524fcf7dbec9d51e Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 14 Nov 2018 15:03:22 -0500 Subject: [PATCH] Add --reset-pass option for admin pass reset Usage: writefreely --reset-pass This closes #25, closes T534 --- admin.go | 21 +++++++++++++++++++++ app.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 admin.go diff --git a/admin.go b/admin.go new file mode 100644 index 0000000..29cff31 --- /dev/null +++ b/admin.go @@ -0,0 +1,21 @@ +package writefreely + +import ( + "fmt" + "github.com/writeas/impart" + "github.com/writeas/web-core/auth" + "net/http" +) + +func adminResetPassword(app *app, u *User, newPass string) error { + hashedPass, err := auth.HashPass([]byte(newPass)) + if err != nil { + return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not create password hash: %v", err)} + } + + err = app.db.ChangePassphrase(u.ID, true, "", hashedPass) + if err != nil { + return impart.HTTPError{http.StatusInternalServerError, fmt.Sprintf("Could not update passphrase: %v", err)} + } + return nil +} diff --git a/app.go b/app.go index 7adc513..acf8490 100644 --- a/app.go +++ b/app.go @@ -18,6 +18,7 @@ import ( "github.com/gorilla/mux" "github.com/gorilla/schema" "github.com/gorilla/sessions" + "github.com/manifoldco/promptui" "github.com/writeas/web-core/converter" "github.com/writeas/web-core/log" "github.com/writeas/writefreely/config" @@ -129,6 +130,7 @@ func Serve() { doConfig := flag.Bool("config", false, "Run the configuration process") genKeys := flag.Bool("gen-keys", false, "Generate encryption and authentication keys") createSchema := flag.Bool("init-db", false, "Initialize app database") + resetPassUser := flag.String("reset-pass", "", "Reset the given user's password") flag.Parse() debugging = *debugPtr @@ -227,6 +229,48 @@ func Serve() { } } os.Exit(0) + } else if *resetPassUser != "" { + // Connect to the database + log.Info("Loading configuration...") + cfg, err := config.Load() + if err != nil { + log.Error("Unable to load configuration: %v", err) + os.Exit(1) + } + app.cfg = cfg + connectToDatabase(app) + defer shutdown(app) + + // Fetch user + u, err := app.db.GetUserForAuth(*resetPassUser) + if err != nil { + log.Error("Get user: %s", err) + os.Exit(1) + } + + // Prompt for new password + prompt := promptui.Prompt{ + Templates: &promptui.PromptTemplates{ + Success: "{{ . | bold | faint }}: ", + }, + Label: "New password", + Mask: '*', + } + newPass, err := prompt.Run() + if err != nil { + log.Error("%s", err) + os.Exit(1) + } + + // Do the update + log.Info("Updating...") + err = adminResetPassword(app, u, newPass) + if err != nil { + log.Error("%s", err) + os.Exit(1) + } + log.Info("Success.") + os.Exit(0) } log.Info("Initializing...")