1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
"""Session represents one connection to Nextcloud. All related stuff for these live here."""
import builtins
import pathlib
import re
import typing
from abc import ABC, abstractmethod
from base64 import b64encode
from dataclasses import dataclass
from enum import IntEnum
from json import loads
from os import environ
from httpx import AsyncClient, Client, Headers, Limits, ReadTimeout, Request, Response
from httpx import __version__ as httpx_version
from starlette.requests import HTTPConnection
from . import options
from ._exceptions import (
NextcloudException,
NextcloudExceptionNotFound,
NextcloudExceptionNotModified,
check_error,
)
from ._misc import get_username_secret_from_headers
class OCSRespond(IntEnum):
"""Special Nextcloud respond statuses for OCS calls."""
RESPOND_SERVER_ERROR = 996
RESPOND_UNAUTHORISED = 997
RESPOND_NOT_FOUND = 998
RESPOND_UNKNOWN_ERROR = 999
class ServerVersion(typing.TypedDict):
"""Nextcloud version information."""
major: int
"""Major version"""
minor: int
"""Minor version"""
micro: int
"""Micro version"""
string: str
"""Full version in string format"""
extended_support: bool
"""Indicates if the subscription has extended support"""
@dataclass
class RuntimeOptions:
xdebug_session: str
timeout: int | None
timeout_dav: int | None
_nc_cert: str | bool
upload_chunk_v2: bool
def __init__(self, **kwargs):
self.xdebug_session = kwargs.get("xdebug_session", options.XDEBUG_SESSION)
self.timeout = kwargs.get("npa_timeout", options.NPA_TIMEOUT)
self.timeout_dav = kwargs.get("npa_timeout_dav", options.NPA_TIMEOUT_DAV)
self._nc_cert = kwargs.get("npa_nc_cert", options.NPA_NC_CERT)
self.upload_chunk_v2 = kwargs.get("chunked_upload_v2", options.CHUNKED_UPLOAD_V2)
@property
def nc_cert(self) -> str | bool:
return self._nc_cert
@dataclass
class BasicConfig:
endpoint: str
dav_endpoint: str
dav_url_suffix: str
options: RuntimeOptions
def __init__(self, **kwargs):
full_nc_url = self._get_config_value("nextcloud_url", **kwargs)
self.endpoint = full_nc_url.removesuffix("/index.php").removesuffix("/")
self.dav_url_suffix = self._get_config_value("dav_url_suffix", raise_not_found=False, **kwargs)
if not self.dav_url_suffix:
self.dav_url_suffix = "remote.php/dav"
self.dav_url_suffix = "/" + self.dav_url_suffix.strip("/")
self.dav_endpoint = self.endpoint + self.dav_url_suffix
self.options = RuntimeOptions(**kwargs)
@staticmethod
def _get_config_value(value_name: str, raise_not_found=True, **kwargs):
if value_name in kwargs:
return kwargs[value_name]
value_name_upper = value_name.upper()
if value_name_upper in environ:
return environ[value_name_upper]
if raise_not_found:
raise ValueError(f"`{value_name}` is not found.")
return None
@dataclass
class Config(BasicConfig):
auth: tuple[str, str] = ("", "")
def __init__(self, **kwargs):
super().__init__(**kwargs)
nc_auth_user = self._get_config_value("nc_auth_user", raise_not_found=False, **kwargs)
nc_auth_pass = self._get_config_value("nc_auth_pass", raise_not_found=False, **kwargs)
if nc_auth_user and nc_auth_pass:
self.auth = (nc_auth_user, nc_auth_pass)
@dataclass
class AppConfig(BasicConfig):
"""Application configuration."""
aa_version: str
"""AppAPI version"""
app_name: str
"""Application ID"""
app_version: str
"""Application version"""
app_secret: str
"""Application authentication secret"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.aa_version = self._get_config_value("aa_version", raise_not_found=False, **kwargs)
if not self.aa_version:
self.aa_version = "2.2.0"
self.app_name = self._get_config_value("app_id", **kwargs)
self.app_version = self._get_config_value("app_version", **kwargs)
self.app_secret = self._get_config_value("app_secret", **kwargs)
class NcSessionBase(ABC):
adapter: AsyncClient | Client
adapter_dav: AsyncClient | Client
cfg: BasicConfig
custom_headers: dict
response_headers: Headers
_user: str
_capabilities: dict
@abstractmethod
def __init__(self, **kwargs):
self._capabilities = {}
self._user = kwargs.get("user", "")
self.custom_headers = kwargs.get("headers", {})
self.limits = Limits(max_keepalive_connections=20, max_connections=20, keepalive_expiry=60.0)
self.init_adapter()
self.init_adapter_dav()
self.response_headers = Headers()
self._ocs_regexp = re.compile(r"/ocs/v[12]\.php/|/apps/groupfolders/")
def init_adapter(self, restart=False) -> None:
if getattr(self, "adapter", None) is None or restart:
self.adapter = self._create_adapter()
self.adapter.headers.update({"OCS-APIRequest": "true"})
if self.custom_headers:
self.adapter.headers.update(self.custom_headers)
if options.XDEBUG_SESSION:
self.adapter.cookies.set("XDEBUG_SESSION", options.XDEBUG_SESSION)
self._capabilities = {}
def init_adapter_dav(self, restart=False) -> None:
if getattr(self, "adapter_dav", None) is None or restart:
self.adapter_dav = self._create_adapter(dav=True)
if self.custom_headers:
self.adapter_dav.headers.update(self.custom_headers)
if options.XDEBUG_SESSION:
self.adapter_dav.cookies.set("XDEBUG_SESSION", options.XDEBUG_SESSION)
@abstractmethod
def _create_adapter(self, dav: bool = False) -> AsyncClient | Client:
pass # pragma: no cover
@property
def ae_url(self) -> str:
"""Return base url for the AppAPI endpoints."""
return "/ocs/v1.php/apps/app_api/api/v1"
@property
def ae_url_v2(self) -> str:
"""Return base url for the AppAPI endpoints(version 2)."""
return "/ocs/v1.php/apps/app_api/api/v2"
class NcSessionBasic(NcSessionBase, ABC):
adapter: Client
adapter_dav: Client
def ocs(
self,
method: str,
path: str,
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
files: dict | None = None,
**kwargs,
):
self.init_adapter()
info = f"request: {method} {path}"
nested_req = kwargs.pop("nested_req", False)
try:
response = self.adapter.request(
method, path, content=content, json=json, params=params, files=files, **kwargs
)
except ReadTimeout:
raise NextcloudException(408, info=info) from None
check_error(response, info)
if response.status_code == 204: # NO_CONTENT
return []
response_data = loads(response.text)
if response_type == "json":
return response_data
ocs_meta = response_data["ocs"]["meta"]
if ocs_meta["status"] != "ok":
if (
not nested_req
and ocs_meta["statuscode"] == 403
and str(ocs_meta["message"]).lower().find("password confirmation is required") != -1
):
self.adapter.close()
self.init_adapter(restart=True)
return self.ocs(method, path, **kwargs, content=content, json=json, params=params, nested_req=True)
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
if ocs_meta["statuscode"] == 304:
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info)
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)
return response_data["ocs"]["data"]
def update_server_info(self) -> None:
self._capabilities = self.ocs("GET", "/ocs/v1.php/cloud/capabilities")
@property
def capabilities(self) -> dict:
if not self._capabilities:
self.update_server_info()
return self._capabilities["capabilities"]
@property
def nc_version(self) -> ServerVersion:
if not self._capabilities:
self.update_server_info()
v = self._capabilities["version"]
return ServerVersion(
major=v["major"],
minor=v["minor"],
micro=v["micro"],
string=v["string"],
extended_support=v["extendedSupport"],
)
@property
def user(self) -> str:
"""Current user ID. Can be different from the login name."""
if isinstance(self, NcSession) and not self._user: # do not trigger for NextcloudApp
self._user = self.ocs("GET", "/ocs/v1.php/cloud/user")["id"]
return self._user
def set_user(self, user_id: str) -> None:
self._user = user_id
def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs):
if isinstance(fp, str | pathlib.Path):
with builtins.open(fp, "wb") as f:
self.download2fp(url_path, f, dav, **kwargs)
elif hasattr(fp, "write"):
self.download2fp(url_path, fp, dav, **kwargs)
else:
raise TypeError("`fp` must be a path to file or an object with `write` method.")
def _get_adapter_kwargs(self, dav: bool) -> dict[str, typing.Any]:
if dav:
return {
"base_url": self.cfg.dav_endpoint,
"timeout": self.cfg.options.timeout_dav,
"event_hooks": {"request": [], "response": [self._response_event]},
}
return {
"base_url": self.cfg.endpoint,
"timeout": self.cfg.options.timeout,
"event_hooks": {"request": [self._request_event_ocs], "response": [self._response_event]},
}
def _request_event_ocs(self, request: Request) -> None:
str_url = str(request.url)
if re.search(self._ocs_regexp, str_url) is not None: # this is OCS call
request.url = request.url.copy_merge_params({"format": "json"})
request.headers["Accept"] = "application/json"
def _response_event(self, response: Response) -> None:
str_url = str(response.request.url)
# we do not want ResponseHeaders for those two endpoints, as call to them can occur during DAV calls.
for i in ("/ocs/v1.php/cloud/capabilities?format=json", "/ocs/v1.php/cloud/user?format=json"):
if str_url.endswith(i):
return
self.response_headers = response.headers
def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
adapter = self.adapter_dav if dav else self.adapter
with adapter.stream("GET", url_path, params=params, headers=kwargs.get("headers")) as response:
check_error(response)
for data_chunk in response.iter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
fp.write(data_chunk)
class AsyncNcSessionBasic(NcSessionBase, ABC):
adapter: AsyncClient
adapter_dav: AsyncClient
async def ocs(
self,
method: str,
path: str,
*,
content: bytes | str | typing.Iterable[bytes] | typing.AsyncIterable[bytes] | None = None,
json: dict | list | None = None,
response_type: str | None = None,
params: dict | None = None,
files: dict | None = None,
**kwargs,
):
self.init_adapter()
info = f"request: {method} {path}"
nested_req = kwargs.pop("nested_req", False)
try:
response = await self.adapter.request(
method, path, content=content, json=json, params=params, files=files, **kwargs
)
except ReadTimeout:
raise NextcloudException(408, info=info) from None
check_error(response, info)
if response.status_code == 204: # NO_CONTENT
return []
response_data = loads(response.text)
if response_type == "json":
return response_data
ocs_meta = response_data["ocs"]["meta"]
if ocs_meta["status"] != "ok":
if (
not nested_req
and ocs_meta["statuscode"] == 403
and str(ocs_meta["message"]).lower().find("password confirmation is required") != -1
):
await self.adapter.aclose()
self.init_adapter(restart=True)
return await self.ocs(
method, path, **kwargs, content=content, json=json, params=params, nested_req=True
)
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
if ocs_meta["statuscode"] == 304:
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info)
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)
return response_data["ocs"]["data"]
async def update_server_info(self) -> None:
self._capabilities = await self.ocs("GET", "/ocs/v1.php/cloud/capabilities")
@property
async def capabilities(self) -> dict:
if not self._capabilities:
await self.update_server_info()
return self._capabilities["capabilities"]
@property
async def nc_version(self) -> ServerVersion:
if not self._capabilities:
await self.update_server_info()
v = self._capabilities["version"]
return ServerVersion(
major=v["major"],
minor=v["minor"],
micro=v["micro"],
string=v["string"],
extended_support=v["extendedSupport"],
)
@property
async def user(self) -> str:
"""Current user ID. Can be different from the login name."""
if isinstance(self, AsyncNcSession) and not self._user: # do not trigger for NextcloudApp
self._user = (await self.ocs("GET", "/ocs/v1.php/cloud/user"))["id"]
return self._user
def set_user(self, user: str) -> None:
self._user = user
async def download2stream(self, url_path: str, fp, dav: bool = False, **kwargs):
if isinstance(fp, str | pathlib.Path):
with builtins.open(fp, "wb") as f:
await self.download2fp(url_path, f, dav, **kwargs)
elif hasattr(fp, "write"):
await self.download2fp(url_path, fp, dav, **kwargs)
else:
raise TypeError("`fp` must be a path to file or an object with `write` method.")
def _get_adapter_kwargs(self, dav: bool) -> dict[str, typing.Any]:
if dav:
return {
"base_url": self.cfg.dav_endpoint,
"timeout": self.cfg.options.timeout_dav,
"event_hooks": {"request": [], "response": [self._response_event]},
}
return {
"base_url": self.cfg.endpoint,
"timeout": self.cfg.options.timeout,
"event_hooks": {"request": [self._request_event_ocs], "response": [self._response_event]},
}
async def _request_event_ocs(self, request: Request) -> None:
str_url = str(request.url)
if re.search(self._ocs_regexp, str_url) is not None: # this is OCS call
request.url = request.url.copy_merge_params({"format": "json"})
request.headers["Accept"] = "application/json"
async def _response_event(self, response: Response) -> None:
str_url = str(response.request.url)
# we do not want ResponseHeaders for those two endpoints, as call to them can occur during DAV calls.
for i in ("/ocs/v1.php/cloud/capabilities?format=json", "/ocs/v1.php/cloud/user?format=json"):
if str_url.endswith(i):
return
self.response_headers = response.headers
async def download2fp(self, url_path: str, fp, dav: bool, params=None, **kwargs):
adapter = self.adapter_dav if dav else self.adapter
async with adapter.stream("GET", url_path, params=params, headers=kwargs.get("headers")) as response:
check_error(response)
async for data_chunk in response.aiter_raw(chunk_size=kwargs.get("chunk_size", 5 * 1024 * 1024)):
fp.write(data_chunk)
class NcSession(NcSessionBasic):
cfg: Config
def __init__(self, **kwargs):
self.cfg = Config(**kwargs)
super().__init__()
def _create_adapter(self, dav: bool = False) -> AsyncClient | Client:
return Client(
follow_redirects=True,
limits=self.limits,
verify=self.cfg.options.nc_cert,
**self._get_adapter_kwargs(dav),
auth=self.cfg.auth,
)
class AsyncNcSession(AsyncNcSessionBasic):
cfg: Config
def __init__(self, **kwargs):
self.cfg = Config(**kwargs)
super().__init__()
def _create_adapter(self, dav: bool = False) -> AsyncClient | Client:
return AsyncClient(
follow_redirects=True,
limits=self.limits,
verify=self.cfg.options.nc_cert,
**self._get_adapter_kwargs(dav),
auth=self.cfg.auth,
)
class NcSessionAppBasic(ABC):
cfg: AppConfig
_user: str
adapter: AsyncClient | Client
adapter_dav: AsyncClient | Client
def __init__(self, **kwargs):
self.cfg = AppConfig(**kwargs)
super().__init__(**kwargs)
def sign_check(self, request: HTTPConnection) -> str:
headers = {
"EX-APP-ID": request.headers.get("EX-APP-ID", ""),
"EX-APP-VERSION": request.headers.get("EX-APP-VERSION", ""),
"AUTHORIZATION-APP-API": request.headers.get("AUTHORIZATION-APP-API", ""),
}
empty_headers = [k for k, v in headers.items() if not v]
if empty_headers:
raise ValueError(f"Missing required headers:{empty_headers}")
if headers["EX-APP-ID"] != self.cfg.app_name:
raise ValueError(f"Invalid EX-APP-ID:{headers['EX-APP-ID']} != {self.cfg.app_name}")
username, app_secret = get_username_secret_from_headers(headers)
if app_secret != self.cfg.app_secret:
raise ValueError(f"Invalid App secret:{app_secret} != {self.cfg.app_secret}")
return username
class NcSessionApp(NcSessionAppBasic, NcSessionBasic):
cfg: AppConfig
def _create_adapter(self, dav: bool = False) -> AsyncClient | Client:
r = self._get_adapter_kwargs(dav)
r["event_hooks"]["request"].append(self._add_auth)
return Client(
follow_redirects=True,
limits=self.limits,
verify=self.cfg.options.nc_cert,
**r,
headers={
"AA-VERSION": self.cfg.aa_version,
"EX-APP-ID": self.cfg.app_name,
"EX-APP-VERSION": self.cfg.app_version,
"user-agent": f"ExApp/{self.cfg.app_name}/{self.cfg.app_version} (httpx/{httpx_version})",
},
)
def _add_auth(self, request: Request):
request.headers.update(
{"AUTHORIZATION-APP-API": b64encode(f"{self._user}:{self.cfg.app_secret}".encode("UTF=8"))}
)
class AsyncNcSessionApp(NcSessionAppBasic, AsyncNcSessionBasic):
cfg: AppConfig
def _create_adapter(self, dav: bool = False) -> AsyncClient | Client:
r = self._get_adapter_kwargs(dav)
r["event_hooks"]["request"].append(self._add_auth)
return AsyncClient(
follow_redirects=True,
limits=self.limits,
verify=self.cfg.options.nc_cert,
**r,
headers={
"AA-VERSION": self.cfg.aa_version,
"EX-APP-ID": self.cfg.app_name,
"EX-APP-VERSION": self.cfg.app_version,
"User-Agent": f"ExApp/{self.cfg.app_name}/{self.cfg.app_version} (httpx/{httpx_version})",
},
)
async def _add_auth(self, request: Request):
request.headers.update(
{"AUTHORIZATION-APP-API": b64encode(f"{self._user}:{self.cfg.app_secret}".encode("UTF=8"))}
)
|