mirror of
				https://github.com/steveiliop56/tinyauth.git
				synced 2025-11-04 08:05:42 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			149 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			149 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package utils
 | 
						|
 | 
						|
import (
 | 
						|
	"errors"
 | 
						|
	"net/url"
 | 
						|
	"os"
 | 
						|
	"slices"
 | 
						|
	"strings"
 | 
						|
	"tinyauth/internal/constants"
 | 
						|
	"tinyauth/internal/types"
 | 
						|
 | 
						|
	"github.com/rs/zerolog/log"
 | 
						|
)
 | 
						|
 | 
						|
func ParseUsers(users string) (types.Users, error) {
 | 
						|
	log.Debug().Msg("Parsing users")
 | 
						|
	var usersParsed types.Users
 | 
						|
	userList := strings.Split(users, ",")
 | 
						|
 | 
						|
	if len(userList) == 0 {
 | 
						|
		return types.Users{}, errors.New("invalid user format")
 | 
						|
	}
 | 
						|
 | 
						|
	for _, user := range userList {
 | 
						|
		userSplit := strings.Split(user, ":")
 | 
						|
		if len(userSplit) != 2 {
 | 
						|
			return types.Users{}, errors.New("invalid user format")
 | 
						|
		}
 | 
						|
		usersParsed = append(usersParsed, types.User{
 | 
						|
			Username: userSplit[0],
 | 
						|
			Password: userSplit[1],
 | 
						|
		})
 | 
						|
	}
 | 
						|
 | 
						|
	log.Debug().Msg("Parsed users")
 | 
						|
 | 
						|
	return usersParsed, nil
 | 
						|
}
 | 
						|
 | 
						|
func GetRootURL(urlSrc string) (string, error) {
 | 
						|
	urlParsed, parseErr := url.Parse(urlSrc)
 | 
						|
 | 
						|
	if parseErr != nil {
 | 
						|
		return "", parseErr
 | 
						|
	}
 | 
						|
 | 
						|
	urlSplitted := strings.Split(urlParsed.Hostname(), ".")
 | 
						|
 | 
						|
	urlFinal := strings.Join(urlSplitted[1:], ".")
 | 
						|
 | 
						|
	return urlFinal, nil
 | 
						|
}
 | 
						|
 | 
						|
func ReadFile(file string) (string, error) {
 | 
						|
	_, statErr := os.Stat(file)
 | 
						|
 | 
						|
	if statErr != nil {
 | 
						|
		return "", statErr
 | 
						|
	}
 | 
						|
 | 
						|
	data, readErr := os.ReadFile(file)
 | 
						|
 | 
						|
	if readErr != nil {
 | 
						|
		return "", readErr
 | 
						|
	}
 | 
						|
 | 
						|
	return string(data), nil
 | 
						|
}
 | 
						|
 | 
						|
func ParseFileToLine(content string) string {
 | 
						|
	lines := strings.Split(content, "\n")
 | 
						|
	users := make([]string, 0)
 | 
						|
 | 
						|
	for _, line := range lines {
 | 
						|
		if strings.TrimSpace(line) == "" {
 | 
						|
			continue
 | 
						|
		}
 | 
						|
 | 
						|
		users = append(users, strings.TrimSpace(line))
 | 
						|
	}
 | 
						|
 | 
						|
	return strings.Join(users, ",")
 | 
						|
}
 | 
						|
 | 
						|
func GetSecret(conf string, file string) string {
 | 
						|
	if conf == "" && file == "" {
 | 
						|
		return ""
 | 
						|
	}
 | 
						|
 | 
						|
	if conf != "" {
 | 
						|
		return conf
 | 
						|
	}
 | 
						|
 | 
						|
	contents, err := ReadFile(file)
 | 
						|
 | 
						|
	if err != nil {
 | 
						|
		return ""
 | 
						|
	}
 | 
						|
 | 
						|
	return contents
 | 
						|
}
 | 
						|
 | 
						|
func GetUsers(conf string, file string) (types.Users, error) {
 | 
						|
	var users string
 | 
						|
 | 
						|
	if conf == "" && file == "" {
 | 
						|
		return types.Users{}, errors.New("no users provided")
 | 
						|
	}
 | 
						|
 | 
						|
	if conf != "" {
 | 
						|
		log.Debug().Msg("Using users from config")
 | 
						|
		users += conf
 | 
						|
	}
 | 
						|
 | 
						|
	if file != "" {
 | 
						|
		fileContents, fileErr := ReadFile(file)
 | 
						|
 | 
						|
		if fileErr == nil {
 | 
						|
			log.Debug().Msg("Using users from file")
 | 
						|
			if users != "" {
 | 
						|
				users += ","
 | 
						|
			}
 | 
						|
			users += ParseFileToLine(fileContents)
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	return ParseUsers(users)
 | 
						|
}
 | 
						|
 | 
						|
func OAuthConfigured(config types.Config) bool {
 | 
						|
	return (config.GithubClientId != "" && config.GithubClientSecret != "") || (config.GoogleClientId != "" && config.GoogleClientSecret != "") || (config.GenericClientId != "" && config.GenericClientSecret != "")
 | 
						|
}
 | 
						|
 | 
						|
func GetTinyauthLabels(labels map[string]string) types.TinyauthLabels {
 | 
						|
	var tinyauthLabels types.TinyauthLabels
 | 
						|
	for label, value := range labels {
 | 
						|
		if slices.Contains(constants.TinyauthLabels, label) {
 | 
						|
			log.Debug().Str("label", label).Msg("Found label")
 | 
						|
			switch label {
 | 
						|
			case "tinyauth.oauth.whitelist":
 | 
						|
				tinyauthLabels.OAuthWhitelist = strings.Split(value, ",")
 | 
						|
			case "tinyauth.users":
 | 
						|
				tinyauthLabels.Users = strings.Split(value, ",")
 | 
						|
			}
 | 
						|
		}
 | 
						|
	}
 | 
						|
	return tinyauthLabels
 | 
						|
}
 |