Returns a Basic Auth string.
(username: bytes | str, password: bytes | str)
| 32 | |
| 33 | |
| 34 | def _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 | |
| 78 | class AuthBase: |