| 467 | log.addHandler(h) |
| 468 | |
| 469 | def _get_user(self, environ): |
| 470 | user = None |
| 471 | http_auth = environ.get("HTTP_AUTHORIZATION") |
| 472 | if http_auth and http_auth.lower().startswith('basic'): |
| 473 | auth = http_auth.split(" ", 1) |
| 474 | if len(auth) == 2: |
| 475 | try: |
| 476 | # b64decode doesn't accept unicode in Python < 3.3 |
| 477 | # so we need to convert it to a byte string |
| 478 | auth = base64.b64decode(auth[1].strip().encode('utf-8')) |
| 479 | # b64decode returns a byte string |
| 480 | user = auth.split(b":", 1)[0].decode("UTF-8") |
| 481 | except (TypeError, binascii.Error, UnicodeDecodeError) as exc: |
| 482 | self.debug("Couldn't get username: %s", exc) |
| 483 | return user |