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

Commit 52ffb04c authored by Jonathan Klee's avatar Jonathan Klee Committed by Fahim Salam Chowdhury
Browse files

Update AccountAuthenticatorService according to review

parent a6a658b4
Loading
Loading
Loading
Loading
Loading
+55 −48
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.annotation.AnyThread
import at.bitfire.davdroid.OpenIdUtils
import at.bitfire.davdroid.db.AppDatabase
import at.bitfire.davdroid.log.Logger
@@ -57,8 +58,9 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
            addressBookAccounts.map { LocalAddressBook(context, it, null) }
                .forEach {
                    try {
                        if (!accountNames.contains(it.mainAccount.name))
                        if (!accountNames.contains(it.mainAccount.name)) {
                            it.delete()
                        }
                    } catch (e: Exception) {
                        Logger.log.log(Level.SEVERE, "Couldn't delete address book account", e)
                    }
@@ -66,11 +68,12 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL

            // delete orphaned services in DB
            val serviceDao = db.serviceDao()
            if (accountNames.isEmpty())
            if (accountNames.isEmpty()) {
                serviceDao.deleteAll()
            else
            } else {
                serviceDao.deleteExceptAccounts(accountNames.toTypedArray())
            }
        }

    }

@@ -92,10 +95,9 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
    override fun onBind(intent: Intent?) =
        accountAuthenticator.iBinder.takeIf { intent?.action == AccountManager.ACTION_AUTHENTICATOR_INTENT }

    @AnyThread
    override fun onAccountsUpdated(accounts: Array<out Account>?) {
        /* onAccountsUpdated may be called from the main thread, but cleanupAccounts
        requires disk (database) access. So we launch it in a separate thread. */
        CoroutineScope(Dispatchers.Default).launch {
        CoroutineScope(Dispatchers.IO).launch {
            val db = EntryPointAccessors.fromApplication(
                applicationContext,
                AccountsUpdatedListener.AccountsUpdatedListenerEntryPoint::class.java
@@ -145,16 +147,16 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
        override fun getAuthTokenLabel(p0: String?) = null

        override fun confirmCredentials(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: Bundle?
            rssponse: AccountAuthenticatorResponse?,
            account: Account?,
            options: Bundle?
        ) = null

        override fun updateCredentials(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: String?,
            p3: Bundle?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            authTokenType: String?,
            options: Bundle?
        ) = null

        override fun getAuthToken(
@@ -164,16 +166,29 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
            options: Bundle?
        ): Bundle? {
            val accountManager = AccountManager.get(context)
            val authStateString =
                accountManager.getUserData(account, AccountSettings.KEY_AUTH_STATE) ?: return null
            val authStateString = accountManager.getUserData(account, AccountSettings.KEY_AUTH_STATE) ?: return null

            val authState = AuthState.jsonDeserialize(authStateString)

            if (authState != null) {
                if (authState.needsTokenRefresh) {
            if (authState == null) {
                val result = Bundle()
                result.putInt(
                    AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE,
                    AccountManager.ERROR_CODE_UNSUPPORTED_OPERATION
                )
                return result
            }

            if (!authState.needsTokenRefresh) {
                val result = Bundle()
                result.putString(AccountManager.KEY_ACCOUNT_NAME, account!!.name)
                result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)
                result.putString(AccountManager.KEY_AUTHTOKEN, authState.accessToken)
                return result
            }

            val tokenRequest = authState.createTokenRefreshRequest()
                    val clientSecretString =
                        accountManager.getUserData(account, AccountSettings.KEY_CLIENT_SECRET)
            val clientSecretString = accountManager.getUserData(account, AccountSettings.KEY_CLIENT_SECRET)
            val clientSecret = OpenIdUtils.getClientAuthentication(clientSecretString)

            AuthorizationService(context).performTokenRequest(
@@ -192,14 +207,6 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
                result.putString(AccountManager.KEY_AUTHTOKEN, authState.accessToken)
                response?.onResult(result)
            }
                } else {
                    val result = Bundle()
                    result.putString(AccountManager.KEY_ACCOUNT_NAME, account!!.name)
                    result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)
                    result.putString(AccountManager.KEY_AUTHTOKEN, authState.accessToken)
                    return result
                }
            }

            val result = Bundle()
            result.putInt(
@@ -210,9 +217,9 @@ abstract class DefaultAccountAuthenticatorService : Service(), OnAccountsUpdateL
        }

        override fun hasFeatures(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: Array<out String>?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            features: Array<out String>?
        ) = null
    }
}
+15 −15
Original line number Diff line number Diff line
@@ -59,31 +59,31 @@ abstract class DefaultAddressBookAuthenticatorService : Service() {
        override fun editProperties(response: AccountAuthenticatorResponse?, accountType: String?) =
            null

        override fun getAuthTokenLabel(p0: String?) = null
        override fun getAuthTokenLabel(authTokenType: String?) = null
        override fun confirmCredentials(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: Bundle?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            options: Bundle?
        ) = null

        override fun updateCredentials(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: String?,
            p3: Bundle?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            authTokenType: String?,
            options: Bundle?
        ) = null

        override fun getAuthToken(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: String?,
            p3: Bundle?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            authTokenType: String?,
            options: Bundle?
        ) = null

        override fun hasFeatures(
            p0: AccountAuthenticatorResponse?,
            p1: Account?,
            p2: Array<out String>?
            response: AccountAuthenticatorResponse?,
            account: Account?,
            features: Array<out String>?
        ) = null
    }
}
+1 −1

File changed.

Contains only whitespace changes.