Perform HTTP Basic Authentication. If it succeeds, set the connection's ``username`` attribute and return :obj:`None`. If it fails, return an HTTP 401 Unauthorized responss.
(
connection: ServerConnection,
request: Request,
)
| 721 | assert check_credentials is not None # help mypy |
| 722 | |
| 723 | def process_request( |
| 724 | connection: ServerConnection, |
| 725 | request: Request, |
| 726 | ) -> Response | None: |
| 727 | """ |
| 728 | Perform HTTP Basic Authentication. |
| 729 | |
| 730 | If it succeeds, set the connection's ``username`` attribute and return |
| 731 | :obj:`None`. If it fails, return an HTTP 401 Unauthorized responss. |
| 732 | |
| 733 | """ |
| 734 | try: |
| 735 | authorization = request.headers["Authorization"] |
| 736 | except KeyError: |
| 737 | response = connection.respond( |
| 738 | http.HTTPStatus.UNAUTHORIZED, |
| 739 | "Missing credentials\n", |
| 740 | ) |
| 741 | response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm) |
| 742 | return response |
| 743 | |
| 744 | try: |
| 745 | username, password = parse_authorization_basic(authorization) |
| 746 | except InvalidHeader: |
| 747 | response = connection.respond( |
| 748 | http.HTTPStatus.UNAUTHORIZED, |
| 749 | "Unsupported credentials\n", |
| 750 | ) |
| 751 | response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm) |
| 752 | return response |
| 753 | |
| 754 | if not check_credentials(username, password): |
| 755 | response = connection.respond( |
| 756 | http.HTTPStatus.UNAUTHORIZED, |
| 757 | "Invalid credentials\n", |
| 758 | ) |
| 759 | response.headers["WWW-Authenticate"] = build_www_authenticate_basic(realm) |
| 760 | return response |
| 761 | |
| 762 | connection.username = username |
| 763 | return None |
| 764 | |
| 765 | return process_request |
no test coverage detected
searching dependent graphs…