2019-12-06 08:15:54 +00:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2020-12-03 05:23:52 +00:00
|
|
|
"errors"
|
2019-12-06 08:15:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-03-19 04:22:46 +00:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2020-05-04 19:39:25 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-20 19:34:53 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-06 08:15:54 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 12:37:21 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-12-06 08:15:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCreateRawConnectionWhenSchemeIsLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2019-12-06 08:15:54 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2019-12-06 08:15:54 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
_, err := ldapClient.connect("cn=admin,dc=example,dc=com", "password")
|
2019-12-06 08:15:54 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCreateTLSConnectionWhenSchemeIsLDAPS(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2019-12-06 08:15:54 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldaps://127.0.0.1:389"), gomock.Any()).
|
2019-12-06 08:15:54 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
_, err := ldapClient.connect("cn=admin,dc=example,dc=com", "password")
|
2019-12-06 08:15:54 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-01-20 19:34:53 +00:00
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
func TestEscapeSpecialCharsFromUserInput(t *testing.T) {
|
2020-01-20 19:34:53 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
2021-01-04 10:28:55 +00:00
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-20 19:34:53 +00:00
|
|
|
|
|
|
|
// No escape
|
2021-01-04 10:28:55 +00:00
|
|
|
assert.Equal(t, "xyz", ldapClient.ldapEscape("xyz"))
|
2020-01-20 19:34:53 +00:00
|
|
|
|
|
|
|
// Escape
|
2021-01-04 10:28:55 +00:00
|
|
|
assert.Equal(t, "test\\,abc", ldapClient.ldapEscape("test,abc"))
|
|
|
|
assert.Equal(t, "test\\5cabc", ldapClient.ldapEscape("test\\abc"))
|
|
|
|
assert.Equal(t, "test\\2aabc", ldapClient.ldapEscape("test*abc"))
|
|
|
|
assert.Equal(t, "test \\28abc\\29", ldapClient.ldapEscape("test (abc)"))
|
|
|
|
assert.Equal(t, "test\\#abc", ldapClient.ldapEscape("test#abc"))
|
|
|
|
assert.Equal(t, "test\\+abc", ldapClient.ldapEscape("test+abc"))
|
|
|
|
assert.Equal(t, "test\\<abc", ldapClient.ldapEscape("test<abc"))
|
|
|
|
assert.Equal(t, "test\\>abc", ldapClient.ldapEscape("test>abc"))
|
|
|
|
assert.Equal(t, "test\\;abc", ldapClient.ldapEscape("test;abc"))
|
|
|
|
assert.Equal(t, "test\\\"abc", ldapClient.ldapEscape("test\"abc"))
|
|
|
|
assert.Equal(t, "test\\=abc", ldapClient.ldapEscape("test=abc"))
|
|
|
|
assert.Equal(t, "test\\,\\5c\\28abc\\29", ldapClient.ldapEscape("test,\\(abc)"))
|
2020-03-30 22:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEscapeSpecialCharsInGroupsFilter(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2020-01-20 19:34:53 +00:00
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
2021-01-04 10:28:55 +00:00
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
GroupsFilter: "(|(member={dn})(uid={username})(uid={input}))",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-03-30 22:36:04 +00:00
|
|
|
|
|
|
|
profile := ldapUserProfile{
|
2020-06-19 10:50:21 +00:00
|
|
|
DN: "cn=john (external),dc=example,dc=com",
|
|
|
|
Username: "john",
|
|
|
|
DisplayName: "John Doe",
|
|
|
|
Emails: []string{"john.doe@authelia.com"},
|
2020-03-30 22:36:04 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
filter, _ := ldapClient.resolveGroupsFilter("john", &profile)
|
2020-03-30 22:36:04 +00:00
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john))", filter)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
filter, _ = ldapClient.resolveGroupsFilter("john#=(abc,def)", &profile)
|
2020-03-30 22:36:04 +00:00
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john\\#\\=\\28abc\\,def\\29))", filter)
|
2020-01-20 19:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SearchRequestMatcher struct {
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSearchRequestMatcher(expected string) *SearchRequestMatcher {
|
|
|
|
return &SearchRequestMatcher{expected}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srm *SearchRequestMatcher) Matches(x interface{}) bool {
|
|
|
|
sr := x.(*ldap.SearchRequest)
|
|
|
|
return sr.Filter == srm.expected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srm *SearchRequestMatcher) String() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldEscapeUserInput(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-20 19:34:53 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 07:10:25 +00:00
|
|
|
// Here we ensure that the input has been correctly escaped.
|
2020-03-30 22:36:04 +00:00
|
|
|
Search(NewSearchRequestMatcher("(|(uid=john\\=abc)(mail=john\\=abc))")).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
2020-01-20 19:34:53 +00:00
|
|
|
|
2020-12-03 05:23:52 +00:00
|
|
|
_, err := ldapClient.getUserProfile(mockConn, "john=abc")
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "user not found")
|
2020-03-15 07:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCombineUsernameFilterAndUsersFilter(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
UsersFilter: "(&({username_attribute}={input})(&(objectCategory=person)(objectClass=user)))",
|
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-01-20 19:34:53 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 07:10:25 +00:00
|
|
|
Search(NewSearchRequestMatcher("(&(uid=john)(&(objectCategory=person)(objectClass=user)))")).
|
2020-01-20 19:34:53 +00:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
|
|
|
|
2020-12-03 05:23:52 +00:00
|
|
|
_, err := ldapClient.getUserProfile(mockConn, "john")
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.EqualError(t, err, "user not found")
|
2020-01-20 19:34:53 +00:00
|
|
|
}
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
func createSearchResultWithAttributes(attributes ...*ldap.EntryAttribute) *ldap.SearchResult {
|
|
|
|
return &ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 01:05:17 +00:00
|
|
|
{Attributes: attributes},
|
2020-02-27 22:21:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createSearchResultWithAttributeValues(values ...string) *ldap.SearchResult {
|
|
|
|
return createSearchResultWithAttributes(&ldap.EntryAttribute{
|
|
|
|
Values: values,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCrashWhenGroupsAreNotRetrievedFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(mockConn, nil)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(nil)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 07:10:25 +00:00
|
|
|
Close()
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
2020-03-15 07:10:25 +00:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-27 22:21:07 +00:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-06-19 10:50:21 +00:00
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
2020-06-19 10:50:21 +00:00
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
2020-03-15 07:10:25 +00:00
|
|
|
assert.Equal(t, details.Username, "john")
|
2020-02-27 22:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCrashWhenEmailsAreNotRetrievedFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(mockConn, nil)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(nil)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 07:10:25 +00:00
|
|
|
Close()
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
|
2020-03-15 07:10:25 +00:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-27 22:21:07 +00:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-02-27 22:21:07 +00:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{"group1", "group2"})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{})
|
2020-03-15 07:10:25 +00:00
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnUsernameFromLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-03-15 07:10:25 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-03-15 07:10:25 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close()
|
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-06-19 10:50:21 +00:00
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 01:05:17 +00:00
|
|
|
{
|
2020-03-15 07:10:25 +00:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-30 22:36:04 +00:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-03-15 07:10:25 +00:00
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{"group1", "group2"})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
2020-06-19 10:50:21 +00:00
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
2020-03-15 07:10:25 +00:00
|
|
|
assert.Equal(t, details.Username, "John")
|
2020-02-27 22:21:07 +00:00
|
|
|
}
|
2020-12-03 05:23:52 +00:00
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func TestShouldUpdateUserPassword(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
modifyRequest := ldap.NewModifyRequest("uid=test,dc=example,dc=com", nil)
|
|
|
|
modifyRequest.Replace("userPassword", []string{"password"})
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Modify(modifyRequest).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close(),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := ldapClient.UpdatePassword("john", "password")
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckValidUserPassword(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=test,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close().Times(2),
|
|
|
|
)
|
|
|
|
|
|
|
|
valid, err := ldapClient.CheckUserPassword("john", "password")
|
|
|
|
|
|
|
|
assert.True(t, valid)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCheckInvalidUserPassword(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
gomock.InOrder(
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil),
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
|
|
|
Return(mockConn, nil),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("uid=test,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(errors.New("Invalid username or password")),
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close(),
|
|
|
|
)
|
|
|
|
|
|
|
|
valid, err := ldapClient.CheckUserPassword("john", "password")
|
|
|
|
|
|
|
|
assert.False(t, valid)
|
|
|
|
require.EqualError(t, err, "Authentication of user john failed. Cause: Invalid username or password")
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:23:52 +00:00
|
|
|
func TestShouldCallStartTLSWhenEnabled(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 05:23:52 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 05:23:52 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
StartTLS(ldapClient.tlsConfig)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close()
|
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
func TestShouldParseDynamicConfiguration(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "(&(|({username_attribute}={0})({mail_attribute}={0})({display_name_attribute}={0}))(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!pwdLastSet=0))",
|
|
|
|
GroupsFilter: "(&(|(member={dn})(member={0})(member={1}))(objectClass=group))",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
AdditionalGroupsDN: "ou=groups",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
|
|
|
|
|
|
|
assert.Equal(t, "(&(|(uid={input})(mail={input})(displayname={input}))(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(!pwdLastSet=0))", ldapClient.configuration.UsersFilter)
|
|
|
|
assert.Equal(t, "(&(|(member={dn})(member={input})(member={username}))(objectClass=group))", ldapClient.configuration.GroupsFilter)
|
2021-04-12 01:10:50 +00:00
|
|
|
assert.Equal(t, "ou=users,dc=example,dc=com", ldapClient.usersBaseDN)
|
|
|
|
assert.Equal(t, "ou=groups,dc=example,dc=com", ldapClient.groupsBaseDN)
|
2021-01-04 10:28:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-03 05:23:52 +00:00
|
|
|
func TestShouldCallStartTLSWithInsecureSkipVerifyWhenSkipVerifyTrue(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
TLS: &schema.TLSConfig{
|
|
|
|
SkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 05:23:52 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldap://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 05:23:52 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
StartTLS(ldapClient.tlsConfig)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Close()
|
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
|
|
|
searchProfile := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
|
|
|
{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
{
|
|
|
|
Name: "displayname",
|
|
|
|
Values: []string{"John Doe"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
|
|
|
|
|
|
|
details, err := ldapClient.GetDetails("john")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.ElementsMatch(t, details.Groups, []string{})
|
|
|
|
assert.ElementsMatch(t, details.Emails, []string{"test@example.com"})
|
|
|
|
assert.Equal(t, details.DisplayName, "John Doe")
|
|
|
|
assert.Equal(t, details.Username, "john")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldReturnLDAPSAlreadySecuredWhenStartTLSAttempted(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
2021-01-04 10:28:55 +00:00
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(
|
|
|
|
schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
|
|
|
DisplayNameAttribute: "displayname",
|
|
|
|
UsersFilter: "uid={input}",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
StartTLS: true,
|
|
|
|
TLS: &schema.TLSConfig{
|
|
|
|
SkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
mockFactory)
|
2020-12-03 05:23:52 +00:00
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
2021-01-04 10:28:55 +00:00
|
|
|
DialURL(gomock.Eq("ldaps://127.0.0.1:389"), gomock.Any()).
|
2020-12-03 05:23:52 +00:00
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
StartTLS(ldapClient.tlsConfig).
|
|
|
|
Return(errors.New("LDAP Result Code 200 \"Network Error\": ldap: already encrypted"))
|
|
|
|
|
|
|
|
_, err := ldapClient.GetDetails("john")
|
|
|
|
assert.EqualError(t, err, "LDAP Result Code 200 \"Network Error\": ldap: already encrypted")
|
|
|
|
}
|