@@ -13,6 +13,7 @@ import (
1313 "tinyauth/internal/docker"
1414 "tinyauth/internal/handlers"
1515 "tinyauth/internal/hooks"
16+ "tinyauth/internal/ldap"
1617 "tinyauth/internal/providers"
1718 "tinyauth/internal/server"
1819 "tinyauth/internal/types"
@@ -58,10 +59,6 @@ var rootCmd = &cobra.Command{
5859 users , err := utils .GetUsers (config .Users , config .UsersFile )
5960 HandleError (err , "Failed to parse users" )
6061
61- if len (users ) == 0 && ! utils .OAuthConfigured (config ) {
62- HandleError (errors .New ("no users or OAuth configured" ), "No users or OAuth configured" )
63- }
64-
6562 // Get domain
6663 log .Debug ().Msg ("Getting domain" )
6764 domain , err := utils .GetUpperDomain (config .AppURL )
@@ -143,8 +140,35 @@ var rootCmd = &cobra.Command{
143140 docker , err := docker .NewDocker ()
144141 HandleError (err , "Failed to initialize docker" )
145142
143+ // Create LDAP service if configured
144+ var ldapService * ldap.LDAP
145+
146+ if config .LdapAddress != "" {
147+ log .Info ().Msg ("Using LDAP for authentication" )
148+
149+ ldapConfig := types.LdapConfig {
150+ Address : config .LdapAddress ,
151+ BindDN : config .LdapBindDN ,
152+ BindPassword : config .LdapBindPassword ,
153+ BaseDN : config .LdapBaseDN ,
154+ Insecure : config .LdapInsecure ,
155+ SearchFilter : config .LdapSearchFilter ,
156+ }
157+
158+ // Create LDAP service
159+ ldapService , err = ldap .NewLDAP (ldapConfig )
160+ HandleError (err , "Failed to create LDAP service" )
161+ } else {
162+ log .Info ().Msg ("LDAP not configured, using local users or OAuth" )
163+ }
164+
165+ // Check if we have any users configured
166+ if len (users ) == 0 && ! utils .OAuthConfigured (config ) && ldapService == nil {
167+ HandleError (errors .New ("err no users" ), "Unable to find a source of users" )
168+ }
169+
146170 // Create auth service
147- auth := auth .NewAuth (authConfig , docker )
171+ auth := auth .NewAuth (authConfig , docker , ldapService )
148172
149173 // Create OAuth providers service
150174 providers := providers .NewProviders (oauthConfig )
@@ -221,6 +245,12 @@ func init() {
221245 rootCmd .Flags ().String ("app-title" , "Tinyauth" , "Title of the app." )
222246 rootCmd .Flags ().String ("forgot-password-message" , "You can reset your password by changing the `USERS` environment variable." , "Message to show on the forgot password page." )
223247 rootCmd .Flags ().String ("background-image" , "/background.jpg" , "Background image URL for the login page." )
248+ rootCmd .Flags ().String ("ldap-address" , "" , "LDAP server address (e.g. ldap://localhost:389)." )
249+ rootCmd .Flags ().String ("ldap-bind-dn" , "" , "LDAP bind DN (e.g. uid=user,dc=example,dc=com)." )
250+ rootCmd .Flags ().String ("ldap-bind-password" , "" , "LDAP bind password." )
251+ rootCmd .Flags ().String ("ldap-base-dn" , "" , "LDAP base DN (e.g. dc=example,dc=com)." )
252+ rootCmd .Flags ().Bool ("ldap-insecure" , false , "Skip certificate verification for the LDAP server." )
253+ rootCmd .Flags ().String ("ldap-search-filter" , "(uid=%s)" , "LDAP search filter for user lookup." )
224254
225255 // Bind flags to environment
226256 viper .BindEnv ("port" , "PORT" )
@@ -256,6 +286,12 @@ func init() {
256286 viper .BindEnv ("login-max-retries" , "LOGIN_MAX_RETRIES" )
257287 viper .BindEnv ("forgot-password-message" , "FORGOT_PASSWORD_MESSAGE" )
258288 viper .BindEnv ("background-image" , "BACKGROUND_IMAGE" )
289+ viper .BindEnv ("ldap-address" , "LDAP_ADDRESS" )
290+ viper .BindEnv ("ldap-bind-dn" , "LDAP_BIND_DN" )
291+ viper .BindEnv ("ldap-bind-password" , "LDAP_BIND_PASSWORD" )
292+ viper .BindEnv ("ldap-base-dn" , "LDAP_BASE_DN" )
293+ viper .BindEnv ("ldap-insecure" , "LDAP_INSECURE" )
294+ viper .BindEnv ("ldap-search-filter" , "LDAP_SEARCH_FILTER" )
259295
260296 // Bind flags to viper
261297 viper .BindPFlags (rootCmd .Flags ())
0 commit comments