refactor: better is configured check for ldap and oidc service

This commit is contained in:
Stavros
2026-02-02 16:18:29 +02:00
parent 51d95fa455
commit 285edba88c
3 changed files with 36 additions and 16 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}