Skip to content

FGAAuthorizer: expose on_unauthorized handler#8

Merged
siacomuzzi merged 1 commit intomainfrom
on_unauthorized
Mar 18, 2025
Merged

FGAAuthorizer: expose on_unauthorized handler#8
siacomuzzi merged 1 commit intomainfrom
on_unauthorized

Conversation

@siacomuzzi
Copy link
Member

fga = FGAAuthorizer.create()

async def build_fga_query(tool_input):
    user_id = ensure_config().get("configurable",{}).get("user_id")
    return {
        "user": f"user:{user_id}",
        "object": f"asset:{tool_input["ticker"]}",
        "relation": "can_buy",
        "context": {"current_time": datetime.now(timezone.utc).isoformat()}
    }

def on_unauthorized(tool_input):
    return f"The user is not allowed to buy {tool_input["qty"]} shares of {tool_input["ticker"]}."

use_fga = fga(FGAAuthorizerOptions(
    build_query=build_fga_query,
    on_unauthorized=on_unauthorized,
))

async def buy_tool_function(ticker: str, qty: int) -> str:
    # TODO: implement buy operation
    return f"Purchased {qty} shares of {ticker}"

func=use_fga(buy_tool_function)

@siacomuzzi siacomuzzi merged commit aa336da into main Mar 18, 2025
@siacomuzzi siacomuzzi deleted the on_unauthorized branch March 18, 2025 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants