fix(jellyfin): authenticate with the header Jellyfin 12 requires
Jellyfin 12.0 disables legacy authorization by default and its migration turns it off on upgraded servers (server PR #15559). Sign-in still worked, but every later request carried the token in X-Emby-Token, so the first library read answered 401 and the panel reported "Signed in, but library access failed" (#502). Send every authenticated request with Authorization: MediaBrowser ... Token and the install's persisted DeviceId, percent-encode the values the server URL-decodes, stream with ApiKey instead of api_key, and read libraries from /UserViews with a 404-only fallback to the obsolete /Users/{id}/Views route for 10.8. Both modern forms are read by every server since 10.8. A socket fixture now pins the header shape, the absence of legacy headers and query keys, the views route and its fallback. Live suites pass against 12.1.0 and 10.11.11, and a token issued by 10.11.11 keeps working after the same server is migrated to 12.1, so stored sessions need no re-login. cc
This commit is contained in:
6 files changed
+278
-32
No files matched your search
@@ -284,7 +284,7 @@ async fn verified_jellyfin_session(
|
||||
session: qbz_jellyfin::Session,
|
||||
mut cfg: MediaServerSettings,
|
||||
) -> Result<MediaServerSettings, String> {
|
||||
qbz_jellyfin::JellyfinClient::new(url, &session.access_token, &session.user_id)
|
||||
qbz_jellyfin::JellyfinClient::new(url, &session.access_token, &session.user_id, &cfg.device_id)
|
||||
.map_err(|e| e.to_string())?
|
||||
.music_libraries()
|
||||
.await
|
||||
@@ -740,10 +740,13 @@ mod tests {
|
||||
}
|
||||
let request = String::from_utf8_lossy(&bytes);
|
||||
assert!(request.lines().next().unwrap().contains(path));
|
||||
if path.contains("/Views") {
|
||||
assert!(request
|
||||
.to_ascii_lowercase()
|
||||
.contains("x-emby-token: test-token"));
|
||||
if path.contains("Views") {
|
||||
// Jellyfin 12 reads only the modern form (#502).
|
||||
let lower = request.to_ascii_lowercase();
|
||||
assert!(lower.contains("authorization: mediabrowser "));
|
||||
assert!(request.contains(r#"Token="test-token""#));
|
||||
assert!(request.contains(r#"DeviceId="test-device""#));
|
||||
assert!(!lower.contains("x-emby-token"));
|
||||
}
|
||||
let response = format!("HTTP/1.1 {status} Test\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",body.len());
|
||||
socket.write_all(response.as_bytes()).await.unwrap();
|
||||
@@ -761,19 +764,41 @@ mod tests {
|
||||
const SUB_NO: &str =
|
||||
r#"{"subsonic-response":{"status":"failed","error":{"code":40,"message":"denied"}}}"#;
|
||||
for kind in MediaServerKind::ALL {
|
||||
for mode in ["login-rejected", "library-rejected", "success"] {
|
||||
// "success-10.8": a server that predates `/UserViews` (10.9) must
|
||||
// still connect through the obsolete route. Self-hosted servers
|
||||
// often sit on stable distributions and lag behind releases.
|
||||
for mode in [
|
||||
"login-rejected",
|
||||
"library-rejected",
|
||||
"success",
|
||||
"success-10.8",
|
||||
] {
|
||||
let responses = match (kind, mode) {
|
||||
(MediaServerKind::Jellyfin, "login-rejected") => {
|
||||
vec![("/Users/AuthenticateByName", 401, "{}")]
|
||||
}
|
||||
(MediaServerKind::Jellyfin, "library-rejected") => vec![
|
||||
("/Users/AuthenticateByName", 200, JF_AUTH),
|
||||
("/Users/uid/Views", 401, "{}"),
|
||||
("/UserViews?userId=uid", 401, "{}"),
|
||||
],
|
||||
(MediaServerKind::Jellyfin, "success-10.8") => vec![
|
||||
("/Users/AuthenticateByName", 200, JF_AUTH),
|
||||
("/UserViews?userId=uid", 404, ""),
|
||||
(
|
||||
"/Users/uid/Views",
|
||||
200,
|
||||
r#"{"Items":[],"TotalRecordCount":0}"#,
|
||||
),
|
||||
(
|
||||
"/System/Info/Public",
|
||||
200,
|
||||
r#"{"ServerName":"test","Version":"10.8.13","Id":"server"}"#,
|
||||
),
|
||||
],
|
||||
(MediaServerKind::Jellyfin, _) => vec![
|
||||
("/Users/AuthenticateByName", 200, JF_AUTH),
|
||||
(
|
||||
"/Users/uid/Views",
|
||||
"/UserViews?userId=uid",
|
||||
200,
|
||||
r#"{"Items":[],"TotalRecordCount":0}"#,
|
||||
),
|
||||
|
||||
@@ -292,7 +292,7 @@ pub async fn sync_jellyfin(full: bool) -> Result<SyncReport, String> {
|
||||
async fn sync_jellyfin_inner(cfg: MediaServerSettings, full: bool) -> Result<SyncReport, String> {
|
||||
let kind = MediaServerKind::Jellyfin;
|
||||
|
||||
let client = qbz_jellyfin::JellyfinClient::new(&cfg.base_url, &cfg.token, &cfg.username)
|
||||
let client = qbz_jellyfin::JellyfinClient::new(&cfg.base_url, &cfg.token, &cfg.username, &cfg.device_id)
|
||||
.map_err(|e| e.to_string())?;
|
||||
// `username` holds the Jellyfin USER ID for the API's purposes — see
|
||||
// `connect_jellyfin`, which stores the id the auth response returned rather
|
||||
@@ -548,7 +548,7 @@ async fn jellyfin_quality_worker(epoch: u64) -> Result<(), String> {
|
||||
if !cfg.is_configured(MediaServerKind::Jellyfin) {
|
||||
return Ok(());
|
||||
}
|
||||
let client = qbz_jellyfin::JellyfinClient::new(&cfg.base_url, &cfg.token, &cfg.username)
|
||||
let client = qbz_jellyfin::JellyfinClient::new(&cfg.base_url, &cfg.token, &cfg.username, &cfg.device_id)
|
||||
.map_err(|error| error.to_string())?;
|
||||
let mut hydrated = 0usize;
|
||||
let mut batches = 0usize;
|
||||
|
||||
Reference in new issue
Block a user