Update to not use pointers, verify username length

This commit is contained in:
Till Faelligen 2022-03-18 08:24:29 +01:00
parent 28d2134752
commit 1f21b7c8d9
2 changed files with 17 additions and 26 deletions

View file

@ -81,7 +81,11 @@ func main() {
os.Exit(1)
}
pass, err := getPassword(password, pwdFile, pwdStdin, os.Stdin)
if len(fmt.Sprintf("@%s:%s", *username, cfg.Global.ServerName)) > 255 {
logrus.Fatalln("Username can not be longer than 255 characters.")
}
pass, err := getPassword(*password, *pwdFile, *pwdStdin, os.Stdin)
if err != nil {
logrus.Fatalln(err)
}
@ -124,10 +128,10 @@ func main() {
logrus.Infoln("Created account", *username)
}
func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string, error) {
func getPassword(password, pwdFile string, pwdStdin bool, r io.Reader) (string, error) {
// read password from file
if *pwdFile != "" {
pw, err := ioutil.ReadFile(*pwdFile)
if pwdFile != "" {
pw, err := ioutil.ReadFile(pwdFile)
if err != nil {
return "", fmt.Errorf("Unable to read password from file: %v", err)
}
@ -135,7 +139,7 @@ func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string
}
// read password from stdin
if *pwdStdin {
if pwdStdin {
data, err := ioutil.ReadAll(r)
if err != nil {
return "", fmt.Errorf("Unable to read password from stdin: %v", err)
@ -143,7 +147,7 @@ func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string
return strings.TrimSpace(string(data)), nil
}
if *password == "" && *pwdFile == "" && !*pwdStdin {
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()))
@ -162,5 +166,5 @@ func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string
}
return strings.TrimSpace(string(bytePassword)), nil
}
return *password, nil
return password, nil
}

View file

@ -8,18 +8,14 @@ import (
func Test_getPassword(t *testing.T) {
type args struct {
password *string
pwdFile *string
pwdStdin *bool
password string
pwdFile string
pwdStdin bool
reader io.Reader
writer func()
}
pass := "mySecretPass"
empty := ""
f := false
passwordFile := "testdata/my.pass"
passwordStdin := true
reader := &bytes.Buffer{}
_, err := reader.WriteString(pass)
if err != nil {
@ -34,37 +30,28 @@ func Test_getPassword(t *testing.T) {
{
name: "password defined",
args: args{
password: &pass,
pwdFile: &empty,
pwdStdin: &f,
password: pass,
},
want: pass,
},
{
name: "pwdFile defined",
args: args{
pwdFile: &passwordFile,
password: &empty,
pwdStdin: &f,
pwdFile: passwordFile,
},
want: pass,
},
{
name: "read pass from stdin defined",
args: args{
pwdStdin: &passwordStdin,
pwdStdin: true,
reader: reader,
password: &empty,
pwdFile: &empty,
},
want: pass,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.args.writer != nil {
go tt.args.writer()
}
got, err := getPassword(tt.args.password, tt.args.pwdFile, tt.args.pwdStdin, tt.args.reader)
if !tt.wantErr && err != nil {
t.Errorf("expected no error, but got %v", err)