2019-12-06 08:15:54 +00:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-03-19 04:22:46 +00:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
2019-12-06 08:15:54 +00:00
|
|
|
gomock "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)
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
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-30 22:36:04 +00: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-20 19:34:53 +00: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-30 22:36:04 +00: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-20 19:34:53 +00:00
|
|
|
|
2020-03-30 22:36:04 +00: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-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)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
2020-03-30 22:36:04 +00:00
|
|
|
UsersFilter: "(|({username_attribute}={input})({mail_attribute}={input}))",
|
2020-03-15 07:10:25 +00:00
|
|
|
UsernameAttribute: "uid",
|
2020-03-30 22:36:04 +00:00
|
|
|
MailAttribute: "mail",
|
2020-01-20 19:34:53 +00:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
|
|
|
}, mockFactory)
|
|
|
|
|
|
|
|
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-03-15 07:10:25 +00:00
|
|
|
ldapClient.getUserProfile(mockConn, "john=abc")
|
|
|
|
}
|
|
|
|
|
|
|
|
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-30 22:36:04 +00:00
|
|
|
UsersFilter: "(&({username_attribute}={input})(&(objectCategory=person)(objectClass=user)))",
|
2020-03-15 07:10:25 +00:00
|
|
|
Password: "password",
|
|
|
|
AdditionalUsersDN: "ou=users",
|
|
|
|
BaseDN: "dc=example,dc=com",
|
2020-03-30 22:36:04 +00:00
|
|
|
MailAttribute: "mail",
|
2020-03-15 07:10:25 +00:00
|
|
|
}, 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-03-15 07:10:25 +00:00
|
|
|
ldapClient.getUserProfile(mockConn, "john")
|
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{
|
|
|
|
&ldap.Entry{Attributes: attributes},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 07:10:25 +00:00
|
|
|
UsernameAttribute: "uid",
|
|
|
|
MailAttribute: "mail",
|
2020-03-30 22:36:04 +00:00
|
|
|
UsersFilter: "uid={input}",
|
2020-02-27 22:21:07 +00: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 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{
|
|
|
|
&ldap.Entry{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
&ldap.EntryAttribute{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
&ldap.EntryAttribute{
|
|
|
|
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-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)
|
|
|
|
|
|
|
|
ldapClient := NewLDAPUserProviderWithFactory(schema.LDAPAuthenticationBackendConfiguration{
|
|
|
|
URL: "ldap://127.0.0.1:389",
|
|
|
|
User: "cn=admin,dc=example,dc=com",
|
|
|
|
Password: "password",
|
2020-03-15 07:10:25 +00:00
|
|
|
UsernameAttribute: "uid",
|
2020-03-30 22:36:04 +00:00
|
|
|
UsersFilter: "uid={input}",
|
2020-02-27 22:21:07 +00: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 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{
|
|
|
|
&ldap.Entry{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
&ldap.EntryAttribute{
|
|
|
|
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)
|
|
|
|
|
|
|
|
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-30 22:36:04 +00:00
|
|
|
UsersFilter: "uid={input}",
|
2020-03-15 07:10:25 +00: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{
|
|
|
|
&ldap.Entry{
|
|
|
|
DN: "uid=test,dc=example,dc=com",
|
|
|
|
Attributes: []*ldap.EntryAttribute{
|
|
|
|
&ldap.EntryAttribute{
|
|
|
|
Name: "mail",
|
|
|
|
Values: []string{"test@example.com"},
|
|
|
|
},
|
|
|
|
&ldap.EntryAttribute{
|
|
|
|
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"})
|
|
|
|
assert.Equal(t, details.Username, "John")
|
2020-02-27 22:21:07 +00:00
|
|
|
}
|