Skip to content

Commit

Permalink
Use unique id per session for Jellyfin authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
voodoos committed May 14, 2024
1 parent 545b4dc commit ad9ebc7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions bin/db_worker/db_worker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ module Worker () = struct
let on_query (type a) (q : a query) : (a, error) Fut.result =
let open Fut.Result_syntax in
match q with
| Set_session_uuid s ->
let () = Data_source.Jellyfin_api.set_session_uuid s in
Fut.ok ()
| Add_servers l ->
let* idb = idb in
let open Fut.Syntax in
Expand Down
12 changes: 12 additions & 0 deletions bin/main.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
open Import
open Brr

(* The session uuid is stored to the local storage and used to identify a user
session. This is required by Jellyfin authorization scheme. *)
let session_uuid =
(* We never react to this var we could replace it *)
Brr_lwd_ui.Persistent.var_f ~key:"session_uuid" (fun () ->
Std.new_uuid_v4 () |> Uuidm.to_string)

let _ =
let uuid = Lwd.peek session_uuid in
Data_source.Jellyfin_api.set_session_uuid uuid;
Worker_client.query (Set_session_uuid (Lwd.peek session_uuid))

let fetch view i = Worker_client.(query (Get (view, i)))

module P = Player.Playback_controller (struct
Expand Down
8 changes: 7 additions & 1 deletion lib/data_source/jellyfin_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,19 @@ module System = struct
end
end

(* Forward declaration to be filled by the app *)
let session_uuid = ref None
let set_session_uuid s = session_uuid := Some s

let authorization ?token () =
let token =
match token with None -> "" | Some t -> Printf.sprintf ", Token=%S" t
in
let session_uuid = Option.value ~default:"" !session_uuid in
Printf.sprintf
"MediaBrowser Client=\"Ocamix\", Device=\"Firefox\", DeviceId=\"%s\", \
Version=\"0.1\"%s"
"abcdef" token
session_uuid token

let request (type pp p r) ?base_url ?token ?headers
(module Q : Query
Expand All @@ -317,6 +322,7 @@ let request (type pp p r) ?base_url ?token ?headers
|> Result.get_ok
in
let authorization = authorization ?token () in
Console.log [ authorization ];
let headers =
Headers.of_assoc ?init:headers
Jstr.
Expand Down
1 change: 1 addition & 0 deletions lib/db/db_worker_api.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type server = string * DS.connexion

module Queries = struct
type 'a query =
| Set_session_uuid : string -> unit query
| Add_servers : server list -> unit query
| Get_all : unit -> Api.Item.t list query
| Get_libraries : unit -> Stores.Items.t list query
Expand Down

0 comments on commit ad9ebc7

Please sign in to comment.