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

Commit 6eb995b4 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

fix: add missing proper cookie setup for some requests

parent e5c96011
Loading
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

package at.bitfire.davdroid.network

import android.accounts.Account
import android.content.Context
import android.os.Build
import android.security.KeyChain
@@ -17,6 +18,7 @@ import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.settings.Settings
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.syncadapter.AccountUtils
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
@@ -172,11 +174,10 @@ class HttpClient private constructor(
            addAuthentication(
                null,
                credential,
                account = accountSettings.account,
                authStateCallback = { authState: AuthState ->
                    updateCredentials(accountSettings, authState, credential.clientSecret)
                })

            cookieStore = createCookieStore(context, accountSettings.account)
        }

        private fun updateCredentials(
@@ -192,7 +193,7 @@ class HttpClient private constructor(
            )
        }

        fun addAuthentication(host: String?, credentials: Credentials, insecurePreemptive: Boolean = false, authStateCallback: BearerAuthInterceptor.AuthStateUpdateCallback? = null): Builder {
        fun addAuthentication(host: String?, credentials: Credentials, insecurePreemptive: Boolean = false, account: Account? = null, authStateCallback: BearerAuthInterceptor.AuthStateUpdateCallback? = null): Builder {
            if (credentials.userName != null && credentials.password != null) {
                val authHandler = BasicDigestAuthHandler(UrlUtils.hostToDomain(host), credentials.userName, credentials.password, insecurePreemptive)
                orig.addNetworkInterceptor(authHandler)
@@ -210,6 +211,10 @@ class HttpClient private constructor(
                    orig.addNetworkInterceptor(bearerAuthInterceptor)
                }
            }

            val accountForCookie = account ?: AccountUtils.getAccount(context, credentials.userName, host)
            cookieStore = createCookieStore(context, accountForCookie)

            return this
        }

+1 −9
Original line number Diff line number Diff line
@@ -157,20 +157,12 @@ class AccountSettings(
            }

            if (!baseURL.isNullOrEmpty()) {
                bundle.putString(NCAccountUtils.Constants.KEY_OC_BASE_URL, getOCBaseUrl(baseURL))
                bundle.putString(NCAccountUtils.Constants.KEY_OC_BASE_URL, AccountUtils.getOwnCloudBaseUrl(baseURL))
            }

            return bundle
        }

        private fun getOCBaseUrl(baseURL: String): String {
            if (baseURL.contains("remote.php")) {
                return baseURL.split("/remote.php")[0]
            }

            return baseURL
        }

    }


+30 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import android.os.Bundle
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.util.setAndVerifyUserData
import com.owncloud.android.lib.common.accounts.AccountUtils

object AccountUtils {

@@ -128,4 +129,33 @@ object AccountUtils {

        return accounts
    }

    fun getOwnCloudBaseUrl(baseURL: String): String {
        if (baseURL.contains("remote.php")) {
            return baseURL.split("/remote.php")[0]
        }

        return baseURL
    }

    fun getAccount(context: Context, userName: String?, requestedBaseUrl: String?): Account? {
        if (userName == null || requestedBaseUrl == null) {
            return null;
        }

        val baseUrl = getOwnCloudBaseUrl(requestedBaseUrl)

        val accountManager = AccountManager.get(context.applicationContext)

        val accounts = getMainAccounts(context)

        for(account in accounts) {
            val url = accountManager.getUserData(account, AccountUtils.Constants.KEY_OC_BASE_URL)
            if (url != null && getOwnCloudBaseUrl(url) == baseUrl) {
                return account
            }
        }

        return null
    }
}