Skip to content

Commit f367fc0

Browse files
committed
Fix groups filtering
1 parent 9f71934 commit f367fc0

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Example `docker-compose` setup could be found in [examples/proxy](./examples/pro
3434
export LDAP_BIND_PASSWORD='<bind user password>'
3535
export LDAP_USER_FILTER='(uid=%s)'
3636
export LDAP_GROUP_FILTER='(&(objectClass=groupOfNames)(member=uid=%s,ou=Users,o=<oid>,dc=jumpcloud,dc=com))'
37+
export GROUP_HEADER='X-Ldap-Group'
3738
export HEADERS_MAP='X-LDAP-Mail:mail,X-LDAP-UID:uid,X-LDAP-CN:cn,X-LDAP-DN:dn'
3839

3940
where `<oid>` is your organisation id.

ldap_auth_proxy.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,7 @@ func (p *LDAPAuthProxy) Authenticate(w http.ResponseWriter, r *http.Request) int
170170
}
171171

172172
filterString := r.Header.Get(p.GroupHeader)
173-
rawGroup := strings.Split(filterString, ",")
174-
175-
for _, g := range filterGroups {
176-
g = strings.TrimSpace(g)
177-
178-
if "*" == g {
179-
filterGroups = []string{"*"}
180-
break
181-
}
182-
183-
if len(g) > 1 {
184-
filterGroups = append(rawGroup, g)
185-
}
186-
}
173+
filterGroups = extractFilterGroups(filterString)
187174

188175
if len(filterGroups) < 1 {
189176
traceWarning(w, fmt.Sprintf("Bad groups filter string: %s", filterString))
@@ -276,3 +263,24 @@ func traceError(w http.ResponseWriter, h string) {
276263
log.Warning(h)
277264
w.Header().Add("X-LdapAuth-Trace", h)
278265
}
266+
267+
func extractFilterGroups(filterString string) []string {
268+
var filterGroups []string
269+
270+
rawGroup := strings.Split(filterString, ",")
271+
272+
for _, g := range rawGroup {
273+
g = strings.TrimSpace(g)
274+
275+
if "*" == g {
276+
// special case, we don't need any other filters with wildcard
277+
return []string{"*"}
278+
}
279+
280+
if len(g) > 1 {
281+
filterGroups = append(filterGroups, g)
282+
}
283+
}
284+
285+
return filterGroups
286+
}

ldap_auth_proxy_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestExtractFilterGroups(t *testing.T) {
9+
assert.Equal(t, []string(nil), extractFilterGroups(""))
10+
assert.Equal(t, []string(nil), extractFilterGroups(","))
11+
assert.Equal(t, []string{"test", "me", "please" , "foo bar"}, extractFilterGroups(",test, me,please ,, ,foo bar"))
12+
assert.Equal(t, []string{"*"}, extractFilterGroups("foo,*,bar"))
13+
}

0 commit comments

Comments
 (0)