in backend/app.py [0:0]
def check_authorization():
if not ENABLE_AUTHENTICATION:
return {
'authorized': True,
'client_principal_id': 'no-auth',
'client_principal_name': 'anonymous',
'client_group_names': [],
'access_token': None
}
user = session.get("user")
if not user:
logging.info("[webbackend] No user in session; user is not authenticated.")
return {
'authorized': False,
'client_principal_id': None,
'client_principal_name': None,
'client_group_names': [],
'access_token': None
}
client_principal_id = user.get("oid")
client_principal_name = user.get("preferred_username") or user.get("upn")
try:
graph_access_token = get_valid_access_token(SCOPE)
session["graph_access_token"] = graph_access_token
except Exception as ex:
logging.error(f"[webbackend] Failed to refresh Graph token: {str(ex)}")
graph_access_token = session.get("graph_access_token", None)
other_access_token = None
if OTHER_AUTH_SCOPES:
try:
other_access_token = get_valid_access_token(OTHER_AUTH_SCOPES)
session["other_access_token"] = other_access_token
except Exception as ex:
logging.error(f"[webbackend] Failed to refresh other scopes token: {str(ex)}")
other_access_token = session.get("other_access_token", None)
access_token = other_access_token if other_access_token else graph_access_token
groups = []
if graph_access_token:
try:
graph_headers = {'Authorization': f'Bearer {graph_access_token}'}
graph_url = 'https://graph.microsoft.com/v1.0/me/memberOf'
graph_response = requests.get(graph_url, headers=graph_headers)
graph_response.raise_for_status()
group_data = graph_response.json()
groups = [group.get('displayName', 'missing-group-read-all-permission') for group in group_data.get('value', [])]
logging.info(f"[webbackend] User groups from Graph API: {groups}")
except Exception as e:
logging.info(f"[webbackend] Failed to get user groups from Graph API: {e}")
else:
logging.info("[webbackend] No valid Graph access token available; cannot get user groups")
authorized = True
if ALLOWED_GROUP_NAMES or ALLOWED_USER_PRINCIPALS or ALLOWED_USER_NAMES:
authorized = False
if client_principal_name in ALLOWED_USER_NAMES:
authorized = True
elif client_principal_id in ALLOWED_USER_PRINCIPALS:
authorized = True
elif any(group in ALLOWED_GROUP_NAMES for group in groups):
authorized = True
if not authorized:
logging.info("[webbackend] User is not in allowed groups or users.")
return {
'authorized': authorized,
'client_principal_id': client_principal_id,
'client_principal_name': client_principal_name,
'client_group_names': groups,
'access_token': access_token
}