Browse Source

#79: Add log statements in authentication code

enhancement/speed-up-pipelines
Ryan Harg 3 years ago
parent
commit
c997405906
No known key found for this signature in database
GPG Key ID: 89106F3A84E6958C
  1. 3
      app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt
  2. 25
      app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt
  3. 8
      app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt

3
app/src/main/java/audio/funkwhale/ffa/activities/SplashActivity.kt

@ -3,6 +3,7 @@ package audio.funkwhale.ffa.activities @@ -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() { @@ -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)

25
app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt

@ -2,6 +2,8 @@ package audio.funkwhale.ffa.utils @@ -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)) { @@ -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 { @@ -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()
}

8
app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt

@ -4,6 +4,7 @@ import android.app.Activity @@ -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 @@ -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 @@ -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()
}

Loading…
Cancel
Save