Skip to content

Commit 61535b4

Browse files
authored
Merge pull request #19 from itk-dev-rpa/release-1.0.0
Release 1.0.0
2 parents 4ec0c4d + bef5624 commit 61535b4

27 files changed

+1164
-392
lines changed

.github/workflows/python-publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88

99
name: Upload Python Package
1010

11-
on: workflow_dispatch
11+
on:
12+
release:
13+
types: [published]
1214

1315
permissions:
1416
contents: read
1517

1618
jobs:
1719
deploy:
18-
1920
runs-on: ubuntu-latest
20-
2121
steps:
2222
- uses: actions/checkout@v3
2323
- name: Set up Python

.pylintrc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
[pylint.messages_control]
22
disable =
3-
C0303, # Trailing whitespace
4-
C0103, # Variable names
5-
C0305, # Trailing newlines
6-
C0304, # Missing final line
73
C0301, # Line too long
84
I1101, E1101, # C-modules members
9-
W0621, # Redefine outer name
105
R0913 # Too many arguments
11-
12-
[MASTER]
13-
ignore-paths = ^tests/ # Ignore the tests folder

ITK_dev_shared_components/SAP/sap_login.py

Lines changed: 0 additions & 119 deletions
This file was deleted.

ITK_dev_shared_components/SAP/tree_util.py

Lines changed: 0 additions & 71 deletions
This file was deleted.
File renamed without changes.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""This module is responsible for authenticating a Microsoft Graph
2+
connection."""
3+
4+
import msal
5+
6+
# pylint: disable-next=too-few-public-methods
7+
class GraphAccess:
8+
"""An object that handles access to the Graph api.
9+
This object should not be created directly but instead
10+
using one of the authorize methods in the graph.authentication module.
11+
"""
12+
def __init__(self, app: msal.PublicClientApplication, scopes: list[str]) -> str:
13+
self.app = app
14+
self.scopes = scopes
15+
16+
def get_access_token(self):
17+
"""Get the access token to Graph.
18+
This function automatically reuses an existing token
19+
or refreshes an expired one.
20+
21+
Raises:
22+
RuntimeError: If the access token couldn't be acquired.
23+
24+
Returns:
25+
str: The Graph access token.
26+
"""
27+
account = self.app.get_accounts()[0]
28+
token = self.app.acquire_token_silent(self.scopes, account)
29+
30+
if "access_token" in token:
31+
return token['access_token']
32+
33+
if 'error_description' in token:
34+
raise RuntimeError(f"Token could not be acquired. {token['error_description']}")
35+
36+
raise RuntimeError("Something went wrong. No error description was returned from Graph.")
37+
38+
39+
def authorize_by_username_password(username: str, password: str, *, client_id: str, tenant_id: str) -> GraphAccess:
40+
"""Get a bearer token for the given user.
41+
This is used in most other Graph API calls.
42+
43+
Args:
44+
username: The username of the user (email address).
45+
password: The password of the user.
46+
client_id: The Graph API client id in 8-4-4-12 format.
47+
tenant_id: The Graph API tenant id in 8-4-4-12 format.
48+
49+
Returns:
50+
GraphAccess: The GraphAccess object used to authorize Graph access.
51+
"""
52+
authority = f"https://login.microsoftonline.com/{tenant_id}"
53+
scopes = ["https://graph.microsoft.com/.default"]
54+
55+
app = msal.PublicClientApplication(client_id, authority=authority)
56+
app.acquire_token_by_username_password(username, password, scopes)
57+
58+
graph_access = GraphAccess(app, scopes)
59+
60+
# Test connection
61+
graph_access.get_access_token()
62+
63+
return graph_access

0 commit comments

Comments
 (0)