diff --git a/cmd/root.go b/cmd/root.go index 336db04..916f55b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -66,13 +66,11 @@ func init() { rootCmd.Flags().IntP("port", "p", 3000, "Port to run the server on.") rootCmd.Flags().String("address", "0.0.0.0", "Address to bind the server to.") rootCmd.Flags().String("secret", "", "Secret to use for the cookie.") - rootCmd.Flags().String("root-url", "", "Root URL of traefik.") rootCmd.Flags().String("app-url", "", "The tinyauth URL.") rootCmd.Flags().String("users", "", "Comma separated list of users in the format username:bcrypt-hashed-password.") viper.BindEnv("port", "PORT") viper.BindEnv("address", "ADDRESS") viper.BindEnv("secret", "SECRET") - viper.BindEnv("root-url", "ROOT_URL") viper.BindEnv("app-url", "APP_URL") viper.BindEnv("users", "USERS") viper.BindPFlags(rootCmd.Flags()) diff --git a/docker-compose.example.yml b/docker-compose.example.yml index f6c553c..d3fc1a1 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -24,7 +24,6 @@ services: image: ghcr.io/steveiliop56/tinyauth:latest environment: - SECRET=some-random-32-chars-string - - ROOT_URL=https://example.com - APP_URL=https://tinyauth.example.com - USERS=user:$$2a$$10$$UdLYoJ5lgPsC0RKqYH/jMua7zIn0g9kPqWmhYayJYLaZQ/FTmH2/u labels: diff --git a/internal/api/api.go b/internal/api/api.go index 25a84ff..7d59472 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -11,6 +11,7 @@ import ( "tinyauth/internal/auth" "tinyauth/internal/hooks" "tinyauth/internal/types" + "tinyauth/internal/utils" "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" @@ -33,8 +34,13 @@ func Run(config types.Config, users types.UserList) { fileServer := http.FileServer(http.FS(dist)) store := cookie.NewStore([]byte(config.Secret)) - domain := strings.Split(config.RootURL, "://")[1] + domain, domainErr := utils.GetRootURL(config.AppURL) + if domainErr != nil { + log.Fatal().Err(domainErr).Msg("Failed to get domain") + os.Exit(1) + } + store.Options(sessions.Options{ Domain: fmt.Sprintf(".%s", domain), Path: "/", diff --git a/internal/types/types.go b/internal/types/types.go index dda53b3..cdefc75 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -22,7 +22,6 @@ type Config struct { Port int `validate:"number" mapstructure:"port"` Address string `mapstructure:"address, ip4_addr"` Secret string `validate:"required,len=32" mapstructure:"secret"` - RootURL string `validate:"required,url" mapstructure:"root-url"` AppURL string `validate:"required,url" mapstructure:"app-url"` Users string `validate:"required" mapstructure:"users"` } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index ecba7c8..9fc024b 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "net/url" "strings" "tinyauth/internal/types" ) @@ -26,4 +27,18 @@ func CreateUsersList(users string) (types.UserList, error) { } return userList, nil +} + +func GetRootURL(urlSrc string) (string, error) { + urlParsed, parseErr := url.Parse(urlSrc) + + if parseErr != nil { + return "", parseErr + } + + urlSplitted := strings.Split(urlParsed.Host, ".") + + urlFinal := urlSplitted[len(urlSplitted)-2] + "." + urlSplitted[len(urlSplitted)-1] + + return urlFinal, nil } \ No newline at end of file