Skip to content

Commit

Permalink
AuthenticationContext change: allow_ntlm _browser_mode class props
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jan 19, 2025
1 parent 6d7f9d4 commit 3d98588
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 54 deletions.
4 changes: 3 additions & 1 deletion examples/onedrive/sites/get_by_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from office365.graph_client import GraphClient
from tests import test_client_id, test_client_secret, test_team_site_url, test_tenant

client = GraphClient(tenant=test_tenant).with_client_secret(test_client_id, test_client_secret)
client = GraphClient(tenant=test_tenant).with_client_secret(
test_client_id, test_client_secret
)
site = client.sites.get_by_url(test_team_site_url).get().execute_query()
print("Site Id: {0}".format(site.id))
4 changes: 2 additions & 2 deletions office365/graph_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ def pending_request(self):
# type: () -> GraphRequest
if self._pending_request is None:
self._pending_request = GraphRequest(
tenant=self._tenant, environment=self._environment
tenant=self._tenant, environment=self._environment
)
if callable(self._token_callback):
self._pending_request.with_access_token(self._token_callback)
self._pending_request.with_access_token(self._token_callback)
self._pending_request.beforeExecute += self._build_specific_query
return self._pending_request

Expand Down
38 changes: 23 additions & 15 deletions office365/runtime/auth/authentication_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,22 @@ def _get_authorization_header(token):
class AuthenticationContext(object):
"""Authentication context for SharePoint Online/OneDrive For Business"""

def __init__(self, url):
def __init__(
self, url, environment="commercial", allow_ntlm=False, browser_mode=False
):
"""
:param str url: SharePoint absolute web or site Url
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
defaults to 'commercial'.
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
:param bool browser_mode: Allow browser authentication
"""
self.url = url.rstrip("/")
self._authenticate = None
self._cached_token = None
self._environment = environment
self._allow_ntlm = allow_ntlm
self._browser_mode = browser_mode

def with_client_certificate(
self,
Expand Down Expand Up @@ -171,33 +180,32 @@ def _authenticate(request):
)

self._authenticate = _authenticate
return self

def with_credentials(self, credentials, **kwargs):
def with_credentials(self, credentials):
# type: (UserCredential | ClientCredential) -> "AuthenticationContext"
"""
Initializes a client to acquire a token via user or client credentials
:param UserCredential or ClientCredential credentials:
"""
if isinstance(credentials, ClientCredential):
environment = kwargs.get("environment")
provider = ACSTokenProvider(
self.url, credentials.clientId, credentials.clientSecret, environment
self.url,
credentials.clientId,
credentials.clientSecret,
self._environment,
)
elif isinstance(credentials, UserCredential):
allow_ntlm = kwargs.get("allow_ntlm", False)
if allow_ntlm:
if self._allow_ntlm:
from office365.runtime.auth.providers.ntlm_provider import NtlmProvider

provider = NtlmProvider(credentials.userName, credentials.password)
else:
browser_mode = kwargs.get("browser_mode", False)
environment = kwargs.get("environment")
provider = SamlTokenProvider(
self.url,
credentials.userName,
credentials.password,
browser_mode,
environment,
self._browser_mode,
self._environment,
)
else:
raise ValueError("Unknown credential type")
Expand All @@ -206,17 +214,17 @@ def _authenticate(request):
provider.authenticate_request(request)

self._authenticate = _authenticate
return self

def acquire_token_for_user(self, username, password, browser_mode=False):
def acquire_token_for_user(self, username, password):
"""
Initializes a client to acquire a token via user credentials
Status: deprecated!
:param str password: The user password
:param str username: Typically a UPN in the form of an email address
:param bool browser_mode:
"""
provider = SamlTokenProvider(self.url, username, password, browser_mode)
provider = SamlTokenProvider(self.url, username, password, self._browser_mode)

def _authenticate(request):
provider.authenticate_request(request)
Expand Down
60 changes: 24 additions & 36 deletions office365/sharepoint/client_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@
class ClientContext(ClientRuntimeContext):
"""SharePoint client context (SharePoint v1 API)"""

def __init__(self, base_url, auth_context=None):
# type: (str, AuthenticationContext | None) -> None
def __init__(
self,
base_url,
auth_context=None,
environment="commercial",
allow_ntlm=False,
browser_mode=False,
):
# type: (str, Optional[AuthenticationContext], str, bool, bool) -> None
"""
Instantiates a SharePoint client context
Expand All @@ -51,7 +58,12 @@ def __init__(self, base_url, auth_context=None):
"""
super(ClientContext, self).__init__()
if auth_context is None:
auth_context = AuthenticationContext(url=base_url)
auth_context = AuthenticationContext(
url=base_url,
environment=environment,
allow_ntlm=allow_ntlm,
browser_mode=browser_mode,
)
self._auth_context = auth_context
self._web = None
self._site = None
Expand Down Expand Up @@ -138,36 +150,18 @@ def with_access_token(self, token_func):
self.authentication_context.with_access_token(token_func)
return self

def with_user_credentials(
self,
username,
password,
allow_ntlm=False,
browser_mode=False,
environment="commercial",
):
# type: (str, str, bool, bool, Optional[str]) -> Self
def with_user_credentials(self, username, password):
# type: (str, str) -> Self
"""
Initializes a client to acquire a token via user credentials.
:param str username: Typically, a UPN in the form of an email address
:param str password: The password
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
:param bool browser_mode:
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
defaults to 'commercial'.
"""
self.authentication_context.with_credentials(
UserCredential(username, password),
allow_ntlm=allow_ntlm,
browser_mode=browser_mode,
environment=environment,
)
self.authentication_context.with_credentials(UserCredential(username, password))
return self

def with_client_credentials(
self, client_id, client_secret, environment="commercial"
):
# type: (str, str, Optional[str]) -> Self
def with_client_credentials(self, client_id, client_secret):
# type: (str, str) -> Self
"""
Initializes a client to acquire a token via client credentials (SharePoint App-Only)
Expand All @@ -176,25 +170,19 @@ def with_client_credentials(
:param str client_id: The OAuth client id of the calling application
:param str client_secret: Secret string that the application uses to prove its identity when requesting a token
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
defaults to 'commercial'.
"""
self.authentication_context.with_credentials(
ClientCredential(client_id, client_secret), environment=environment
ClientCredential(client_id, client_secret)
)
return self

def with_credentials(self, credentials, environment="commercial"):
# type: (UserCredential|ClientCredential, Optional[str]) -> Self
def with_credentials(self, credentials):
# type: (UserCredential|ClientCredential) -> Self
"""
Initializes a client to acquire a token via user or client credentials
:type credentials: UserCredential or ClientCredential
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
defaults to 'commercial'.
"""
self.authentication_context.with_credentials(
credentials, environment=environment
)
self.authentication_context.with_credentials(credentials)
return self

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

0 comments on commit 3d98588

Please sign in to comment.