SQL-Based API Key Issuance
Generate and manage API keys directly within your Supabase database
KeyHippo provides a mechanism for generating and managing API keys directly within your Supabase database using SQL functions.
Core Concepts
- Database-Centric: API keys are issued and managed directly within Postgres, utilizing its built-in security and performance.
- Cryptographic Security: Keys are generated using secure cryptographic methods to ensure uniqueness and resistance to tampering.
- Stateless Validation: API keys can be validated without requiring frequent database queries, improving performance and scalability.
Key Generation
API keys are generated using the keyhippo.create_api_key
function, which issues a new key for a specified user. The function takes the user’s unique identifier and a description of the key’s intended use as parameters.
Parameters:
id_of_user
: The unique identifier (UUID) of the user for whom the API key is being generated.key_description
: A human-readable description of the API key’s purpose.
Returns:
- The API key as a
TEXT
value.
Example Usage
The following query generates a new API key for a user:
Key Generation Process
When an API key is generated, several steps are performed:
- Permission Verification: The function checks that the caller has the necessary permissions to create API keys.
- Unique Key Creation: A UUID is generated as a unique identifier for the key.
- JWT Creation: A JSON Web Token (JWT) is created containing key metadata.
- Signature: The JWT is signed to prevent tampering.
- Cryptographic Hashing: The final API key is created by hashing the signed JWT.
- Secure Storage: Key metadata is securely stored in the database. The raw API key is never saved.
Additional Key Management Functions
Several additional SQL functions are provided to manage the lifecycle of API keys:
Revoking a Key
An API key can be revoked using the keyhippo.revoke_api_key
function. Once revoked, the key can no longer be used.
Retrieving Key Information
Key metadata for a user’s API keys can be retrieved using the keyhippo.load_api_key_info
function.
Fetching Key Metadata
Detailed metadata for all API keys associated with a user can be fetched using the keyhippo.get_api_key_metadata
function.
Best Practices
- Principle of Least Privilege: Generate API keys with only the permissions needed for their intended use.
- Key Rotation: Implement policies to regularly rotate API keys and revoke those no longer in use.
- Audit Logging: Maintain logs of all key generation and management activities for security auditing purposes.
- Error Handling: Ensure error handling when working with key management functions to prevent unintended access issues.
Performance Considerations
- Efficient Validation: Key validation can be performed without requiring frequent database lookups, enhancing system performance.
- Scalability: Stateless validation allows the system to scale efficiently without bottlenecks.
- Indexing: Proper indexing on key management tables is recommended to improve query performance.
Security Considerations
- Access Control: Restrict access to key management functions to trusted users and roles.
- Secure Transmission: Always use encrypted connections (e.g., HTTPS) when transmitting API keys.
- Sensitive Data Handling: Treat API keys as sensitive data. Never log or display full key values in insecure environments.
API Key Management Guide
Learn more about managing the full lifecycle of your API keys.