diff --git a/internal/bootstrap/service_bootstrap.go b/internal/bootstrap/service_bootstrap.go index 12336cc..e9a27a7 100644 --- a/internal/bootstrap/service_bootstrap.go +++ b/internal/bootstrap/service_bootstrap.go @@ -3,6 +3,7 @@ package bootstrap import ( "github.com/steveiliop56/tinyauth/internal/repository" "github.com/steveiliop56/tinyauth/internal/service" + "github.com/steveiliop56/tinyauth/internal/utils/tlog" ) type Services struct { @@ -31,7 +32,8 @@ func (app *BootstrapApp) initServices(queries *repository.Queries) (Services, er err := ldapService.Init() if err != nil { - return Services{}, err + tlog.App.Warn().Err(err).Msg("Failed to setup LDAP service, starting without it") + ldapService.Unconfigure() } services.ldapService = ldapService diff --git a/internal/service/ldap_service.go b/internal/service/ldap_service.go index 2bfadd1..a86bfce 100644 --- a/internal/service/ldap_service.go +++ b/internal/service/ldap_service.go @@ -24,10 +24,11 @@ type LdapServiceConfig struct { } type LdapService struct { - config LdapServiceConfig - conn *ldapgo.Conn - mutex sync.RWMutex - cert *tls.Certificate + config LdapServiceConfig + conn *ldapgo.Conn + mutex sync.RWMutex + cert *tls.Certificate + isConfigured bool } func NewLdapService(config LdapServiceConfig) *LdapService { @@ -36,13 +37,28 @@ func NewLdapService(config LdapServiceConfig) *LdapService { } } -// If you have an ldap address then you must need ldap func (ldap *LdapService) IsConfigured() bool { - return ldap.config.Address != "" + return ldap.isConfigured +} + +func (ldap *LdapService) Unconfigure() error { + if !ldap.isConfigured { + return nil + } + + if ldap.conn != nil { + if err := ldap.conn.Close(); err != nil { + return fmt.Errorf("failed to close LDAP connection: %w", err) + } + } + + ldap.isConfigured = false + return nil } func (ldap *LdapService) Init() error { - if !ldap.IsConfigured() { + if ldap.config.Address == "" { + ldap.isConfigured = false return nil } diff --git a/internal/service/oidc_service.go b/internal/service/oidc_service.go index 47c0b05..03a245c 100644 --- a/internal/service/oidc_service.go +++ b/internal/service/oidc_service.go @@ -83,12 +83,13 @@ type OIDCServiceConfig struct { } type OIDCService struct { - config OIDCServiceConfig - queries *repository.Queries - clients map[string]config.OIDCClientConfig - privateKey *rsa.PrivateKey - publicKey crypto.PublicKey - issuer string + config OIDCServiceConfig + queries *repository.Queries + clients map[string]config.OIDCClientConfig + privateKey *rsa.PrivateKey + publicKey crypto.PublicKey + issuer string + isConfigured bool } func NewOIDCService(config OIDCServiceConfig, queries *repository.Queries) *OIDCService { @@ -99,12 +100,13 @@ func NewOIDCService(config OIDCServiceConfig, queries *repository.Queries) *OIDC } func (service *OIDCService) IsConfigured() bool { - return len(service.config.Clients) > 0 + return service.isConfigured } func (service *OIDCService) Init() error { // If not configured, skip init - if !service.IsConfigured() { + if len(service.config.Clients) == 0 { + service.isConfigured = false return nil }