Check user existence

Fallback to asking for the password if non is defined
This commit is contained in:
Till Faelligen 2022-03-17 17:11:13 +01:00
parent 1894f34868
commit 003fd3478e

View file

@ -24,12 +24,11 @@ import (
"regexp"
"strings"
"github.com/matrix-org/dendrite/setup"
"github.com/matrix-org/dendrite/setup/base"
"github.com/matrix-org/dendrite/userapi/api"
"github.com/sirupsen/logrus"
"golang.org/x/term"
"github.com/matrix-org/dendrite/setup"
"github.com/matrix-org/dendrite/userapi/api"
)
const usage = `Usage: %s
@ -43,7 +42,7 @@ Example:
# use password from file
%s --config dendrite.yaml -username alice -passwordfile my.pass
# ask user to provide password
%s --config dendrite.yaml -username alice -ask-pass
%s --config dendrite.yaml -username alice
# read password from stdin
%s --config dendrite.yaml -username alice -passwordstdin < my.pass
cat my.pass | %s --config dendrite.yaml -username alice -passwordstdin
@ -56,10 +55,9 @@ Arguments:
var (
username = flag.String("username", "", "The username of the account to register (specify the localpart only, e.g. 'alice' for '@alice:domain.com')")
password = flag.String("password", "", "The password to associate with the account (optional, account will be password-less if not specified)")
password = flag.String("password", "", "The password to associate with the account")
pwdFile = flag.String("passwordfile", "", "The file to use for the password (e.g. for automated account creation)")
pwdStdin = flag.Bool("passwordstdin", false, "Reads the password from stdin")
askPass = flag.Bool("ask-pass", false, "Ask for the password to use")
isAdmin = flag.Bool("admin", false, "Create an admin account")
resetPassword = flag.Bool("reset-password", false, "Resets the password for the given username")
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-=./]+$`)
@ -83,7 +81,10 @@ func main() {
os.Exit(1)
}
pass := getPassword(password, pwdFile, pwdStdin, askPass, os.Stdin)
pass, err := getPassword(password, pwdFile, pwdStdin, os.Stdin)
if err != nil {
logrus.Fatalln(err)
}
b := base.NewBaseDendrite(cfg, "create-account")
accountDB := b.CreateAccountsDB()
@ -92,8 +93,15 @@ func main() {
if *isAdmin {
accType = api.AccountTypeAdmin
}
var err error
available, err := accountDB.CheckAccountAvailability(context.Background(), *username)
if err != nil {
logrus.Fatalln("Unable check username existence.")
}
if *resetPassword {
if available {
logrus.Fatalln("Username could not be found.")
}
err = accountDB.SetPassword(context.Background(), *username, pass)
if err != nil {
logrus.Fatalf("Failed to update password for user %s: %s", *username, err.Error())
@ -104,6 +112,9 @@ func main() {
logrus.Infof("Updated password for user %s and invalidated all logins\n", *username)
return
}
if !available {
logrus.Fatalln("Username is already in use.")
}
_, err = accountDB.CreateAccount(context.Background(), *username, pass, "", accType)
if err != nil {
@ -113,53 +124,43 @@ func main() {
logrus.Infoln("Created account", *username)
}
func getPassword(password, pwdFile *string, pwdStdin, askPass *bool, r io.Reader) string {
// no password option set, use empty password
if password == nil && pwdFile == nil && pwdStdin == nil && askPass == nil {
return ""
}
// password defined as parameter
if password != nil && *password != "" {
return *password
}
func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string, error) {
// read password from file
if pwdFile != nil && *pwdFile != "" {
if *pwdFile != "" {
pw, err := ioutil.ReadFile(*pwdFile)
if err != nil {
logrus.Fatalln("Unable to read password from file:", err)
return "", fmt.Errorf("Unable to read password from file: %v", err)
}
return strings.TrimSpace(string(pw))
return strings.TrimSpace(string(pw)), nil
}
// read password from stdin
if pwdStdin != nil && *pwdStdin {
if *pwdStdin {
data, err := ioutil.ReadAll(r)
if err != nil {
logrus.Fatalln("Unable to read password from stdin:", err)
return "", fmt.Errorf("Unable to read password from stdin: %v", err)
}
return strings.TrimSpace(string(data))
return strings.TrimSpace(string(data)), nil
}
// ask the user to provide the password
if *askPass {
if *password == "" && *pwdFile == "" && !*pwdStdin {
// If no parameter was set, ask the user to provide the password
fmt.Print("Enter Password: ")
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
logrus.Fatalln("Unable to read password:", err)
return "", fmt.Errorf("Unable to read password: %v", err)
}
fmt.Println()
fmt.Print("Confirm Password: ")
bytePassword2, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
logrus.Fatalln("Unable to read password:", err)
return "", fmt.Errorf("Unable to read password: %v", err)
}
fmt.Println()
if strings.TrimSpace(string(bytePassword)) != strings.TrimSpace(string(bytePassword2)) {
logrus.Fatalln("Entered passwords don't match")
return "", fmt.Errorf("Entered passwords don't match")
}
return strings.TrimSpace(string(bytePassword))
return strings.TrimSpace(string(bytePassword)), nil
}
return ""
return *password, nil
}