Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (controller *UserController) loginHandler(c *gin.Context) {
if search.Email != "" {
sessionCookie.Email = search.Email
}
if search.Name != "" {
sessionCookie.Name = search.Name
}
}

cookie, err := controller.auth.CreateSession(c, sessionCookie)
Expand Down
12 changes: 10 additions & 2 deletions internal/middleware/context_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ func (m *ContextMiddleware) cookieAuth(ctx context.Context, uuid string, ip stri
}

userContext.LDAP.Groups = user.Groups
userContext.LDAP.Name = utils.Capitalize(userContext.LDAP.Username)
if search.Name != "" {
userContext.LDAP.Name = search.Name
} else {
userContext.LDAP.Name = utils.Capitalize(userContext.LDAP.Username)
}

userContext.LDAP.Email = utils.CompileUserEmail(userContext.LDAP.Username, m.runtime.CookieDomain)
if search.Email != "" {
Expand Down Expand Up @@ -291,10 +295,14 @@ func (m *ContextMiddleware) basicAuth(username string, password string) (*model.
return nil, nil, fmt.Errorf("error retrieving ldap user details: %w", err)
}

name := search.Name
if name == "" {
name = utils.Capitalize(username)
}
userContext.LDAP = &model.LDAPContext{
BaseContext: model.BaseContext{
Username: username,
Name: utils.Capitalize(username),
Name: name,
},
Groups: user.Groups,
}
Expand Down
1 change: 1 addition & 0 deletions internal/model/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ type LocalUser struct {
type UserSearch struct {
Username string
Email string // used for LDAP, we can't throw it to LDAPUser because it would need another cache or an LDAP lookup every time
Name string // used for LDAP cn attribute
Type UserSearchType
}
3 changes: 2 additions & 1 deletion internal/service/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (auth *AuthService) SearchUser(username string) (*model.UserSearch, error)
}

if auth.ldap != nil {
userDN, email, err := auth.ldap.GetUserInfo(username)
userDN, email, cn, err := auth.ldap.GetUserInfo(username)

if err != nil {
return nil, fmt.Errorf("failed to get ldap user: %w", err)
Expand All @@ -182,6 +182,7 @@ func (auth *AuthService) SearchUser(username string) (*model.UserSearch, error)
return &model.UserSearch{
Username: userDN,
Email: email,
Name: cn,
Type: model.UserLDAP,
}, nil
}
Expand Down
10 changes: 5 additions & 5 deletions internal/service/ldap_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func (ldap *LdapService) connect() (*ldapgo.Conn, error) {
return ldap.conn, nil
}

func (ldap *LdapService) GetUserInfo(username string) (dn string, email string, err error) {
func (ldap *LdapService) GetUserInfo(username string) (dn string, email string, cn string, err error) {
escapedUsername := ldapgo.EscapeFilter(username)
filter := fmt.Sprintf(ldap.config.LDAP.SearchFilter, escapedUsername)

searchRequest := ldapgo.NewSearchRequest(
ldap.config.LDAP.BaseDN,
ldapgo.ScopeWholeSubtree, ldapgo.NeverDerefAliases, 0, 0, false,
filter,
[]string{"dn", "mail"},
[]string{"dn", "mail", "cn"},
nil,
)

Expand All @@ -163,15 +163,15 @@ func (ldap *LdapService) GetUserInfo(username string) (dn string, email string,

searchResult, err := ldap.conn.Search(searchRequest)
if err != nil {
return "", "", err
return "", "", "", err
}

if len(searchResult.Entries) != 1 {
return "", "", fmt.Errorf("multiple or no entries found for user %s", username)
return "", "", "", fmt.Errorf("multiple or no entries found for user %s", username)
}

entry := searchResult.Entries[0]
return entry.DN, entry.GetAttributeValue("mail"), nil
return entry.DN, entry.GetAttributeValue("mail"), entry.GetAttributeValue("cn"), nil
}

func (ldap *LdapService) GetUserCount() (int, error) {
Expand Down