Skip to content

Commit

Permalink
Merge pull request #364 from metabrainz/login-fix
Browse files Browse the repository at this point in the history
Login issue fix
  • Loading branch information
akshaaatt authored Feb 15, 2024
2 parents c1241ac + 50312c3 commit 17dc8e6
Showing 1 changed file with 34 additions and 38 deletions.
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)
}
}
})
}
}

}

0 comments on commit 17dc8e6

Please sign in to comment.