Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit dfc57bd7 authored by Romain Hunault's avatar Romain Hunault 🚴🏻
Browse files

feat(workspace): remove hardcoded Murena bootstrap assumptions

parent 2af8f529
Loading
Loading
Loading
Loading
+0 −5
Original line number Diff line number Diff line
@@ -30,10 +30,5 @@ object Constants {

    const val AUTH_TOKEN_TYPE = "oauth2-access-token"

    const val EELO_SYNC_HOST = "murena.io"
    const val E_SYNC_URL = "e.email"

    const val MURENA_DAV_URL = "https://murena.io/remote.php/dav"

    const val E_BROWSER_PACKAGE_NAME = "foundation.e.browser"
}
+0 −12
Original line number Diff line number Diff line
@@ -71,23 +71,11 @@ class DetectConfigurationFragment: Fragment() {
            parentFragmentManager.popBackStack()

            if (result.calDAV != null || result.cardDAV != null) {
                intent.putExtra(LoginActivity.RETRY_ON_401, false)

                parentFragmentManager.beginTransaction()
                    .replace(android.R.id.content, AccountDetailsFragment())
                    .addToBackStack(null)
                    .commit()
            } else if (intent.getBooleanExtra(
                    LoginActivity.RETRY_ON_401,
                    false
                ) && loginModel.configuration?.encountered401 == true
            ) {
                // murena account has encounters 401, most-probably user put wrong accountId (ex: abc@murena.io instead of abc@e.email)
                // do nothing, EeloAuthenticatorFragment will retry with another time with another user email
                return@observe
            } else {
                intent.putExtra(LoginActivity.RETRY_ON_401, false)

                parentFragmentManager.beginTransaction()
                    .add(NothingDetectedFragment(), null)
                    .commit()
+9 −66
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.ECloudAccountHelper
import at.bitfire.davdroid.R
import at.bitfire.davdroid.databinding.FragmentEeloAuthenticatorBinding
@@ -40,6 +39,7 @@ import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.murenasso.MurenaSsoMigrationPreferences
import at.bitfire.davdroid.ui.ShowUrlActivity
import at.bitfire.davdroid.ui.account.SettingsActivity
import at.bitfire.davdroid.util.MurenaServerConfig
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
@@ -68,6 +68,10 @@ class EeloAuthenticatorFragment : Fragment() {
        requireActivity().intent.getStringExtra(SettingsActivity.EXTRA_ACCOUNT_NAME_HINT)
    }

    private val workspaceDescriptor by lazy {
        MurenaServerConfig.getDescriptor(requireContext())
    }

    private fun isNetworkAvailable(): Boolean {
        val connectivityManager = requireActivity().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
@@ -147,72 +151,22 @@ class EeloAuthenticatorFragment : Fragment() {
    override fun onResume() {
        super.onResume()

        if (requireActivity().intent.getBooleanExtra(LoginActivity.RETRY_ON_401, false) && switchUserName()) {
            // user wants to login with murena account, but most probably provided wrong accountId as email.
            // switching email is done, retry login
            login()
            requireActivity().intent.putExtra(LoginActivity.RETRY_ON_401, false) // disable retry option to mitigate infinite looping
        }

        val accountName = requireActivity().intent.getStringExtra(LoginActivity.EXTRA_USERNAME)
        if (accountName != null) {
            userIdEditText.setText(accountName)
        }
    }

    /***
     * user put wrong accountId for the first time (for ex: abc@murena.io instead of abc@e.email)
     * this method check provided email & switch to alternative email if possible so retry can be handled in bg
     * @return email field's value is successfully switched or not. If true, proceed to retry
      */
    @SuppressLint("SetTextI18n")
    private fun switchUserName(): Boolean {
       if (userIdEditText.text.toString().contains("@")) {
           val username = userIdEditText.text.toString().substringBefore("@")
           val dns = userIdEditText.text.toString().substringAfter("@")

           if (dns == Constants.E_SYNC_URL) {
               userIdEditText.setText(username + "@" + Constants.EELO_SYNC_HOST)
               return true
           }

           if (dns == Constants.EELO_SYNC_HOST) {
               userIdEditText.setText(username + "@" + Constants.E_SYNC_URL)
               return true
           }
       }

        return false
    }

    override fun onSaveInstanceState(outState: Bundle) {
        outState.putBoolean(toggleButtonCheckedKey, toggleButtonState)
        super.onSaveInstanceState(outState)
    }

    /**
     * murena.io account can have userName which is not email address
     * But, we want to have email as userName so that auth to services like Mail doesn't break.
     * This method check the provided userName if it is not email & server is https://murena.io;
     * then add `@murena.io` after the userName to make full email address.
     */
    @SuppressLint("SetTextI18n")
    private fun purifyUserName(serverUrl: String) {
        val providedUserName = userIdEditText.text.toString()

        if (!providedUserName.contains("@") && serverUrl == "https://${Constants.EELO_SYNC_HOST}") {
            userIdEditText.setText("$providedUserName@${Constants.EELO_SYNC_HOST}")
        }
    }

    private fun computeDomain(username: CharSequence?) : String {
        var domain = "https://${Constants.EELO_SYNC_HOST}"
        var domain = workspaceDescriptor.baseWebUrl

        if (!username.isNullOrBlank() && username.toString().contains("@")) {
            var dns = username.toString().substringAfter("@")
            if (dns == Constants.E_SYNC_URL) {
                dns = Constants.EELO_SYNC_HOST
            }
            val dns = username.toString().substringAfter("@")
            domain = "https://$dns"
        }
        return domain
@@ -259,14 +213,6 @@ class EeloAuthenticatorFragment : Fragment() {
            .commit()
    }

    // if user wants to login with murena account, add support to automated retry
    // if user in any case provide wrong accountId as email (ex: abc@murena.io instead of abc@e.email)
    private fun addSupportRetryOn401IfPossible(serverUrl: String) {
        if ("https://${Constants.EELO_SYNC_HOST}" == serverUrl) {
            requireActivity().intent.putExtra(LoginActivity.RETRY_ON_401, true)
        }
    }

    private fun validate(): Boolean {
        var valid = false

@@ -274,11 +220,8 @@ class EeloAuthenticatorFragment : Fragment() {

        if (serverUrl.isEmpty()) {
            serverUrl = computeDomain(userIdEditText.text.toString())
            addSupportRetryOn401IfPossible(serverUrl)
        }

        purifyUserName(serverUrl)

        fun validateUrl() {

            model.baseUrlError.value = null
@@ -314,8 +257,8 @@ class EeloAuthenticatorFragment : Fragment() {
    }

    private fun getServerURI(serverUrl: String): URI {
        if (serverUrl.startsWith("https://${Constants.EELO_SYNC_HOST}")) {
            return URI(Constants.MURENA_DAV_URL)
        if (serverUrl.startsWith(workspaceDescriptor.baseWebUrl)) {
            return URI(workspaceDescriptor.davBaseUrl)
        }

        return URI(serverUrl)
+0 −2
Original line number Diff line number Diff line
@@ -48,8 +48,6 @@ class LoginActivity : AppCompatActivity() {
        const val OPEN_APP_ACTIVITY_AFTER_AUTH = "open_app_activity_after_auth"

        const val IGNORE_ACCOUNT_SETUP = "ignore_account_setup"

        const val RETRY_ON_401 = "retry_on_401"
    }

    @Inject