2018-08-29 16:20:37 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
lm "github.com/documize/community/model/auth"
|
|
|
|
)
|
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
// Works against AD server in Azure configured using:
|
2018-08-30 11:37:05 +01:00
|
|
|
//
|
2018-08-30 16:01:36 +01:00
|
|
|
// https://auth0.com/docs/connector/test-dc
|
2018-08-30 11:37:05 +01:00
|
|
|
//
|
|
|
|
// Ensure VM network settings open up ports 389 and 636.
|
|
|
|
|
2018-08-30 16:01:36 +01:00
|
|
|
var testConfigPublicAD = lm.LDAPConfig{
|
|
|
|
ServerType: lm.ServerTypeAD,
|
|
|
|
ServerHost: "documize-ad.eastus.cloudapp.azure.com",
|
|
|
|
ServerPort: 389,
|
2018-09-01 14:50:37 +01:00
|
|
|
EncryptionType: lm.EncryptionTypeNone,
|
2018-08-30 16:01:36 +01:00
|
|
|
BaseDN: "DC=mycompany,DC=local",
|
|
|
|
BindDN: "CN=ad-admin,CN=Users,DC=mycompany,DC=local",
|
|
|
|
BindPassword: "8B5tNRLvbk8K",
|
|
|
|
UserFilter: "",
|
|
|
|
GroupFilter: "",
|
|
|
|
AttributeUserRDN: "sAMAccountName",
|
|
|
|
AttributeUserFirstname: "givenName",
|
|
|
|
AttributeUserLastname: "sn",
|
|
|
|
AttributeUserEmail: "mail",
|
|
|
|
AttributeUserDisplayName: "",
|
|
|
|
AttributeUserGroupName: "",
|
|
|
|
AttributeGroupMember: "member",
|
|
|
|
}
|
2018-08-29 16:20:37 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
func TestUserFilter_PublicAD(t *testing.T) {
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicAD.UserFilter = "(|(objectCategory=person)(objectClass=user)(objectClass=inetOrgPerson))"
|
2018-08-29 16:20:37 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
e, err := executeUserFilter(testConfigPublicAD)
|
2018-08-29 16:20:37 +01:00
|
|
|
if err != nil {
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Error("unable to exeucte user filter", err.Error())
|
2018-08-29 16:20:37 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 21:00:51 +01:00
|
|
|
if len(e) == 0 {
|
2018-09-01 14:50:37 +01:00
|
|
|
t.Error("Received zero user LDAP search entries")
|
2018-08-29 16:20:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Logf("LDAP search entries found: %d", len(e))
|
2018-08-29 16:20:37 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
for _, u := range e {
|
2018-08-29 16:20:37 +01:00
|
|
|
t.Logf("[%s] %s (%s %s) @ %s\n",
|
2018-08-31 21:00:51 +01:00
|
|
|
u.RemoteID, u.CN, u.Firstname, u.Lastname, u.Email)
|
2018-08-29 16:20:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-01 14:50:37 +01:00
|
|
|
func TestGroupFilter_PublicAD(t *testing.T) {
|
2018-08-30 16:01:36 +01:00
|
|
|
testConfigPublicAD.GroupFilter = "(|(cn=Accounting)(cn=IT))"
|
|
|
|
|
2018-09-01 14:50:37 +01:00
|
|
|
e, err := executeGroupFilter(testConfigPublicAD)
|
2018-08-29 16:20:37 +01:00
|
|
|
if err != nil {
|
2018-09-01 14:50:37 +01:00
|
|
|
t.Error("unable to exeucte group filter", err.Error())
|
2018-08-29 16:20:37 +01:00
|
|
|
return
|
|
|
|
}
|
2018-09-01 14:50:37 +01:00
|
|
|
if len(e) == 0 {
|
|
|
|
t.Error("Received zero group LDAP search entries")
|
2018-08-29 16:20:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-01 14:50:37 +01:00
|
|
|
t.Logf("LDAP group search entries found: %d", len(e))
|
2018-08-29 16:20:37 +01:00
|
|
|
|
2018-09-01 14:50:37 +01:00
|
|
|
for _, u := range e {
|
|
|
|
t.Logf("[%s] %s (%s %s) @ %s\n",
|
|
|
|
u.RemoteID, u.CN, u.Firstname, u.Lastname, u.Email)
|
2018-08-29 16:20:37 +01:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 16:01:36 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
func TestAuthenticate_PublicAD(t *testing.T) {
|
|
|
|
l, err := connect(testConfigPublicAD)
|
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()
|
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
ok, err := authenticate(l, testConfigPublicAD, "bob.johnson", "Pass@word1!")
|
2018-08-29 16:58:54 +01:00
|
|
|
if err != nil {
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Error("error during LDAP authentication: ", err.Error())
|
2018-08-29 16:58:54 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 21:00:51 +01:00
|
|
|
if !ok {
|
|
|
|
t.Error("failed LDAP authentication")
|
|
|
|
}
|
2018-08-29 16:58:54 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Log("Authenticated")
|
|
|
|
}
|
2018-08-29 16:58:54 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
func TestNotAuthenticate_PublicAD(t *testing.T) {
|
|
|
|
l, err := connect(testConfigPublicAD)
|
2018-08-29 16:58:54 +01:00
|
|
|
if err != nil {
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Error("Error: unable to dial LDAP server: ", err.Error())
|
2018-08-29 16:58:54 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 21:00:51 +01:00
|
|
|
defer l.Close()
|
2018-08-29 16:58:54 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
ok, err := authenticate(l, testConfigPublicAD, "junk", "junk")
|
2018-08-29 16:58:54 +01:00
|
|
|
if err != nil {
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Error("error during LDAP authentication: ", err.Error())
|
2018-08-29 16:58:54 +01:00
|
|
|
return
|
|
|
|
}
|
2018-08-31 21:00:51 +01:00
|
|
|
if ok {
|
|
|
|
t.Error("incorrect LDAP authentication")
|
|
|
|
}
|
2018-08-29 16:58:54 +01:00
|
|
|
|
2018-08-31 21:00:51 +01:00
|
|
|
t.Log("Not authenticated")
|
2018-08-29 16:58:54 +01:00
|
|
|
}
|