"""Logic for JWTs.""" from typing import Any, Dict, Optional CLAIM_ORCHARD_IDENTITY_ID = "orchardIdentityId" PROFILE_ID = "profile_id" PROFILE_TYPE = "profile_type" AUTHORIZATION = "Authorization" AUTH0_ALGORITHMS = ["RS256"] JWT_USER_METADATA = "https://grass.theorchard.com/user_metadata" JWT_PROFILES = "https://grass.theorchard.com/profiles" def get_identity_uuid(decoded_jwt: Dict[str, Any]) -> Optional[str]: """Get a user's orchardIdentityId from a decoded JWT.""" identity_uuid = None grass_data = decoded_jwt.get(JWT_USER_METADATA) if grass_data: identity_uuid = grass_data.get(CLAIM_ORCHARD_IDENTITY_ID) return identity_uuid def get_profiles(decoded_jwt: Dict[str, Any]) -> list[tuple[str, int]]: """Get a list of (profile_type, profile_id) tuples from a decoded JWT. profile_id is an int.""" grass_data = decoded_jwt.get(JWT_PROFILES) if grass_data: profiles = [] for entry in grass_data: profile_type = entry.get(PROFILE_TYPE) profile_id = entry.get(PROFILE_ID) if profile_type is not None and profile_id is not None: try: profile_id_int = int(profile_id) profiles.append((profile_type, profile_id_int)) except (ValueError, TypeError): continue # skip if profile_id is not convertible to int return profiles return []