Overview
Definable provides two main security dependencies:- JWTBearer: Validates JWT tokens (both Stytch session tokens and API keys)
- RBAC: Enforces role-based permissions on protected resources
Architecture
JWTBearer Dependency
TheJWTBearer class validates JWT tokens and extracts user identity.
Location
File:src/dependencies/security.py
Implementation
Using JWTBearer
Basic Authentication
Protect an endpoint by requiring a valid JWT token:Accessing User Information
TheJWTBearer dependency returns a dictionary with user information:
RBAC Dependency
TheRBAC class enforces role-based permissions on resources.
Implementation
Using RBAC
Method 1: Dependencies Parameter (Recommended)
Use thedependencies parameter for cleaner code when you donβt need the user context:
Method 2: Direct Dependency
Use as a direct dependency when you need the user context:Permission Patterns
Standard Permissions
Resource Types
Common resources in Definable:kb- Knowledge basesconversation- Conversations/chatsagent- AI agentstool- Toolsorganization- Organization settingsuser- User managementrole- Role managementapi_key- API key management
Action Types
Common actions:read- View/list resourceswrite- Create/update resourcesdelete- Delete resourcesadmin- Full administrative accessexecute- Execute/run resources (agents, tools)
Combining Dependencies
Both Authentication and Authorization
Most endpoints need both:Multiple Permission Checks
For endpoints requiring multiple permissions:WebSocket Authentication
WebSockets require special handling since they canβt use the Authorization header.WebSocket Token Passing
Pass the JWT token as a query parameter:Protecting WebSocket Endpoints
WebSocket with RBAC
Error Handling
Authentication Errors
401 Unauthorized - Token is invalid or missing:- Token expired
- Invalid token signature
- Malformed token
- Token from wrong environment (test vs live)
Authorization Errors
403 Forbidden - User lacks required permission:- User doesnβt have the required role
- Userβs role lacks the required permission
- User is not a member of the organization
- Userβs membership is not active
Custom Error Responses
Testing Protected Endpoints
Unit Testing with Mocked Dependencies
Integration Testing with Real Tokens
Best Practices
1. Always Use Dependencies
Donβt manually parse tokens or check permissions:2. Specific Permissions
Use specific permissions instead of wildcards when possible:3. Organization Context
Always passorg_id for RBAC checks:
4. Consistent Error Messages
Use consistent error messages for security:5. Logging
Log authentication failures for security monitoring:Performance Considerations
Database Queries
The RBAC dependency performs database queries to check permissions. For high-traffic endpoints, consider:- Caching: Cache role-permission mappings
- Connection Pooling: Use proper database connection pools
- Indexes: Ensure proper indexes on
organization_members,roles, andpermissionstables
Token Validation
- JWKS Caching: Stytch public keys are cached for 10 minutes
- Connection Reuse: HTTP connections to Stytch are reused
- Local Validation: Most JWT validation happens locally
Next Steps
- RBAC Permissions: Understanding the permission system
- JWT Authentication: How JWT tokens work
- Stytch Integration: Stytch authentication details
- Invitation Flow: User invitation system