mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-03 23:55:44 +00:00 
			
		
		
		
	fix: handle new lines and spaces in the secret files
This commit is contained in:
		@@ -130,7 +130,7 @@ func GetSecret(conf string, file string) string {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Return the contents of the file
 | 
						// Return the contents of the file
 | 
				
			||||||
	return contents
 | 
						return ParseSecretFile(contents)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Get the users from the config or file
 | 
					// 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")
 | 
							return types.User{}, errors.New("invalid user format")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check if the user has a totp secret
 | 
						// Check for empty strings
 | 
				
			||||||
	if len(userSplit) == 2 {
 | 
						for _, userPart := range userSplit {
 | 
				
			||||||
		// Check for empty username or password
 | 
							if strings.TrimSpace(userPart) == "" {
 | 
				
			||||||
		if userSplit[1] == "" || userSplit[0] == "" {
 | 
					 | 
				
			||||||
			return types.User{}, errors.New("invalid user format")
 | 
								return types.User{}, errors.New("invalid user format")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Check if the user has a totp secret
 | 
				
			||||||
 | 
						if len(userSplit) == 2 {
 | 
				
			||||||
		return types.User{
 | 
							return types.User{
 | 
				
			||||||
			Username: userSplit[0],
 | 
								Username: userSplit[0],
 | 
				
			||||||
			Password: userSplit[1],
 | 
								Password: userSplit[1],
 | 
				
			||||||
		}, nil
 | 
							}, 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 the user struct
 | 
				
			||||||
	return types.User{
 | 
						return types.User{
 | 
				
			||||||
		Username:   userSplit[0],
 | 
							Username:   userSplit[0],
 | 
				
			||||||
@@ -265,3 +263,23 @@ func ParseUser(user string) (types.User, error) {
 | 
				
			|||||||
		TotpSecret: userSplit[2],
 | 
							TotpSecret: userSplit[2],
 | 
				
			||||||
	}, nil
 | 
						}, 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 ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package utils_test
 | 
					package utils_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
@@ -123,7 +124,7 @@ func TestGetSecret(t *testing.T) {
 | 
				
			|||||||
	expected := "test"
 | 
						expected := "test"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Create file
 | 
						// Create file
 | 
				
			||||||
	err := os.WriteFile(file, []byte(expected), 0644)
 | 
						err := os.WriteFile(file, []byte(fmt.Sprintf("\n\n    \n\n\n  %s   \n\n    \n  ", expected)), 0644)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Check if there was an error
 | 
						// Check if there was an error
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user