diff --git a/server.py b/server.py index e7c927c..be17447 100644 --- a/server.py +++ b/server.py @@ -297,49 +297,38 @@ async def create_files(request: Request): #, authorized: bool = Depends(verify_t with open(json_filename, 'rb') as file: files = {'file': (json_filename, file, 'application/json')} - with httpx.Client() as client: - # Send POST request to Azure AD token endpoint - azure_response = client.post( - url=f'https://login.microsoftonline.com/{PRES_API_TENANT_ID}/oauth2/v2.0/token', - data={ - 'grant_type': 'client_credentials', - 'client_id': PRES_API_CLIENT_ID, # The client ID of your registered app - 'client_secret': PRES_API_CLIENT_SECRET, # The secret you created for the app - 'scope': f'api://{PRES_API_CLIENT_ID}/.default', # API permission scope (app-to-app) - } + timeout = httpx.Timeout(3000.0, read=3000.0) + with httpx.Client(timeout=timeout) as client: + token = jwt.encode( + {"pld": "editor-api-source"}, + key=AAD_CLIENT_SECRET, + algorithm="HS256", ) - # Check for successful response - if azure_response.status_code == 200: - # Extract access token from the response - token = azure_response.json().get('access_token') - print(token) - if token: - print("Access token successfully acquired.") - # Step 3: Use the access token to call the protected API - url = f"https://{PRES_API_HOST}/admin/file" - headers = { - "Authorization": f"Bearer {token}", - "Content-Type": "application/json", - } - response = client.put(url, files=files, headers=headers) - return { - "status_code": response.status_code, - "response_body": response.json() - } - else: - return { - "success" : False, - "message" : "No access token in the response." - } + print(token) + if token: + print("Access token successfully acquired.") + url = f"https://{PRES_API_HOST}/admin/file" + headers = { + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", + } + response = client.put(url, files=files, headers=headers) + return { + "status_code": response.status_code, + "response_body": response.json() + } else: - # If the response status is not 200, print the error return { "success" : False, - "message" : f"Error: {azure_response.status_code} - {azure_response.text}" + "message" : "No access token in the response." } + return { + "success" : False, + "message" : f"Error sending request to API." + } return { "success" : False, - "message" : f"Oops - this shouldn't happen. Your manifest was not saved." + "message" : f"Error compiling JSON data." } @app.post("/uploadfiles/{prefix}/{noid}") @@ -529,3 +518,27 @@ async def protected_endpoint(user: OpenID = Depends(get_logged_user)): """ # https://stackoverflow.com/questions/45244998/azure-ad-authentication-python-web-api + +''' +# Send POST request to Azure AD token endpoint +azure_response = client.post( + url=f'https://login.microsoftonline.com/{PRES_API_TENANT_ID}/oauth2/v2.0/token', + data={ + 'grant_type': 'client_credentials', + 'client_id': PRES_API_CLIENT_ID, # The client ID of your registered app + 'client_secret': PRES_API_CLIENT_SECRET, # The secret you created for the app + 'scope': f'api://{PRES_API_CLIENT_ID}/.default', # API permission scope (app-to-app) + } +) +# Check for successful response +if azure_response.status_code == 200: +else: + # If the response status is not 200, print the error + return { + "success" : False, + "message" : f"Error: {azure_response.status_code} - {azure_response.text}" + } + +# Extract access token from the response +token = azure_response.json().get('access_token') + ''' \ No newline at end of file