Skip to content

Commit 0885ff8

Browse files
committed
#836 ClientContext.with_client_certificate support for Passphrase if the private_key is encrypted
1 parent 2ed6c44 commit 0885ff8

19 files changed

+83
-21
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Steps to access:
7575
- [Granting access using SharePoint App-Only](https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs)
7676
- [wiki](https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-and-and-SharePoint-2013-2016-2019-on-premises--with-app-principal)
7777
78-
Example: [connect_with_app_principal.py](examples/sharepoint/connect_with_app_only_principal.py)
78+
Example: [connect_with_app_principal.py](examples/sharepoint/auth_app_only.py)
7979
8080
#### 2. Using username and password
8181
@@ -85,15 +85,15 @@ Steps to access:
8585
ctx = ClientContext('{url}').with_credentials(user_credentials)
8686
```
8787
88-
Example: [connect_with_user_credential.py](examples/sharepoint/connect_with_user_credential.py)
88+
Example: [connect_with_user_credential.py](examples/sharepoint/auth_user_credential.py)
8989
9090
#### 3. Using an Azure AD application (certificate credentials flow)
9191
9292
Documentation:
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/connect_with_client_certificate.py)
96+
Example: [connect_with_client_certificate.py](examples/sharepoint/auth_client_certificate.py)
9797
9898
#### 4. Interactive
9999
@@ -104,7 +104,7 @@ Steps to access:
104104
> In Azure Portal, configure the Redirect URI of your
105105
"Mobile and Desktop application" as ``http://localhost``.
106106
107-
Example: [connect_interactive.py](examples/sharepoint/connect_interactive.py)
107+
Example: [connect_interactive.py](examples/sharepoint/auth_interactive.py)
108108
109109
Usage:
110110
```python

examples/communications/create_call.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Create peer-to-peer VoIP call with service hosted media
33
"""
44
from office365.graph_client import GraphClient
5-
from tests.graph_case import acquire_token_by_client_credentials
5+
from tests import test_client_id, test_client_secret, test_tenant
66

7-
client = GraphClient(acquire_token_by_client_credentials)
7+
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
88
call = client.communications.calls.create(
99
"https://mediadev8.com/teamsapp/api/calling"
1010
).execute_query()

examples/outlook/messages/download_with_attachments.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
import tempfile
99

1010
from office365.graph_client import GraphClient
11-
from tests import test_user_principal_name
12-
from tests.graph_case import acquire_token_by_client_credentials
11+
from tests import (
12+
test_client_id,
13+
test_client_secret,
14+
test_tenant,
15+
test_user_principal_name,
16+
)
1317

14-
client = GraphClient(acquire_token_by_client_credentials)
18+
client = GraphClient.with_client_secret(test_tenant, test_client_id, test_client_secret)
1519
user = client.users[test_user_principal_name]
1620
messages = (
1721
user.messages.filter("hasAttachments eq true")

examples/sharepoint/connect_with_client_certificate.py renamed to examples/sharepoint/auth_client_certificate.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
test_client_id,
2121
test_site_url,
2222
test_tenant,
23-
test_tenant_name,
2423
)
2524

2625
cert_credentials = {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
When using SharePoint Online you can define applications in Azure AD and these applications can
3+
be granted permissions to SharePoint, but also to all the other services in Office 365.
4+
This model is the preferred model in case you're using SharePoint Online, if you're using SharePoint on-premises
5+
you have to use the SharePoint Only model via based Azure ACS as described in here:
6+
7+
Demonstrates how to use Azure AD App-Only auth flow
8+
9+
https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
10+
11+
Refer wiki for a more details:
12+
https://github.com/vgrem/Office365-REST-Python-Client/wiki/How-to-connect-to-SharePoint-Online-with-certificate-credentials
13+
14+
To create a self signed certificate with encrypted private key run:
15+
openssl req -x509 -newkey rsa:2048 -keyout selfsignkey.pem -out selfsigncert.pem -days 365
16+
"""
17+
18+
import os
19+
20+
from office365.sharepoint.client_context import ClientContext
21+
from tests import (
22+
test_cert_thumbprint,
23+
test_client_id,
24+
test_site_url,
25+
test_tenant,
26+
)
27+
28+
cert_credentials = {
29+
"tenant": test_tenant,
30+
"client_id": test_client_id,
31+
"thumbprint": test_cert_thumbprint,
32+
"cert_path": "{0}/../selfsignkeyenc.pem".format(os.path.dirname(__file__)),
33+
"passphrase": "Password",
34+
}
35+
ctx = ClientContext(test_site_url).with_client_certificate(**cert_credentials)
36+
current_web = ctx.web.get().execute_query()
37+
print("{0}".format(current_web.url))

0 commit comments

Comments
 (0)