54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
import logging
|
|
from typing import Any, Dict
|
|
|
|
from amqp.rabbitmq.user_management_service_client import (
|
|
UserManagementServiceClient,
|
|
UserManagementServiceException,
|
|
)
|
|
|
|
|
|
async def get_user_info_from_token(
|
|
client: UserManagementServiceClient, token: str
|
|
) -> Dict[str, Any]:
|
|
"""
|
|
Get user information from a JWT token.
|
|
|
|
Args:
|
|
client: User Management Service client
|
|
token: JWT token
|
|
|
|
Returns:
|
|
User information
|
|
|
|
Raises:
|
|
UserManagementServiceException: If an error occurs
|
|
"""
|
|
try:
|
|
# Validate the token
|
|
token_info = await client.validate_token(token)
|
|
|
|
# Extract user ID from token info
|
|
user_id = None
|
|
for item in token_info:
|
|
if isinstance(item, dict) and "sub" in item:
|
|
user_id = item["sub"]
|
|
break
|
|
|
|
if not user_id:
|
|
raise UserManagementServiceException(
|
|
"cleverthis.clevermicro.auth.invalid_token",
|
|
"Token does not contain user ID (sub claim)",
|
|
{},
|
|
)
|
|
|
|
# Query user information
|
|
user_info = await client.query_user_by_id(user_id)
|
|
return user_info
|
|
|
|
except UserManagementServiceException as e:
|
|
logging.error(f"User Management Service error: {e.exception_type}: {e.message}")
|
|
raise
|
|
except Exception as e:
|
|
logging.error(f"Unexpected error: {e}")
|
|
raise
|