fix: google account login - 2
This commit is contained in:
@@ -40,8 +40,16 @@ import com.aurora.store.viewmodel.auth.AuthViewModel
|
|||||||
|
|
||||||
private const val EMBEDDED_SETUP_URL = "https://accounts.google.com/EmbeddedSetup"
|
private const val EMBEDDED_SETUP_URL = "https://accounts.google.com/EmbeddedSetup"
|
||||||
private const val AUTH_TOKEN = "oauth_token"
|
private const val AUTH_TOKEN = "oauth_token"
|
||||||
private const val JS_PROFILE_EMAIL =
|
|
||||||
"(function() { return document.getElementById('profileIdentifier').innerHTML; })();"
|
// Google's EmbeddedSetup post-login page renders the account as e.g.
|
||||||
|
// <div data-profile-identifier data-email="user@gmail.com">...</div>;
|
||||||
|
// the legacy id="profileIdentifier" selector no longer matches.
|
||||||
|
private const val JS_PROFILE_EMAIL = """
|
||||||
|
(function() {
|
||||||
|
var el = document.querySelector('[data-profile-identifier][data-email]');
|
||||||
|
return el ? el.getAttribute('data-email') : null;
|
||||||
|
})();
|
||||||
|
"""
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun GoogleLoginScreen(
|
fun GoogleLoginScreen(
|
||||||
@@ -120,16 +128,12 @@ fun GoogleLoginScreen(
|
|||||||
|
|
||||||
webViewClient = object : WebViewClient() {
|
webViewClient = object : WebViewClient() {
|
||||||
override fun onPageFinished(view: WebView, url: String) {
|
override fun onPageFinished(view: WebView, url: String) {
|
||||||
val cookies = CookieManager.getInstance().getCookie(url)
|
val cookies = CookieManager.getInstance().getCookie(url) ?: return
|
||||||
if (cookies != null) {
|
val cookieMap = AC2DMUtil.parseCookieString(cookies)
|
||||||
val cookieMap = AC2DMUtil.parseCookieString(cookies)
|
val oauthToken = cookieMap[AUTH_TOKEN] ?: return
|
||||||
if (cookieMap.isNotEmpty() && cookieMap[AUTH_TOKEN] != null) {
|
view.evaluateJavascript(JS_PROFILE_EMAIL) { result ->
|
||||||
val oauthToken = cookieMap[AUTH_TOKEN]
|
val email = result.trim('"')
|
||||||
view.evaluateJavascript(JS_PROFILE_EMAIL) { result ->
|
viewModel.buildAuthData(view.context, email, oauthToken)
|
||||||
val email = result.replace("\"", "")
|
|
||||||
viewModel.buildAuthData(view.context, email, oauthToken)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,11 +27,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
|
|||||||
class AC2DMTask @Inject constructor(private val httpClient: HttpClient) {
|
class AC2DMTask @Inject constructor(private val httpClient: HttpClient) {
|
||||||
|
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun getAC2DMResponse(email: String?, oAuthToken: String?): Map<String, String> {
|
fun getAC2DMResponse(email: String, oAuthToken: String): Map<String, String> {
|
||||||
if (email == null || oAuthToken == null) {
|
|
||||||
return mapOf()
|
|
||||||
}
|
|
||||||
|
|
||||||
val params: MutableMap<String, Any> = hashMapOf()
|
val params: MutableMap<String, Any> = hashMapOf()
|
||||||
params["lang"] = Locale.getDefault().toString().replace("_", "-")
|
params["lang"] = Locale.getDefault().toString().replace("_", "-")
|
||||||
params["google_play_services_version"] = PLAY_SERVICES_VERSION_CODE
|
params["google_play_services_version"] = PLAY_SERVICES_VERSION_CODE
|
||||||
@@ -60,7 +56,7 @@ class AC2DMTask @Inject constructor(private val httpClient: HttpClient) {
|
|||||||
return if (response.isSuccessful) {
|
return if (response.isSuccessful) {
|
||||||
AC2DMUtil.parseResponse(String(response.responseBytes))
|
AC2DMUtil.parseResponse(String(response.responseBytes))
|
||||||
} else {
|
} else {
|
||||||
mapOf()
|
emptyMap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ import com.aurora.store.util.PackageUtil
|
|||||||
import com.aurora.store.util.Preferences
|
import com.aurora.store.util.Preferences
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||||
import java.net.ConnectException
|
|
||||||
import java.net.UnknownHostException
|
|
||||||
import javax.inject.Inject
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import java.net.ConnectException
|
||||||
|
import java.net.UnknownHostException
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
@HiltViewModel
|
@HiltViewModel
|
||||||
class AuthViewModel @Inject constructor(
|
class AuthViewModel @Inject constructor(
|
||||||
@@ -92,16 +92,19 @@ class AuthViewModel @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun buildAuthData(context: Context, email: String, oauthToken: String?) {
|
fun buildAuthData(context: Context, email: String, oauthToken: String) {
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
val response = aC2DMTask.getAC2DMResponse(email, oauthToken)
|
val response = aC2DMTask.getAC2DMResponse(email, oauthToken)
|
||||||
if (response.isNotEmpty()) {
|
if (response.isNotEmpty()) {
|
||||||
val aasToken = response["Token"]
|
val aasToken = response["Token"]
|
||||||
if (aasToken != null) {
|
if (aasToken != null) {
|
||||||
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, email)
|
val accountEmail = response["Email"]?.takeIf { it.isNotBlank() } ?: email
|
||||||
|
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, accountEmail)
|
||||||
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, aasToken)
|
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, aasToken)
|
||||||
AuroraApp.events.send(AuthEvent.GoogleLogin(true, email, aasToken))
|
AuroraApp.events.send(
|
||||||
|
AuthEvent.GoogleLogin(true, accountEmail, aasToken)
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, "")
|
Preferences.putString(context, Constants.ACCOUNT_EMAIL_PLAIN, "")
|
||||||
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, "")
|
Preferences.putString(context, Constants.ACCOUNT_AAS_PLAIN, "")
|
||||||
@@ -191,8 +194,8 @@ class AuthViewModel @Inject constructor(
|
|||||||
context,
|
context,
|
||||||
Preferences.PREFERENCE_AUTH_VIA_MICROG,
|
Preferences.PREFERENCE_AUTH_VIA_MICROG,
|
||||||
accountType == AccountType.GOOGLE &&
|
accountType == AccountType.GOOGLE &&
|
||||||
tokenType == AuthHelper.Token.AUTH &&
|
tokenType == AuthHelper.Token.AUTH &&
|
||||||
PackageUtil.hasSupportedMicroGVariant(context)
|
PackageUtil.hasSupportedMicroGVariant(context)
|
||||||
)
|
)
|
||||||
_authState.value = AuthState.SignedIn
|
_authState.value = AuthState.SignedIn
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user