Merge branch 'housekeeping/remove-unnessary-code' into 'develop'

Remove OAuth interface

See merge request funkwhale/funkwhale-android!60
This commit is contained in:
Ryan Harg 2021-08-09 06:48:45 +00:00
commit 4a6d98ca14
6 changed files with 33 additions and 44 deletions

View File

@ -4,10 +4,13 @@ import android.content.Context
import audio.funkwhale.ffa.playback.CacheDataSourceFactoryProvider import audio.funkwhale.ffa.playback.CacheDataSourceFactoryProvider
import audio.funkwhale.ffa.playback.MediaSession import audio.funkwhale.ffa.playback.MediaSession
import audio.funkwhale.ffa.utils.AuthorizationServiceFactory import audio.funkwhale.ffa.utils.AuthorizationServiceFactory
import audio.funkwhale.ffa.utils.DefaultOAuth
import audio.funkwhale.ffa.utils.OAuth import audio.funkwhale.ffa.utils.OAuth
import com.google.android.exoplayer2.database.DatabaseProvider import com.google.android.exoplayer2.database.DatabaseProvider
import com.google.android.exoplayer2.database.ExoDatabaseProvider import com.google.android.exoplayer2.database.ExoDatabaseProvider
import com.google.android.exoplayer2.offline.DefaultDownloadIndex
import com.google.android.exoplayer2.offline.DefaultDownloaderFactory
import com.google.android.exoplayer2.offline.DownloadManager
import com.google.android.exoplayer2.offline.DownloaderConstructorHelper
import com.google.android.exoplayer2.upstream.cache.Cache import com.google.android.exoplayer2.upstream.cache.Cache
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor
@ -22,6 +25,19 @@ fun exoplayerModule(context: Context) = module {
ExoDatabaseProvider(context) ExoDatabaseProvider(context)
} }
single {
val cacheDataSourceFactoryProvider = get<CacheDataSourceFactoryProvider>()
DownloaderConstructorHelper(
get(named("exoDownloadCache")), cacheDataSourceFactoryProvider.create(context)
).run {
DownloadManager(
context,
DefaultDownloadIndex(get(named("exoDatabase"))),
DefaultDownloaderFactory(this)
)
}
}
single { single {
CacheDataSourceFactoryProvider( CacheDataSourceFactoryProvider(
get(), get(),
@ -52,7 +68,6 @@ fun exoplayerModule(context: Context) = module {
} }
fun authModule() = module { fun authModule() = module {
single<OAuth> { DefaultOAuth(get()) } single { OAuth(get()) }
single { AuthorizationServiceFactory() } single { AuthorizationServiceFactory() }
} }

View File

@ -7,7 +7,6 @@ import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.offline.DownloadManager
import com.google.android.exoplayer2.upstream.cache.Cache import com.google.android.exoplayer2.upstream.cache.Cache
import com.google.android.exoplayer2.upstream.cache.SimpleCache
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope

View File

@ -5,7 +5,6 @@ import audio.funkwhale.ffa.utils.*
import com.github.kittinunf.fuel.gson.gsonDeserializerOf import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.offline.DownloadManager
import com.google.android.exoplayer2.upstream.cache.Cache import com.google.android.exoplayer2.upstream.cache.Cache
import com.google.android.exoplayer2.upstream.cache.SimpleCache
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList

View File

@ -6,7 +6,6 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf
import com.google.android.exoplayer2.offline.Download import com.google.android.exoplayer2.offline.Download
import com.google.android.exoplayer2.offline.DownloadManager import com.google.android.exoplayer2.offline.DownloadManager
import com.google.android.exoplayer2.upstream.cache.Cache import com.google.android.exoplayer2.upstream.cache.Cache
import com.google.android.exoplayer2.upstream.cache.SimpleCache
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList

View File

@ -30,27 +30,6 @@ fun AuthState.save() {
} }
} }
interface OAuth {
fun exchange(context: Context, authorization: Intent, success: () -> Unit, error: () -> Unit)
fun init(hostname: String): AuthState
fun register(authState: AuthState? = null, callback: () -> Unit)
fun authorize(activity: Activity)
fun isAuthorized(context: Context): Boolean
fun tryRefreshAccessToken(context: Context): Boolean
fun tryState(): AuthState?
fun state(): AuthState
fun service(context: Context): AuthorizationService
}
class AuthorizationServiceFactory { class AuthorizationServiceFactory {
fun create(context: Context): AuthorizationService { fun create(context: Context): AuthorizationService {
@ -58,17 +37,16 @@ class AuthorizationServiceFactory {
} }
} }
class DefaultOAuth(private val authorizationServiceFactory: AuthorizationServiceFactory) : OAuth { class OAuth(private val authorizationServiceFactory: AuthorizationServiceFactory) {
companion object { companion object {
private val REDIRECT_URI = private val REDIRECT_URI =
Uri.parse("urn:/audio.funkwhale.funkwhale-android/oauth/callback") Uri.parse("urn:/audio.funkwhale.funkwhale-android/oauth/callback")
} }
data class App(val client_id: String, val client_secret: String) data class App(val client_id: String, val client_secret: String)
override fun tryState(): AuthState? { fun tryState(): AuthState? {
val savedState = PowerPreference val savedState = PowerPreference
.getFileByName(AppContext.PREFS_CREDENTIALS) .getFileByName(AppContext.PREFS_CREDENTIALS)
@ -81,10 +59,10 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
} }
} }
override fun state(): AuthState = fun state(): AuthState =
tryState() ?: throw IllegalStateException("Couldn't find saved state") tryState() ?: throw IllegalStateException("Couldn't find saved state")
override fun isAuthorized(context: Context): Boolean { fun isAuthorized(context: Context): Boolean {
val state = tryState() val state = tryState()
return if (state != null) { return if (state != null) {
state.isAuthorized || doTryRefreshAccessToken(state, context) state.isAuthorized || doTryRefreshAccessToken(state, context)
@ -95,7 +73,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
} }
} }
override fun tryRefreshAccessToken(context: Context): Boolean { fun tryRefreshAccessToken(context: Context): Boolean {
tryState()?.let { state -> tryState()?.let { state ->
return doTryRefreshAccessToken(state, context) return doTryRefreshAccessToken(state, context)
} }
@ -124,23 +102,22 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
} }
} }
override fun init(hostname: String): AuthState { fun init(hostname: String): AuthState {
return AuthState( return AuthState(
AuthorizationServiceConfiguration( AuthorizationServiceConfiguration(
Uri.parse("$hostname/authorize"), Uri.parse("$hostname/authorize"),
Uri.parse("$hostname/api/v1/oauth/token/"), Uri.parse("$hostname/api/v1/oauth/token/"),
Uri.parse("$hostname/api/v1/oauth/apps/") Uri.parse("$hostname/api/v1/oauth/apps/")
) )
) ).also {
.also { it.save()
it.save() }
}
} }
override fun service(context: Context): AuthorizationService = fun service(context: Context): AuthorizationService =
authorizationServiceFactory.create(context) authorizationServiceFactory.create(context)
override fun register(authState: AuthState?, callback: () -> Unit) { fun register(authState: AuthState? = null, callback: () -> Unit) {
(authState ?: state()).authorizationServiceConfiguration?.let { config -> (authState ?: state()).authorizationServiceConfiguration?.let { config ->
runBlocking { runBlocking {
val (_, _, result: Result<App, FuelError>) = Fuel.post(config.registrationEndpoint.toString()) val (_, _, result: Result<App, FuelError>) = Fuel.post(config.registrationEndpoint.toString())
@ -183,7 +160,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
) )
} }
override fun authorize(activity: Activity) { fun authorize(activity: Activity) {
val authService = service(activity) val authService = service(activity)
authorizationRequest()?.let { it -> authorizationRequest()?.let { it ->
val intent = authService.getAuthorizationRequestIntent(it) val intent = authService.getAuthorizationRequestIntent(it)
@ -191,7 +168,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
} }
} }
override fun exchange( fun exchange(
context: Context, context: Context,
authorization: Intent, authorization: Intent,
success: () -> Unit, success: () -> Unit,

View File

@ -33,10 +33,10 @@ import strikt.assertions.isNotNull
import strikt.assertions.isNull import strikt.assertions.isNull
import strikt.assertions.isTrue import strikt.assertions.isTrue
class DefaultOAuthTest { class OAuthTest {
@InjectMockKs @InjectMockKs
private lateinit var oAuth: DefaultOAuth private lateinit var oAuth: OAuth
@MockK @MockK
private lateinit var authServiceFactory: AuthorizationServiceFactory private lateinit var authServiceFactory: AuthorizationServiceFactory