Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /namastexlabs_automagik-tools /automagik_tools /hub /credentials.py
| """Credential management tools for handling user secrets and OAuth tokens.""" | |
| import json | |
| import uuid | |
| from datetime import datetime, timezone | |
| from typing import Any, Dict, List, Optional | |
| from fastmcp import Context | |
| from sqlalchemy import select, delete | |
| from .database import get_db_session | |
| from .models import OAuthToken | |
| async def store_credential( | |
| tool_name: str, | |
| provider: str, | |
| secrets: Dict[str, Any], | |
| ctx: Optional[Context] = None, | |
| *, | |
| user_id: Optional[str] = None | |
| ) -> str: | |
| """ | |
| Store encrypted credentials for a specific tool and provider. | |
| Args: | |
| tool_name: Name of the tool (e.g., 'google_calendar') | |
| provider: Provider name (e.g., 'google', 'github', 'api_key') | |
| secrets: Dictionary containing secrets (e.g., {'access_token': '...', 'refresh_token': '...'}) | |
| ctx: FastMCP context (optional if user_id provided) | |
| user_id: Direct user_id (for HTTP API calls) | |
| Returns: | |
| Success message | |
| """ | |
| # Direct param takes precedence, then try context, then default | |
| if not user_id: | |
| if ctx and hasattr(ctx, "session") and ctx.session.get("user_id"): | |
| user_id = ctx.session.get("user_id") | |
| else: | |
| user_id = "default_user" | |
| # In a real implementation, we would encrypt these values before storing | |
| # For now, we'll store them as JSON strings, but the model field is named 'access_token' (Text) | |
| # We'll map the 'secrets' dict to the model fields | |
| access_token = json.dumps(secrets) # Storing the whole dict for flexibility if it's not just a token | |
| refresh_token = secrets.get("refresh_token") | |
| # If secrets has specific fields, use them, otherwise dump the whole thing | |
| if "access_token" in secrets: | |
| access_token = secrets["access_token"] | |
| async with get_db_session() as session: | |
| # Check if exists | |
| result = await session.execute( | |
| select(OAuthToken).where( | |
| OAuthToken.user_id == user_id, | |
| OAuthToken.tool_name == tool_name, | |
| OAuthToken.provider == provider | |
| ) | |
| ) | |
| existing = result.scalar_one_or_none() | |
| if existing: | |
| existing.access_token = access_token | |
| existing.refresh_token = refresh_token | |
| existing.updated_at = datetime.now(timezone.utc) | |
| else: | |
| new_token = OAuthToken( | |
| id=str(uuid.uuid4()), | |
| user_id=user_id, | |
| tool_name=tool_name, | |
| provider=provider, | |
| access_token=access_token, | |
| refresh_token=refresh_token | |
| ) | |
| session.add(new_token) | |
| await session.commit() | |
| return f"✅ Credentials for {tool_name} ({provider}) stored successfully." | |
| async def get_credential( | |
| tool_name: str, | |
| provider: str, | |
| ctx: Optional[Context] = None, | |
| *, | |
| user_id: Optional[str] = None | |
| ) -> Dict[str, Any]: | |
| """ | |
| Retrieve credentials for a specific tool. | |
| Args: | |
| tool_name: Name of the tool | |
| provider: Provider name | |
| ctx: FastMCP context (optional if user_id provided) | |
| user_id: Direct user_id (for HTTP API calls) | |
| Returns: | |
| Dictionary of secrets | |
| """ | |
| # Direct param takes precedence, then try context, then default | |
| if not user_id: | |
| if ctx and hasattr(ctx, "session") and ctx.session.get("user_id"): | |
| user_id = ctx.session.get("user_id") | |
| else: | |
| user_id = "default_user" | |
| async with get_db_session() as session: | |
| result = await session.execute( | |
| select(OAuthToken).where( | |
| OAuthToken.user_id == user_id, | |
| OAuthToken.tool_name == tool_name, | |
| OAuthToken.provider == provider | |
| ) | |
| ) | |
| token = result.scalar_one_or_none() | |
| if not token: | |
| raise ValueError(f"No credentials found for {tool_name} ({provider})") | |
| # Try to parse as JSON if it was stored as a dict dump, otherwise return as access_token | |
| try: | |
| return json.loads(token.access_token) | |
| except json.JSONDecodeError: | |
| return { | |
| "access_token": token.access_token, | |
| "refresh_token": token.refresh_token | |
| } | |
| async def list_credentials( | |
| ctx: Optional[Context] = None, | |
| *, | |
| user_id: Optional[str] = None | |
| ) -> List[Dict[str, Any]]: | |
| """ | |
| List all stored credentials (metadata only, no secrets). | |
| Args: | |
| ctx: FastMCP context (optional if user_id provided) | |
| user_id: Direct user_id (for HTTP API calls) | |
| Returns: | |
| List of credential metadata | |
| """ | |
| # Direct param takes precedence, then try context, then default | |
| if not user_id: | |
| if ctx and hasattr(ctx, "session") and ctx.session.get("user_id"): | |
| user_id = ctx.session.get("user_id") | |
| else: | |
| user_id = "default_user" | |
| async with get_db_session() as session: | |
| result = await session.execute( | |
| select(OAuthToken).where(OAuthToken.user_id == user_id) | |
| ) | |
| tokens = result.scalars().all() | |
| return [ | |
| { | |
| "tool_name": t.tool_name, | |
| "provider": t.provider, | |
| "updated_at": t.updated_at.isoformat() if t.updated_at else None | |
| } | |
| for t in tokens | |
| ] | |
| async def delete_credential( | |
| tool_name: str, | |
| provider: str, | |
| ctx: Optional[Context] = None, | |
| *, | |
| user_id: Optional[str] = None | |
| ) -> str: | |
| """ | |
| Delete a stored credential. | |
| Args: | |
| tool_name: Name of the tool | |
| provider: Provider name | |
| ctx: FastMCP context (optional if user_id provided) | |
| user_id: Direct user_id (for HTTP API calls) | |
| Returns: | |
| Success message | |
| """ | |
| # Direct param takes precedence, then try context, then default | |
| if not user_id: | |
| if ctx and hasattr(ctx, "session") and ctx.session.get("user_id"): | |
| user_id = ctx.session.get("user_id") | |
| else: | |
| user_id = "default_user" | |
| async with get_db_session() as session: | |
| result = await session.execute( | |
| delete(OAuthToken).where( | |
| OAuthToken.user_id == user_id, | |
| OAuthToken.tool_name == tool_name, | |
| OAuthToken.provider == provider | |
| ) | |
| ) | |
| await session.commit() | |
| return f"✅ Credentials for {tool_name} ({provider}) deleted." | |
Xet Storage Details
- Size:
- 6.58 kB
- Xet hash:
- 1e3202bddba527afd07250ae4932c4cee85f51ae6eb51ab4783c2521f1aa586e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.