-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #364 from metabrainz/login-fix
Login issue fix
- Loading branch information
Showing
1 changed file
with
34 additions
and
38 deletions.
There are no files selected for viewing
72 changes: 34 additions & 38 deletions
72
app/src/main/java/org/listenbrainz/android/ui/screens/profile/ListenBrainzWebClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,53 @@ | ||
package org.listenbrainz.android.ui.screens.profile | ||
|
||
import android.net.Uri | ||
import android.webkit.CookieManager | ||
import android.webkit.WebView | ||
import android.webkit.WebViewClient | ||
import okhttp3.Call | ||
import okhttp3.Callback | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okio.IOException | ||
import org.jsoup.Jsoup | ||
import com.limurse.logger.Logger | ||
|
||
class ListenBrainzWebClient(private val setLBAuthToken: (String) -> Unit): WebViewClient() { | ||
class ListenBrainzWebClient(private val setLBAuthToken: (String) -> Unit) : WebViewClient() { | ||
|
||
private val client: OkHttpClient = OkHttpClient().newBuilder().build() | ||
private var attemptedSettingsNavigation = false | ||
|
||
override fun onPageFinished(view: WebView?, url: String?) { | ||
super.onPageFinished(view, url) | ||
val uri = Uri.parse(url) | ||
if (uri.host == "listenbrainz.org") { | ||
val cookie = CookieManager.getInstance().getCookie(url) | ||
if (cookie != null) { | ||
retrieveLBAuthToken(cookie) | ||
} | ||
Logger.d("ListenBrainzWebClient", "onPageFinished URL: $url") | ||
|
||
if (url == null) { | ||
Logger.d("ListenBrainzWebClient", "URL is null") | ||
return | ||
} | ||
} | ||
|
||
private fun retrieveLBAuthToken(cookie: String) { | ||
val request = Request | ||
.Builder() | ||
.addHeader("Cookie", cookie) | ||
.url("https://listenbrainz.org/profile") | ||
.build() | ||
val uri = Uri.parse(url) | ||
|
||
client.newCall(request).enqueue(object: Callback { | ||
override fun onFailure(call: Call, e: IOException) { | ||
e.printStackTrace() | ||
} | ||
Logger.d("ListenBrainzWebClient", "Host: ${uri.host}, Path: ${uri.path}") | ||
|
||
override fun onResponse(call: Call, response: Response) { | ||
response.use { | ||
if (response.isSuccessful) { | ||
val document = Jsoup.parse(response.body.string()) | ||
val element = document.getElementById("auth-token") | ||
val token = element?.attr("value") | ||
if (!token.isNullOrEmpty()) { | ||
setLBAuthToken(token) | ||
if (uri.host == "listenbrainz.org") { | ||
when { | ||
!attemptedSettingsNavigation -> { | ||
Logger.d("ListenBrainzWebClient", "Navigating to settings page") | ||
attemptedSettingsNavigation = true | ||
view?.loadUrl("https://listenbrainz.org/settings") | ||
} | ||
uri.path?.contains("/settings") == true -> { | ||
Logger.d("ListenBrainzWebClient", "On settings page, waiting to extract token...") | ||
view?.postDelayed({ | ||
view.evaluateJavascript( | ||
"(function() { return document.getElementById('auth-token') ? document.getElementById('auth-token').value : 'not found'; })();" | ||
) { value -> | ||
val token = value.removePrefix("\"").removeSuffix("\"") | ||
when { | ||
token.isNotEmpty() && token != "not found" -> { | ||
setLBAuthToken(token) | ||
} | ||
else -> { | ||
Logger.d("ListenBrainzWebClient", "Token not found or empty") | ||
} | ||
} | ||
} | ||
} | ||
}, 2000) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
} |