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) 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 { if err != nil {
logrus.Fatalln(err) logrus.Fatalln(err)
} }
@ -124,10 +128,10 @@ func main() {
logrus.Infoln("Created account", *username) 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 // read password from file
if *pwdFile != "" { if pwdFile != "" {
pw, err := ioutil.ReadFile(*pwdFile) pw, err := ioutil.ReadFile(pwdFile)
if err != nil { if err != nil {
return "", fmt.Errorf("Unable to read password from file: %v", err) 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 // read password from stdin
if *pwdStdin { if pwdStdin {
data, err := ioutil.ReadAll(r) data, err := ioutil.ReadAll(r)
if err != nil { if err != nil {
return "", fmt.Errorf("Unable to read password from stdin: %v", err) 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 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 // 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()))
@ -162,5 +166,5 @@ func getPassword(password, pwdFile *string, pwdStdin *bool, r io.Reader) (string
} }
return strings.TrimSpace(string(bytePassword)), nil 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) { func Test_getPassword(t *testing.T) {
type args struct { type args struct {
password *string password string
pwdFile *string pwdFile string
pwdStdin *bool pwdStdin bool
reader io.Reader reader io.Reader
writer func()
} }
pass := "mySecretPass" pass := "mySecretPass"
empty := ""
f := false
passwordFile := "testdata/my.pass" passwordFile := "testdata/my.pass"
passwordStdin := true
reader := &bytes.Buffer{} reader := &bytes.Buffer{}
_, err := reader.WriteString(pass) _, err := reader.WriteString(pass)
if err != nil { if err != nil {
@ -34,37 +30,28 @@ func Test_getPassword(t *testing.T) {
{ {
name: "password defined", name: "password defined",
args: args{ args: args{
password: &pass, password: pass,
pwdFile: &empty,
pwdStdin: &f,
}, },
want: pass, want: pass,
}, },
{ {
name: "pwdFile defined", name: "pwdFile defined",
args: args{ args: args{
pwdFile: &passwordFile, pwdFile: passwordFile,
password: &empty,
pwdStdin: &f,
}, },
want: pass, want: pass,
}, },
{ {
name: "read pass from stdin defined", name: "read pass from stdin defined",
args: args{ args: args{
pwdStdin: &passwordStdin, pwdStdin: true,
reader: reader, reader: reader,
password: &empty,
pwdFile: &empty,
}, },
want: pass, want: pass,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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) got, err := getPassword(tt.args.password, tt.args.pwdFile, tt.args.pwdStdin, tt.args.reader)
if !tt.wantErr && err != nil { if !tt.wantErr && err != nil {
t.Errorf("expected no error, but got %v", err) t.Errorf("expected no error, but got %v", err)