Skip to content

Commit 42132d7

Browse files
committed
a few fixes
1 parent a61e4dc commit 42132d7

File tree

5 files changed

+44
-38
lines changed

5 files changed

+44
-38
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from office365.azure_env import AzureEnvironment
21

32
# About
43
Microsoft 365 & Microsoft Graph library for Python
@@ -185,7 +184,7 @@ Refer [examples section](examples/sharepoint) for another scenarios
185184
from office365.sharepoint.client_context import ClientContext
186185
from office365.runtime.auth.client_credential import ClientCredential
187186
client_credentials = ClientCredential('{client_id}','{client_secret}')
188-
ctx = ClientContext('{url}', environment=AzureEnvironment.USGovernmentHigh).with_credentials(client_credentials)
187+
ctx = ClientContext('{site-url}', environment=AzureEnvironment.USGovernmentHigh).with_credentials(client_credentials)
189188
```
190189

191190
# Working with Outlook API
@@ -335,7 +334,7 @@ for drive in drives:
335334

336335
```python
337336
from office365.graph_client import GraphClient
338-
client = GraphClient(acquire_token_func)
337+
client = GraphClient(tenant="contoso.onmicrosoft.com")
339338
# retrieve drive properties
340339
drive = client.users["{user_id_or_principal_name}"].drive.get().execute_query()
341340
# download files from OneDrive into local folder

examples/sharepoint/auth_user_credential.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"""
66

77
from office365.sharepoint.client_context import ClientContext
8-
from tests import test_password, test_site_url, test_username
8+
from tests import test_password, test_site_url, test_username, test_team_site_url
99

10-
ctx = ClientContext(test_site_url).with_user_credentials(test_username, test_password)
10+
ctx = ClientContext(test_team_site_url).with_user_credentials(test_username, test_password)
1111
web = ctx.web.get().execute_query()
1212
print(web.url)

office365/runtime/auth/providers/saml_token_provider.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
office365.logger.ensure_debug_secrets()
1818

1919

20-
def resolve_base_url(url):
21-
parts = url.split("://")
22-
host_name = parts[1].split("/")[0]
23-
return parts[0] + "://" + host_name
24-
25-
2620
def string_escape(value):
2721
value = value.replace("&", "&")
2822
value = value.replace("<", "&lt;")
@@ -37,7 +31,7 @@ def datetime_escape(value):
3731

3832

3933
class SamlTokenProvider(AuthenticationProvider, office365.logger.LoggerContext):
40-
def __init__(self, url, username, password, browser_mode, environment="commercial"):
34+
def __init__(self, url, username, password, browser_mode, environment=None):
4135
"""
4236
SAML Security Token Service provider (claims-based authentication)
4337
@@ -46,13 +40,11 @@ def __init__(self, url, username, password, browser_mode, environment="commercia
4640
:param str password: The password
4741
:param bool browser_mode:
4842
:param str environment: The Office 365 Cloud Environment endpoint used for authentication.
49-
By default, this will be set to commercial ('commercial', 'GCCH')
5043
"""
5144
# Security Token Service info
52-
self._sts_profile = STSProfile(resolve_base_url(url), environment)
45+
self._sts_profile = STSProfile(url, environment)
5346
# Obtain authentication cookies, using the browser mode
5447
self._browser_mode = browser_mode
55-
self._environment = environment
5648
# Last occurred error
5749
self.error = ""
5850
self._username = username
@@ -137,7 +129,7 @@ def _acquire_service_token_from_adfs(self, adfs_url):
137129
"password": string_escape(self._password),
138130
"created": datetime_escape(self._sts_profile.created),
139131
"expires": datetime_escape(self._sts_profile.expires),
140-
"issuer": self._sts_profile.tokenIssuer,
132+
"issuer": self._sts_profile.token_issuer,
141133
},
142134
)
143135

@@ -184,13 +176,13 @@ def _acquire_service_token(self):
184176
payload = self._prepare_request_from_template(
185177
"SAML.xml",
186178
{
187-
"auth_url": self._sts_profile.authorityUrl,
179+
"auth_url": self._sts_profile.site_url,
188180
"username": string_escape(self._username),
189181
"password": string_escape(self._password),
190182
"message_id": str(uuid.uuid4()),
191183
"created": datetime_escape(self._sts_profile.created),
192184
"expires": datetime_escape(self._sts_profile.expires),
193-
"issuer": self._sts_profile.tokenIssuer,
185+
"issuer": self._sts_profile.token_issuer,
194186
},
195187
)
196188
logger.debug_secrets("options: %s", payload)

office365/runtime/auth/sts_profile.py

+33-18
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,55 @@
11
from datetime import datetime, timedelta
22

3+
from office365.azure_env import AzureEnvironment
34
from office365.runtime.compat import timezone, urlparse
45

56

67
class STSProfile(object):
7-
def __init__(self, authority_url, environment):
8+
def __init__(self, site_url, environment=None):
89
# type: (str, str) -> None
9-
self.authorityUrl = authority_url
10-
if environment == "GCCH":
11-
self.serviceUrl = "https://login.microsoftonline.us"
12-
else:
13-
self.serviceUrl = "https://login.microsoftonline.com"
14-
self.securityTokenServicePath = "extSTS.srf"
15-
self.userRealmServicePath = "GetUserRealm.srf"
16-
self.tokenIssuer = "urn:federation:MicrosoftOnline"
17-
self.created = datetime.now(tz=timezone.utc)
18-
self.expires = self.created + timedelta(minutes=30)
19-
self.signInPage = "_forms/default.aspx?wa=wsignin1.0"
10+
self._site_url = site_url
11+
self._environment = environment
12+
self._created = datetime.now(tz=timezone.utc)
13+
self._expires = self._created + timedelta(minutes=30)
2014

2115
def reset(self):
2216
"""Renew the expiration time."""
23-
self.created = datetime.now(tz=timezone.utc)
24-
self.expires = self.created + timedelta(minutes=30)
17+
self._created = datetime.now(tz=timezone.utc)
18+
self._expires = self._created + timedelta(minutes=30)
2519

2620
@property
2721
def tenant(self):
28-
return urlparse(self.authorityUrl).netloc
22+
return urlparse(self._site_url).netloc
2923

3024
@property
3125
def security_token_service_url(self):
32-
return "/".join([self.serviceUrl, self.securityTokenServicePath])
26+
return "{0}/extSTS.srf".format(
27+
AzureEnvironment.get_login_authority(self._environment),
28+
)
3329

3430
@property
3531
def signin_page_url(self):
36-
return "/".join([self.authorityUrl, self.signInPage])
32+
site_info = urlparse(self._site_url)
33+
return "{0}://{1}/_forms/default.aspx?wa=wsignin1.0".format(site_info.scheme, site_info.netloc)
3734

3835
@property
3936
def user_realm_service_url(self):
40-
return "/".join([self.serviceUrl, self.userRealmServicePath])
37+
return "{0}/GetUserRealm.srf".format(
38+
AzureEnvironment.get_login_authority(self._environment),
39+
)
40+
41+
@property
42+
def token_issuer(self):
43+
return "urn:federation:MicrosoftOnline"
44+
45+
@property
46+
def created(self):
47+
return self._created
48+
49+
@property
50+
def expires(self):
51+
return self._expires
52+
53+
@property
54+
def site_url(self):
55+
return self._site_url

office365/sharepoint/request.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from requests import Response
44
from typing_extensions import Self
55

6+
from office365.azure_env import AzureEnvironment
67
from office365.runtime.auth.authentication_context import AuthenticationContext
78
from office365.runtime.auth.client_credential import ClientCredential
89
from office365.runtime.auth.token_response import TokenResponse
@@ -14,12 +15,11 @@
1415

1516
class SharePointRequest(ODataRequest):
1617
def __init__(
17-
self, base_url, environment="commercial", allow_ntlm=False, browser_mode=False
18+
self, base_url, environment=AzureEnvironment.Global, allow_ntlm=False, browser_mode=False
1819
):
1920
"""
2021
:param str base_url: Absolute Web or Site Url
2122
:param str environment: The Office 365 Cloud Environment endpoint used for authentication
22-
defaults to 'commercial'.
2323
:param bool allow_ntlm: Flag indicates whether NTLM scheme is enabled. Disabled by default
2424
:param bool browser_mode: Allow browser authentication
2525
"""

0 commit comments

Comments
 (0)