mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-26 00:06:02 +00:00
fix(epic): hand Epic's terms step to the browser instead of looping the sign-in (#2421)
* fix(epic): hand Epic's terms step to the browser instead of looping the sign-in When Epic's redirect page answers corrective_action_required (privacy policy or EULA acceptance, e.g. for a newly created account), open Epic's account sign-in in the Android browser, where the step is shown, and tell the user to finish it there and retry, instead of failing verification and reloading the sign-in. * test(epic): remove live response identifiers from fixture * test(epic): cover browser step selection * fix(epic): ignore a stale sign-in page read after the login was reloaded * fix(epic): hold the browser handoff until the sign-in screen is started
This commit is contained in:
1 parent
33bdc75a99
commit
67b82ad9b4
37 files changed
+129
-5
No files matched your search
@@ -65,6 +65,7 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
val readsPage: Boolean get() = false
|
||||
val pageScript: String get() = "(function(){ try { return document.body.innerText; } catch(e){ return ''; } })()"
|
||||
fun pageReady(result: String): Boolean = true
|
||||
fun browserStep(page: String): String? = null
|
||||
fun forgetSignIn(done: () -> Unit) = done()
|
||||
fun pageFinished(view: WebView, url: String) {}
|
||||
}
|
||||
@@ -72,6 +73,7 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
private var store: Store? = null
|
||||
private var clientUrl: String? = null
|
||||
private var flow: Flow? = null
|
||||
private var pendingBrowserStep: String? = null
|
||||
private var webViewRef: WebView? = null
|
||||
private var popupWebView: WebView? = null
|
||||
private lateinit var contentHost: FrameLayout
|
||||
@@ -211,6 +213,7 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
|
||||
private fun reloadLogin() {
|
||||
dismissPopup(); clearError()
|
||||
pendingBrowserStep = null
|
||||
captured.set(false)
|
||||
val fresh = generateState()
|
||||
oauthState = fresh
|
||||
@@ -332,9 +335,13 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
|
||||
private fun readPage(view: WebView, f: Flow, left: Int) {
|
||||
view.evaluateJavascript(f.pageScript) { json ->
|
||||
if (flow !== f) return@evaluateJavascript
|
||||
val result = json ?: ""
|
||||
if (!f.pageReady(result) && left > 0) view.postDelayed({ readPage(view, f, left - 1) }, PAGE_RETRY_MS)
|
||||
else Thread({ settle(f.finish(this, view, result)) }, "store-login").start()
|
||||
else {
|
||||
val step = f.browserStep(result)
|
||||
if (step == null || !continueInBrowser(step)) Thread({ settle(f.finish(this, view, result)) }, "store-login").start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,6 +357,21 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
if (f.readsPage) readPage(view, f, PAGE_TRIES) else Thread({ settle(f.finish(this, view, url)) }, "store-login").start()
|
||||
}
|
||||
|
||||
private fun continueInBrowser(url: String): Boolean {
|
||||
if (isFinishing || isDestroyed) return false
|
||||
if (!lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.STARTED)) {
|
||||
pendingBrowserStep = url
|
||||
return true
|
||||
}
|
||||
val opened = runCatching { startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url))) }
|
||||
.onFailure { Log.w(TAG, "no browser took the sign-in step: ${it.javaClass.simpleName}") }
|
||||
.isSuccess
|
||||
if (!opened) return false
|
||||
titleText.text = getString(R.string.stores_login_title, store?.label ?: "")
|
||||
showError(getString(R.string.stores_login_continue_in_browser, store?.label ?: ""))
|
||||
return true
|
||||
}
|
||||
|
||||
private fun settle(problem: String?) {
|
||||
runOnUiThread {
|
||||
if (problem == null) { StoresState.refresh(this); finish(); return@runOnUiThread }
|
||||
@@ -367,6 +389,10 @@ class StoreLoginActivity : ComponentActivity() {
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
pendingBrowserStep?.let { step ->
|
||||
pendingBrowserStep = null
|
||||
if (!continueInBrowser(step)) reloadLogin()
|
||||
}
|
||||
if (clientUrl != null && !isFinishing) com.droiddeck.launcher.session.SessionService.holdForSignIn(this, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,9 @@ import android.webkit.WebView
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.stores.StoreLoginActivity
|
||||
import org.json.JSONObject
|
||||
import org.json.JSONTokener
|
||||
|
||||
internal class EpicLogin private constructor(
|
||||
internal class EpicLogin internal constructor(
|
||||
override val startUrl: String,
|
||||
override val userAgent: String,
|
||||
private val redirectPrefix: String,
|
||||
@@ -15,6 +16,12 @@ internal class EpicLogin private constructor(
|
||||
|
||||
override fun isRedirect(url: String): Boolean = url.startsWith(redirectPrefix)
|
||||
|
||||
override fun browserStep(page: String): String? {
|
||||
val action = correctiveAction(page) ?: return null
|
||||
Log.i(TAG, "the redirect page asks for a corrective action: $action")
|
||||
return ACCOUNT_SIGN_IN_URL
|
||||
}
|
||||
|
||||
override fun finish(activity: StoreLoginActivity, view: WebView, url: String): String? {
|
||||
val code = EpicNative.call("auth.codeFromPage", JSONObject().put("text", url))?.optString("code", "").orEmpty()
|
||||
if (code.isEmpty()) {
|
||||
@@ -38,6 +45,15 @@ internal class EpicLogin private constructor(
|
||||
|
||||
companion object {
|
||||
private const val TAG = "EpicLogin"
|
||||
private const val CORRECTIVE_ACTION_REQUIRED = "errors.com.epicgames.oauth.corrective_action_required"
|
||||
internal const val ACCOUNT_SIGN_IN_URL = "https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Faccount%2Fpersonal"
|
||||
|
||||
internal fun correctiveAction(page: String): String? {
|
||||
val text = runCatching { JSONTokener(page).nextValue() }.getOrNull() as? String ?: page
|
||||
val body = runCatching { JSONObject(text.trim()) }.getOrNull() ?: return null
|
||||
if (body.optString("errorCode") != CORRECTIVE_ACTION_REQUIRED) return null
|
||||
return body.optJSONObject("metadata")?.optString("correctiveAction").orEmpty().ifEmpty { "unknown" }
|
||||
}
|
||||
|
||||
fun create(): StoreLoginActivity.Flow? {
|
||||
val config = EpicNative.call("auth.config") ?: return null
|
||||
|
||||
@@ -1098,8 +1098,6 @@ internal fun GameSettingsMenus(host: MenuHost, game: com.droiddeck.launcher.fron
|
||||
|
||||
internal const val GAME_SETTINGS_MENU = "game-settings"
|
||||
|
||||
private const val EPIC_RESOLVE_URL = "https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Faccount%2Fpersonal"
|
||||
|
||||
@Composable
|
||||
private fun StoreLaunchCard(host: MenuHost, folder: java.io.File, store: com.droiddeck.launcher.stores.Store) {
|
||||
val appContext = androidx.compose.ui.platform.LocalContext.current.applicationContext
|
||||
@@ -1149,7 +1147,7 @@ private fun StoreLaunchCard(host: MenuHost, folder: java.io.File, store: com.dro
|
||||
MenuItem(stringResource(R.string.epic_resolve), checked = false) {
|
||||
host.open = null
|
||||
runCatching {
|
||||
appContext.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(EPIC_RESOLVE_URL)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK))
|
||||
appContext.startActivity(android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(com.droiddeck.launcher.stores.epic.EpicLogin.ACCOUNT_SIGN_IN_URL)).addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user