mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-01 03:03:10 -06:00
Check user existence
Fallback to asking for the password if non is defined
This commit is contained in:
parent
1894f34868
commit
003fd3478e
|
|
@ -24,12 +24,11 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/setup"
|
||||||
"github.com/matrix-org/dendrite/setup/base"
|
"github.com/matrix-org/dendrite/setup/base"
|
||||||
|
"github.com/matrix-org/dendrite/userapi/api"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/setup"
|
|
||||||
"github.com/matrix-org/dendrite/userapi/api"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `Usage: %s
|
const usage = `Usage: %s
|
||||||
|
|
@ -43,7 +42,7 @@ Example:
|
||||||
# use password from file
|
# use password from file
|
||||||
%s --config dendrite.yaml -username alice -passwordfile my.pass
|
%s --config dendrite.yaml -username alice -passwordfile my.pass
|
||||||
# ask user to provide password
|
# ask user to provide password
|
||||||
%s --config dendrite.yaml -username alice -ask-pass
|
%s --config dendrite.yaml -username alice
|
||||||
# read password from stdin
|
# read password from stdin
|
||||||
%s --config dendrite.yaml -username alice -passwordstdin < my.pass
|
%s --config dendrite.yaml -username alice -passwordstdin < my.pass
|
||||||
cat my.pass | %s --config dendrite.yaml -username alice -passwordstdin
|
cat my.pass | %s --config dendrite.yaml -username alice -passwordstdin
|
||||||
|
|
@ -56,10 +55,9 @@ Arguments:
|
||||||
|
|
||||||
var (
|
var (
|
||||||
username = flag.String("username", "", "The username of the account to register (specify the localpart only, e.g. 'alice' for '@alice:domain.com')")
|
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)")
|
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")
|
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")
|
isAdmin = flag.Bool("admin", false, "Create an admin account")
|
||||||
resetPassword = flag.Bool("reset-password", false, "Resets the password for the given username")
|
resetPassword = flag.Bool("reset-password", false, "Resets the password for the given username")
|
||||||
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-=./]+$`)
|
validUsernameRegex = regexp.MustCompile(`^[0-9a-z_\-=./]+$`)
|
||||||
|
|
@ -83,7 +81,10 @@ func main() {
|
||||||
os.Exit(1)
|
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")
|
b := base.NewBaseDendrite(cfg, "create-account")
|
||||||
accountDB := b.CreateAccountsDB()
|
accountDB := b.CreateAccountsDB()
|
||||||
|
|
@ -92,8 +93,15 @@ func main() {
|
||||||
if *isAdmin {
|
if *isAdmin {
|
||||||
accType = api.AccountTypeAdmin
|
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 *resetPassword {
|
||||||
|
if available {
|
||||||
|
logrus.Fatalln("Username could not be found.")
|
||||||
|
}
|
||||||
err = accountDB.SetPassword(context.Background(), *username, pass)
|
err = accountDB.SetPassword(context.Background(), *username, pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatalf("Failed to update password for user %s: %s", *username, err.Error())
|
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)
|
logrus.Infof("Updated password for user %s and invalidated all logins\n", *username)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !available {
|
||||||
|
logrus.Fatalln("Username is already in use.")
|
||||||
|
}
|
||||||
|
|
||||||
_, err = accountDB.CreateAccount(context.Background(), *username, pass, "", accType)
|
_, err = accountDB.CreateAccount(context.Background(), *username, pass, "", accType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -113,53 +124,43 @@ func main() {
|
||||||
logrus.Infoln("Created account", *username)
|
logrus.Infoln("Created account", *username)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPassword(password, pwdFile *string, pwdStdin, askPass *bool, r io.Reader) string {
|
func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string, error) {
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// read password from file
|
// read password from file
|
||||||
if pwdFile != nil && *pwdFile != "" {
|
if *pwdFile != "" {
|
||||||
pw, err := ioutil.ReadFile(*pwdFile)
|
pw, err := ioutil.ReadFile(*pwdFile)
|
||||||
if err != nil {
|
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
|
// read password from stdin
|
||||||
if pwdStdin != nil && *pwdStdin {
|
if *pwdStdin {
|
||||||
data, err := ioutil.ReadAll(r)
|
data, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
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 *password == "" && *pwdFile == "" && !*pwdStdin {
|
||||||
if *askPass {
|
// If no parameter was set, ask the user to provide the password
|
||||||
fmt.Print("Enter Password: ")
|
fmt.Print("Enter Password: ")
|
||||||
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
|
bytePassword, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatalln("Unable to read password:", err)
|
return "", fmt.Errorf("Unable to read password: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Print("Confirm Password: ")
|
fmt.Print("Confirm Password: ")
|
||||||
bytePassword2, err := term.ReadPassword(int(os.Stdin.Fd()))
|
bytePassword2, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatalln("Unable to read password:", err)
|
return "", fmt.Errorf("Unable to read password: %v", err)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
if strings.TrimSpace(string(bytePassword)) != strings.TrimSpace(string(bytePassword2)) {
|
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 *password, nil
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue