diff --git a/examples/onedrive/sites/get_by_url.py b/examples/onedrive/sites/get_by_url.py index f3e205cf..7219f717 100644 --- a/examples/onedrive/sites/get_by_url.py +++ b/examples/onedrive/sites/get_by_url.py @@ -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)) diff --git a/office365/graph_client.py b/office365/graph_client.py index 7d0063b0..63ed7795 100644 --- a/office365/graph_client.py +++ b/office365/graph_client.py @@ -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 diff --git a/office365/runtime/auth/authentication_context.py b/office365/runtime/auth/authentication_context.py index 5d102364..d354746c 100644 --- a/office365/runtime/auth/authentication_context.py +++ b/office365/runtime/auth/authentication_context.py @@ -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, @@ -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") @@ -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) diff --git a/office365/sharepoint/client_context.py b/office365/sharepoint/client_context.py index 6923bfb8..b46d449f 100644 --- a/office365/sharepoint/client_context.py +++ b/office365/sharepoint/client_context.py @@ -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 @@ -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 @@ -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) @@ -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):