mirror of
https://github.com/pocket-id/pocket-id.git
synced 2025-12-16 18:22:59 +03:00
fix: support non UTF-8 LDAP IDs (#714)
This commit is contained in:
@@ -13,6 +13,9 @@ import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/go-ldap/ldap/v3"
|
||||
"github.com/pocket-id/pocket-id/backend/internal/common"
|
||||
@@ -122,7 +125,7 @@ func (s *LdapService) SyncGroups(ctx context.Context, tx *gorm.DB, client *ldap.
|
||||
ldapGroupIDs := make(map[string]struct{}, len(result.Entries))
|
||||
|
||||
for _, value := range result.Entries {
|
||||
ldapId := value.GetAttributeValue(dbConfig.LdapAttributeGroupUniqueIdentifier.Value)
|
||||
ldapId := convertLdapIdToString(value.GetAttributeValue(dbConfig.LdapAttributeGroupUniqueIdentifier.Value))
|
||||
|
||||
// Skip groups without a valid LDAP ID
|
||||
if ldapId == "" {
|
||||
@@ -194,7 +197,7 @@ func (s *LdapService) SyncGroups(ctx context.Context, tx *gorm.DB, client *ldap.
|
||||
syncGroup := dto.UserGroupCreateDto{
|
||||
Name: value.GetAttributeValue(dbConfig.LdapAttributeGroupName.Value),
|
||||
FriendlyName: value.GetAttributeValue(dbConfig.LdapAttributeGroupName.Value),
|
||||
LdapID: value.GetAttributeValue(dbConfig.LdapAttributeGroupUniqueIdentifier.Value),
|
||||
LdapID: ldapId,
|
||||
}
|
||||
|
||||
if databaseGroup.ID == "" {
|
||||
@@ -286,7 +289,7 @@ func (s *LdapService) SyncUsers(ctx context.Context, tx *gorm.DB, client *ldap.C
|
||||
ldapUserIDs := make(map[string]struct{}, len(result.Entries))
|
||||
|
||||
for _, value := range result.Entries {
|
||||
ldapId := value.GetAttributeValue(dbConfig.LdapAttributeUserUniqueIdentifier.Value)
|
||||
ldapId := convertLdapIdToString(value.GetAttributeValue(dbConfig.LdapAttributeUserUniqueIdentifier.Value))
|
||||
|
||||
// Skip users without a valid LDAP ID
|
||||
if ldapId == "" {
|
||||
@@ -468,3 +471,21 @@ func getDNProperty(property string, str string) string {
|
||||
// CN not found, return an empty string
|
||||
return ""
|
||||
}
|
||||
|
||||
// convertLdapIdToString converts LDAP IDs to valid UTF-8 strings.
|
||||
// LDAP servers may return binary UUIDs (16 bytes) or other non-UTF-8 data.
|
||||
func convertLdapIdToString(ldapId string) string {
|
||||
if utf8.ValidString(ldapId) {
|
||||
return ldapId
|
||||
}
|
||||
|
||||
// Try to parse as binary UUID (16 bytes)
|
||||
if len(ldapId) == 16 {
|
||||
if parsedUUID, err := uuid.FromBytes([]byte(ldapId)); err == nil {
|
||||
return parsedUUID.String()
|
||||
}
|
||||
}
|
||||
|
||||
// As a last resort, encode as base64 to make it UTF-8 safe
|
||||
return base64.StdEncoding.EncodeToString([]byte(ldapId))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user