Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 3 additions & 32 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,15 @@ jobs:
with:
args: "check --fix"
version: ${{ env.RUFF_VERSION }}
# A proposal to replace black & pylint
# - uses: chartboost/ruff-action@v1
# with:
# args: "format --check"
# version: ${{ env.RUFF_VERSION }}

black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v3
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- uses: psf/black@24.4.2

pylint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/setup-python@v4
with:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Install uv
uses: astral-sh/setup-uv@v7
- uses: chartboost/ruff-action@v1
with:
version: ${{ env.UV_VERSION }}
- name: Install Requirements
run: uv sync --frozen --all-extras
- name: Run Pylint
run: |
uv run pylint office365
args: "format --check"
version: ${{ env.RUFF_VERSION }}

pytest:
runs-on: ubuntu-latest
needs:
- ruff
- black
- pylint
steps:
- uses: actions/checkout@v6
- name: Set up Python ${{ env.DEFAULT_PYTHON }}
Expand Down
2 changes: 0 additions & 2 deletions .isort.cfg

This file was deleted.

20 changes: 3 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,8 @@ repos:
language: system
pass_filenames: false

# A proposal to replace black & pylint
# - id: ruff-format
# name: ruff-format
# entry: uv run ruff format --exit-non-zero-on-fix
# language: system
# pass_filenames: false

- id: black
name: black
entry: uv run black .
- id: ruff-format
name: ruff-format
entry: uv run ruff format --exit-non-zero-on-fix
language: system
pass_filenames: false

- id: pylint
name: pylint
entry: uv run pylint office365
language: system
pass_filenames: false

9 changes: 2 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ prek run --all-files

The project uses the following tools (mirroring CI):

- Black (formatting)
- Ruff (linting and import sorting)
- Pylint (static analysis)
- Ruff (linting and formatting)

Line length: 121 characters (configured in `pyproject.toml`).

Expand Down Expand Up @@ -127,10 +125,7 @@ CI note: Full E2E tests in CI rely on repository secrets and may not run on fork
### Pull Request Process

1. CI Checks must pass:
- Ruff linting
- Black formatting
- Pylint analysis
- Pytest execution
- Ruff linting and formatting
2. Await maintainer review
3. Update documentation where applicable

Expand Down
9 changes: 4 additions & 5 deletions examples/auth/gcc_high.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Connect via national clouds (Microsoft 365 GCC High environment)
"""Connect via national clouds (Microsoft 365 GCC High environment)

Microsoft Graph for US Government L4: https://graph.microsoft.us
"""
Expand All @@ -13,7 +12,7 @@
test_user_principal_name,
)

client = GraphClient(
tenant=test_tenant, environment=AzureEnvironment.USGovernmentHigh
).with_client_secret(test_client_id, test_client_secret)
client = GraphClient(tenant=test_tenant, environment=AzureEnvironment.USGovernmentHigh).with_client_secret(
test_client_id, test_client_secret
)
messages = client.users[test_user_principal_name].messages.get().execute_query()
8 changes: 4 additions & 4 deletions examples/auth/interactive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to login when the user may be prompted for input by the authorization server.
"""Demonstrates how to login when the user may be prompted for input by the authorization server.
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.

Expand All @@ -9,13 +8,14 @@

https://learn.microsoft.com/en-us/azure/active-directory/develop/
msal-authentication-flows#interactive-and-non-interactive-authentication

"""

from office365.graph_client import GraphClient
from tests import test_client_id, test_tenant

client = GraphClient(tenant=test_tenant).with_token_interactive(test_client_id)
me = client.me.get().execute_query()
print("Welcome, {0}!".format(me.given_name))
print(f"Welcome, {me.given_name}!")
site = client.sites.root.get().execute_query()
print("Site Url: {0}".format(site.web_url))
print(f"Site Url: {site.web_url}")
6 changes: 3 additions & 3 deletions examples/auth/interactive_custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to login when the user may be prompted for input by the authorization server.
"""Demonstrates how to login when the user may be prompted for input by the authorization server.
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.

Expand All @@ -9,6 +8,7 @@

https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows
#interactive-and-non-interactive-authentication

"""

import msal
Expand All @@ -20,7 +20,7 @@
def acquire_token():
app = msal.PublicClientApplication(
test_client_id,
authority="https://login.microsoftonline.com/{0}".format(test_tenant),
authority=f"https://login.microsoftonline.com/{test_tenant}",
client_credential=None,
)
scopes = ["https://graph.microsoft.com/.default"]
Expand Down
3 changes: 1 addition & 2 deletions examples/auth/sharepoint/interactive.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to login when the user may be prompted for input by the authorization server.
"""Demonstrates how to login when the user may be prompted for input by the authorization server.
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.

Expand Down
7 changes: 3 additions & 4 deletions examples/auth/sharepoint/interactive_custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to login when the user may be prompted for input by the authorization server.
"""Demonstrates how to login when the user may be prompted for input by the authorization server.
For example, to sign in, perform multi-factor authentication (MFA), or to grant consent
to more resource access permissions.

Expand All @@ -19,10 +18,10 @@
def acquire_token():
app = msal.PublicClientApplication(
test_client_id,
authority="https://login.microsoftonline.com/{0}".format(test_tenant),
authority=f"https://login.microsoftonline.com/{test_tenant}",
client_credential=None,
)
scopes = ["https://{0}.sharepoint.com/.default".format(test_tenant_name)]
scopes = [f"https://{test_tenant_name}.sharepoint.com/.default"]
result = app.acquire_token_interactive(scopes=scopes)
return TokenResponse.from_json(result)

Expand Down
8 changes: 2 additions & 6 deletions examples/auth/sharepoint/list_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@
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)

resource = client.service_principals.get_by_app_id(
"00000003-0000-0ff1-ce00-000000000000"
)
resource = client.service_principals.get_by_app_id("00000003-0000-0ff1-ce00-000000000000")
# principal = client.service_principals.get_by_app_id(test_client_id).get().execute_query()
result = resource.get_application_permissions(test_client_id).execute_query()
for app_role in result.value:
Expand Down
11 changes: 3 additions & 8 deletions examples/auth/sharepoint/register_apponly.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Setting up an Azure AD app for app-only access for SharePoint API
"""Setting up an Azure AD app for app-only access for SharePoint API

Steps:

Expand All @@ -20,12 +19,8 @@
from office365.graph_client import GraphClient
from tests import test_client_id, test_client_secret, 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)
# client = GraphClient(tenant=test_tenant).with_token_interactive(test_client_id)
resource = client.service_principals.get_by_app_id(
MsAppIds.Office_365_SharePoint_Online
)
resource = client.service_principals.get_by_app_id(MsAppIds.Office_365_SharePoint_Online)
app = client.applications.get_by_app_id(test_client_id)
resource.grant_application_permissions(app, "Sites.FullControl.All").execute_query()
5 changes: 2 additions & 3 deletions examples/auth/with_adal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to acquire access token via ADAL library
"""Demonstrates how to acquire access token via ADAL library

Note: ADAL for Python is no longer receive new feature improvement. Its successor, MSAL for Python,
are now generally available.
Expand All @@ -12,7 +11,7 @@
def acquire_token():
import adal # pylint: disable=E0401

authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant_name)
authority_url = f"https://login.microsoftonline.com/{test_tenant_name}"
auth_ctx = adal.AuthenticationContext(authority_url)
token = auth_ctx.acquire_token_with_username_password(
"https://graph.microsoft.com",
Expand Down
11 changes: 4 additions & 7 deletions examples/auth/with_client_cert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Demonstrates how to acquire a token by using certificate credentials.
"""Demonstrates how to acquire a token by using certificate credentials.

https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#certificates
"""
Expand All @@ -9,10 +8,10 @@


def acquire_token():
with open(test_cert_path, "r", encoding="utf-8") as f:
with open(test_cert_path, encoding="utf-8") as f:
private_key = f.read()

authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant_name)
authority_url = f"https://login.microsoftonline.com/{test_tenant_name}"
credentials = {"thumbprint": test_cert_thumbprint, "private_key": private_key}
import msal

Expand All @@ -21,9 +20,7 @@ def acquire_token():
authority=authority_url,
client_credential=credentials,
)
result = app.acquire_token_for_client(
scopes=["https://graph.microsoft.com/.default"]
)
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
return result


Expand Down
7 changes: 2 additions & 5 deletions examples/auth/with_client_secret.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Acquires a token by using application secret
"""Acquires a token by using application secret

The following options are supported:
- utilize built in GraphClient(tenant=tenant).with_client_secret(client_id, client_secret) method
Expand All @@ -11,8 +10,6 @@
from office365.graph_client import GraphClient
from tests import test_client_id, test_client_secret, 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)
root_site = client.sites.root.get().execute_query()
print(root_site.web_url)
5 changes: 2 additions & 3 deletions examples/auth/with_client_secret_custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Acquires a token by using application secret
"""Acquires a token by using application secret

The following options are supported:
- utilize built in GraphClient(tenant=tenant).with_client_secret(client_id, client_secret) method
Expand All @@ -15,7 +14,7 @@


def acquire_token():
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant)
authority_url = f"https://login.microsoftonline.com/{test_tenant}"
app = msal.ConfidentialClientApplication(
authority=authority_url,
client_id=test_client_id,
Expand Down
7 changes: 2 additions & 5 deletions examples/auth/with_user_creds.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"""
Username Password Authentication flow
"""Username Password Authentication flow

https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication
"""

from office365.graph_client import GraphClient
from tests import test_client_id, test_password, test_tenant, test_username

client = GraphClient(tenant=test_tenant).with_username_and_password(
test_client_id, test_username, test_password
)
client = GraphClient(tenant=test_tenant).with_username_and_password(test_client_id, test_username, test_password)
me = client.me.get().execute_query()
print(me)
9 changes: 3 additions & 6 deletions examples/auth/with_user_creds_custom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Username Password Authentication flow
"""Username Password Authentication flow

https://github.com/AzureAD/microsoft-authentication-library-for-python/wiki/Username-Password-Authentication
"""
Expand All @@ -11,10 +10,8 @@


def acquire_token():
authority_url = "https://login.microsoftonline.com/{0}".format(test_tenant)
app = msal.PublicClientApplication(
authority=authority_url, client_id=test_client_id
)
authority_url = f"https://login.microsoftonline.com/{test_tenant}"
app = msal.PublicClientApplication(authority=authority_url, client_id=test_client_id)

result = app.acquire_token_by_username_password(
username=test_user_credentials.userName,
Expand Down
7 changes: 2 additions & 5 deletions examples/directory/applications/add_cert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Add a certificate to an app using Microsoft Graph
"""Add a certificate to an app using Microsoft Graph

To create the self-signed certificate, run the following command at a terminal prompt:
- openssl req -x509 -newkey rsa:2048 -keyout selfsignkey.pem -out selfsigncert.pem -nodes -days 365
Expand All @@ -12,9 +11,7 @@

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

client = GraphClient(tenant=test_tenant).with_username_and_password(
test_client_id, test_username, test_password
)
client = GraphClient(tenant=test_tenant).with_username_and_password(test_client_id, test_username, test_password)
target_app = client.applications.get_by_app_id(test_client_id)
with open(cert_path, "rb") as f:
cert_data = f.read()
Expand Down
7 changes: 2 additions & 5 deletions examples/directory/applications/app_password.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""
Adds a strong password or secret to an application.
"""Adds a strong password or secret to an application.
https://learn.microsoft.com/en-us/graph/api/application-addpassword?view=graph-rest-1.0
"""

Expand All @@ -12,9 +11,7 @@
test_username,
)

client = GraphClient(tenant=test_tenant).with_username_and_password(
test_client_id, test_username, test_password
)
client = GraphClient(tenant=test_tenant).with_username_and_password(test_client_id, test_username, test_password)
target_app = client.applications.get_by_app_id(test_client_credentials.clientId)
result = target_app.add_password("Password friendly name").execute_query()
print(result.value)
Loading