Skip to content

Commit 31d8f21

Browse files
committed
refactor auth module, new types for tenant namespace
1 parent 93c400d commit 31d8f21

29 files changed

+374
-51
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Steps to access:
9393
- [Granting access via Azure AD App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread)
9494
- [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-with-certificate-credentials)
9595
96-
Example: [connect_with_client_certificate.py](examples/sharepoint/auth_client_certificate.py)
96+
Example: [with_certificate.py](examples/sharepoint/auth_certificate.py)
9797
9898
#### 4. Interactive
9999

generator/import_metadata.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ def export_to_file(path, content):
2626
"--endpoint",
2727
dest="endpoint",
2828
help="Import metadata endpoint",
29-
default="graph",
29+
default="sharepoint",
3030
)
3131
parser.add_argument(
3232
"-p",
3333
"--path",
3434
dest="path",
35-
default="./metadata/Graph.xml",
35+
default="./metadata/SharePoint.xml",
3636
help="Import metadata endpoint",
3737
)
3838

generator/metadata/SharePoint.xml

+107-14
Large diffs are not rendered by default.

office365/runtime/auth/authentication_context.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33
from typing import Any, Callable
44

5-
from typing_extensions import Required, TypedDict
5+
from typing_extensions import Required, Self, TypedDict
66

77
from office365.runtime.auth.client_credential import ClientCredential
88
from office365.runtime.auth.providers.acs_token_provider import ACSTokenProvider
@@ -211,12 +211,14 @@ def with_credentials(self, credentials):
211211
raise ValueError("Unknown credential type")
212212

213213
def _authenticate(request):
214+
# type: (RequestOptions) -> None
214215
provider.authenticate_request(request)
215216

216217
self._authenticate = _authenticate
217218
return self
218219

219220
def acquire_token_for_user(self, username, password):
221+
# type: (str, str) -> Self
220222
"""
221223
Initializes a client to acquire a token via user credentials
222224
Status: deprecated!
@@ -227,6 +229,7 @@ def acquire_token_for_user(self, username, password):
227229
provider = SamlTokenProvider(self.url, username, password, self._browser_mode)
228230

229231
def _authenticate(request):
232+
# type: (RequestOptions) -> None
230233
provider.authenticate_request(request)
231234

232235
self._authenticate = _authenticate
@@ -244,6 +247,7 @@ def acquire_token_for_app(self, client_id, client_secret):
244247
provider = ACSTokenProvider(self.url, client_id, client_secret)
245248

246249
def _authenticate(request):
250+
# type: (RequestOptions) -> None
247251
provider.authenticate_request(request)
248252

249253
self._authenticate = _authenticate

office365/runtime/auth/providers/acs_token_provider.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@ def __init__(self, url, client_id, client_secret, environment="commercial"):
3131

3232
def authenticate_request(self, request):
3333
# type: (RequestOptions) -> None
34-
self.ensure_app_only_access_token()
34+
self._ensure_app_only_access_token()
3535
request.set_header("Authorization", self._get_authorization_header())
3636

37-
def ensure_app_only_access_token(self):
38-
if self._cached_token is None:
39-
self._cached_token = self.get_app_only_access_token()
40-
return self._cached_token and self._cached_token.is_valid
41-
4237
def get_app_only_access_token(self):
4338
"""Retrieves an app-only access token from ACS"""
4439
try:
@@ -53,6 +48,11 @@ def get_app_only_access_token(self):
5348
)
5449
raise ValueError(self.error)
5550

51+
def _ensure_app_only_access_token(self):
52+
if self._cached_token is None:
53+
self._cached_token = self.get_app_only_access_token()
54+
return self._cached_token and self._cached_token.is_valid
55+
5656
def _get_app_only_access_token(self, target_host, target_realm):
5757
"""
5858
Retrieves an app-only access token from ACS to call the specified principal
@@ -119,6 +119,3 @@ def get_security_token_service_url(realm, environment):
119119

120120
def _get_authorization_header(self):
121121
return "Bearer {0}".format(self._cached_token.accessToken)
122-
123-
def get_last_error(self):
124-
return self.error

office365/runtime/auth/providers/saml_token_provider.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,6 @@ def _get_user_realm(self):
132132
return info
133133
return None
134134

135-
def get_last_error(self):
136-
return self.error
137-
138135
def _acquire_service_token_from_adfs(self, adfs_url):
139136
logger = self.logger(self._acquire_service_token_from_adfs.__name__)
140137

@@ -312,19 +309,16 @@ def _get_authentication_cookie(self, security_token, federated=False):
312309
raise ValueError(self.error)
313310
return cookies
314311

315-
@staticmethod
316-
def _prepare_request_from_template(template_name, params):
312+
def _prepare_request_from_template(self, template_name, params):
317313
"""Construct the request body to acquire security token from STS endpoint"""
318-
logger = SamlTokenProvider.logger()
314+
logger = self.logger(self._prepare_request_from_template.__name__)
319315
logger.debug_secrets("params: %s", params)
320-
f = open(
321-
os.path.join(os.path.dirname(__file__), "templates", template_name),
322-
encoding="utf8",
316+
317+
template_path = os.path.join(
318+
os.path.dirname(__file__), "templates", template_name
323319
)
324-
try:
320+
321+
with open(template_path, encoding="utf8") as f:
325322
data = f.read()
326-
for key in params:
327-
data = data.replace("{" + key + "}", str(params[key]))
328-
return data
329-
finally:
330-
f.close()
323+
324+
return data.format(**params)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class CustomFontsResource(ClientValue):
5+
""" """
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from office365.runtime.client_result import ClientResult
2+
from office365.runtime.queries.service_operation import ServiceOperationQuery
3+
from office365.sharepoint.entity import Entity
4+
5+
6+
class PolicyLicenseUtilities(Entity):
7+
""""""
8+
9+
@staticmethod
10+
def check_tenant_m365_copilot_business_chat_license(context, return_type=None):
11+
""""""
12+
if return_type is None:
13+
return_type = ClientResult(context, bool())
14+
qry = ServiceOperationQuery(
15+
PolicyLicenseUtilities(context),
16+
"CheckTenantM365CopilotBusinessChatLicense",
17+
None,
18+
None,
19+
None,
20+
return_type,
21+
)
22+
context.add_query(qry)
23+
return return_type
24+
25+
@property
26+
def entity_type_name(self):
27+
return "SP.Utilities.PolicyLicenseUtilities"

office365/sharepoint/sharing/information.py

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def access_request_settings(self):
2727

2828
@property
2929
def anonymous_link_expiration_restriction_days(self):
30+
# type: () -> Optional[int]
3031
"""Tenant's anonymous link expiration restriction in days."""
3132
return self.properties.get("anonymousLinkExpirationRestrictionDays", None)
3233

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
from office365.runtime.client_value import ClientValue
2+
from office365.runtime.client_value_collection import ClientValueCollection
3+
from office365.sharepoint.tenant.administration.modified_property import (
4+
ModifiedProperty,
5+
)
26

37

48
class AuditData(ClientValue):
9+
10+
def __init__(self, client_ip=None, correlation_id=None, modified_properties=None):
11+
self.ClientIP = client_ip
12+
self.CorrelationId = correlation_id
13+
self.ModifiedProperties = ClientValueCollection(
14+
ModifiedProperty, modified_properties
15+
)
16+
517
@property
618
def entity_type_name(self):
719
return "Microsoft.SharePoint.Administration.TenantAdmin.AuditData"
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
from office365.runtime.client_value import ClientValue
2+
from office365.sharepoint.tenant.administration.audit.data import AuditData
23

34

45
class UnifiedAuditRecord(ClientValue):
6+
7+
def __init__(
8+
self,
9+
audit_data=AuditData(),
10+
creation_date=None,
11+
operation=None,
12+
record_id=None,
13+
record_type=None,
14+
user_id=None,
15+
):
16+
self.AuditData = audit_data
17+
self.CreationDate = creation_date
18+
self.Operation = operation
19+
self.RecordId = record_id
20+
self.RecordType = record_type
21+
self.UserId = user_id
22+
523
@property
624
def entity_type_name(self):
725
return "Microsoft.SharePoint.Administration.TenantAdmin.UnifiedAuditRecord"

office365/sharepoint/tenant/administration/coms/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class MessagesFieldsData(ClientValue):
5+
""" """
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from office365.runtime.paths.resource_path import ResourcePath
2+
from office365.sharepoint.entity import Entity
3+
4+
5+
class Office365CommsMessagesServiceProxy(Entity):
6+
""""""
7+
8+
def __init__(self, context):
9+
static_path = ResourcePath(
10+
"Microsoft.Online.SharePoint.TenantAdministration.Office365CommsMessagesServiceProxy"
11+
)
12+
super(Office365CommsMessagesServiceProxy, self).__init__(context, static_path)
13+
14+
@property
15+
def entity_type_name(self):
16+
return "Microsoft.Online.SharePoint.TenantAdministration.Office365CommsMessagesServiceProxy"

office365/sharepoint/tenant/administration/datagovernance/insight_client.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class SPDataGovernanceInsightRestApiClient(SPDataGovernanceRestApiClientBase):
2020
def __init__(self, context, authorization_header, url, user_agent):
2121
# type: (ClientContext, str, str, str) -> None
2222
static_path = ServiceOperationPath(
23-
"SP.AppContextSite",
23+
"Microsoft.Online.SharePoint.TenantAdministration.SPDataGovernanceInsightRestApiClient",
2424
{
2525
"authorizationHeader": authorization_header,
2626
"url": url,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class ModifiedProperty(ClientValue):
5+
""""""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from office365.runtime.client_value import ClientValue
2+
3+
4+
class TenantAdminRecentAction(ClientValue):
5+
""""""
6+
7+
@property
8+
def entity_type_name(self):
9+
return "Microsoft.SharePoint.Administration.TenantAdmin.TenantAdminRecentAction"

office365/sharepoint/tenant/administration/settings_service.py

+18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from office365.sharepoint.tenant.administration.types import (
1111
AutoQuotaEnabled,
1212
DisableGroupify,
13+
DisableSelfServiceSiteCreation,
14+
EnableAutoNewsDigest,
1315
)
1416

1517

@@ -56,6 +58,18 @@ def disable_groupify(self):
5658
"""
5759
return self.properties.get("DisableGroupify", DisableGroupify())
5860

61+
@property
62+
def disable_self_service_site_creation(self):
63+
""" " """
64+
return self.properties.get(
65+
"DisableSelfServiceSiteCreation", DisableSelfServiceSiteCreation()
66+
)
67+
68+
@property
69+
def enable_auto_news_digest(self):
70+
""" " """
71+
return self.properties.get("EnableAutoNewsDigest", EnableAutoNewsDigest())
72+
5973
@property
6074
def smtp_server(self):
6175
"""Specifies the server address or endpoint of the SMTP server that SharePoint Online or tenant-related
@@ -75,6 +89,10 @@ def get_property(self, name, default_value=None):
7589
if default_value is None:
7690
property_mapping = {
7791
"AvailableManagedPathsForSiteCreation": self.available_managed_paths_for_site_creation,
92+
"AutoQuotaEnabled": self.auto_quota_enabled,
93+
"DisableGroupify": self.disable_groupify,
94+
"DisableSelfServiceSiteCreation": self.disable_self_service_site_creation,
95+
"EnableAutoNewsDigest": self.enable_auto_news_digest,
7896
"SmtpServer": self.smtp_server,
7997
"TenantDefaultTimeZoneId": self.tenant_default_time_zone_id,
8098
}

office365/sharepoint/tenant/administration/tenant.py

+10
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,16 @@ def migration_center(self):
825825

826826
return MigrationCenterServices(self.context)
827827

828+
@property
829+
def comms_messages(self):
830+
""" """
831+
832+
from office365.sharepoint.tenant.administration.coms.messages_service_proxy import (
833+
Office365CommsMessagesServiceProxy,
834+
)
835+
836+
return Office365CommsMessagesServiceProxy(self.context)
837+
828838
@property
829839
def multi_geo(self):
830840
""" """

0 commit comments

Comments
 (0)