diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index cfdbd24f..08b3fdc2 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -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) - } - } \ No newline at end of file