AuthViewModel: Use auth validation logic from AuthProvider
Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
@@ -49,7 +49,7 @@ class AuthProvider @Inject constructor(
|
||||
return AccountType.valueOf(name) == AccountType.ANONYMOUS
|
||||
}
|
||||
|
||||
suspend fun isAuthDataValid(): Boolean {
|
||||
suspend fun isSavedAuthDataValid(): Boolean {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
AuthValidator(getSavedAuthData())
|
||||
|
||||
@@ -97,7 +97,7 @@ class UpdateWorker @AssistedInject constructor(
|
||||
}
|
||||
|
||||
withContext(Dispatchers.IO) {
|
||||
if (!authProvider.isAuthDataValid()) {
|
||||
if (!authProvider.isSavedAuthDataValid()) {
|
||||
Log.i(TAG, "AuthData is not valid, retrying later!")
|
||||
return@withContext Result.retry()
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ import com.aurora.gplayapi.data.models.AuthData
|
||||
import com.aurora.gplayapi.data.models.PlayResponse
|
||||
import com.aurora.gplayapi.data.providers.DeviceInfoProvider
|
||||
import com.aurora.gplayapi.helpers.AuthHelper
|
||||
import com.aurora.gplayapi.helpers.AuthValidator
|
||||
import com.aurora.store.AccountType
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.AuthState
|
||||
@@ -38,6 +37,7 @@ import com.aurora.store.data.event.BusEvent
|
||||
import com.aurora.store.data.model.InsecureAuth
|
||||
import com.aurora.store.data.network.HttpClient
|
||||
import com.aurora.store.data.providers.AccountProvider
|
||||
import com.aurora.store.data.providers.AuthProvider
|
||||
import com.aurora.store.data.providers.NativeDeviceInfoProvider
|
||||
import com.aurora.store.data.providers.SpoofProvider
|
||||
import com.aurora.store.util.AC2DMTask
|
||||
@@ -60,7 +60,8 @@ import javax.inject.Inject
|
||||
@SuppressLint("StaticFieldLeak") // false positive, see https://github.com/google/dagger/issues/3253
|
||||
class AuthViewModel @Inject constructor(
|
||||
@ApplicationContext private val context: Context,
|
||||
private val gson: Gson
|
||||
private val gson: Gson,
|
||||
private val authProvider: AuthProvider
|
||||
) : ViewModel() {
|
||||
|
||||
private val TAG = AuthViewModel::class.java.simpleName
|
||||
@@ -106,7 +107,7 @@ class AuthViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun buildAnonymousAuthData() {
|
||||
private fun buildAnonymousAuthData() {
|
||||
if (Preferences.getBoolean(context, Preferences.PREFERENCE_INSECURE_ANONYMOUS)) {
|
||||
buildInSecureAnonymousAuthData()
|
||||
} else {
|
||||
@@ -220,10 +221,7 @@ class AuthViewModel @Inject constructor(
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
supervisorScope {
|
||||
try {
|
||||
//Load & validate saved AuthData
|
||||
val savedAuthData = getSavedAuthData()
|
||||
|
||||
if (isValid(savedAuthData)) {
|
||||
if (authProvider.isSavedAuthDataValid()) {
|
||||
liveData.postValue(AuthState.Valid)
|
||||
} else {
|
||||
//Generate and validate new auth
|
||||
@@ -263,24 +261,6 @@ class AuthViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun getSavedAuthData(): AuthData {
|
||||
val rawAuth: String = Preferences.getString(context, PREFERENCE_AUTH_DATA)
|
||||
return if (rawAuth.isNotBlank())
|
||||
gson.fromJson(rawAuth, AuthData::class.java)
|
||||
else
|
||||
AuthData("", "")
|
||||
}
|
||||
|
||||
private fun isValid(authData: AuthData): Boolean {
|
||||
return try {
|
||||
AuthValidator(authData)
|
||||
.using(HttpClient.getPreferredClient(context))
|
||||
.isValid()
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun verifyAndSaveAuth(authData: AuthData, type: AccountType) {
|
||||
liveData.postValue(AuthState.Verifying)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user