Skip to content

Commit f942fc3

Browse files
committed
move to util
1 parent 2c2f32f commit f942fc3

File tree

4 files changed

+57
-50
lines changed

4 files changed

+57
-50
lines changed

onvif/client.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import os.path
99
from typing import Any
1010
from collections.abc import Callable
11-
from yarl import URL
12-
from multidict import CIMultiDict
1311
import httpx
1412
from httpx import AsyncClient, BasicAuth, DigestAuth
1513
from zeep.cache import SqliteCache
@@ -28,7 +26,14 @@
2826
from .settings import DEFAULT_SETTINGS
2927
from .transport import ASYNC_TRANSPORT
3028
from .types import FastDateTime, ForgivingTime
31-
from .util import create_no_verify_ssl_context, normalize_url, path_isfile, utcnow
29+
from .util import (
30+
create_no_verify_ssl_context,
31+
normalize_url,
32+
path_isfile,
33+
utcnow,
34+
strip_user_pass_url,
35+
obscure_user_pass_url,
36+
)
3237
from .wrappers import retry_connection_error # noqa: F401
3338
from .wsa import WsAddressingIfMissingPlugin
3439

@@ -45,38 +50,6 @@
4550
_WRITE_TIMEOUT = 90
4651
_HTTPX_LIMITS = httpx.Limits(keepalive_expiry=KEEPALIVE_EXPIRY)
4752
_NO_VERIFY_SSL_CONTEXT = create_no_verify_ssl_context()
48-
_CREDENTIAL_KEYS = ("username", "user", "pass", "password")
49-
50-
51-
def strip_user_pass_url(url: str) -> str:
52-
"""Strip password from URL."""
53-
parsed_url = URL(url)
54-
query = parsed_url.query
55-
new_query: CIMultiDict | None = None
56-
for key in _CREDENTIAL_KEYS:
57-
if key in query:
58-
if new_query is None:
59-
new_query = CIMultiDict(parsed_url.query)
60-
new_query.popall(key)
61-
if new_query is not None:
62-
return str(parsed_url.with_query(new_query))
63-
return url
64-
65-
66-
def obscure_user_pass_url(url: str) -> str:
67-
"""Obscure user and password from URL."""
68-
parsed_url = URL(url)
69-
query = parsed_url.query
70-
new_query: CIMultiDict | None = None
71-
for key in _CREDENTIAL_KEYS:
72-
if key in query:
73-
if new_query is None:
74-
new_query = CIMultiDict(parsed_url.query)
75-
new_query.popall(key)
76-
new_query[key] = "********"
77-
if new_query is not None:
78-
return str(parsed_url.with_query(new_query))
79-
return url
8053

8154

8255
def safe_func(func):

onvif/util.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import ssl
1010
from typing import Any
1111
from urllib.parse import ParseResultBytes, urlparse, urlunparse
12-
12+
from yarl import URL
13+
from multidict import CIMultiDict
1314
from zeep.exceptions import Fault
1415

1516
utcnow: partial[dt.datetime] = partial(dt.datetime.now, dt.timezone.utc)
@@ -18,6 +19,8 @@
1819
# to minimize the impact of the blocking I/O.
1920
path_isfile = lru_cache(maxsize=128)(os.path.isfile)
2021

22+
_CREDENTIAL_KEYS = ("username", "password", "user", "pass")
23+
2124

2225
def normalize_url(url: bytes | str | None) -> str | None:
2326
"""Normalize URL.
@@ -105,3 +108,34 @@ def create_no_verify_ssl_context() -> ssl.SSLContext:
105108
# ssl.OP_LEGACY_SERVER_CONNECT is only available in Python 3.12a4+
106109
sslcontext.options |= getattr(ssl, "OP_LEGACY_SERVER_CONNECT", 0x4)
107110
return sslcontext
111+
112+
113+
def strip_user_pass_url(url: str) -> str:
114+
"""Strip password from URL."""
115+
parsed_url = URL(url)
116+
query = parsed_url.query
117+
new_query: CIMultiDict | None = None
118+
for key in _CREDENTIAL_KEYS:
119+
if key in query:
120+
if new_query is None:
121+
new_query = CIMultiDict(parsed_url.query)
122+
new_query.popall(key)
123+
if new_query is not None:
124+
return str(parsed_url.with_query(new_query))
125+
return url
126+
127+
128+
def obscure_user_pass_url(url: str) -> str:
129+
"""Obscure user and password from URL."""
130+
parsed_url = URL(url)
131+
query = parsed_url.query
132+
new_query: CIMultiDict | None = None
133+
for key in _CREDENTIAL_KEYS:
134+
if key in query:
135+
if new_query is None:
136+
new_query = CIMultiDict(parsed_url.query)
137+
new_query.popall(key)
138+
new_query[key] = "********"
139+
if new_query is not None:
140+
return str(parsed_url.with_query(new_query))
141+
return url

tests/test_client.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/test_util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66
from zeep.loader import parse_xml
7+
from onvif.util import strip_user_pass_url, obscure_user_pass_url
78

89
from onvif.client import ONVIFCamera
910
from onvif.settings import DEFAULT_SETTINGS
@@ -40,3 +41,16 @@ async def test_normalize_url_with_missing_url():
4041
)
4142
result = operation.process_reply(envelope)
4243
assert normalize_url(result.SubscriptionReference.Address._value_1) is None
44+
45+
46+
def test_strip_user_pass_url():
47+
assert strip_user_pass_url("http://1.2.3.4/?user=foo&pass=bar") == "http://1.2.3.4/"
48+
assert strip_user_pass_url("http://1.2.3.4/") == "http://1.2.3.4/"
49+
50+
51+
def test_obscure_user_pass_url():
52+
assert (
53+
obscure_user_pass_url("http://1.2.3.4/?user=foo&pass=bar")
54+
== "http://1.2.3.4/?user=********&pass=********"
55+
)
56+
assert obscure_user_pass_url("http://1.2.3.4/") == "http://1.2.3.4/"

0 commit comments

Comments
 (0)