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

Commit dd3c4632 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

refac:3870: Simplify login in domain layers.

parent 62b3d115
Loading
Loading
Loading
Loading
+1 −42
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ import javax.inject.Singleton
@Singleton
class AuthenticatorRepository @Inject constructor(
    private val loginCommon: LoginCommon,
    private val authenticators: List<StoreAuthenticator>,
    private val authenticators: List<AuthDataValidator>,
    private val appLoungeDataStore: AppLoungeDataStore
) {

@@ -45,51 +45,10 @@ class AuthenticatorRepository @Inject constructor(
        appLoungeDataStore.saveAuthData(auth)
    }

    suspend fun fetchAuthObjects(authTypes: List<String> = listOf()): List<AuthObject> {

        val authObjectsLocal = ArrayList<AuthObject>()

        for (authenticator in authenticators) {
            if (!authenticator.isStoreActive()) continue
            if (authenticator::class.java.simpleName in authTypes) {
                authenticator.logout()
            }

            val authObject = authenticator.login()
            authObjectsLocal.add(authObject)

            if (authObject is AuthObject.GPlayAuth) {
                appLoungeDataStore.saveAuthData(authObject.result.data)
            }
        }

        return authObjectsLocal
    }

    suspend fun saveUserType(user: User) {
        loginCommon.saveUserType(user)
    }

    suspend fun saveGoogleLogin(email: String, oauth: String) {
        loginCommon.saveGoogleLogin(email, oauth)
    }

    suspend fun setNoGoogleMode() {
        loginCommon.setNoGoogleMode()
    }

    suspend fun logout() {
        loginCommon.logout()
    }

    suspend fun getValidatedAuthData(): ResultSupreme<AuthData?> {
        val authDataValidator = (authenticators.find { it is AuthDataValidator } as AuthDataValidator)
        val validateAuthData = authDataValidator.validateAuthData()
        appLoungeDataStore.saveAuthData(validateAuthData.data)
        return validateAuthData
    }

    private fun getUserType(): User {
        return loginCommon.getUserType()
    }
}
+0 −58
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019-2022  E FOUNDATION
 *
 * 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.data.login

import foundation.e.apps.data.ResultSupreme
import foundation.e.apps.data.enums.User
import foundation.e.apps.data.preference.AppLoungeDataStore
import foundation.e.apps.data.preference.AppLoungePreference
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Just a dummy class for CleanApk, as it requires no authentication.
 * https://gitlab.e.foundation/e/backlog/-/issues/5680
 */
@Singleton
class CleanApkAuthenticator @Inject constructor(
    private val appLoungeDataStore: AppLoungeDataStore,
    private val appLoungePreference: AppLoungePreference,
) : StoreAuthenticator {

    private val user: User
        get() = appLoungeDataStore.getUser()

    override fun isStoreActive(): Boolean {
        if (user == User.UNAVAILABLE) {
            /*
             * UNAVAILABLE user means first login is not completed.
             */
            return false
        }
        return appLoungePreference.isOpenSourceSelected() || appLoungePreference.isPWASelected()
    }

    override suspend fun login(): AuthObject.CleanApk {
        return AuthObject.CleanApk(
            ResultSupreme.Success(Unit),
            user,
        )
    }

    override suspend fun logout() {}
}
+3 −50
Original line number Diff line number Diff line
@@ -49,8 +49,7 @@ class PlayStoreAuthenticator @Inject constructor(
    @ApplicationContext private val context: Context,
    private val json: Json,
    private val appLoungeDataStore: AppLoungeDataStore,
    private val appLoungePreference: AppLoungePreference,
) : StoreAuthenticator, AuthDataValidator {
): AuthDataValidator {

    @Inject
    lateinit var loginManagerFactory: PlayStoreLoginManagerFactory
@@ -67,52 +66,6 @@ class PlayStoreAuthenticator @Inject constructor(
    private val locale: Locale
        get() = context.resources.configuration.locales[0]

    override fun isStoreActive(): Boolean {
        if (user == User.UNAVAILABLE) {
            /*
             * UNAVAILABLE user means first login is not completed.
             */
            return false
        }
        return appLoungePreference.isPlayStoreSelected()
    }

    /**
     * Main entry point to get GPlay auth data.
     */
    override suspend fun login(): AuthObject.GPlayAuth {
        val savedAuth = getSavedAuthData()

        val authData = savedAuth ?: run {
            // if no saved data, then generate new auth data.
            val result = retryWithBackoff {
                generateAuthData()
            }

            result?.let {
                if (it.isSuccess()) it.data!!
                else return AuthObject.GPlayAuth(it, user)
            }
        }

        val formattedAuthData = authData?.let { formatAuthData(it) }
        formattedAuthData?.locale = locale
        val result: ResultSupreme<AuthData?> = ResultSupreme.create(
            status = ResultStatus.OK,
            data = formattedAuthData
        )
        result.otherPayload = formattedAuthData?.email

        if (savedAuth == null && formattedAuthData != null) {
            saveAuthData(formattedAuthData)
        }

        return AuthObject.GPlayAuth(result, user)
    }

    override suspend fun logout() {
        appLoungeDataStore.saveAuthData(null)
    }

    /**
     * Get authData stored as JSON and convert to AuthData class.
@@ -153,14 +106,14 @@ class PlayStoreAuthenticator @Inject constructor(
        return json.decodeFromString<AuthData>(localAuthDataJson)
    }

    private suspend fun getAuthDataAnonymously(): ResultSupreme<AuthData?> {
    suspend fun getAuthDataAnonymously(): ResultSupreme<AuthData?> {
        return loginWrapper.login(locale).run {
            if (isSuccess()) ResultSupreme.Success(formatAuthData(this.data!!))
            else this
        }
    }

    private suspend fun getAuthDataWithGoogleAccount(): ResultSupreme<AuthData?> {
    suspend fun getAuthDataWithGoogleAccount(): ResultSupreme<AuthData?> {

        val email = appLoungeDataStore.emailData.getSync()
        val oauthToken = appLoungeDataStore.oauthToken.getSync()
+0 −27
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019-2022  E FOUNDATION
 *
 * 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.data.login

/**
 * Store (Google Play Store, Clean Apk) authenticator.
 */
interface StoreAuthenticator {
    suspend fun login(): AuthObject
    suspend fun logout()
    fun isStoreActive(): Boolean
}
+17 −17
Original line number Diff line number Diff line
@@ -74,21 +74,21 @@ class AnonymousLoginManager(
        return authData
    }

    /**
     * Check if an AuthData is valid. Returns a [PlayResponse].
     * Check [PlayResponse.isSuccessful] to see if the validation was successful.
     */
    override suspend fun validate(authData: AuthData): PlayResponse {
        var result = PlayResponse()
        withContext(Dispatchers.IO) {
            try {
                val authValidator = CustomAuthValidator(authData).using(gPlayHttpClient)
                result = authValidator.getValidityResponse()
            } catch (e: Exception) {
                e.printStackTrace()
                throw e
            }
        }
        return result
    }
//    /**
//     * Check if an AuthData is valid. Returns a [PlayResponse].
//     * Check [PlayResponse.isSuccessful] to see if the validation was successful.
//     */
//    override suspend fun validate(authData: AuthData): PlayResponse {
//        var result = PlayResponse()
//        withContext(Dispatchers.IO) {
//            try {
//                val authValidator = CustomAuthValidator(authData).using(gPlayHttpClient)
//                result = authValidator.getValidityResponse()
//            } catch (e: Exception) {
//                e.printStackTrace()
//                throw e
//            }
//        }
//        return result
//    }
}
Loading