Skip to content

Commit 2c02851

Browse files
committed
send user detaisl if introspection fails
1 parent 898b3f6 commit 2c02851

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM python:3.10
22
ENV PYTHONDONTWRITEBYTECODE=1
33
ENV PYTHONUNBUFFERED=1
44

5-
# Install system dependencies without mixing old Debian repos
5+
# Install system dependencies
66
RUN apt-get update && \
77
apt-get autoremove -y && \
88
apt-get install -y \
@@ -19,7 +19,7 @@ RUN apt-get update && \
1919
libdbus-1-3 \
2020
libexpat1 \
2121
libfontconfig1 \
22-
libgdk-pixbuf2.0-0 \
22+
libgdk-pixbuf-2.0-0 \
2323
libglib2.0-0 \
2424
libgtk-3-0 \
2525
libnspr4 \

api/utils/keycloak_utils.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,33 @@ def validate_token(self, token: str) -> Dict[str, Any]:
7171
logger.warning("Token is not active")
7272
return {}
7373

74-
# Get user info from the token
75-
user_info = self.keycloak_openid.userinfo(token)
76-
return user_info
74+
# Try to get user info from the userinfo endpoint
75+
# If that fails (403), fall back to token introspection data
76+
try:
77+
user_info = self.keycloak_openid.userinfo(token)
78+
return user_info
79+
except KeycloakError as userinfo_error:
80+
# If userinfo fails (e.g., 403), extract user info from token introspection
81+
logger.warning(
82+
f"Userinfo endpoint failed ({userinfo_error}), using token introspection data"
83+
)
84+
85+
# Build user info from introspection response
86+
user_info = {
87+
"sub": token_info.get("sub"),
88+
"preferred_username": token_info.get("username")
89+
or token_info.get("preferred_username"),
90+
"email": token_info.get("email"),
91+
"email_verified": token_info.get("email_verified", False),
92+
"name": token_info.get("name"),
93+
"given_name": token_info.get("given_name"),
94+
"family_name": token_info.get("family_name"),
95+
}
96+
97+
# Remove None values
98+
user_info = {k: v for k, v in user_info.items() if v is not None}
99+
return user_info
100+
77101
except KeycloakError as e:
78102
logger.error(f"Error validating token: {e}")
79103
return {}
@@ -129,9 +153,7 @@ def get_user_organizations(self, token: str) -> List[Dict[str, Any]]:
129153
if len(parts) >= 3:
130154
org_id = parts[1]
131155
role_name = parts[2]
132-
organizations.append(
133-
{"organization_id": org_id, "role": role_name}
134-
)
156+
organizations.append({"organization_id": org_id, "role": role_name})
135157

136158
return organizations
137159
except KeycloakError as e:
@@ -198,9 +220,7 @@ def sync_user_from_keycloak(
198220
# Process organizations from Keycloak
199221
for org_info in organizations:
200222
org_id = org_info.get("organization_id")
201-
role = org_info.get(
202-
"role", "viewer"
203-
) # Default to viewer if role not specified
223+
role = org_info.get("role", "viewer") # Default to viewer if role not specified
204224

205225
# Try to get the organization
206226
try:

0 commit comments

Comments
 (0)