76 lines
3.2 KiB
Python
76 lines
3.2 KiB
Python
import os
|
|
import requests
|
|
import msal
|
|
import json
|
|
|
|
# ==============================================================================
|
|
# KONFIGURATION
|
|
# ==============================================================================
|
|
TENANT_ID = "caee3499-03f8-4175-9fa8-a935248d0ece"
|
|
CLIENT_ID = "3a08b279-1fc3-419f-a77e-31f12a0f65f7"
|
|
CLIENT_SECRET = "Rk-8Q~nJ.sZ-xUiNxtEDdzVgoFFosODLVHX~jdrh"
|
|
|
|
GRAPH_API_ENDPOINT = "https://graph.microsoft.com/v1.0"
|
|
AUTHORITY_URL = f"https://login.microsoftonline.com/{TENANT_ID}"
|
|
SCOPES = ["https://graph.microsoft.com/.default"]
|
|
|
|
def get_access_token():
|
|
"""Ruft ein Zugriffstoken mittels Client-Anmeldeinformationen-Fluss ab."""
|
|
app = msal.ConfidentialClientApplication(
|
|
CLIENT_ID,
|
|
authority=AUTHORITY_URL,
|
|
client_credential=CLIENT_SECRET
|
|
)
|
|
result = app.acquire_token_for_client(scopes=SCOPES)
|
|
if "access_token" in result:
|
|
return result["access_token"]
|
|
else:
|
|
raise Exception(f"Zugriffstoken konnte nicht abgerufen werden: {result.get('error_description')}")
|
|
|
|
def get_application_permissions(access_token, app_id):
|
|
"""
|
|
Ruft die appRoles (Berechtigungen) für eine gegebene Anwendung (Dienstprinzipal) ab.
|
|
"""
|
|
headers = {
|
|
'Authorization': 'Bearer ' + access_token,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
# Erstellt die URL, um den Dienstprinzipal anhand der appId abzufragen und appRoles auszuwählen
|
|
url = (f"{GRAPH_API_ENDPOINT}/servicePrincipals?"
|
|
f"$filter=appId+eq+'{app_id}'&"
|
|
f"$select=displayName,appId,appRoles")
|
|
|
|
response = requests.get(url, headers=headers)
|
|
response.raise_for_status() # Löst eine Ausnahme für HTTP-Fehler aus
|
|
return response.json()
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
print("Zugriffstoken wird abgerufen...")
|
|
token = get_access_token()
|
|
print("Zugriffstoken erfolgreich abgerufen.")
|
|
|
|
print(f"Berechtigungen für Anwendung (Client ID: {CLIENT_ID}) werden abgerufen...")
|
|
service_principal_data = get_application_permissions(token, CLIENT_ID)
|
|
|
|
if service_principal_data and service_principal_data.get('value'):
|
|
for sp in service_principal_data['value']:
|
|
print(f"\nAnzeigename der Anwendung: {sp.get('displayName')}")
|
|
print(f"Anwendungs-ID: {sp.get('appId')}")
|
|
app_roles = sp.get('appRoles', [])
|
|
if app_roles:
|
|
print("Anwendungsberechtigungen (appRoles):")
|
|
for role in app_roles:
|
|
print(f" - Anzeigename: {role.get('displayName')}")
|
|
print(f" Beschreibung: {role.get('description')}")
|
|
print(f" Wert: {role.get('value')}")
|
|
print(f" ID: {role.get('id')}")
|
|
print(f" Aktiviert: {role.get('isEnabled')}")
|
|
else:
|
|
print("Keine Anwendungsberechtigungen (appRoles) für diesen Dienstprinzipal gefunden.")
|
|
else:
|
|
print("Kein Dienstprinzipal mit der angegebenen Client ID gefunden oder keine Daten zurückgegeben.")
|
|
|
|
except Exception as e:
|
|
print(f"Ein Fehler ist aufgetreten: {e}")
|