This commit is contained in:
Stavros
2025-03-04 17:51:47 +02:00
parent d45b148725
commit 746ce016cb
8 changed files with 314 additions and 35 deletions

View File

@@ -15,8 +15,9 @@ type LoginRequest struct {
// User is the struct for a user
type User struct {
Username string
Password string
Username string
Password string
TotpSecret string
}
// Users is a list of users

View File

@@ -29,19 +29,15 @@ func ParseUsers(users string) (types.Users, error) {
// Loop through the users and split them by colon
for _, user := range userList {
// Split the user by colon
userSplit := strings.Split(user, ":")
parsed, parseErr := ParseUser(user)
// Check if the user is in the correct format
if len(userSplit) != 2 {
return types.Users{}, errors.New("invalid user format")
// Check if there was an error
if parseErr != nil {
return types.Users{}, parseErr
}
// Append the user to the users struct
usersParsed = append(usersParsed, types.User{
Username: userSplit[0],
Password: userSplit[1],
})
usersParsed = append(usersParsed, parsed)
}
log.Debug().Msg("Parsed users")
@@ -219,3 +215,29 @@ func Filter[T any](slice []T, test func(T) bool) (res []T) {
}
return res
}
// Parse user
func ParseUser(user string) (types.User, error) {
// Split the user by colon
userSplit := strings.Split(user, ":")
// Check if the user is in the correct format
if len(userSplit) < 2 || len(userSplit) > 3 {
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
}
// Return the user struct
return types.User{
Username: userSplit[0],
Password: userSplit[1],
TotpSecret: userSplit[2],
}, nil
}