mirror of
				https://github.com/h44z/wg-portal.git
				synced 2025-11-03 23:56:18 +00:00 
			
		
		
		
	Brought into working condition for LDAP authentication.
This commit is contained in:
		@@ -4,11 +4,12 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/h44z/wg-portal/internal/config"
 | 
			
		||||
	"github.com/h44z/wg-portal/internal/domain"
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
	evbus "github.com/vardius/message-bus"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type App struct {
 | 
			
		||||
@@ -59,6 +60,7 @@ func New(cfg *config.Config, bus evbus.MessageBus, authenticator Authenticator,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (a *App) Startup(ctx context.Context) error {
 | 
			
		||||
	
 | 
			
		||||
	a.UserManager.StartBackgroundJobs(ctx)
 | 
			
		||||
	a.StatisticsCollector.StartBackgroundJobs(ctx)
 | 
			
		||||
	a.WireGuardManager.StartBackgroundJobs(ctx)
 | 
			
		||||
 
 | 
			
		||||
@@ -4,11 +4,12 @@ import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/h44z/wg-portal/internal/app"
 | 
			
		||||
	"math"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/h44z/wg-portal/internal/app"
 | 
			
		||||
 | 
			
		||||
	"github.com/h44z/wg-portal/internal"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-ldap/ldap/v3"
 | 
			
		||||
@@ -87,7 +88,9 @@ func (m Manager) NewUser(ctx context.Context, user *domain.User) error {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m Manager) StartBackgroundJobs(ctx context.Context) {
 | 
			
		||||
 | 
			
		||||
	go m.runLdapSynchronizationService(ctx)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m Manager) GetUser(ctx context.Context, id domain.UserIdentifier) (*domain.User, error) {
 | 
			
		||||
@@ -322,7 +325,7 @@ func (m Manager) runLdapSynchronizationService(ctx context.Context) {
 | 
			
		||||
			if !ldapCfg.Synchronize {
 | 
			
		||||
				continue // sync disabled
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			//logrus.Tracef(&ldapCfg)
 | 
			
		||||
			err := m.synchronizeLdapUsers(ctx, &ldapCfg)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				logrus.Errorf("failed to synchronize LDAP users for %s: %v", ldapCfg.ProviderName, err)
 | 
			
		||||
@@ -382,15 +385,20 @@ func (m Manager) updateLdapUsers(ctx context.Context, providerName string, rawUs
 | 
			
		||||
			return fmt.Errorf("find error for user id %s: %w", user.Identifier, err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		tctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
 | 
			
		||||
		defer cancel()
 | 
			
		||||
		tctx = domain.SetUserInfo(tctx, domain.SystemAdminContextUserInfo())
 | 
			
		||||
		
 | 
			
		||||
		if existingUser == nil {
 | 
			
		||||
			err := m.NewUser(ctx, user)
 | 
			
		||||
			err := m.NewUser(tctx, user)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return fmt.Errorf("create error for user id %s: %w", user.Identifier, err)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if existingUser != nil && existingUser.Source == domain.UserSourceLdap && userChangedInLdap(existingUser, user) {
 | 
			
		||||
			err := m.users.SaveUser(ctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
 | 
			
		||||
		
 | 
			
		||||
			err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
 | 
			
		||||
				u.UpdatedAt = time.Now()
 | 
			
		||||
				u.UpdatedBy = "ldap_sync"
 | 
			
		||||
				u.Email = user.Email
 | 
			
		||||
 
 | 
			
		||||
@@ -3,9 +3,10 @@ package internal
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"github.com/sirupsen/logrus"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-ldap/ldap/v3"
 | 
			
		||||
	"github.com/h44z/wg-portal/internal/config"
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user