mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-05-08 05:18:11 +00:00
1382ab41e7
* wip * fix: fix util imports * fix: fix bootstrap import issues * fix: fix cli imports * fix: context controller * fix: use new context in user controller * fix: fix imports and context in proxy controller * fix: fix oauth and oidc controller imports and context * feat: finalize context functionality * refactor: simplify acls checking logic by passing the entire acl struct * chore: rename get basic auth to encode basic auth for clarity * fix: fix controller tests * tests: fix service tests * tests: fix utils tests * tests: move to testify for testing in utils * fix: fix config reference generator * tests: add tests for context parsing * tests: add tests for context middleware * tests: remove error wrapper from context tests * tests: fix log wrapper tests * fix: fix verion setting in cd and dockerfiles * fix: review comments batch 1 * fix: review comments batch 2 * fix: review comments batch 3 * fix: delete totp pending session cookie on totp success * tests: fix user controller tests * fix: don't audit login too early * fix: own comments
108 lines
2.1 KiB
Go
108 lines
2.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/mail"
|
|
"strings"
|
|
|
|
"github.com/tinyauthapp/tinyauth/internal/model"
|
|
)
|
|
|
|
func ParseUsers(usersStr []string, userAttributes map[string]model.UserAttributes) (*[]model.LocalUser, error) {
|
|
var users []model.LocalUser
|
|
|
|
if len(usersStr) == 0 {
|
|
return &users, nil
|
|
}
|
|
|
|
for _, user := range usersStr {
|
|
if strings.TrimSpace(user) == "" {
|
|
continue
|
|
}
|
|
parsed, err := ParseUser(strings.TrimSpace(user))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if attrs, ok := userAttributes[parsed.Username]; ok {
|
|
parsed.Attributes = attrs
|
|
}
|
|
users = append(users, *parsed)
|
|
}
|
|
|
|
return &users, nil
|
|
}
|
|
|
|
func GetUsers(usersCfg []string, usersPath string, userAttributes map[string]model.UserAttributes) (*[]model.LocalUser, error) {
|
|
var usersStr []string
|
|
|
|
if len(usersCfg) == 0 && usersPath == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
if len(usersCfg) > 0 {
|
|
usersStr = append(usersStr, usersCfg...)
|
|
}
|
|
|
|
if usersPath != "" {
|
|
contents, err := ReadFile(usersPath)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
lines := strings.SplitSeq(contents, "\n")
|
|
|
|
for line := range lines {
|
|
lineTrimmed := strings.TrimSpace(line)
|
|
if lineTrimmed == "" {
|
|
continue
|
|
}
|
|
usersStr = append(usersStr, lineTrimmed)
|
|
}
|
|
}
|
|
|
|
return ParseUsers(usersStr, userAttributes)
|
|
}
|
|
|
|
func ParseUser(userStr string) (*model.LocalUser, error) {
|
|
if strings.Contains(userStr, "$$") {
|
|
userStr = strings.ReplaceAll(userStr, "$$", "$")
|
|
}
|
|
|
|
parts := strings.SplitN(userStr, ":", 4)
|
|
|
|
if len(parts) < 2 || len(parts) > 3 {
|
|
return nil, errors.New("invalid user format")
|
|
}
|
|
|
|
for i, part := range parts {
|
|
trimmed := strings.TrimSpace(part)
|
|
if trimmed == "" {
|
|
return nil, errors.New("invalid user format")
|
|
}
|
|
parts[i] = trimmed
|
|
}
|
|
|
|
user := model.LocalUser{
|
|
Username: parts[0],
|
|
Password: parts[1],
|
|
}
|
|
|
|
if len(parts) == 3 {
|
|
user.TOTPSecret = parts[2]
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
func CompileUserEmail(username string, domain string) string {
|
|
_, err := mail.ParseAddress(username)
|
|
|
|
if err != nil {
|
|
return fmt.Sprintf("%s@%s", strings.ToLower(username), domain)
|
|
}
|
|
|
|
return username
|
|
}
|