Browse Source

fix authorization

technical/upgrade-exoplayer
Ryan Harg 2 years ago
parent
commit
20ee27da21
  1. 8
      app/src/main/java/audio/funkwhale/ffa/utils/Extensions.kt
  2. 37
      app/src/main/java/audio/funkwhale/ffa/utils/OAuth.kt

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

@ -76,8 +76,13 @@ fun Request.authorize(context: Context, oAuth: OAuth): Request {
val old = state.accessToken val old = state.accessToken
val auth = ClientSecretPost(oAuth.state().clientSecret) val auth = ClientSecretPost(oAuth.state().clientSecret)
val done = CompletableDeferred<Boolean>() val done = CompletableDeferred<Boolean>()
val tokenService = oAuth.service(context)
state.performActionWithFreshTokens(oAuth.service(context), auth) { token, _, _ -> state.performActionWithFreshTokens(tokenService, auth) { token, _, e ->
if (e != null) {
Log.e("Request.authorize()", "performActionWithFreshToken failed: ${e}")
Log.e("Request.authorize()", Log.getStackTraceString(e))
}
if (token == old) { if (token == old) {
Log.i("Request.authorize()", "Accesstoken not renewed") Log.i("Request.authorize()", "Accesstoken not renewed")
} }
@ -88,6 +93,7 @@ fun Request.authorize(context: Context, oAuth: OAuth): Request {
done.complete(true) done.complete(true)
} }
done.await() done.await()
tokenService.dispose()
return@runBlocking this return@runBlocking this
} }
} }

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

@ -98,15 +98,22 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory
return if (state.refreshToken != null) { return if (state.refreshToken != null) {
val refreshRequest = state.createTokenRefreshRequest() val refreshRequest = state.createTokenRefreshRequest()
val auth = ClientSecretPost(state.clientSecret) val auth = ClientSecretPost(state.clientSecret)
val refreshService = service(context)
runBlocking { runBlocking {
service(context).performTokenRequest(refreshRequest, auth) { response, e -> refreshService.performTokenRequest(refreshRequest, auth) { response, e ->
state.apply { if (e != null) {
Log.i("OAuth", "applying new authState") Log.e("OAuth", "performTokenRequest failed: ${e}")
update(response, e) Log.e("OAuth", Log.getStackTraceString(e))
save() } else {
state.apply {
Log.i("OAuth", "applying new authState")
update(response, e)
save()
}
} }
} }
} }
refreshService.dispose()
true true
} else { } else {
false false
@ -202,17 +209,23 @@ class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory
AuthorizationResponse.fromIntent(authorization)?.let { AuthorizationResponse.fromIntent(authorization)?.let {
val auth = ClientSecretPost(state().clientSecret) val auth = ClientSecretPost(state().clientSecret)
val requestService = service(context)
service(context).performTokenRequest(it.createTokenExchangeRequest(), auth) { response, e ->
state requestService.performTokenRequest(it.createTokenExchangeRequest(), auth) { response, e ->
.apply { if (e != null) {
update(response, e) Log.e("FFA", "performTokenRequest failed: ${e}")
save() Log.e("FFA", Log.getStackTraceString(e))
} } else {
state.apply {
update(response, e)
save()
}
}
if (response != null) success() if (response != null) success()
else Log.e("FFA", "performTokenRequest() not successful") else Log.e("FFA", "performTokenRequest() not successful")
} }
requestService.dispose()
} }
} }
} }

Loading…
Cancel
Save