|
| 1 | +"""FastMCP integration examples for authorization. |
| 2 | +
|
| 3 | +Since FastMCP doesn't have built-in middleware, identity context must be |
| 4 | +built explicitly by the executor and passed to tools. |
| 5 | +""" |
| 6 | + |
| 7 | +from scouter.auth.exceptions import PermissionDeniedError, UserNotFoundError |
| 8 | +from scouter.auth.rbac import has_permission |
| 9 | +from scouter.auth.types import IdentityContext |
| 10 | +from scouter.db.auth import build_identity_context, resolve_user_from_oauth |
| 11 | +from scouter.db.neo4j import get_neo4j_driver |
| 12 | + |
| 13 | + |
| 14 | +def build_identity_from_token( |
| 15 | + token_payload: dict, |
| 16 | + tenant_id: str, |
| 17 | +) -> IdentityContext: |
| 18 | + """Build identity context from decoded JWT payload for FastMCP. |
| 19 | +
|
| 20 | + This function should be called by the FastMCP executor after verifying |
| 21 | + the OAuth token externally. |
| 22 | +
|
| 23 | + Args: |
| 24 | + token_payload: Decoded JWT payload |
| 25 | + tenant_id: Tenant identifier (must be provided explicitly) |
| 26 | +
|
| 27 | + Returns: |
| 28 | + IdentityContext dict |
| 29 | +
|
| 30 | + Raises: |
| 31 | + ValueError: If user cannot be resolved or is not in tenant |
| 32 | + """ |
| 33 | + provider = token_payload.get("iss") |
| 34 | + sub = token_payload.get("sub") |
| 35 | + if not provider or not sub: |
| 36 | + msg = "Missing provider or sub in token" |
| 37 | + raise ValueError(msg) |
| 38 | + |
| 39 | + driver = get_neo4j_driver() |
| 40 | + user_id = resolve_user_from_oauth(driver, provider, sub) |
| 41 | + if not user_id: |
| 42 | + msg = "User not found for OAuth identity" |
| 43 | + raise UserNotFoundError(msg) |
| 44 | + |
| 45 | + return build_identity_context(driver, user_id, tenant_id, token_payload) |
| 46 | + |
| 47 | + |
| 48 | +def create_user_tool(identity: IdentityContext, payload: dict) -> dict: |
| 49 | + """Example tool that requires user:write permission. |
| 50 | +
|
| 51 | + Args: |
| 52 | + identity: Identity context from build_identity_from_token |
| 53 | + payload: Tool payload |
| 54 | +
|
| 55 | + Returns: |
| 56 | + Tool result |
| 57 | +
|
| 58 | + Raises: |
| 59 | + PermissionError: If permission is denied |
| 60 | + """ |
| 61 | + if not has_permission(identity["permissions"], "user:write"): |
| 62 | + msg = "Permission denied: user:write" |
| 63 | + raise PermissionDeniedError(msg) |
| 64 | + |
| 65 | + # Tool implementation here |
| 66 | + return {"status": "user created", "user_id": payload.get("user_id")} |
| 67 | + |
| 68 | + |
| 69 | +def list_users_tool(identity: IdentityContext, payload: dict) -> dict: |
| 70 | + """Example tool that requires user:read permission. |
| 71 | +
|
| 72 | + Args: |
| 73 | + identity: Identity context from build_identity_from_token |
| 74 | + payload: Tool payload |
| 75 | +
|
| 76 | + Returns: |
| 77 | + Tool result |
| 78 | +
|
| 79 | + Raises: |
| 80 | + PermissionError: If permission is denied |
| 81 | + """ |
| 82 | + if not has_permission(identity["permissions"], "user:read"): |
| 83 | + msg = "Permission denied: user:read" |
| 84 | + raise PermissionDeniedError(msg) |
| 85 | + |
| 86 | + # Tool implementation here |
| 87 | + return {"users": []} |
0 commit comments