public function authenticate(Request $request, $serverName)
    {
        $server = $this->get($serverName);
        $username = $request->get('_username');
        $password = $request->get('_password');
        if (
            !empty($username) &&
            !empty($password) &&
            $this->connect($server, $this->prepareUsername($server, $username), $password, true)
        ) {
            $ldapUser = $this->ldapUserRepo->findOneBy(['serverName' => $server, 'ldapId' => $username]);
            if (null !== $ldapUser) {
                return $this->registrationManager->loginUser($ldapUser->getUser(), $request);
            }
            if ($this->platformConfigHandler->getParameter('direct_third_party_authentication')) {
                $user = $this->userManager->getUserByUsername($username);
                if (null === $user) {
                    throw $this->getUsernameNotFoundException($username);
                }
                $this->createLdapUser($serverName, $username, $user);

                return $this->registrationManager->loginUser($user);
            }

            throw $this->getUsernameNotFoundException($username);
        }

        throw new InvalidLdapCredentialsException();
    }