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

Commit f9bf20fb authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch '1121-auto_update_authData' into 'main'

introduced auth validation before update process

See merge request !295
parents 4f8e0cd3 699cad48
Loading
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2023
 * Apps  Quickly and easily install Android apps onto your device!
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.apps.login

import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.api.ResultSupreme

interface AuthDataValidator {
    suspend fun validateAuthData(): ResultSupreme<AuthData?>
}
 No newline at end of file
+33 −14
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import foundation.e.apps.login.api.LoginApiRepository
import foundation.e.apps.utils.enums.ResultStatus
import foundation.e.apps.utils.enums.User
import foundation.e.apps.utils.exceptions.GPlayValidationException
import timber.log.Timber
import java.util.Locale
import javax.inject.Inject
import javax.inject.Singleton
@@ -44,7 +45,7 @@ class LoginSourceGPlay @Inject constructor(
    @ApplicationContext private val context: Context,
    private val gson: Gson,
    private val loginDataStore: LoginDataStore,
) : LoginSourceInterface {
) : LoginSourceInterface, AuthDataValidator {

    @Inject
    lateinit var gPlayApiFactory: GPlayApiFactory
@@ -251,4 +252,22 @@ class LoginSourceGPlay @Inject constructor(
            )
        }
    }

    override suspend fun validateAuthData(): ResultSupreme<AuthData?> {
        val savedAuth = getSavedAuthData()
        if (!isAuthDataValid(savedAuth)) {
            Timber.i("Validating AuthData...")
            val authData = generateAuthData()
            authData.data?.let {
                saveAuthData(it)
                return authData
            }
            return ResultSupreme.create(ResultStatus.UNKNOWN)
        }

        return ResultSupreme.create(ResultStatus.OK, savedAuth)
    }

    private suspend fun isAuthDataValid(savedAuth: AuthData?) =
        savedAuth != null && loginApiRepository.login(savedAuth).exception == null
}
+7 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

package foundation.e.apps.login

import com.aurora.gplayapi.data.models.AuthData
import foundation.e.apps.api.ResultSupreme
import foundation.e.apps.utils.enums.User
import javax.inject.Inject
import javax.inject.Singleton
@@ -58,4 +60,9 @@ class LoginSourceRepository @Inject constructor(
    suspend fun logout() {
        loginCommon.logout()
    }

    suspend fun getValidatedAuthData(): ResultSupreme<AuthData?> {
        val authDataValidator = (sources.find { it is AuthDataValidator } as AuthDataValidator)
        return authDataValidator.validateAuthData()
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import foundation.e.apps.api.ResultSupreme
import foundation.e.apps.api.cleanapk.CleanAPKInterface
import foundation.e.apps.api.fused.FusedAPIRepository
import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.login.LoginSourceRepository
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.manager.workmanager.InstallWorkManager
@@ -49,6 +50,7 @@ class UpdatesWorker @AssistedInject constructor(
    private val fusedAPIRepository: FusedAPIRepository,
    private val fusedManagerRepository: FusedManagerRepository,
    private val dataStoreManager: DataStoreManager,
    private val loginSourceRepository: LoginSourceRepository,
    private val gson: Gson,
) : CoroutineWorker(context, params) {

@@ -110,7 +112,7 @@ class UpdatesWorker @AssistedInject constructor(
        val isConnectedToUnmeteredNetwork = isConnectedToUnmeteredNetwork(applicationContext)
        val appsNeededToUpdate = mutableListOf<FusedApp>()
        val user = getUser()
        val authData = getAuthData()
        val authData = loginSourceRepository.getValidatedAuthData().data
        val resultStatus: ResultStatus

        if (user in listOf(User.ANONYMOUS, User.GOOGLE) && authData != null) {
@@ -225,6 +227,7 @@ class UpdatesWorker @AssistedInject constructor(
            if (!fusedApp.isFree && authData.isAnonymous) {
                return@forEach
            }

            val iconBase64 = getIconImageToBase64(fusedApp)

            val fusedDownload = FusedDownload(