Skip to content

Commit eff5fc8

Browse files
committed
refactor: use is configured check in ldap service
1 parent 671343f commit eff5fc8

3 files changed

Lines changed: 17 additions & 9 deletions

File tree

internal/bootstrap/service_bootstrap.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bootstrap
33
import (
44
"github.com/steveiliop56/tinyauth/internal/repository"
55
"github.com/steveiliop56/tinyauth/internal/service"
6-
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
76
)
87

98
type Services struct {
@@ -31,12 +30,12 @@ func (app *BootstrapApp) initServices(queries *repository.Queries) (Services, er
3130

3231
err := ldapService.Init()
3332

34-
if err == nil {
35-
services.ldapService = ldapService
36-
} else {
37-
tlog.App.Warn().Err(err).Msg("Failed to initialize LDAP service, continuing without it")
33+
if err != nil {
34+
return Services{}, err
3835
}
3936

37+
services.ldapService = ldapService
38+
4039
dockerService := service.NewDockerService()
4140

4241
err = dockerService.Init()

internal/service/auth_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (auth *AuthService) SearchUser(username string) config.UserSearch {
7878
}
7979
}
8080

81-
if auth.ldap != nil {
81+
if auth.ldap.IsConfigured() {
8282
userDN, err := auth.ldap.GetUserDN(username)
8383

8484
if err != nil {
@@ -105,7 +105,7 @@ func (auth *AuthService) VerifyUser(search config.UserSearch, password string) b
105105
user := auth.GetLocalUser(search.Username)
106106
return auth.CheckPassword(user, password)
107107
case "ldap":
108-
if auth.ldap != nil {
108+
if auth.ldap.IsConfigured() {
109109
err := auth.ldap.Bind(search.Username, password)
110110
if err != nil {
111111
tlog.App.Warn().Err(err).Str("username", search.Username).Msg("Failed to bind to LDAP")
@@ -141,7 +141,7 @@ func (auth *AuthService) GetLocalUser(username string) config.User {
141141
}
142142

143143
func (auth *AuthService) GetLdapUser(userDN string) (config.LdapUser, error) {
144-
if auth.ldap == nil {
144+
if !auth.ldap.IsConfigured() {
145145
return config.LdapUser{}, errors.New("LDAP service not initialized")
146146
}
147147

@@ -398,7 +398,7 @@ func (auth *AuthService) LocalAuthConfigured() bool {
398398
}
399399

400400
func (auth *AuthService) LdapAuthConfigured() bool {
401-
return auth.ldap != nil
401+
return auth.ldap.IsConfigured()
402402
}
403403

404404
func (auth *AuthService) IsUserAllowed(c *gin.Context, context config.UserContext, acls config.App) bool {

internal/service/ldap_service.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ func NewLdapService(config LdapServiceConfig) *LdapService {
3636
}
3737
}
3838

39+
// If you have an ldap address then you must need ldap
40+
func (ldap *LdapService) IsConfigured() bool {
41+
return ldap.config.Address != ""
42+
}
43+
3944
func (ldap *LdapService) Init() error {
45+
if !ldap.IsConfigured() {
46+
return nil
47+
}
48+
4049
// Check whether authentication with client certificate is possible
4150
if ldap.config.AuthCert != "" && ldap.config.AuthKey != "" {
4251
cert, err := tls.LoadX509KeyPair(ldap.config.AuthCert, ldap.config.AuthKey)

0 commit comments

Comments
 (0)