Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
166 lines
7.2 KiB
Go
166 lines
7.2 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
ldap "github.com/go-ldap/ldap/v3"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
type fakeLDAPDirectory struct {
|
|
result *ldap.SearchResult
|
|
searchErr error
|
|
userBindErr error
|
|
binds [][2]string
|
|
searchFilter string
|
|
}
|
|
|
|
func (f *fakeLDAPDirectory) Bind(dn, password string) error {
|
|
f.binds = append(f.binds, [2]string{dn, password})
|
|
if len(f.binds) > 1 {
|
|
return f.userBindErr
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeLDAPDirectory) Search(request *ldap.SearchRequest) (*ldap.SearchResult, error) {
|
|
f.searchFilter = request.Filter
|
|
return f.result, f.searchErr
|
|
}
|
|
|
|
func (f *fakeLDAPDirectory) Close() error { return nil }
|
|
|
|
func ldapEntry(uid, username, email, displayName string) *ldap.Entry {
|
|
return ldap.NewEntry("uid="+username+",ou=people,dc=example,dc=test", map[string][]string{
|
|
"entryUUID": {uid},
|
|
"uid": {username},
|
|
"mail": {email},
|
|
"displayName": {displayName},
|
|
})
|
|
}
|
|
|
|
func configuredLDAPServer(t *testing.T) (*Server, string, store.AuthProviderConfig) {
|
|
t.Helper()
|
|
s, _, domain := configFixture(t)
|
|
config := store.AuthProviderConfig{
|
|
Kind: "ldap", Enabled: true, LDAPURL: "ldaps://directory.example.test:636",
|
|
LDAPBindDN: "cn=pageswiki,ou=services,dc=example,dc=test", LDAPBindPassword: "service-secret",
|
|
LDAPSearchBase: "ou=people,dc=example,dc=test", LDAPSearchFilter: "(&(objectClass=person)(uid={{username}}))",
|
|
LDAPUIDAttribute: "entryUUID", LDAPUsernameAttribute: "uid", LDAPEmailAttribute: "mail", LDAPDisplayAttribute: "displayName",
|
|
LDAPMatchBy: "username",
|
|
}
|
|
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config, err := s.store.AuthProviderConfig(domain, "ldap")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.ldapSlots = make(chan struct{}, 4)
|
|
return s, domain, config
|
|
}
|
|
|
|
func TestLDAPAuthenticationSearchesSafelyAndBindsAsUser(t *testing.T) {
|
|
s, domain, _ := configuredLDAPServer(t)
|
|
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Directory Admin")}}}
|
|
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
|
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "admin", "user-secret")
|
|
if err != nil || principal.Username != "admin" {
|
|
t.Fatalf("LDAP principal = %#v, %v", principal, err)
|
|
}
|
|
if len(directory.binds) != 2 || directory.binds[0] != [2]string{"cn=pageswiki,ou=services,dc=example,dc=test", "service-secret"} || directory.binds[1][1] != "user-secret" {
|
|
t.Fatalf("LDAP binds = %#v", directory.binds)
|
|
}
|
|
if directory.searchFilter != "(&(objectClass=person)(uid=admin))" {
|
|
t.Fatalf("search filter = %q", directory.searchFilter)
|
|
}
|
|
loaded, err := s.store.AuthProviderConfig(domain, "ldap")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
subject, _ := ldapStableSubject([]byte("stable-id"))
|
|
bound, err := s.store.PrincipalByExternalIdentity(domain, "ldap", loaded.UUID, "", subject)
|
|
if err != nil || bound.UUID != principal.UUID {
|
|
t.Fatalf("immutable LDAP binding = %#v, %v", bound, err)
|
|
}
|
|
}
|
|
|
|
func TestLDAPAuthenticationEscapesFilterAndRejectsAmbiguousOrBadPassword(t *testing.T) {
|
|
s, _, config := configuredLDAPServer(t)
|
|
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}}
|
|
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
|
login := `admin*)(uid=*)`
|
|
if _, err := s.authenticateLDAPDirectory(context.Background(), config, login, "user-secret"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(directory.searchFilter, login) || !strings.Contains(directory.searchFilter, ldap.EscapeFilter(login)) {
|
|
t.Fatalf("LDAP search input was not escaped: %q", directory.searchFilter)
|
|
}
|
|
|
|
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("one", "admin", "", "Admin"), ldapEntry("two", "admin", "", "Admin")}}
|
|
directory.binds = nil
|
|
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "user-secret"); !errors.Is(err, store.ErrAccessDenied) {
|
|
t.Fatalf("ambiguous directory result accepted: %v", err)
|
|
}
|
|
if len(directory.binds) != 1 {
|
|
t.Fatalf("ambiguous result attempted a user bind: %#v", directory.binds)
|
|
}
|
|
|
|
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "", "Admin")}}
|
|
directory.binds = nil
|
|
directory.userBindErr = errors.New("invalid credentials")
|
|
if _, err := s.authenticateLDAPDirectory(context.Background(), config, "admin", "wrong"); !errors.Is(err, store.ErrAccessDenied) {
|
|
t.Fatalf("bad LDAP password accepted: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestLDAPAutoProvisionUsesImmutableSubjectOnFollowingLogins(t *testing.T) {
|
|
s, domain, config := configuredLDAPServer(t)
|
|
config.LDAPAutoProvision = true
|
|
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "directory-user", "directory@example.test", "Directory User")}}}
|
|
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
|
principal, err := s.authenticateLDAPPrincipal(context.Background(), domain, "directory-user", "user-secret")
|
|
if err != nil || principal.Username != "directory-user" {
|
|
t.Fatalf("provisioned LDAP principal = %#v, %v", principal, err)
|
|
}
|
|
// Mutable directory attributes may change, but the immutable configured UID
|
|
// remains the authoritative identity after first login.
|
|
directory.result = &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("new-stable-id", "renamed-user", "renamed@example.test", "Renamed User")}}
|
|
directory.binds = nil
|
|
again, err := s.authenticateLDAPPrincipal(context.Background(), domain, "renamed-user", "user-secret")
|
|
if err != nil || again.UUID != principal.UUID {
|
|
t.Fatalf("immutable LDAP re-login = %#v, %v", again, err)
|
|
}
|
|
}
|
|
|
|
func TestLDAPLoginHandlerCreatesTypedBrowserSession(t *testing.T) {
|
|
s, domain, _ := configuredLDAPServer(t)
|
|
directory := &fakeLDAPDirectory{result: &ldap.SearchResult{Entries: []*ldap.Entry{ldapEntry("stable-id", "admin", "admin@example.test", "Admin")}}}
|
|
s.ldapDial = func(context.Context, store.AuthProviderConfig) (ldapDirectory, error) { return directory, nil }
|
|
form := url.Values{"username": {"admin"}, "password": {"user-secret"}, "return": {"/v/tree/docs"}}
|
|
request := httptest.NewRequest(http.MethodPost, "https://localhost/_/auth/ldap", strings.NewReader(form.Encode()))
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
response := httptest.NewRecorder()
|
|
s.handleLDAPLogin(response, request)
|
|
if response.Code != http.StatusSeeOther || response.Header().Get("Location") != "/v/tree/docs" {
|
|
t.Fatalf("LDAP browser login: %d %s", response.Code, response.Body.String())
|
|
}
|
|
cookies := response.Result().Cookies()
|
|
if len(cookies) != 1 || cookies[0].Name != authCookieName || !cookies[0].HttpOnly || !cookies[0].Secure || !strings.HasPrefix(cookies[0].Value, "pw_browser_") {
|
|
t.Fatalf("LDAP browser cookie: %#v", cookies)
|
|
}
|
|
principal, err := s.store.PrincipalForSession(domain, cookies[0].Value)
|
|
if err != nil || principal.Username != "admin" {
|
|
t.Fatalf("LDAP browser session: %#v, %v", principal, err)
|
|
}
|
|
}
|