Added Jellyfin Auth

This commit is contained in:
2026-02-13 22:18:05 -06:00
parent 5f07f6e611
commit e3f312ac52
5 changed files with 1292 additions and 98 deletions

View File

@@ -1,33 +1,82 @@
use serde::{Deserialize, Serialize};
use super::config::Config;
#[derive(Clone)]
pub struct JellyfinClient {
pub cfg: Config,
base_url: String,
client: reqwest::blocking::Client,
}
#[derive(Debug, Clone)]
pub struct JfItem {
pub id: String,
pub name: String,
pub item_type: String, // "Movie", "Series", etc.
pub image_tag: Option<String>,
pub struct LoginResult {
pub access_token: String,
pub user_id: Option<String>,
}
impl JellyfinClient {
pub fn new(cfg: Config) -> Self {
Self { cfg }
pub fn from_config(cfg: &Config) -> Self {
Self {
base_url: cfg.base_url.trim_end_matches('/').to_string(),
client: reqwest::blocking::Client::new(),
}
}
// Placeholder for now — next step will use reqwest and actually call Jellyfin.
pub fn test_connection_stub(&self) -> Result<String, String> {
Ok(format!("Configured for {}", self.cfg.base_url))
}
pub fn login_by_name(&self, username: &str, password: &str) -> Result<LoginResult, String> {
let url = format!("{}/Users/AuthenticateByName", self.base_url);
// Placeholder list so we can wire UI without network yet
pub fn list_movies_stub(&self) -> Vec<JfItem> {
vec![
JfItem { id: "1".into(), name: "Movie A".into(), item_type: "Movie".into(), image_tag: None },
JfItem { id: "2".into(), name: "Movie B".into(), item_type: "Movie".into(), image_tag: None },
]
// Jellyfin expects this format. :contentReference[oaicite:0]{index=0}
let body = AuthenticateUserByName {
Username: username,
Pw: password,
};
// This header identifies the client/device. :contentReference[oaicite:1]{index=1}
let auth_header = r#"MediaBrowser Client="cj-jftui", Device="kitty", DeviceId="cj-jftui", Version="0.1.0""#;
let resp = self
.client
.post(url)
.header("X-Emby-Authorization", auth_header)
.json(&body)
.send()
.map_err(|e| format!("request failed: {e}"))?;
if !resp.status().is_success() {
let status = resp.status();
let text = resp.text().unwrap_or_default();
return Err(format!("login failed: {status} {text}"));
}
let parsed: AuthenticationResult = resp
.json()
.map_err(|e| format!("bad response json: {e}"))?;
let token = parsed
.AccessToken
.ok_or_else(|| "missing AccessToken in response".to_string())?;
let user_id = parsed.User.and_then(|u| u.Id);
Ok(LoginResult {
access_token: token,
user_id,
})
}
}
#[derive(Serialize)]
struct AuthenticateUserByName<'a> {
Username: &'a str,
Pw: &'a str,
}
#[derive(Deserialize)]
struct AuthenticationResult {
AccessToken: Option<String>,
User: Option<UserDto>,
}
#[derive(Deserialize)]
struct UserDto {
Id: Option<String>,
}