API Key Module
Maginiumβs API Key Module provides a secure and flexible way to authenticate requests to your applicationβs API. This module is designed to ensure that only authorized clients can access your API endpoints. Follow this guide to configure and use API Key-based authentication in your Maginium-powered application.
Features of the API Key Module
Secure Access Control: Ensures only authorized clients can access APIs.
Key Management: Generate, revoke, and manage API keys.
Rate Limiting: Protect APIs from abuse by setting usage limits.
Custom Scopes: Define fine-grained permissions for API keys.
Logging and Monitoring: Track API key usage and detect anomalies.
Setting Up the API Key Module
Step 1: Enable the API Key Module
Install the necessary components for API Key authentication using the Artisan command:
This will scaffold the required files and configurations for the module.
Step 2: Migrate the Database
Run the following migration command to create the api_keys
table:
The api_keys
table will store information about each API key, including the associated user, scopes, and expiration.
Generating API Keys
Maginium provides a simple command to create new API keys:
Parameters:
--user
: The ID of the user the key is associated with.--scopes
: A comma-separated list of scopes.--expires
: Expiration date of the key (optional).
Example output:
Store this key securely, as it wonβt be shown again.
Authenticating API Requests
Clients must include the API key in the request headers for authentication:
Middleware for API Key Validation
Protect API routes by applying the api.key
middleware:
The api.key
middleware will verify the provided key, including its validity, scopes, and expiration.
Managing API Keys
Revoking API Keys
To revoke an API key, use the following command:
Revoked keys will no longer be valid for authentication.
Listing All API Keys
View all active API keys with:
Example output:
Configuring API Key Settings
Edit the config/api-key.php
file to customize settings like expiration defaults and key length. Example configuration:
Scopes and Permissions
Scopes allow fine-grained access control for API keys. Define available scopes in the api-key
configuration file:
When generating API keys, assign scopes to limit their permissions. For example, a read
-only key cannot modify resources.
Logging and Monitoring
Track API key usage with built-in logging. Logs include details like:
Key used for the request
Endpoint accessed
Timestamp
Example log entry:
Use this data to monitor key usage patterns and detect unauthorized access.
Best Practices for API Key Security
Important: Always follow these best practices to secure your API keys:
Use HTTPS to encrypt communication.
Regenerate keys periodically and revoke old ones.
Restrict scopes to the minimum required for each key.
Monitor usage for anomalies.
Store keys securely and avoid exposing them in logs or URLs.
By implementing Maginiumβs API Key Module, you can ensure secure and controlled access to your API endpoints, providing peace of mind for your application and its users.
Last updated