|
|
@ -11,10 +11,7 @@ import androidx.lifecycle.lifecycleScope |
|
|
|
import audio.funkwhale.ffa.R |
|
|
|
import audio.funkwhale.ffa.R |
|
|
|
import audio.funkwhale.ffa.databinding.ActivityLoginBinding |
|
|
|
import audio.funkwhale.ffa.databinding.ActivityLoginBinding |
|
|
|
import audio.funkwhale.ffa.fragments.LoginDialog |
|
|
|
import audio.funkwhale.ffa.fragments.LoginDialog |
|
|
|
import audio.funkwhale.ffa.utils.AppContext |
|
|
|
import audio.funkwhale.ffa.utils.* |
|
|
|
import audio.funkwhale.ffa.utils.OAuth |
|
|
|
|
|
|
|
import audio.funkwhale.ffa.utils.Userinfo |
|
|
|
|
|
|
|
import audio.funkwhale.ffa.utils.log |
|
|
|
|
|
|
|
import com.github.kittinunf.fuel.Fuel |
|
|
|
import com.github.kittinunf.fuel.Fuel |
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult |
|
|
|
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult |
|
|
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf |
|
|
|
import com.github.kittinunf.fuel.gson.gsonDeserializerOf |
|
|
@ -22,6 +19,7 @@ import com.github.kittinunf.result.Result |
|
|
|
import com.preference.PowerPreference |
|
|
|
import com.preference.PowerPreference |
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
|
|
|
import kotlinx.coroutines.runBlocking |
|
|
|
import org.koin.java.KoinJavaComponent.inject |
|
|
|
import org.koin.java.KoinJavaComponent.inject |
|
|
|
|
|
|
|
|
|
|
|
data class FwCredentials(val token: String, val non_field_errors: List<String>?) |
|
|
|
data class FwCredentials(val token: String, val non_field_errors: List<String>?) |
|
|
@ -74,27 +72,27 @@ class LoginActivity : AppCompatActivity() { |
|
|
|
var hostname = hostname.text.toString().trim() |
|
|
|
var hostname = hostname.text.toString().trim() |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
if (hostname.isEmpty()) throw Exception(getString(R.string.login_error_hostname)) |
|
|
|
validateHostname(hostname, cleartext.isChecked)?.let { |
|
|
|
|
|
|
|
hostnameField.error = it |
|
|
|
Uri.parse(hostname).apply { |
|
|
|
return@setOnClickListener |
|
|
|
if (!cleartext.isChecked && scheme == "http") { |
|
|
|
|
|
|
|
throw Exception(getString(R.string.login_error_hostname_https)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (scheme == null) { |
|
|
|
val uri = Uri.parse(hostname) |
|
|
|
|
|
|
|
if (uri.scheme == null) { |
|
|
|
hostname = when (cleartext.isChecked) { |
|
|
|
hostname = when (cleartext.isChecked) { |
|
|
|
true -> "http://$hostname" |
|
|
|
true -> "http://$hostname" |
|
|
|
false -> "https://$hostname" |
|
|
|
false -> "https://$hostname" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hostnameField.error = "" |
|
|
|
hostnameField.error = "" |
|
|
|
|
|
|
|
|
|
|
|
when (anonymous.isChecked) { |
|
|
|
val fuelResult = when (anonymous.isChecked) { |
|
|
|
false -> authedLogin(hostname) |
|
|
|
false -> authedLogin(hostname) |
|
|
|
true -> anonymousLogin(hostname) |
|
|
|
true -> anonymousLogin(hostname) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hostnameField.error = mapFuelResultToError(fuelResult) |
|
|
|
} catch (e: Exception) { |
|
|
|
} catch (e: Exception) { |
|
|
|
val message = |
|
|
|
val message = |
|
|
|
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname) |
|
|
|
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname) |
|
|
@ -106,29 +104,49 @@ class LoginActivity : AppCompatActivity() { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun mapFuelResultToError(fuelResult: FuelResult) = when { |
|
|
|
|
|
|
|
fuelResult.httpStatus == 404 -> |
|
|
|
|
|
|
|
getString(R.string.login_error_funkwhale_not_found) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
!fuelResult.success -> |
|
|
|
|
|
|
|
getString(R.string.login_error, fuelResult.message) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else -> "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onConfigurationChanged(newConfig: Configuration) { |
|
|
|
override fun onConfigurationChanged(newConfig: Configuration) { |
|
|
|
super.onConfigurationChanged(newConfig) |
|
|
|
super.onConfigurationChanged(newConfig) |
|
|
|
|
|
|
|
|
|
|
|
limitContainerWidth() |
|
|
|
limitContainerWidth() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun authedLogin(hostname: String) { |
|
|
|
private fun validateHostname(hostname: String, cleartext: Boolean): String? { |
|
|
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname) |
|
|
|
if (hostname.isEmpty()) { |
|
|
|
|
|
|
|
return getString(R.string.login_error_hostname) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!cleartext && hostname.startsWith("http")) { |
|
|
|
|
|
|
|
return getString(R.string.login_error_hostname_https) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun authedLogin(hostname: String): FuelResult { |
|
|
|
oAuth.init(hostname) |
|
|
|
oAuth.init(hostname) |
|
|
|
oAuth.register { |
|
|
|
return oAuth.register { |
|
|
|
|
|
|
|
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).setString("hostname", hostname) |
|
|
|
oAuth.authorize(this) |
|
|
|
oAuth.authorize(this) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun anonymousLogin(hostname: String) { |
|
|
|
private fun anonymousLogin(hostname: String): FuelResult { |
|
|
|
val dialog = LoginDialog().apply { |
|
|
|
val dialog = LoginDialog().apply { |
|
|
|
show(supportFragmentManager, "LoginDialog") |
|
|
|
show(supportFragmentManager, "LoginDialog") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
lifecycleScope.launch(Main) { |
|
|
|
val uri = "$hostname/api/v1/tracks/" |
|
|
|
try { |
|
|
|
val (_, _, result) = runBlocking { |
|
|
|
val (_, _, result) = Fuel.get("$hostname/api/v1/tracks/") |
|
|
|
Fuel.get(uri).awaitObjectResponseResult(gsonDeserializerOf(FwCredentials::class.java)) |
|
|
|
.awaitObjectResponseResult(gsonDeserializerOf(FwCredentials::class.java)) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
when (result) { |
|
|
|
when (result) { |
|
|
|
is Result.Success -> { |
|
|
|
is Result.Success -> { |
|
|
@ -140,22 +158,12 @@ class LoginActivity : AppCompatActivity() { |
|
|
|
dialog.dismiss() |
|
|
|
dialog.dismiss() |
|
|
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java)) |
|
|
|
startActivity(Intent(this@LoginActivity, MainActivity::class.java)) |
|
|
|
finish() |
|
|
|
finish() |
|
|
|
|
|
|
|
return FuelResult.ok() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
is Result.Failure -> { |
|
|
|
is Result.Failure -> { |
|
|
|
dialog.dismiss() |
|
|
|
dialog.dismiss() |
|
|
|
|
|
|
|
return FuelResult.from(result) |
|
|
|
binding.hostnameField.error = result.error.localizedMessage |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
|
|
dialog.dismiss() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val message = |
|
|
|
|
|
|
|
if (e.message?.isEmpty() == true) getString(R.string.login_error_hostname) |
|
|
|
|
|
|
|
else e.message |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
binding.hostnameField.error = message |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|