2019-12-06 15:15:54 +07:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-03-19 11:22:46 +07:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2020-05-05 02:39:25 +07:00
|
|
|
"github.com/golang/mock/gomock"
|
2020-01-21 02:34:53 +07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-12-06 15:15:54 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-05 19:37:21 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/internal/configuration/schema"
|
2019-12-06 15:15:54 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShouldCreateRawConnectionWhenSchemeIsLDAP(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldap := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
|
|
|
|
Return(mockConn, nil)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
|
|
|
Return(nil)
|
|
|
|
|
|
|
|
_, err := ldap.connect("cn=admin,dc=example,dc=com", "password")
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCreateTLSConnectionWhenSchemeIsLDAPS(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
mockConn := NewMockLDAPConnection(ctrl)
|
|
|
|
|
|
|
|
ldap := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
DialTLS(gomock.Eq("tcp"), gomock.Eq("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)
|
|
|
|
|
|
|
|
_, err := ldap.connect("cn=admin,dc=example,dc=com", "password")
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
func TestEscapeSpecialCharsFromUserInput(t *testing.T) {
|
2020-01-21 02:34:53 +07:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
|
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
ldap := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
// No escape
|
|
|
|
assert.Equal(t, "xyz", ldap.ldapEscape("xyz"))
|
|
|
|
|
|
|
|
// Escape
|
|
|
|
assert.Equal(t, "test\\,abc", ldap.ldapEscape("test,abc"))
|
2020-03-31 05:36:04 +07:00
|
|
|
assert.Equal(t, "test\\5cabc", ldap.ldapEscape("test\\abc"))
|
|
|
|
assert.Equal(t, "test\\2aabc", ldap.ldapEscape("test*abc"))
|
|
|
|
assert.Equal(t, "test \\28abc\\29", ldap.ldapEscape("test (abc)"))
|
2020-01-21 02:34:53 +07:00
|
|
|
assert.Equal(t, "test\\#abc", ldap.ldapEscape("test#abc"))
|
|
|
|
assert.Equal(t, "test\\+abc", ldap.ldapEscape("test+abc"))
|
|
|
|
assert.Equal(t, "test\\<abc", ldap.ldapEscape("test<abc"))
|
|
|
|
assert.Equal(t, "test\\>abc", ldap.ldapEscape("test>abc"))
|
|
|
|
assert.Equal(t, "test\\;abc", ldap.ldapEscape("test;abc"))
|
|
|
|
assert.Equal(t, "test\\\"abc", ldap.ldapEscape("test\"abc"))
|
|
|
|
assert.Equal(t, "test\\=abc", ldap.ldapEscape("test=abc"))
|
2020-03-31 05:36:04 +07:00
|
|
|
assert.Equal(t, "test\\,\\5c\\28abc\\29", ldap.ldapEscape("test,\\(abc)"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEscapeSpecialCharsInGroupsFilter(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
mockFactory := NewMockLDAPConnectionFactory(ctrl)
|
|
|
|
ldap := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldaps://127.0.0.1:389",
|
|
|
|
GroupsFilter: "(|(member={dn})(uid={username})(uid={input}))",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
profile := ldapUserProfile{
|
|
|
|
DN: "cn=john (external),dc=example,dc=com",
|
|
|
|
Username: "john",
|
|
|
|
Emails: []string{"john.doe@authelia.com"},
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, _ := ldap.resolveGroupsFilter("john", &profile)
|
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john))", filter)
|
|
|
|
|
|
|
|
filter, _ = ldap.resolveGroupsFilter("john#=(abc,def)", &profile)
|
|
|
|
assert.Equal(t, "(|(member=cn=john \\28external\\29,dc=example,dc=com)(uid=john)(uid=john\\#\\=\\28abc\\,def\\29))", filter)
|
2020-01-21 02:34:53 +07: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)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
2020-03-31 05:36:04 +07:00
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
2020-03-15 14:10:25 +07:00
|
|
|
UsernameAttribute: "uid",
|
2020-03-31 05:36:04 +07:00
|
|
|
MailAttribute: "mail",
|
2020-01-21 02:34:53 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
// Here we ensure that the input has been correctly escaped.
|
2020-03-31 05:36:04 +07:00
|
|
|
Search(NewSearchRequestMatcher("(|(uid=john\\=abc)(mail=john\\=abc))")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
2020-04-22 10:33:14 +07:00
|
|
|
ldapClient.getUserProfile(mockConn, "john=abc") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2020-03-15 14:10:25 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldCombineUsernameFilterAndUsersFilter(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",
|
|
|
|
UsernameAttribute: "uid",
|
2020-03-31 05:36:04 +07:00
|
|
|
UsersFilter: "(&({username_attribute}={input})(&(objectCategory=person)(objectClass=user)))",
|
2020-03-15 14:10:25 +07:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
2020-03-31 05:36:04 +07:00
|
|
|
MailAttribute: "mail",
|
2020-03-15 14:10:25 +07:00
|
|
|
}, mockFactory)
|
2020-01-21 02:34:53 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
Search(NewSearchRequestMatcher("(&(uid=john)(&(objectCategory=person)(objectClass=user)))")).
|
2020-01-21 02:34:53 +07:00
|
|
|
Return(&ldap.SearchResult{}, nil)
|
|
|
|
|
2020-04-22 10:33:14 +07:00
|
|
|
ldapClient.getUserProfile(mockConn, "john") //nolint:errcheck // TODO: Legacy code, consider refactoring time permitting.
|
2020-01-21 02:34:53 +07:00
|
|
|
}
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
func createSearchResultWithAttributes(attributes ...*ldap.EntryAttribute) *ldap.SearchResult {
|
|
|
|
return &ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{Attributes: attributes},
|
2020-02-28 05:21:07 +07: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)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
2020-03-15 14:10:25 +07:00
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2020-03-31 05:36:04 +07:00
|
|
|
UsersFilter: "uid={input}",
|
2020-02-28 05:21:07 +07:00
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(mockConn, nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
Close()
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributes(), nil)
|
2020-03-15 14:10:25 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-02-28 05:21:07 +07: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-03-15 14:10:25 +07:00
|
|
|
assert.Equal(t, details.Username, "john")
|
2020-02-28 05:21:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShouldNotCrashWhenEmailsAreNotRetrievedFromLDAP(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",
|
2020-03-15 14:10:25 +07:00
|
|
|
UsernameAttribute: "uid",
|
2020-03-31 05:36:04 +07:00
|
|
|
UsersFilter: "uid={input}",
|
2020-02-28 05:21:07 +07:00
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(mockConn, nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
|
|
|
Bind(gomock.Eq("cn=admin,dc=example,dc=com"), gomock.Eq("password")).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(nil)
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
mockConn.EXPECT().
|
2020-03-15 14:10:25 +07:00
|
|
|
Close()
|
2020-02-28 05:21:07 +07:00
|
|
|
|
|
|
|
searchGroups := mockConn.EXPECT().
|
|
|
|
Search(gomock.Any()).
|
|
|
|
Return(createSearchResultWithAttributeValues("group1", "group2"), nil)
|
2020-03-15 14:10:25 +07:00
|
|
|
searchProfile := mockConn.EXPECT().
|
2020-02-28 05:21:07 +07:00
|
|
|
Search(gomock.Any()).
|
2020-03-15 14:10:25 +07:00
|
|
|
Return(&ldap.SearchResult{
|
|
|
|
Entries: []*ldap.Entry{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"john"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-02-28 05:21:07 +07: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 14:10:25 +07: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)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2020-03-31 05:36:04 +07:00
|
|
|
UsersFilter: "uid={input}",
|
2020-03-15 14:10:25 +07:00
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
mockFactory.EXPECT().
|
|
|
|
Dial(gomock.Eq("tcp"), gomock.Eq("127.0.0.1:389")).
|
|
|
|
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 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
2020-04-09 08:05:17 +07:00
|
|
|
{
|
2020-03-15 14:10:25 +07:00
|
|
|
Name: "uid",
|
|
|
|
Values: []string{"John"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
|
2020-03-31 05:36:04 +07:00
|
|
|
gomock.InOrder(searchProfile, searchGroups)
|
2020-03-15 14:10:25 +07: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"})
|
|
|
|
assert.Equal(t, details.Username, "John")
|
2020-02-28 05:21:07 +07:00
|
|
|
}
|