Encode HTTP Basic Authentication credentials as an Authorization header value. Returns a string of the form ``"Basic "`` suitable for use as the value of the ``Authorization`` (or ``Proxy-Authorization``) header.
(login: str, password: str = "", encoding: str = "utf-8")
| 117 | |
| 118 | |
| 119 | def encode_basic_auth(login: str, password: str = "", encoding: str = "utf-8") -> str: |
| 120 | """Encode HTTP Basic Authentication credentials as an Authorization header value. |
| 121 | |
| 122 | Returns a string of the form ``"Basic <base64>"`` suitable for use as the |
| 123 | value of the ``Authorization`` (or ``Proxy-Authorization``) header. |
| 124 | """ |
| 125 | if ":" in login: |
| 126 | raise ValueError('A ":" is not allowed in login (RFC 7617#section-2)') |
| 127 | creds = f"{login}:{password}".encode(encoding) |
| 128 | return "Basic " + base64.b64encode(creds).decode(encoding) |
| 129 | |
| 130 | |
| 131 | class BasicAuth(namedtuple("BasicAuth", ["login", "password", "encoding"])): |