Skip to content

Commit 3d98588

Browse files
committed
AuthenticationContext change: allow_ntlm _browser_mode class props
1 parent 6d7f9d4 commit 3d98588

File tree

4 files changed

+52
-54
lines changed

4 files changed

+52
-54
lines changed

examples/onedrive/sites/get_by_url.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from office365.graph_client import GraphClient
88
from tests import test_client_id, test_client_secret, test_team_site_url, test_tenant
99

10-
client = GraphClient(tenant=test_tenant).with_client_secret(test_client_id, test_client_secret)
10+
client = GraphClient(tenant=test_tenant).with_client_secret(
11+
test_client_id, test_client_secret
12+
)
1113
site = client.sites.get_by_url(test_team_site_url).get().execute_query()
1214
print("Site Id: {0}".format(site.id))

office365/graph_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ def pending_request(self):
166166
# type: () -> GraphRequest
167167
if self._pending_request is None:
168168
self._pending_request = GraphRequest(
169-
tenant=self._tenant, environment=self._environment
169+
tenant=self._tenant, environment=self._environment
170170
)
171171
if callable(self._token_callback):
172-
self._pending_request.with_access_token(self._token_callback)
172+
self._pending_request.with_access_token(self._token_callback)
173173
self._pending_request.beforeExecute += self._build_specific_query
174174
return self._pending_request
175175

office365/runtime/auth/authentication_context.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,22 @@ def _get_authorization_header(token):
3131
class AuthenticationContext(object):
3232
"""Authentication context for SharePoint Online/OneDrive For Business"""
3333

34-
def __init__(self, url):
34+
def __init__(
35+
self, url, environment="commercial", allow_ntlm=False, browser_mode=False
36+
):
3537
"""
3638
:param str url: SharePoint absolute web or site Url
39+
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
40+
defaults to 'commercial'.
41+
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
42+
:param bool browser_mode: Allow browser authentication
3743
"""
3844
self.url = url.rstrip("/")
3945
self._authenticate = None
4046
self._cached_token = None
47+
self._environment = environment
48+
self._allow_ntlm = allow_ntlm
49+
self._browser_mode = browser_mode
4150

4251
def with_client_certificate(
4352
self,
@@ -171,33 +180,32 @@ def _authenticate(request):
171180
)
172181

173182
self._authenticate = _authenticate
183+
return self
174184

175-
def with_credentials(self, credentials, **kwargs):
185+
def with_credentials(self, credentials):
186+
# type: (UserCredential | ClientCredential) -> "AuthenticationContext"
176187
"""
177188
Initializes a client to acquire a token via user or client credentials
178-
179-
:param UserCredential or ClientCredential credentials:
180189
"""
181190
if isinstance(credentials, ClientCredential):
182-
environment = kwargs.get("environment")
183191
provider = ACSTokenProvider(
184-
self.url, credentials.clientId, credentials.clientSecret, environment
192+
self.url,
193+
credentials.clientId,
194+
credentials.clientSecret,
195+
self._environment,
185196
)
186197
elif isinstance(credentials, UserCredential):
187-
allow_ntlm = kwargs.get("allow_ntlm", False)
188-
if allow_ntlm:
198+
if self._allow_ntlm:
189199
from office365.runtime.auth.providers.ntlm_provider import NtlmProvider
190200

191201
provider = NtlmProvider(credentials.userName, credentials.password)
192202
else:
193-
browser_mode = kwargs.get("browser_mode", False)
194-
environment = kwargs.get("environment")
195203
provider = SamlTokenProvider(
196204
self.url,
197205
credentials.userName,
198206
credentials.password,
199-
browser_mode,
200-
environment,
207+
self._browser_mode,
208+
self._environment,
201209
)
202210
else:
203211
raise ValueError("Unknown credential type")
@@ -206,17 +214,17 @@ def _authenticate(request):
206214
provider.authenticate_request(request)
207215

208216
self._authenticate = _authenticate
217+
return self
209218

210-
def acquire_token_for_user(self, username, password, browser_mode=False):
219+
def acquire_token_for_user(self, username, password):
211220
"""
212221
Initializes a client to acquire a token via user credentials
213222
Status: deprecated!
214223
215224
:param str password: The user password
216225
:param str username: Typically a UPN in the form of an email address
217-
:param bool browser_mode:
218226
"""
219-
provider = SamlTokenProvider(self.url, username, password, browser_mode)
227+
provider = SamlTokenProvider(self.url, username, password, self._browser_mode)
220228

221229
def _authenticate(request):
222230
provider.authenticate_request(request)

office365/sharepoint/client_context.py

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,15 @@
4141
class ClientContext(ClientRuntimeContext):
4242
"""SharePoint client context (SharePoint v1 API)"""
4343

44-
def __init__(self, base_url, auth_context=None):
45-
# type: (str, AuthenticationContext | None) -> None
44+
def __init__(
45+
self,
46+
base_url,
47+
auth_context=None,
48+
environment="commercial",
49+
allow_ntlm=False,
50+
browser_mode=False,
51+
):
52+
# type: (str, Optional[AuthenticationContext], str, bool, bool) -> None
4653
"""
4754
Instantiates a SharePoint client context
4855
@@ -51,7 +58,12 @@ def __init__(self, base_url, auth_context=None):
5158
"""
5259
super(ClientContext, self).__init__()
5360
if auth_context is None:
54-
auth_context = AuthenticationContext(url=base_url)
61+
auth_context = AuthenticationContext(
62+
url=base_url,
63+
environment=environment,
64+
allow_ntlm=allow_ntlm,
65+
browser_mode=browser_mode,
66+
)
5567
self._auth_context = auth_context
5668
self._web = None
5769
self._site = None
@@ -138,36 +150,18 @@ def with_access_token(self, token_func):
138150
self.authentication_context.with_access_token(token_func)
139151
return self
140152

141-
def with_user_credentials(
142-
self,
143-
username,
144-
password,
145-
allow_ntlm=False,
146-
browser_mode=False,
147-
environment="commercial",
148-
):
149-
# type: (str, str, bool, bool, Optional[str]) -> Self
153+
def with_user_credentials(self, username, password):
154+
# type: (str, str) -> Self
150155
"""
151156
Initializes a client to acquire a token via user credentials.
152157
:param str username: Typically, a UPN in the form of an email address
153158
:param str password: The password
154-
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
155-
:param bool browser_mode:
156-
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
157-
defaults to 'commercial'.
158159
"""
159-
self.authentication_context.with_credentials(
160-
UserCredential(username, password),
161-
allow_ntlm=allow_ntlm,
162-
browser_mode=browser_mode,
163-
environment=environment,
164-
)
160+
self.authentication_context.with_credentials(UserCredential(username, password))
165161
return self
166162

167-
def with_client_credentials(
168-
self, client_id, client_secret, environment="commercial"
169-
):
170-
# type: (str, str, Optional[str]) -> Self
163+
def with_client_credentials(self, client_id, client_secret):
164+
# type: (str, str) -> Self
171165
"""
172166
Initializes a client to acquire a token via client credentials (SharePoint App-Only)
173167
@@ -176,25 +170,19 @@ def with_client_credentials(
176170
177171
:param str client_id: The OAuth client id of the calling application
178172
:param str client_secret: Secret string that the application uses to prove its identity when requesting a token
179-
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
180-
defaults to 'commercial'.
181173
"""
182174
self.authentication_context.with_credentials(
183-
ClientCredential(client_id, client_secret), environment=environment
175+
ClientCredential(client_id, client_secret)
184176
)
185177
return self
186178

187-
def with_credentials(self, credentials, environment="commercial"):
188-
# type: (UserCredential|ClientCredential, Optional[str]) -> Self
179+
def with_credentials(self, credentials):
180+
# type: (UserCredential|ClientCredential) -> Self
189181
"""
190182
Initializes a client to acquire a token via user or client credentials
191183
:type credentials: UserCredential or ClientCredential
192-
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
193-
defaults to 'commercial'.
194184
"""
195-
self.authentication_context.with_credentials(
196-
credentials, environment=environment
197-
)
185+
self.authentication_context.with_credentials(credentials)
198186
return self
199187

200188
def execute_batch(self, items_per_batch=100, success_callback=None):

0 commit comments

Comments
 (0)