Skip to content

Commit 9584c62

Browse files
committed
AuthenticationContext change: allow_ntlm _browser_mode class props
1 parent 3d98588 commit 9584c62

File tree

1 file changed

+95
-10
lines changed

1 file changed

+95
-10
lines changed

office365/sharepoint/request.py

+95-10
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,127 @@
1-
from typing import Optional
1+
from typing import Optional, List, Callable
22

33
from requests import Response
44
from typing_extensions import Self
55

66
from office365.runtime.auth.authentication_context import AuthenticationContext
77
from office365.runtime.auth.client_credential import ClientCredential
8+
from office365.runtime.auth.token_response import TokenResponse
89
from office365.runtime.auth.user_credential import UserCredential
910
from office365.runtime.http.request_options import RequestOptions
1011
from office365.runtime.odata.request import ODataRequest
1112
from office365.runtime.odata.v3.json_light_format import JsonLightFormat
1213

1314

1415
class SharePointRequest(ODataRequest):
15-
def __init__(self, base_url):
16+
def __init__(
17+
self, base_url, environment="commercial", allow_ntlm=False, browser_mode=False
18+
):
1619
"""
1720
:param str base_url: Absolute Web or Site Url
21+
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
22+
defaults to 'commercial'.
23+
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
24+
:param bool browser_mode: Allow browser authentication
1825
"""
1926
super().__init__(JsonLightFormat())
20-
self._auth_context = AuthenticationContext(url=base_url)
27+
self._auth_context = AuthenticationContext(
28+
url=base_url,
29+
environment=environment,
30+
allow_ntlm=allow_ntlm,
31+
browser_mode=browser_mode,
32+
)
2133
self.beforeExecute += self._authenticate_request
2234

2335
def execute_request(self, path):
2436
# type: (str) -> Response
25-
request_url = "{0}/_api/{1}".format(self._auth_context.url, path)
37+
request_url = "{0}/{1}".format(self.service_root_url, path)
2638
return self.execute_request_direct(RequestOptions(request_url))
2739

28-
def with_credentials(self, credentials, environment="commercial"):
29-
# type: (UserCredential|ClientCredential, Optional[str]) -> Self
40+
def with_credentials(self, credentials):
41+
# type: (UserCredential|ClientCredential) -> Self
3042
"""
3143
Initializes a client to acquire a token via user or client credentials
32-
:type credentials: UserCredential or ClientCredential
33-
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
34-
defaults to 'commercial'.
3544
"""
36-
self._auth_context.with_credentials(credentials, environment=environment)
45+
self._auth_context.with_credentials(credentials)
46+
return self
47+
48+
def with_client_certificate(
49+
self,
50+
tenant,
51+
client_id,
52+
thumbprint,
53+
cert_path=None,
54+
private_key=None,
55+
scopes=None,
56+
passphrase=None,
57+
):
58+
# type: (str, str, str, Optional[str], Optional[str], Optional[List[str]], Optional[str]) -> Self
59+
"""
60+
Creates authenticated SharePoint context via certificate credentials
61+
62+
:param str tenant: Tenant name
63+
:param str or None cert_path: Path to A PEM encoded certificate private key.
64+
:param str or None private_key: A PEM encoded certificate private key.
65+
:param str thumbprint: Hex encoded thumbprint of the certificate.
66+
:param str client_id: The OAuth client id of the calling application.
67+
:param list[str] or None scopes: Scopes requested to access a protected API (a resource)
68+
:param str passphrase: Passphrase if the private_key is encrypted
69+
"""
70+
self._auth_context.with_client_certificate(
71+
tenant, client_id, thumbprint, cert_path, private_key, scopes, passphrase
72+
)
73+
return self
74+
75+
def with_device_flow(self, tenant, client_id, scopes=None):
76+
# type: (str, str, Optional[List[str]]) -> Self
77+
"""
78+
Initializes a client to acquire a token via device flow auth.
79+
80+
:param str tenant: Tenant name, for example: contoso.onmicrosoft.com
81+
:param str client_id: The OAuth client id of the calling application.
82+
:param list[str] or None scopes: Scopes requested to access a protected API (a resource)
83+
"""
84+
self._auth_context.with_device_flow(tenant, client_id, scopes)
85+
return self
86+
87+
def with_interactive(self, tenant, client_id, scopes=None):
88+
# type: (str, str, Optional[List[str]]) -> Self
89+
"""
90+
Initializes a client to acquire a token interactively i.e. via a local browser.
91+
92+
Prerequisite: In Azure Portal, configure the Redirect URI of your
93+
"Mobile and Desktop application" as ``http://localhost``.
94+
95+
:param str tenant: Tenant name, for example: contoso.onmicrosoft.com
96+
:param str client_id: The OAuth client id of the calling application.
97+
:param list[str] or None scopes: Scopes requested to access a protected API (a resource)
98+
"""
99+
self._auth_context.with_interactive(tenant, client_id, scopes)
100+
return self
101+
102+
def with_access_token(self, token_func):
103+
# type: (Callable[[], TokenResponse]) -> Self
104+
"""
105+
Initializes a client to acquire a token from a callback
106+
:param () -> TokenResponse token_func: A token callback
107+
"""
108+
self._auth_context.with_access_token(token_func)
37109
return self
38110

39111
def _authenticate_request(self, request):
40112
# type: (RequestOptions) -> None
41113
"""Authenticate request"""
42114
self._auth_context.authenticate_request(request)
115+
116+
@property
117+
def authentication_context(self):
118+
return self._auth_context
119+
120+
@property
121+
def base_url(self):
122+
"""Represents Base Url"""
123+
return self._auth_context.url
124+
125+
@property
126+
def service_root_url(self):
127+
return "{0}/_api".format(self._auth_context.url)

0 commit comments

Comments
 (0)