Skip to content

Commit

Permalink
[Jellyfin] Use User Auth endpoint that isn't bugged until 10.9
Browse files Browse the repository at this point in the history
  • Loading branch information
Schaka committed Feb 15, 2024
1 parent 2a329c9 commit bf5245f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.schaka.janitorr.jellyfin

import com.github.schaka.janitorr.jellyfin.api.User
import com.github.schaka.janitorr.jellyfin.library.*
import com.github.schaka.janitorr.jellyfin.library.items.ItemPage
import com.github.schaka.janitorr.jellyfin.library.items.MediaFolderItem
Expand All @@ -8,6 +9,9 @@ import feign.RequestLine

interface JellyfinClient {

@RequestLine("GET /Users")
fun listUsers(): List<User>

@RequestLine("GET /Library/VirtualFolders")
fun listLibraries(): List<VirtualFolderResponse>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.schaka.janitorr.jellyfin

import com.fasterxml.jackson.databind.ObjectMapper
import com.github.schaka.janitorr.jellyfin.api.User
import feign.Feign
import feign.jackson.JacksonDecoder
import feign.jackson.JacksonEncoder
Expand All @@ -9,9 +10,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.web.client.RestTemplateBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpHeaders.AUTHORIZATION
import org.springframework.http.HttpHeaders.CONTENT_TYPE
import org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import org.springframework.http.ResponseEntity
import org.springframework.util.LinkedMultiValueMap
import org.springframework.web.client.RestTemplate

@Configuration
Expand Down Expand Up @@ -44,17 +49,12 @@ class JellyfinClientConfig {
}

@Bean
fun jellyfinUserClient(properties: JellyfinProperties, mapper: ObjectMapper, builder: RestTemplateBuilder): JellyfinUserClient {
val rest = builder
.rootUri("${properties.url}/")
.build()
fun jellyfinUserClient(properties: JellyfinProperties, mapper: ObjectMapper, jellyfin: JellyfinClient): JellyfinUserClient {

// TODO: Do this via interceptor, if we ever get logged out?
val userInfo = rest.postForEntity("/Users/AuthenticateByName", object {
val Username = properties.username
val Pw = properties.password
}, Map::class.java)
val user = jellyfin.listUsers().filter { it.Name.lowercase() == properties.username.lowercase() }.firstOrNull()
?: throw IllegalArgumentException("User ${properties.username} not found")

val userInfo = getUserInfo(properties, user)
val accessToken = userInfo.body?.get("AccessToken")

log.info("Logged in to Jellyfin as {} {}", properties.username, accessToken)
Expand All @@ -69,4 +69,12 @@ class JellyfinClientConfig {
.target(JellyfinUserClient::class.java, properties.url)
}

private fun getUserInfo(properties: JellyfinProperties, user: User): ResponseEntity<Map<*, *>> {
val login = RestTemplate()
val map = LinkedMultiValueMap<String, Any>()
val headers = HttpHeaders()
headers.set(CONTENT_TYPE, APPLICATION_JSON_VALUE)
return login.postForEntity("${properties.url}/Users/${user.Id}/Authenticate?pw={password}", HttpEntity(map, headers), Map::class.java, properties.password)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.schaka.janitorr.jellyfin.api

data class User(
val Name: String,
val Id: String
)

0 comments on commit bf5245f

Please sign in to comment.