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

Commit 53234086 authored by Fahim M. Choudhury's avatar Fahim M. Choudhury Committed by Fahim M. Choudhury
Browse files

refactor: inject AuthorizationService using Hilt

This commit refactors the `OpenIdAuthenticationViewModel`, `OpenIdEndSessionActivity`, `SyncManager`, and `DefaultAccountAuthenticatorService` to use Hilt for injecting the `AuthorizationService`. Previously, there were manual creation of it which might cause issues with the dispose() method.

Key changes:
- Ensured `AuthorizationService` is configured to use /e/OS's Browser (`Constants.E_BROWSER_PACKAGE_NAME`) for authentication flows within `OAuthModule`.
- Updated logging level for `AuthorizationService.dispose()` failures from INFO to WARNING.
parent b7c82a32
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -90,10 +90,14 @@ class HttpClient private constructor(

    override fun close() {
        try {
            okHttpClient.cache?.close()
            authService?.dispose()
            okHttpClient.cache?.close()
        } catch (e: Exception) {
           Logger.log.log(Level.INFO, "failed to clear resource on close httpClient", e)
            Logger.log.log(
                Level.WARNING,
                "Failed to clear resource on closing OkHttpClient or disposing AuthorizationService",
                e
            )
        }
    }

+5 −2
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
package at.bitfire.davdroid.network

import android.content.Context
import at.bitfire.davdroid.Constants
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@@ -28,6 +29,8 @@ object OAuthModule {
                    (url.openConnection() as HttpURLConnection).apply {
                        setRequestProperty("User-Agent", HttpClient.UserAgentInterceptor.userAgent)
                    }
                }.build()
                }
                .setBrowserMatcher { it.packageName == Constants.E_BROWSER_PACKAGE_NAME }
                .build()
        )
}
+6 −2
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import androidx.annotation.AnyThread
import at.bitfire.davdroid.OpenIdUtils
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.network.HttpClient.HttpClientEntryPoint
import at.bitfire.davdroid.resource.LocalAddressBook
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.ui.account.SettingsActivity
@@ -51,6 +52,7 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
    @InstallIn(SingletonComponent::class)
    interface DefaultAccountAuthenticatorServiceEntryPoint {
        fun appDatabase(): AppDatabase
        fun authorizationService(): AuthorizationService
    }

    companion object {
@@ -220,7 +222,9 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
            val clientSecretString = accountManager.getUserData(account, AccountSettings.KEY_CLIENT_SECRET)
            val clientSecret = OpenIdUtils.getClientAuthentication(clientSecretString)

            val authorizationService = AuthorizationService(context)
            val authorizationService =
                EntryPointAccessors.fromApplication(context, DefaultAccountAuthenticatorServiceEntryPoint::class.java)
                    .authorizationService()

            authorizationService.performTokenRequest(
                tokenRequest,
@@ -249,7 +253,7 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
                try {
                    authorizationService.dispose()
                } catch (e: Exception) {
                    Logger.log.log(Level.INFO, "failed to dispose oidc authorizationService", e)
                    Logger.log.log(Level.WARNING, "Failed to dispose AuthorizationService", e)
                }
            }

+6 −2
Original line number Diff line number Diff line
@@ -87,6 +87,7 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
    @InstallIn(SingletonComponent::class)
    interface SyncManagerEntryPoint {
        fun appDatabase(): AppDatabase
        fun authorizationService(): AuthorizationService
    }

    enum class SyncAlgorithm {
@@ -194,7 +195,10 @@ abstract class SyncManager<ResourceType: LocalResource<*>, out CollectionType: L
        val clientSecretString = accountSettings.credentials().clientSecret
        val clientSecret = OpenIdUtils.getClientAuthentication(clientSecretString)

        val authorizationService = AuthorizationService(context)
        val authorizationService =
            EntryPointAccessors.fromApplication(context, SyncManagerEntryPoint::class.java)
                .authorizationService()

        authorizationService.performTokenRequest(tokenRequest, clientSecret) { tokenResponse, ex ->
            authState.update(tokenResponse, ex)
            accountSettings.credentials(
+2 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import at.bitfire.davdroid.authorization.IdentityProvider
import at.bitfire.davdroid.db.Credentials
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.ui.NetworkUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.openid.appauth.AuthState.AuthStateAction
@@ -42,6 +43,7 @@ import org.json.JSONObject
import java.net.URI
import java.util.logging.Level

@AndroidEntryPoint
abstract class OpenIdAuthenticationBaseFragment(private val identityProvider: IdentityProvider) :
    Fragment() {

Loading