Merge branch 'housekeeping/remove-unnessary-code' into 'develop'
Remove OAuth interface See merge request funkwhale/funkwhale-android!60
This commit is contained in:
commit
4a6d98ca14
|
@ -4,10 +4,13 @@ import android.content.Context
|
|||
import audio.funkwhale.ffa.playback.CacheDataSourceFactoryProvider
|
||||
import audio.funkwhale.ffa.playback.MediaSession
|
||||
import audio.funkwhale.ffa.utils.AuthorizationServiceFactory
|
||||
import audio.funkwhale.ffa.utils.DefaultOAuth
|
||||
import audio.funkwhale.ffa.utils.OAuth
|
||||
import com.google.android.exoplayer2.database.DatabaseProvider
|
||||
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.LeastRecentlyUsedCacheEvictor
|
||||
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor
|
||||
|
@ -22,6 +25,19 @@ fun exoplayerModule(context: Context) = module {
|
|||
ExoDatabaseProvider(context)
|
||||
}
|
||||
|
||||
single {
|
||||
val cacheDataSourceFactoryProvider = get<CacheDataSourceFactoryProvider>()
|
||||
DownloaderConstructorHelper(
|
||||
get(named("exoDownloadCache")), cacheDataSourceFactoryProvider.create(context)
|
||||
).run {
|
||||
DownloadManager(
|
||||
context,
|
||||
DefaultDownloadIndex(get(named("exoDatabase"))),
|
||||
DefaultDownloaderFactory(this)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
single {
|
||||
CacheDataSourceFactoryProvider(
|
||||
get(),
|
||||
|
@ -52,7 +68,6 @@ fun exoplayerModule(context: Context) = module {
|
|||
}
|
||||
|
||||
fun authModule() = module {
|
||||
single<OAuth> { DefaultOAuth(get()) }
|
||||
|
||||
single { OAuth(get()) }
|
||||
single { AuthorizationServiceFactory() }
|
||||
}
|
|
@ -7,7 +7,6 @@ import com.github.kittinunf.fuel.coroutines.awaitByteArrayResponseResult
|
|||
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
||||
import com.google.android.exoplayer2.offline.DownloadManager
|
||||
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.reflect.TypeToken
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
|
|
|
@ -5,7 +5,6 @@ import audio.funkwhale.ffa.utils.*
|
|||
import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
||||
import com.google.android.exoplayer2.offline.DownloadManager
|
||||
import com.google.android.exoplayer2.upstream.cache.Cache
|
||||
import com.google.android.exoplayer2.upstream.cache.SimpleCache
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.toList
|
||||
|
|
|
@ -6,7 +6,6 @@ import com.github.kittinunf.fuel.gson.gsonDeserializerOf
|
|||
import com.google.android.exoplayer2.offline.Download
|
||||
import com.google.android.exoplayer2.offline.DownloadManager
|
||||
import com.google.android.exoplayer2.upstream.cache.Cache
|
||||
import com.google.android.exoplayer2.upstream.cache.SimpleCache
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.toList
|
||||
|
|
|
@ -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 {
|
||||
|
||||
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 {
|
||||
|
||||
private val REDIRECT_URI =
|
||||
Uri.parse("urn:/audio.funkwhale.funkwhale-android/oauth/callback")
|
||||
}
|
||||
|
||||
data class App(val client_id: String, val client_secret: String)
|
||||
|
||||
override fun tryState(): AuthState? {
|
||||
fun tryState(): AuthState? {
|
||||
|
||||
val savedState = PowerPreference
|
||||
.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")
|
||||
|
||||
override fun isAuthorized(context: Context): Boolean {
|
||||
fun isAuthorized(context: Context): Boolean {
|
||||
val state = tryState()
|
||||
return if (state != null) {
|
||||
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 ->
|
||||
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(
|
||||
AuthorizationServiceConfiguration(
|
||||
Uri.parse("$hostname/authorize"),
|
||||
Uri.parse("$hostname/api/v1/oauth/token/"),
|
||||
Uri.parse("$hostname/api/v1/oauth/apps/")
|
||||
)
|
||||
)
|
||||
.also {
|
||||
it.save()
|
||||
}
|
||||
).also {
|
||||
it.save()
|
||||
}
|
||||
}
|
||||
|
||||
override fun service(context: Context): AuthorizationService =
|
||||
fun service(context: Context): AuthorizationService =
|
||||
authorizationServiceFactory.create(context)
|
||||
|
||||
override fun register(authState: AuthState?, callback: () -> Unit) {
|
||||
fun register(authState: AuthState? = null, callback: () -> Unit) {
|
||||
(authState ?: state()).authorizationServiceConfiguration?.let { config ->
|
||||
runBlocking {
|
||||
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)
|
||||
authorizationRequest()?.let { it ->
|
||||
val intent = authService.getAuthorizationRequestIntent(it)
|
||||
|
@ -191,7 +168,7 @@ class DefaultOAuth(private val authorizationServiceFactory: AuthorizationService
|
|||
}
|
||||
}
|
||||
|
||||
override fun exchange(
|
||||
fun exchange(
|
||||
context: Context,
|
||||
authorization: Intent,
|
||||
success: () -> Unit,
|
||||
|
|
|
@ -33,10 +33,10 @@ import strikt.assertions.isNotNull
|
|||
import strikt.assertions.isNull
|
||||
import strikt.assertions.isTrue
|
||||
|
||||
class DefaultOAuthTest {
|
||||
class OAuthTest {
|
||||
|
||||
@InjectMockKs
|
||||
private lateinit var oAuth: DefaultOAuth
|
||||
private lateinit var oAuth: OAuth
|
||||
|
||||
@MockK
|
||||
private lateinit var authServiceFactory: AuthorizationServiceFactory
|
Loading…
Reference in New Issue