Merge branch 'service-leak' into 'develop'

fix authorization

See merge request funkwhale/funkwhale-android!168
This commit is contained in:
Ryan Harg 2022-06-11 14:37:39 +00:00
commit 3f6e010ace
2 changed files with 31 additions and 12 deletions

View File

@ -76,8 +76,13 @@ fun Request.authorize(context: Context, oAuth: OAuth): Request {
val old = state.accessToken
val auth = ClientSecretPost(oAuth.state().clientSecret)
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) {
Log.i("Request.authorize()", "Accesstoken not renewed")
}
@ -88,6 +93,7 @@ fun Request.authorize(context: Context, oAuth: OAuth): Request {
done.complete(true)
}
done.await()
tokenService.dispose()
return@runBlocking this
}
}

View File

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