From 89b379f55795b92dddb2db44b9ccd5fcc44a4a05 Mon Sep 17 00:00:00 2001 From: Rex Zhang Date: Mon, 2 Aug 2021 13:23:59 +0800 Subject: [PATCH] Fix auth method logical error --- asgi_webdav/auth.py | 29 ++++++++++++++++++----------- asgi_webdav/dev.py | 4 ++-- test_client.py | 15 +++++++++++---- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/asgi_webdav/auth.py b/asgi_webdav/auth.py index e2f42e99..3e86c891 100644 --- a/asgi_webdav/auth.py +++ b/asgi_webdav/auth.py @@ -338,20 +338,27 @@ def pick_out_user(self, request: DAVRequest) -> (Optional[DAVUser], str): return None, "Unknown authentication method" def create_response_401(self, request: DAVRequest, message: str) -> DAVResponse: - if not self.config.http_digest_auth.enable or self._match_user_agent( - rule=self.config.http_digest_auth.disable_rule, - user_agent=request.client_user_agent, - ): - challenge_string = self.basic_auth.make_auth_challenge_string() - logger.debug("response Basic auth challenge") + if self.config.http_digest_auth.enable: + if self._match_user_agent( + rule=self.config.http_digest_auth.disable_rule, + user_agent=request.client_user_agent, + ): + enable_digest = False + else: + enable_digest = True - elif self.config.http_digest_auth.enable or self._match_user_agent( - rule=self.config.http_digest_auth.enable_rule, - user_agent=request.client_user_agent, - ): + else: + if self._match_user_agent( + rule=self.config.http_digest_auth.enable_rule, + user_agent=request.client_user_agent, + ): + enable_digest = True + else: + enable_digest = False + + if enable_digest: challenge_string = self.digest_auth.make_auth_challenge_string() logger.debug("response Digest auth challenge") - else: challenge_string = self.basic_auth.make_auth_challenge_string() logger.debug("response Basic auth challenge") diff --git a/asgi_webdav/dev.py b/asgi_webdav/dev.py index 4698e9ef..adf161b6 100644 --- a/asgi_webdav/dev.py +++ b/asgi_webdav/dev.py @@ -15,8 +15,9 @@ {"username": "guest", "password": "password", "permissions": list()}, ], "http_digest_auth": { - "enable": True, + "enable": False, # "disable_rule": "neon/", + "enable_rule": "TEST", }, "provider_mapping": [ { @@ -69,7 +70,6 @@ "user_ignore_rule": "", }, "logging_level": "DEBUG", # for debug - # "logging_level": "INFO", # for debug } app_args = AppArgs(in_docker_container=False) diff --git a/test_client.py b/test_client.py index 9c5d9fa0..c744d461 100644 --- a/test_client.py +++ b/test_client.py @@ -1,7 +1,7 @@ from typing import Optional import json import xml -import pprint +from pprint import pprint from dataclasses import dataclass import requests @@ -26,12 +26,13 @@ def call(conn): print("---------------------") print(result) pprint(result.headers) - print(result.content) if len(result.content) > 0: try: pprint(json.loads((json.dumps(xmltodict.parse(result.content))))) except xml.parsers.expat.ExpatError: - pprint(result.content) + pass + else: + pprint(result.content) def server_basic_auth(method, path, headers=None): @@ -64,6 +65,11 @@ def main_test_auth(): server_digest_auth("OPTIONS", "/litmus") +def user_agent_test(): + headers = {"user-agent": "TEST-AGENT"} + server_digest_auth("GET", "/", headers=headers) + + def main(): # test_apache('PROPFIND', '/home', headers={'depth': '1'}) # test_apache('PROPFIND', '/.sync') @@ -79,4 +85,5 @@ def main(): if __name__ == "__main__": - main_test_auth() + # main_test_auth() + user_agent_test()