2018-08-30 16:01:36 +01:00
|
|
|
// Copyright 2016 Documize IntestConfigPublicLDAP. <legal@documize.com>. All rights reserved.
|
2018-08-28 10:19:22 +01:00
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-08-29 16:20:37 +01:00
|
|
|
"strings"
|
2018-08-28 10:19:22 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
lm "github.com/documize/community/model/auth"
|
|
|
|
ld "gopkg.in/ldap.v2"
|
|
|
|
)
|
|
|
|
|
2018-08-29 16:58:54 +01:00
|
|
|
// Works against https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/
|
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
var testConfigPublicLDAP = lm.LDAPConfig{
|
|
|
|
ServerType: lm.ServerTypeLDAP,
|
|
|
|
ServerHost: "ldap.forumsys.com",
|
|
|
|
ServerPort: 389,
|
|
|
|
EncryptionType: "none",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
BindDN: "cn=read-only-admin,dc=example,dc=com",
|
|
|
|
BindPassword: "password",
|
|
|
|
UserFilter: "",
|
|
|
|
GroupFilter: "",
|
|
|
|
AttributeUserRDN: "uid",
|
|
|
|
AttributeUserFirstname: "givenName",
|
|
|
|
AttributeUserLastname: "sn",
|
|
|
|
AttributeUserEmail: "mail",
|
|
|
|
AttributeUserDisplayName: "",
|
|
|
|
AttributeUserGroupName: "",
|
|
|
|
AttributeGroupMember: "uniqueMember",
|
|
|
|
}
|
2018-08-28 10:19:22 +01:00
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
func TestPublicLDAPServer_UserList(t *testing.T) {
|
|
|
|
testConfigPublicLDAP.UserFilter = "(|(objectClass=person)(objectClass=user)(objectClass=inetOrgPerson))"
|
|
|
|
testConfigPublicLDAP.GroupFilter = ""
|
|
|
|
userAttrs := testConfigPublicLDAP.GetUserFilterAttributes()
|
2018-08-28 10:19:22 +01:00
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
l, err := Connect(testConfigPublicLDAP)
|
2018-08-28 10:19:22 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to dial LDAP server: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// Authenticate with LDAP server using admin credentials.
|
|
|
|
t.Log("Binding LDAP admin user")
|
2018-08-30 16:01:36 +01:00
|
|
|
err = l.Bind(testConfigPublicLDAP.BindDN, testConfigPublicLDAP.BindPassword)
|
2018-08-28 10:19:22 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to bind specified admin user to LDAP: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
searchRequest := ld.NewSearchRequest(
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.BaseDN,
|
2018-08-28 10:19:22 +01:00
|
|
|
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.UserFilter,
|
|
|
|
userAttrs,
|
2018-08-28 10:19:22 +01:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to execute directory search: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:20:37 +01:00
|
|
|
t.Logf("LDAP search entries found: %d", len(sr.Entries))
|
2018-08-28 10:19:22 +01:00
|
|
|
if len(sr.Entries) == 0 {
|
2018-08-29 16:20:37 +01:00
|
|
|
t.Error("Received ZERO LDAP search entries")
|
2018-08-28 10:19:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range sr.Entries {
|
2018-08-30 16:01:36 +01:00
|
|
|
t.Logf("[%s] %s (%s %s) @ %s\n",
|
|
|
|
entry.GetAttributeValue(testConfigPublicLDAP.AttributeUserRDN),
|
2018-08-28 10:19:22 +01:00
|
|
|
entry.GetAttributeValue("cn"),
|
2018-08-30 16:01:36 +01:00
|
|
|
entry.GetAttributeValue(testConfigPublicLDAP.AttributeUserFirstname),
|
|
|
|
entry.GetAttributeValue(testConfigPublicLDAP.AttributeUserLastname),
|
|
|
|
entry.GetAttributeValue(testConfigPublicLDAP.AttributeUserEmail))
|
2018-08-28 10:19:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:20:37 +01:00
|
|
|
func TestPublicLDAPServer_Groups(t *testing.T) {
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.UserFilter = ""
|
|
|
|
testConfigPublicLDAP.GroupFilter = "(|(ou=mathematicians)(ou=chemists))"
|
|
|
|
groupAttrs := testConfigPublicLDAP.GetGroupFilterAttributes()
|
|
|
|
userAttrs := testConfigPublicLDAP.GetUserFilterAttributes()
|
|
|
|
|
|
|
|
l, err := Connect(testConfigPublicLDAP)
|
2018-08-29 16:20:37 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to dial LDAP server: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// Authenticate with LDAP server using admin credentials.
|
|
|
|
t.Log("Binding LDAP admin user")
|
2018-08-30 16:01:36 +01:00
|
|
|
err = l.Bind(testConfigPublicLDAP.BindDN, testConfigPublicLDAP.BindPassword)
|
2018-08-29 16:20:37 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to bind specified admin user to LDAP: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
searchRequest := ld.NewSearchRequest(
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.BaseDN,
|
2018-08-29 16:20:37 +01:00
|
|
|
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.GroupFilter,
|
|
|
|
groupAttrs,
|
2018-08-29 16:20:37 +01:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
t.Log("LDAP search filter:", testConfigPublicLDAP.GroupFilter)
|
2018-08-29 16:20:37 +01:00
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to execute directory search: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("LDAP search entries found: %d", len(sr.Entries))
|
|
|
|
if len(sr.Entries) == 0 {
|
|
|
|
t.Error("Received ZERO LDAP search entries")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-30 11:37:05 +01:00
|
|
|
// Get list of group members per group found.
|
|
|
|
for _, group := range sr.Entries {
|
|
|
|
t.Log("Found group", group.DN)
|
2018-08-29 16:20:37 +01:00
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
rawMembers := group.GetAttributeValues(testConfigPublicLDAP.AttributeGroupMember)
|
2018-08-30 11:37:05 +01:00
|
|
|
if len(rawMembers) == 0 {
|
|
|
|
t.Log("Error: group member attribute returned no users")
|
2018-08-29 16:20:37 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-08-30 11:37:05 +01:00
|
|
|
t.Logf("LDAP group contains %d members", len(rawMembers))
|
|
|
|
|
|
|
|
for _, entry := range rawMembers {
|
|
|
|
// get CN element from DN
|
|
|
|
parts := strings.Split(entry, ",")
|
|
|
|
if len(parts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
filter := fmt.Sprintf("(%s)", parts[0])
|
|
|
|
|
|
|
|
usr := ld.NewSearchRequest(
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.BaseDN,
|
2018-08-30 11:37:05 +01:00
|
|
|
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
|
|
|
|
filter,
|
2018-08-30 16:01:36 +01:00
|
|
|
userAttrs,
|
2018-08-30 11:37:05 +01:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
ue, err := l.Search(usr)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Error: unable to execute directory search for group member: ", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ue.Entries) > 0 {
|
|
|
|
for _, ur := range ue.Entries {
|
2018-08-30 16:01:36 +01:00
|
|
|
t.Logf("[%s] %s (%s %s) @ %s\n",
|
|
|
|
ur.GetAttributeValue(testConfigPublicLDAP.AttributeUserRDN),
|
|
|
|
ur.GetAttributeValue("cn"),
|
|
|
|
ur.GetAttributeValue(testConfigPublicLDAP.AttributeUserFirstname),
|
|
|
|
ur.GetAttributeValue(testConfigPublicLDAP.AttributeUserLastname),
|
|
|
|
ur.GetAttributeValue(testConfigPublicLDAP.AttributeUserEmail))
|
2018-08-30 11:37:05 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Log("group member search failed:", filter)
|
2018-08-29 16:20:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:58:54 +01:00
|
|
|
func TestPublicLDAP_Authenticate(t *testing.T) {
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.UserFilter = ""
|
|
|
|
testConfigPublicLDAP.GroupFilter = ""
|
|
|
|
userAttrs := testConfigPublicLDAP.GetUserFilterAttributes()
|
|
|
|
|
|
|
|
l, err := Connect(testConfigPublicLDAP)
|
2018-08-29 16:58:54 +01:00
|
|
|
if err != nil {
|
2018-08-30 16:01:36 +01:00
|
|
|
t.Error("Error: unable to dial LDAP server: ", err.Error())
|
2018-08-29 16:58:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
// Authenticate with LDAP server using admin credentials.
|
|
|
|
t.Log("Binding LDAP admin user")
|
2018-08-30 16:01:36 +01:00
|
|
|
err = l.Bind(testConfigPublicLDAP.BindDN, testConfigPublicLDAP.BindPassword)
|
2018-08-29 16:58:54 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to bind specified admin user to LDAP: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
username := "newton"
|
|
|
|
password := "password"
|
2018-08-30 16:01:36 +01:00
|
|
|
filter := fmt.Sprintf("(%s=%s)", testConfigPublicLDAP.AttributeUserRDN, username)
|
2018-08-29 16:58:54 +01:00
|
|
|
|
|
|
|
searchRequest := ld.NewSearchRequest(
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicLDAP.BaseDN,
|
2018-08-29 16:58:54 +01:00
|
|
|
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
|
|
|
|
filter,
|
2018-08-30 16:01:36 +01:00
|
|
|
userAttrs,
|
2018-08-29 16:58:54 +01:00
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Log("LDAP search filter:", filter)
|
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: unable to execute directory search: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.Entries) == 0 {
|
|
|
|
t.Error("Error: user not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(sr.Entries) != 1 {
|
|
|
|
t.Error("Error: too many users found during authentication")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userdn := sr.Entries[0].DN
|
|
|
|
|
|
|
|
// Bind as the user to verify their password
|
|
|
|
err = l.Bind(userdn, password)
|
|
|
|
if err != nil {
|
|
|
|
t.Error("Error: invalid credentials", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log("Authenticated", username)
|
|
|
|
}
|