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
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ type LdapUser struct {
type UserSearch struct {
Username string
Type string // local, ldap or unknown
Email string
}

type UserContext struct {
Expand Down
3 changes: 3 additions & 0 deletions internal/controller/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (controller *UserController) loginHandler(c *gin.Context) {

if userSearch.Type == "ldap" {
sessionCookie.Provider = "ldap"
if userSearch.Email != "" {
sessionCookie.Email = userSearch.Email
}
}

tlog.App.Trace().Interface("session_cookie", sessionCookie).Msg("Creating session cookie")
Expand Down
7 changes: 6 additions & 1 deletion internal/middleware/context_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,15 @@ func (m *ContextMiddleware) Middleware() gin.HandlerFunc {
return
}

email := utils.CompileUserEmail(basic.Username, m.config.CookieDomain)
if userSearch.Email != "" {
email = userSearch.Email
}

c.Set("context", &config.UserContext{
Username: basic.Username,
Name: utils.Capitalize(basic.Username),
Email: utils.CompileUserEmail(basic.Username, m.config.CookieDomain),
Email: email,
Provider: "ldap",
IsLoggedIn: true,
LdapGroups: strings.Join(ldapUser.Groups, ","),
Expand Down
3 changes: 2 additions & 1 deletion internal/service/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (auth *AuthService) SearchUser(username string) config.UserSearch {
}

if auth.ldap.IsConfigured() {
userDN, err := auth.ldap.GetUserDN(username)
userDN, email, err := auth.ldap.GetUserInfo(username)

if err != nil {
tlog.App.Warn().Err(err).Str("username", username).Msg("Failed to search for user in LDAP")
Expand All @@ -135,6 +135,7 @@ func (auth *AuthService) SearchUser(username string) config.UserSearch {
return config.UserSearch{
Username: userDN,
Type: "ldap",
Email: email,
}
}

Expand Down
13 changes: 6 additions & 7 deletions internal/service/ldap_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,15 @@ func (ldap *LdapService) connect() (*ldapgo.Conn, error) {
return ldap.conn, nil
}

func (ldap *LdapService) GetUserDN(username string) (string, error) {
// Escape the username to prevent LDAP injection
func (ldap *LdapService) GetUserInfo(username string) (dn string, email string, err error) {
escapedUsername := ldapgo.EscapeFilter(username)
filter := fmt.Sprintf(ldap.config.SearchFilter, escapedUsername)

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

Expand All @@ -161,15 +160,15 @@ func (ldap *LdapService) GetUserDN(username string) (string, error) {

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

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

func (ldap *LdapService) GetUserGroups(userDN string) ([]string, error) {
Expand Down