Skip to content

Commit 091ab2a

Browse files
author
Matthew Fortunka
committed
added timeouts to graph and REST calls
1 parent 408144f commit 091ab2a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

api_app/services/aad_authentication.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727

2828
MICROSOFT_GRAPH_URL = config.MICROSOFT_GRAPH_URL.strip("/")
29+
GRAPH_REQUEST_TIMEOUT = 10
2930

3031

3132
class PrincipalType(Enum):
@@ -325,7 +326,8 @@ def get_assignable_users(self, filter: str = "", maxResultCount: int = 5) -> Lis
325326
users_endpoint = f"{MICROSOFT_GRAPH_URL}/v1.0/users?$filter=startswith(displayName,'{filter}')&$top={maxResultCount}"
326327

327328
graph_data = requests.get(users_endpoint,
328-
headers=self._get_auth_header(msgraph_token)).json()
329+
headers=self._get_auth_header(msgraph_token),
330+
timeout=GRAPH_REQUEST_TIMEOUT).json()
329331
result = []
330332

331333
for user_data in graph_data["value"]:
@@ -443,7 +445,7 @@ def _assign_workspace_user_to_application(self, user_id: str, workspace: Workspa
443445
"appRoleId": role_id,
444446
}
445447

446-
response = requests.post(url, json=body, headers=self._get_auth_header(msgraph_token))
448+
response = requests.post(url, json=body, headers=self._get_auth_header(msgraph_token), timeout=GRAPH_REQUEST_TIMEOUT)
447449
return response
448450

449451
def _get_role_assignment_for_user(self, user_id: str, role_id: str) -> dict:
@@ -468,7 +470,7 @@ def _remove_workspace_user_from_application(self, user_id: str, role_id: str) ->
468470

469471
msgraph_token = self._get_msgraph_token()
470472
url = f"{MICROSOFT_GRAPH_URL}/v1.0/users/{user_id}/appRoleAssignments/{role_assignment['id']}"
471-
response = requests.delete(url, headers=self._get_auth_header(msgraph_token))
473+
response = requests.delete(url, headers=self._get_auth_header(msgraph_token), timeout=GRAPH_REQUEST_TIMEOUT)
472474
return response
473475

474476
def _get_batch_users_by_role_assignments_body(self, roles_graph_data):
@@ -519,9 +521,9 @@ def _ms_graph_query(self, url: str, http_method: str, json=None) -> dict:
519521
break
520522
logger.debug(f"Making request to: {url}")
521523
if json:
522-
response = requests.request(method=http_method, url=url, json=json, headers=auth_headers)
524+
response = requests.request(method=http_method, url=url, json=json, headers=auth_headers, timeout=GRAPH_REQUEST_TIMEOUT)
523525
else:
524-
response = requests.request(method=http_method, url=url, headers=auth_headers)
526+
response = requests.request(method=http_method, url=url, headers=auth_headers, timeout=GRAPH_REQUEST_TIMEOUT)
525527
url = ""
526528
if response.status_code == 200:
527529
json_response = response.json()

0 commit comments

Comments
 (0)