Merge branch 'bugfix/79-add-auth-logging' into 'develop'

#79: Add log statements in authentication code

See merge request funkwhale/funkwhale-android!62
This commit is contained in:
Ryan Harg 2021-08-09 18:12:48 +00:00
commit ffcbabedce
3 changed files with 17 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package audio.funkwhale.ffa.activities
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import audio.funkwhale.ffa.FFA
import audio.funkwhale.ffa.utils.*
@ -20,12 +21,14 @@ class SplashActivity : AppCompatActivity() {
when (oAuth.isAuthorized(this@SplashActivity) || Settings.isAnonymous()) {
true -> Intent(this@SplashActivity, MainActivity::class.java)
.apply {
Log.i("SplashActivity", "Authorized, redirecting to MainActivity")
flags = Intent.FLAG_ACTIVITY_NO_ANIMATION
startActivity(this)
}
false -> Intent(this@SplashActivity, LoginActivity::class.java)
.apply {
Log.i("SplashActivity", "Not authorized, redirecting to LoginActivity")
FFA.get().deleteAllData(this@SplashActivity)
flags = Intent.FLAG_ACTIVITY_NO_ANIMATION
startActivity(this)

View File

@ -2,6 +2,8 @@ package audio.funkwhale.ffa.utils
import android.content.Context
import android.os.Build
import android.os.SystemClock
import android.util.Log
import androidx.fragment.app.Fragment
import audio.funkwhale.ffa.R
import audio.funkwhale.ffa.fragments.BrowseFragment
@ -55,22 +57,6 @@ fun <T, U> Int.onApi(block: () -> T, elseBlock: (() -> U)) {
}
}
fun <T> Int.onApiForResult(block: () -> T, elseBlock: (() -> T)): T {
return if (Build.VERSION.SDK_INT >= this) {
block()
} else {
elseBlock()
}
}
fun <T> T.applyOnApi(api: Int, block: T.() -> T): T {
return if (Build.VERSION.SDK_INT >= api) {
block()
} else {
this
}
}
fun Picasso.maybeLoad(url: String?): RequestCreator {
return if (url == null) load(R.drawable.cover)
else load(url)
@ -81,11 +67,18 @@ fun Request.authorize(context: Context, oAuth: OAuth): Request {
this@authorize.apply {
if (!Settings.isAnonymous()) {
oAuth.state().let { state ->
val now = SystemClock.currentThreadTimeMillis()
state.accessTokenExpirationTime?.let {
Log.i("Request.authorize()", "Accesstoken expiration: ${it - now}")
}
val old = state.accessToken
val auth = ClientSecretPost(oAuth.state().clientSecret)
val done = CompletableDeferred<Boolean>()
state.performActionWithFreshTokens(oAuth.service(context), auth) { token, _, _ ->
if (token == old) {
Log.i("Request.authorize()", "Accesstoken not renewed")
}
if (token != old && token != null) {
state.save()
}

View File

@ -4,6 +4,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.coroutines.awaitObjectResponseResult
@ -25,8 +26,7 @@ import net.openid.appauth.ResponseTypeValues
fun AuthState.save() {
PowerPreference.getFileByName(AppContext.PREFS_CREDENTIALS).apply {
val value = jsonSerializeString()
setString("state", value)
setString("state", jsonSerializeString())
}
}
@ -84,12 +84,14 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory
state: AuthState,
context: Context
): Boolean {
if (state.needsTokenRefresh && state.refreshToken != null) {
if (state.needsTokenRefresh.also { it.log("needsTokenRefresh()") }
&& state.refreshToken != null) {
val refreshRequest = state.createTokenRefreshRequest()
val auth = ClientSecretPost(state.clientSecret)
runBlocking {
service(context).performTokenRequest(refreshRequest, auth) { response, e ->
state.apply {
Log.i("OAuth", "applying new autState")
update(response, e)
save()
}