MCPcopy
hub / github.com/psf/requests / _basic_auth_str

Function _basic_auth_str

src/requests/auth.py:34–75  ·  view source on GitHub ↗

Returns a Basic Auth string.

(username: bytes | str, password: bytes | str)

Source from the content-addressed store, hash-verified

32
33
34def _basic_auth_str(username: bytes | str, password: bytes | str) -> str:
35 """Returns a Basic Auth string."""
36
37 # "I want us to put a big-ol' comment on top of it that
38 # says that this behaviour is dumb but we need to preserve
39 # it because people are relying on it."
40 # - Lukasa
41 #
42 # These are here solely to maintain backwards compatibility
43 # for things like ints. This will be removed in 3.0.0.
44 if not isinstance(username, basestring): # type: ignore[reportUnnecessaryIsInstance] # runtime guard for non-str/bytes
45 warnings.warn(
46 "Non-string usernames will no longer be supported in Requests "
47 f"3.0.0. Please convert the object you've passed in ({username!r}) to "
48 "a string or bytes object in the near future to avoid "
49 "problems.",
50 category=DeprecationWarning,
51 )
52 username = str(username)
53
54 if not isinstance(password, basestring): # type: ignore[reportUnnecessaryIsInstance] # runtime guard for non-str/bytes
55 warnings.warn(
56 "Non-string passwords will no longer be supported in Requests "
57 f"3.0.0. Please convert the object you've passed in ({type(password)!r}) to "
58 "a string or bytes object in the near future to avoid "
59 "problems.",
60 category=DeprecationWarning,
61 )
62 password = str(password)
63 # -- End Removal --
64
65 if isinstance(username, str):
66 username = username.encode("latin1")
67
68 if isinstance(password, str):
69 password = password.encode("latin1")
70
71 authstr = "Basic " + to_native_string(
72 b64encode(b":".join((username, password))).strip()
73 )
74
75 return authstr
76
77
78class AuthBase:

Callers 6

test_set_basicauthMethod · 0.90
__call__Method · 0.85
__call__Method · 0.85
proxy_headersMethod · 0.85
rebuild_proxiesMethod · 0.85

Calls 1

to_native_stringFunction · 0.85

Tested by 2

test_set_basicauthMethod · 0.72