From c997405906e9ed5f2e36df32f12ddae2f460a344 Mon Sep 17 00:00:00 2001 From: Ryan Harg Date: Mon, 9 Aug 2021 20:04:33 +0200 Subject: [PATCH] #79: Add log statements in authentication code --- .../ffa/activities/SplashActivity.kt | 3 +++ .../audio/funkwhale/ffa/utils/Extensions.kt | 25 +++++++------------ .../java/audio/funkwhale/ffa/utils/OAuth.kt | 8 +++--- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt b/app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt index a71d730..297d0bf 100644 --- a/app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt +++ b/app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt @@ -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) diff --git a/app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt b/app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt index 0e4ee2b..ab083aa 100644 --- a/app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt +++ b/app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt @@ -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 Int.onApi(block: () -> T, elseBlock: (() -> U)) { } } -fun Int.onApiForResult(block: () -> T, elseBlock: (() -> T)): T { - return if (Build.VERSION.SDK_INT >= this) { - block() - } else { - elseBlock() - } -} - -fun 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() state.performActionWithFreshTokens(oAuth.service(context), auth) { token, _, _ -> + if (token == old) { + Log.i("Request.authorize()", "Accesstoken not renewed") + } if (token != old && token != null) { state.save() } diff --git a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt index 25fcd32..6a7ca00 100644 --- a/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt +++ b/app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt @@ -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() }