refactor(remote): hold who licenses a remote session as one licensing mode

This commit is contained in:
imSp4rky
2026-11-29 10:16:08 -07:00
parent d4af4c77f4
commit efd8ca65c7
18 changed files with 458 additions and 160 deletions
+101
View File
@@ -8,6 +8,7 @@ older peer.
from __future__ import annotations
from dataclasses import dataclass, replace
from enum import Enum, StrEnum
from typing import Any, NotRequired, TypedDict, TypeVar
@@ -163,6 +164,106 @@ class SessionCreated(TypedDict, total=False):
server_device: bool
class Licensor(StrEnum):
"""Who licenses a remote session."""
SERVER_CDM = "server_cdm"
"""The server's device licenses live, up to the licence cap."""
CLIENT_DEVICE = "client_device"
"""The client's own device licenses, and the service sees a stand-in for it."""
LENT_DEVICE = "lent_device"
"""The client's stand-in lent the server's device to the service, so the keys come only from a vault."""
NO_DEVICE = "no_device"
"""No server CDM, and no client device that the service can see."""
@dataclass(frozen=True)
class LicensingMode:
"""Who licenses a remote session, its licence cap and its access to the server vault, fixed at session create.
Only a client-device session, lent or not, can read the server vault. The wire keeps the older fields
``server_cdm``, ``server_cdm_max_height``, ``server_vault`` and ``server_device``.
"""
licensor: Licensor
max_height: int | None = None
server_vault: bool = False
def __post_init__(self) -> None:
if self.server_vault and not self.client_device:
raise ValueError(f"A {self.licensor} session cannot read the server vault")
@classmethod
def from_created(cls, reply: SessionCreated) -> LicensingMode:
"""The mode that the reply to a remote session creation gives."""
vault = bool(reply.get("server_vault"))
if reply.get("server_device"):
return cls(Licensor.LENT_DEVICE, reply.get("server_cdm_max_height"), vault)
if reply.get("server_cdm"):
return cls(Licensor.SERVER_CDM, reply.get("server_cdm_max_height"))
return cls(Licensor.CLIENT_DEVICE, reply.get("server_cdm_max_height"), vault)
def created_fields(self) -> SessionCreated:
"""The licensing fields of the reply to a remote session creation."""
return SessionCreated(
server_cdm=self.server_cdm,
server_cdm_max_height=self.max_height,
server_vault=self.server_vault,
server_device=self.server_device,
)
def lent(self) -> LicensingMode:
"""This client-device mode after the service took the server's device in its place."""
if self.licensor is not Licensor.CLIENT_DEVICE:
raise ValueError(f"A {self.licensor} session has no client device to lend the server's device")
return replace(self, licensor=Licensor.LENT_DEVICE)
@property
def server_cdm(self) -> bool:
"""True when the server's device can license live."""
return self.licensor is Licensor.SERVER_CDM
@property
def server_device(self) -> bool:
"""True when the service runs on the server's lent device."""
return self.licensor is Licensor.LENT_DEVICE
@property
def client_device(self) -> bool:
"""True when the service gets the stand-in for the client's device, lent or not."""
return self.licensor in (Licensor.CLIENT_DEVICE, Licensor.LENT_DEVICE)
@property
def client_identity(self) -> bool:
"""True when the device identity that the service builds is the client's own."""
return self.licensor is Licensor.CLIENT_DEVICE
@property
def vault_only(self) -> bool:
"""True when the server keys come only from a vault, so both DRM systems are candidates."""
return not self.server_cdm
@property
def keys_from_server(self) -> bool:
"""True when dl takes the content keys from the server and not from the challenge relay."""
return self.licensor in (Licensor.SERVER_CDM, Licensor.LENT_DEVICE)
@property
def reads_server_keys(self) -> bool:
"""True when the client asks the server for keys: its CDM licenses, or the API key reads the server vault."""
return self.server_cdm or self.server_vault
@property
def vault_first(self) -> bool:
"""True when the server vault answers before the client's own device licenses."""
return self.licensor is Licensor.CLIENT_DEVICE and self.server_vault
@property
def local_vaults_only(self) -> bool:
"""True for a lent-device session whose API key cannot read the server vault."""
return self.server_device and not self.server_vault
class TracksRequest(TypedDict):
title_id: str