GithubApi.kt fixes

This commit is contained in:
Antony 2022-08-22 00:22:54 +02:00
parent 798ff9a436
commit 9c6465c26e

View file

@ -2,7 +2,8 @@ package com.lagradost.cloudstream3.syncproviders.providers
import com.lagradost.cloudstream3.*
import com.lagradost.cloudstream3.AcraApplication.Companion.getKey
import com.lagradost.cloudstream3.subtitles.AbstractSubApi
import com.lagradost.cloudstream3.AcraApplication.Companion.setKey
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.syncproviders.AuthAPI
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPI
import com.lagradost.cloudstream3.syncproviders.InAppAuthAPIManager
@ -17,25 +18,54 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
override val createAccountUrl = "https://github.com/settings/tokens/new"
data class GithubOAuthEntity(
var repository: String,
var access_token: String,
var user: String,
var pass: String
)
private fun getAuthKey(): GithubOAuthEntity? {
return getKey(accountId, OpenSubtitlesApi.OPEN_SUBTITLES_USER_KEY)
companion object {
const val GITHUB_USER_KEY: String = "github_user" // user data like profile
var currentSession: GithubOAuthEntity? = null
}
private fun getAuthKey(): GithubOAuthEntity? {
return getKey(accountId, GITHUB_USER_KEY)
}
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
switchToNewAccount()
val username = data.username ?: throw ErrorLoadingException("Requires Username")
val password = data.password ?: throw ErrorLoadingException("Requires Password")
try {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(username, password))
registerAccount()
return true
} catch (e: Exception) {
logError(e)
switchToOldAccount()
}
switchToOldAccount()
return false
}
override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
val current = getAuthKey() ?: return null
return InAppAuthAPI.LoginData(username = current.user, current.pass)
}
override suspend fun initialize() {
currentSession = getAuthKey() ?: return // just in case the following fails
setKey(currentSession!!.user, currentSession!!.pass)
}
override fun logOut() {
AcraApplication.removeKey(accountId, GITHUB_USER_KEY)
removeAccountKeys()
currentSession = getAuthKey()
}
override fun loginInfo(): AuthAPI.LoginInfo? {
getAuthKey()?.let { user ->
return AuthAPI.LoginInfo(
profilePicture = null,
name = user.repository,
name = user.user,
accountIndex = accountIndex
)
}
return null
}
override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
val current = getAuthKey() ?: return null
return InAppAuthAPI.LoginData(username = current.repository, current.access_token)
}
}