From 986a8ad007d7621a464ddee681769fc186a4ed01 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Fri, 7 Jun 2024 10:01:23 +1000 Subject: [PATCH] Apply bulk black and isort changes --- requests_ntlm/__init__.py | 2 +- requests_ntlm/requests_ntlm.py | 10 +- tests/functional/test_functional.py | 27 +- tests/test_server.py | 59 ++-- tests/test_utils.py | 8 +- tests/unit/test_requests_ntlm.py | 501 ++++++++++++++++------------ 6 files changed, 347 insertions(+), 260 deletions(-) diff --git a/requests_ntlm/__init__.py b/requests_ntlm/__init__.py index 7981835..88aac14 100644 --- a/requests_ntlm/__init__.py +++ b/requests_ntlm/__init__.py @@ -1,3 +1,3 @@ from .requests_ntlm import HttpNtlmAuth -__all__ = ('HttpNtlmAuth',) +__all__ = ("HttpNtlmAuth",) diff --git a/requests_ntlm/requests_ntlm.py b/requests_ntlm/requests_ntlm.py index 3521071..3b4f60b 100644 --- a/requests_ntlm/requests_ntlm.py +++ b/requests_ntlm/requests_ntlm.py @@ -1,18 +1,16 @@ from __future__ import annotations -import warnings import base64 import typing as t - +import warnings from urllib.parse import urlparse import requests import spnego - from cryptography import x509 +from cryptography.exceptions import UnsupportedAlgorithm from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import hashes -from cryptography.exceptions import UnsupportedAlgorithm from requests.auth import AuthBase from requests.packages.urllib3.response import HTTPResponse @@ -170,7 +168,9 @@ def retry_using_http_NTLM_auth( ) if not ntlm_header_value: - raise PermissionError("Access denied: Server did not respond with NTLM challenge token") + raise PermissionError( + "Access denied: Server did not respond with NTLM challenge token" + ) # Parse the challenge in the ntlm context and perform # the second step of authentication diff --git a/tests/functional/test_functional.py b/tests/functional/test_functional.py index 6a33158..d56b011 100644 --- a/tests/functional/test_functional.py +++ b/tests/functional/test_functional.py @@ -1,4 +1,5 @@ import requests + import requests_ntlm """ @@ -7,18 +8,19 @@ with the 4 scenarios tested below if you wish to run a sanity check """ -username = '.\\User' -password = 'Password01' -http_with_cbt = 'http://127.0.0.1:81/contents.txt' -http_without_cbt = 'http://127.0.0.1:82/contents.txt' -https_with_cbt = 'https://127.0.0.1:441/contents.txt' -https_without_cbt = 'https://127.0.0.1:442/contents.txt' -expected = 'contents' +username = ".\\User" +password = "Password01" +http_with_cbt = "http://127.0.0.1:81/contents.txt" +http_without_cbt = "http://127.0.0.1:82/contents.txt" +https_with_cbt = "https://127.0.0.1:441/contents.txt" +https_without_cbt = "https://127.0.0.1:442/contents.txt" +expected = "contents" + -class Test_Functional(): +class Test_Functional: def test_ntlm_http_with_cbt(self): actual = send_request(http_with_cbt, username, password) - actual_content = actual.content.decode('utf-8') + actual_content = actual.content.decode("utf-8") actual_code = actual.status_code assert actual_code == 200 @@ -26,7 +28,7 @@ def test_ntlm_http_with_cbt(self): def test_ntlm_http_without_cbt(self): actual = send_request(http_without_cbt, username, password) - actual_content = actual.content.decode('utf-8') + actual_content = actual.content.decode("utf-8") actual_code = actual.status_code assert actual_code == 200 @@ -34,7 +36,7 @@ def test_ntlm_http_without_cbt(self): def test_ntlm_https_with_cbt(self): actual = send_request(https_with_cbt, username, password) - actual_content = actual.content.decode('utf-8') + actual_content = actual.content.decode("utf-8") actual_code = actual.status_code assert actual_code == 200 @@ -42,12 +44,13 @@ def test_ntlm_https_with_cbt(self): def test_ntlm_https_without_cbt(self): actual = send_request(https_without_cbt, username, password) - actual_content = actual.content.decode('utf-8') + actual_content = actual.content.decode("utf-8") actual_code = actual.status_code assert actual_code == 200 assert actual_content == expected + def send_request(url, username, password): """ Sends a request to the url with the credentials specified. Returns the final response diff --git a/tests/test_server.py b/tests/test_server.py index 801b69e..c776b48 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,73 +1,90 @@ import base64 import struct -from flask import Flask,request -from tests.test_utils import domain, username, password +from flask import Flask, request + +from tests.test_utils import domain, password, username app = Flask(__name__) + @app.route("/ntlm") def ntlm_auth(): - return get_auth_response('NTLM') + return get_auth_response("NTLM") + @app.route("/negotiate") def negotiate_auth(): - return get_auth_response('Negotiate') + return get_auth_response("Negotiate") + @app.route("/both") def negotiate_and_ntlm_auth(): - return get_auth_response('NTLM', advertise_nego_and_ntlm=True) + return get_auth_response("NTLM", advertise_nego_and_ntlm=True) + @app.route("/no_challenge") def no_challenge(): - return get_auth_response('Negotiate', no_challenge=True) + return get_auth_response("Negotiate", no_challenge=True) + def get_auth_response(auth_type, advertise_nego_and_ntlm=False, no_challenge=False): # Get the actual header that is returned by requests_ntlm - actual_header = request.headers.get('Authorization', '') + actual_header = request.headers.get("Authorization", "") # Check what the message type is from the header - if actual_header == '': + if actual_header == "": # This is the initial connection, need to return a 401 - response_headers = {'WWW-Authenticate': auth_type if not advertise_nego_and_ntlm else 'Negotiate, NTLM'} + response_headers = { + "WWW-Authenticate": ( + auth_type if not advertise_nego_and_ntlm else "Negotiate, NTLM" + ) + } status_code = 401 response = "auth with '%s\\%s':'%s'" % (domain, username, password) else: # Set human readable names for message types # see https://msdn.microsoft.com/en-us/library/cc236639.aspx for more details - expected_signature = b'NTLMSSP\x00' + expected_signature = b"NTLMSSP\x00" negotiate_message_type = 1 authenticate_message_type = 3 - msg = base64.b64decode(actual_header[len(auth_type):]) + msg = base64.b64decode(actual_header[len(auth_type) :]) signature = msg[0:8] if signature != expected_signature: - raise ValueError("Mismatch on NTLM message signature, expecting: %s, actual: %s" % (expected_signature, - signature)) + raise ValueError( + "Mismatch on NTLM message signature, expecting: %s, actual: %s" + % (expected_signature, signature) + ) # Get the NTLM version number (bytes 9 - 12) message_type = struct.unpack("