@@ -1081,21 +1081,30 @@ def get_user_role(self, user, user_data):
10811081 log .debug (f'Accepted user roles: { oauth_allowed_roles } ' )
10821082 log .debug (f'Accepted admin roles: { oauth_admin_roles } ' )
10831083
1084- # If any roles are found, check if they match the allowed or admin roles
1084+ # If roles are present in the token, they must match; otherwise deny access
10851085 if oauth_roles :
1086- # If role management is enabled, and matching roles are provided, use the roles
1086+ matched = False
10871087 for allowed_role in oauth_allowed_roles :
1088- # If the user has any of the allowed roles, assign the role "user"
10891088 if allowed_role in oauth_roles :
10901089 log .debug ('Assigned user the user role' )
10911090 role = 'user'
1091+ matched = True
10921092 break
10931093 for admin_role in oauth_admin_roles :
1094- # If the user has any of the admin roles, assign the role "admin"
10951094 if admin_role in oauth_roles :
10961095 log .debug ('Assigned user the admin role' )
10971096 role = 'admin'
1097+ matched = True
10981098 break
1099+ if not matched :
1100+ log .warning (
1101+ f'OAuth role management enabled but user roles do not match any allowed/admin roles. '
1102+ f'User roles: { oauth_roles } , allowed: { oauth_allowed_roles } , admin: { oauth_admin_roles } '
1103+ )
1104+ raise HTTPException (
1105+ status .HTTP_403_FORBIDDEN ,
1106+ detail = ERROR_MESSAGES .ACCESS_PROHIBITED ,
1107+ )
10991108 else :
11001109 if not user :
11011110 # If role management is disabled, use the default role for new users
0 commit comments