Skip to content

Commit

Permalink
#735 fix for SiteProperties.update() and examples updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Nov 5, 2023
1 parent 08e2eb5 commit 3932c7f
Show file tree
Hide file tree
Showing 15 changed files with 169 additions and 161 deletions.
File renamed without changes.
18 changes: 0 additions & 18 deletions examples/directory/applications/add_cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,10 @@
from tests import test_client_id, test_tenant
from tests.graph_case import acquire_token_by_username_password


def verify_connect():
"""Test the app-only authentication"""

thumbprint = "12FC1BB6796D114AF4FEBBE95FCA8084CF47D81F"
cert_key_path = "../../selfsignkey.pem"
with open(cert_key_path, "r") as fh:
private_key = fh.read()

ctx = GraphClient.with_certificate(
test_tenant, test_client_id, thumbprint, private_key
)
site = ctx.sites.root.get().execute_query()
print(site.web_url)


cert_path = "../../selfsigncert.pem"

client = GraphClient(acquire_token_by_username_password)
target_app = client.applications.get_by_app_id(test_client_id)
with open(cert_path, "rb") as f:
cert_data = f.read()
target_app.add_certificate(cert_data, "Internet Widgits Pty Ltd").execute_query()

verify_connect()
2 changes: 1 addition & 1 deletion examples/directory/applications/get_by_app_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
.get()
.execute_query()
)
print(app.display_name)
print(app)
29 changes: 29 additions & 0 deletions examples/directory/applications/grant_perms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
"""
Grant or revoke API permissions
Steps:
1. Register an application with Azure AD...
2. Addressing an application or a service principal object
3. Configure other basic properties for your app
4. Limit app sign-in to only assigned identities
5. Assign permissions to an app
6. Create app roles
7. Manage owners
https://learn.microsoft.com/en-us/graph/permissions-grant-via-msgraph?tabs=http&pivots=grant-application-permissions
"""

Expand All @@ -11,6 +21,22 @@
test_tenant,
)


def verify_connect():
"""Test the app-only authentication"""

thumbprint = "12FC1BB6796D114AF4FEBBE95FCA8084CF47D81F"
cert_key_path = "../../selfsignkey.pem"
with open(cert_key_path, "r") as fh:
private_key = fh.read()

ctx = GraphClient.with_certificate(
test_tenant, test_client_id, thumbprint, private_key
)
site = ctx.sites.root.get().execute_query()
print(site.web_url)


client = GraphClient.with_token_interactive(
test_tenant, test_client_id, test_admin_principal_name
)
Expand All @@ -36,3 +62,6 @@
result = resource.app_role_assigned_to.get_all().execute_query()
for app_role_assignment in result:
print(app_role_assignment)


verify_connect()
20 changes: 0 additions & 20 deletions examples/directory/applications/register.py

This file was deleted.

Empty file.
6 changes: 4 additions & 2 deletions examples/sharepoint/connect_with_client_certificate_scopes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""
Demonstrates how to authenticate using App-Only flow
Refer this article for a detailed instruction:
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
"""
import os

Expand All @@ -9,7 +12,6 @@
test_client_id,
test_site_url,
test_tenant,
test_tenant_name,
)

cert_credentials = {
Expand All @@ -22,4 +24,4 @@

ctx = ClientContext(test_site_url).with_client_certificate(**cert_credentials)
current_web = ctx.web.get().execute_query()
print("{0}".format(current_web.url))
print(current_web)
31 changes: 31 additions & 0 deletions examples/sharepoint/tenant/change_sharing_capability.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Set external sharing on site collections in Office 365
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/set-external-sharing-on-site-collections-in-office-365
"""

from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.tenant.administration.sharing_capabilities import (
SharingCapabilities,
)
from tests import (
test_admin_site_url,
test_cert_thumbprint,
test_client_id,
test_team_site_url,
test_tenant,
)

admin_client = ClientContext(test_admin_site_url).with_client_certificate(
test_tenant, test_client_id, test_cert_thumbprint, "../../selfsignkey.pem"
)

# admin_client = ClientContext(test_admin_site_url).with_credentials(
# test_admin_credentials
# )

site_props = admin_client.tenant.get_site_properties_by_url(
test_team_site_url
).execute_query()
site_props.sharing_capability = SharingCapabilities.ExternalUserAndGuestSharing
site_props.update().execute_query()
6 changes: 4 additions & 2 deletions examples/sharepoint/users/whoami.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import json
"""
Retrieves the current user details
"""

from office365.sharepoint.client_context import ClientContext
from tests import test_client_credentials, test_team_site_url

ctx = ClientContext(test_team_site_url).with_credentials(test_client_credentials)
whoami = ctx.web.current_user.get().execute_query()
print(json.dumps(whoami.to_json(), indent=4))
print(whoami)
17 changes: 11 additions & 6 deletions office365/directory/serviceprincipals/service_principal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
class ServicePrincipal(DirectoryObject):
"""Represents an instance of an application in a directory."""

def __str__(self):
return self.display_name

def add_key(self, key_credential, password_credential, proof):
"""
Adds a key credential to a servicePrincipal. This method along with removeKey can be used by a servicePrincipal
Expand Down Expand Up @@ -185,9 +188,7 @@ def app_description(self):
@property
def app_display_name(self):
# type: () -> Optional[str]
"""
The display name exposed by the associated application.
"""
"""The display name exposed by the associated application."""
return self.properties.get("appDisplayName", None)

@property
Expand All @@ -209,12 +210,16 @@ def app_roles(self):
"""
return self.properties.get("appRoles", AppRoleCollection())

@property
def display_name(self):
# type: () -> Optional[str]
"""The display name."""
return self.properties.get("displayName", None)

@property
def homepage(self):
# type: () -> Optional[str]
"""
Home page or landing page of the application.
"""
"""Home page or landing page of the application"""
return self.properties.get("homepage", None)

@property
Expand Down
28 changes: 0 additions & 28 deletions office365/sharepoint/internal/key_value.py

This file was deleted.

9 changes: 9 additions & 0 deletions office365/sharepoint/internal/paths/static_operation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import Dict, Optional

from office365.runtime.paths.service_operation import ServiceOperationPath


class StaticOperationPath(ServiceOperationPath):
def __init__(self, static_name, parameters=None):
# type: (str, Optional[Dict]) -> None
super(StaticOperationPath, self).__init__(static_name, parameters)
Loading

0 comments on commit 3932c7f

Please sign in to comment.