generated from one-zero-eight/fastapi-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use NTLM for authentication in EWS
- Loading branch information
1 parent
84830d3
commit 4e5316a
Showing
4 changed files
with
125 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,48 @@ | ||
import exchangelib | ||
import httpx | ||
import pycurl | ||
import requests | ||
from requests_curl import CURLAdapter | ||
from requests_curl.request import CURLRequest | ||
|
||
from src.config import settings | ||
|
||
USERPWD = f"{settings.exchange.username}:{settings.exchange.password.get_secret_value()}" | ||
|
||
original_build_curl_options = CURLRequest._build_curl_options | ||
|
||
|
||
def build_curl_options(self): | ||
curl_options = original_build_curl_options(self) | ||
|
||
# Authentication using NTLM | ||
curl_options.update( | ||
{ | ||
pycurl.HTTPAUTH: pycurl.HTTPAUTH_NTLM, | ||
pycurl.USERPWD: USERPWD, | ||
} | ||
) | ||
|
||
# Let curl handle the POSTFIELDS instead of using READFUNCTION | ||
curl_options.update( | ||
{ | ||
pycurl.POSTFIELDS: curl_options[pycurl.READFUNCTION]().decode(), | ||
} | ||
) | ||
curl_options.pop(pycurl.READFUNCTION) | ||
curl_options.pop(pycurl.UPLOAD) | ||
return curl_options | ||
|
||
|
||
CURLRequest._build_curl_options = build_curl_options | ||
|
||
|
||
def raw_session(cls, prefix, oauth2_client=None, oauth2_session_params=None, oauth2_token_endpoint=None): | ||
# Use httpx with http2. Return a session that is somewhat compatible with requests.Session | ||
session = httpx.Client(http2=True) | ||
session = requests.Session() | ||
session.mount("http://", CURLAdapter()) | ||
session.mount("https://", CURLAdapter()) | ||
session.headers.update(exchangelib.protocol.DEFAULT_HEADERS) | ||
session.headers["User-Agent"] = cls.USERAGENT | ||
session.get_adapter = lambda _: None | ||
_post = session.post | ||
|
||
def post(url, data, *args, **kwargs): | ||
kwargs.pop("allow_redirects", None) | ||
kwargs.pop("stream", None) | ||
response = _post(url, content=data, *args, **kwargs) | ||
response.iter_content = response.iter_bytes | ||
return response | ||
|
||
session.post = post | ||
return session | ||
|
||
|
||
# Patch exchangelib to use httpx with http2 instead of requests | ||
exchangelib.protocol.BaseProtocol.raw_session = raw_session |