fix: handle new lines and spaces in the secret files

This commit is contained in:
Stavros
2025-04-10 15:34:46 +03:00
parent 1169c633cc
commit 8a21345706
2 changed files with 30 additions and 11 deletions

View File

@@ -130,7 +130,7 @@ func GetSecret(conf string, file string) string {
}
// Return the contents of the file
return contents
return ParseSecretFile(contents)
}
// Get the users from the config or file
@@ -241,23 +241,21 @@ func ParseUser(user string) (types.User, error) {
return types.User{}, errors.New("invalid user format")
}
// Check if the user has a totp secret
if len(userSplit) == 2 {
// Check for empty username or password
if userSplit[1] == "" || userSplit[0] == "" {
// Check for empty strings
for _, userPart := range userSplit {
if strings.TrimSpace(userPart) == "" {
return types.User{}, errors.New("invalid user format")
}
}
// Check if the user has a totp secret
if len(userSplit) == 2 {
return types.User{
Username: userSplit[0],
Password: userSplit[1],
}, nil
}
// Check for empty username, password or totp secret
if userSplit[2] == "" || userSplit[1] == "" || userSplit[0] == "" {
return types.User{}, errors.New("invalid user format")
}
// Return the user struct
return types.User{
Username: userSplit[0],
@@ -265,3 +263,23 @@ func ParseUser(user string) (types.User, error) {
TotpSecret: userSplit[2],
}, nil
}
// Parse secret file
func ParseSecretFile(contents string) string {
// Split to lines
lines := strings.Split(contents, "\n")
// Loop through the lines
for _, line := range lines {
// Check if the line is empty
if strings.TrimSpace(line) == "" {
continue
}
// Return the line
return strings.TrimSpace(line)
}
// Return an empty string
return ""
}