Unified RLS Policies
Integrating API key authentication with Supabase Row Level Security
KeyHippo simplifies the creation of unified RLS policies that handle both session-based and API key authentication methods.
Understanding Unified Policies
Unified RLS policies in KeyHippo allow you to define access rules that work consistently across different authentication methods. This approach ensures that your security policies are comprehensive and easy to maintain.
Key Components:
- auth.uid(): Supabase’s built-in function for session-based authentication.
- auth.keyhippo_check(): KeyHippo’s function for API key authentication.
Basic Unified Policy
Here’s a simple example of a unified RLS policy:
This policy grants access if:
- The user is authenticated via a session and their ID matches the
owner_id
, OR - The request includes a valid API key associated with the
owner_id
.
Advanced Policy Patterns
Role-Based Access Control (RBAC)
Combine KeyHippo with role checks for more granular control:
This policy allows access only if the user (authenticated via session or API key) has an ‘admin’ role.
Multi-Tenant Policies
For multi-tenant applications:
This policy ensures that users can only access data for their specific tenant, regardless of authentication method.
Leveraging KeyHippo-Specific Functions
KeyHippo provides additional functions to enhance your RLS policies:
keyhippo.key_uid()
Retrieves the user ID associated with the current API key:
This policy allows access if the user is authenticated or if the API key used is specifically allowed for the resource.
keyhippo.get_api_key_metadata()
Fetches metadata about API keys, useful for policies based on key properties:
This policy grants access to premium resources only if the API key has ‘premium’ permission.
Best Practices for Unified Policies
-
Consistency: Ensure that your policies treat session-based and API key authentication consistently where appropriate.
-
Granularity: Create separate policies for different operations (SELECT, INSERT, UPDATE, DELETE) rather than using catch-all policies.
-
Performance: Be mindful of policy complexity. Overly complex policies can impact query performance.
-
Testing: Thoroughly test your policies with both authentication methods to ensure they behave as expected.
-
Documentation: Maintain clear documentation of your RLS policies, including the reasoning behind each policy.
Policy Maintenance and Evolution
As your application evolves, so should your RLS policies:
-
Regular Reviews: Periodically review and update your policies to ensure they align with your current security requirements.
-
Version Control: Keep your policies in version control to track changes over time.
-
Audit Logging: Implement logging to track policy evaluations and identify potential issues or optimization opportunities.
Advanced RLS Techniques
Explore more sophisticated RLS policy patterns and techniques.