Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(all/jellyfin): fix login + small code touch-up #2323

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/all/jellyfin/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlinx-serialization'
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.serialization)
}

ext {
extName = 'Jellyfin'
pkgNameSuffix = 'all.jellyfin'
extClass = '.JellyfinFactory'
extVersionCode = 10
extVersionCode = 11
libVersion = '13'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,14 @@ data class LinkData(
val seriesId: String,
val seasonId: String,
)

@Serializable
data class LoginResponse(
val AccessToken: String,
val SessionInfo: SessionObject,
) {
@Serializable
data class SessionObject(
val UserId: String,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,42 @@ package eu.kanade.tachiyomi.animeextension.all.jellyfin

import android.content.SharedPreferences
import android.os.Build
import android.util.Log
import eu.kanade.tachiyomi.AppInfo
import eu.kanade.tachiyomi.network.POST
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlinx.serialization.json.put
import okhttp3.Headers
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import uy.kohesive.injekt.injectLazy

class JellyfinAuthenticator(
private val preferences: SharedPreferences,
private val baseUrl: String,
private val client: OkHttpClient,
) {

private val json: Json by injectLazy()

fun login(username: String, password: String): Pair<String?, String?> {
return try {
return runCatching {
val authResult = authenticateWithPassword(username, password)
?: throw Exception()
val key = authResult["AccessToken"]!!.jsonPrimitive.content
val userId = authResult["SessionInfo"]!!.jsonObject["UserId"]!!.jsonPrimitive.content
val key = authResult.AccessToken
val userId = authResult.SessionInfo.UserId
saveLogin(key, userId)
Pair(key, userId)
} catch (e: Exception) {
}.getOrElse {
Log.e(LOG_TAG, it.stackTraceToString())
Pair(null, null)
}
}

private fun authenticateWithPassword(username: String, password: String): JsonObject? {
private fun authenticateWithPassword(username: String, password: String): LoginResponse {
var deviceId = getPrefDeviceId()
if (deviceId.isNullOrEmpty()) {
deviceId = getRandomString()
Expand All @@ -44,13 +49,15 @@ class JellyfinAuthenticator(
"X-Emby-Authorization",
"MediaBrowser Client=\"$CLIENT\", Device=\"Android $androidVersion\", DeviceId=\"$deviceId\", Version=\"$aniyomiVersion\"",
)
val body = """
{"Username":"$username","Pw":"$password"}
""".trimIndent()
.toRequestBody("application/json".toMediaType())
val body = json.encodeToString(
buildJsonObject {
put("Username", username)
put("Pw", password)
},
).toRequestBody("application/json; charset=utf-8".toMediaType())

val request = POST("$baseUrl/Users/authenticatebyname", headers = authHeader, body = body)
val response = client.newCall(request).execute().body.string()
return response?.let { Json.decodeFromString<JsonObject>(it) }
return client.newCall(request).execute().parseAs()
}

private fun getRandomString(): String {
Expand All @@ -76,7 +83,15 @@ class JellyfinAuthenticator(
DEVICEID_KEY,
value,
).apply()
}

private const val DEVICEID_KEY = "device_id"
private const val CLIENT = "Aniyomi"
private inline fun <reified T> Response.parseAs(transform: (String) -> String = { it }): T {
val responseBody = use { transform(it.body.string()) }
return json.decodeFromString(responseBody)
}

companion object {
private const val DEVICEID_KEY = "device_id"
private const val CLIENT = "Aniyomi"
private const val LOG_TAG = "JellyfinAuthenticator"
}
}