This guide only works if you have CoCart v4.8 and up installed.
What is authentication injection?
Authentication injection refers to the ability to insert custom authentication logic into the existing login process. This allows developers to implement additional security measures, such as custom validation rules, external system integration, role-based access control, or comprehensive logging mechanisms, without modifying the core authentication flow.Overview
CoCart’s authentication injection system is built around a series of hooks (filters and actions) that allow you to customize the login process. These hooks provide entry points to modify the behavior of authentication, enabling you to add layers of security or integrate with external systems.Available Hooks
Each hook is documented below with its description, parameters, and practical usage examples.cocart_login_permission_callback
Made available since v4.8.0
boolean|WP_Error
Current permission status (true after basic auth)
WP_User
The authenticated user object
WP_REST_Request
The current REST API request
string
The endpoint being accessed (‘login’)
boolean|WP_Error
cocart_login_permission_granted
Made available since v4.8.0
WP_User
The authenticated user object
WP_REST_Request
The current REST API request
string
The endpoint being accessed (‘login’)
cocart_login_secure_auth_methods
Made available since v4.8.0
array
Array of authentication methods considered secure (default: [‘jwt’, ‘api_key’])
string
The current authentication method being used
cocart_login_collection_params
Made available since v4.8.0
array
Array of additional parameters to add to the login endpoint
API Usage Examples
- cURL
- PHP
- JavaScript
Standard Login
Standard Login
Login with Custom Headers
Login with Custom Headers
Login with Device Registration
Login with Device Registration
Using JWT Token (bypasses additional checks)
Using JWT Token (bypasses additional checks)
Response Examples
Successful Login
Unsupported Version
Device Not Registered
Rate Limited
Authentication Flow
- First-Time Login (Basic Auth + Custom Validation)
- Subsequent Requests (JWT Token)
1
Initial Authentication
User provides username/password via Basic Auth
2
Basic Validation
Basic authentication succeeds → user authenticated
3
Permission Callback
Permission callback runs → detects
basic_auth method4
Custom Validation
Custom filters execute → check for additional requirements (device, etc.)
5
Additional Data Prompt
If additional data required → returns error with specific requirements
6
Validation Success
User provides required data → validation succeeds
7
JWT Token Issued
Successful login response includes JWT token for future requests (if active)
Advanced Configuration
Customizing Secure Authentication Methods
You can modify which authentication methods skip additional checks like custom validation:Custom Authentication Provider
Create a custom authentication provider that integrates with external systems:Implementation Guidelines
- Always check the current permission status first - If it’s already an error, don’t override it
- Respect secure authentication methods - Use the
cocart_login_secure_auth_methodsfilter appropriately - Provide clear error messages - Include helpful information in error responses
- Use appropriate HTTP status codes - 400 for missing required data, 401 for invalid credentials
- Include metadata in error responses - Help clients understand what’s needed
- Implement rate limiting - Protect against brute force attacks
- Log security events - Use the permission granted action for audit trails
- Validate all inputs - Sanitize and validate all request parameters
- Follow WordPress coding standards - Maintain consistency with WordPress conventions
Security Best Practices
- Validate all input parameters - Use WordPress sanitization functions like
sanitize_text_field() - Always check the permission status first - Return early if permission is already denied
- Use appropriate HTTP status codes - 400 for bad requests, 401 for authentication failures, 403 for insufficient permissions
- Log security events - Use the
cocart_login_permission_grantedaction for audit trails
Testing Your Implementation
Simple Test Filter
Test the authentication injection mechanism with a basic filter:Test with cURL
Common Use Cases
These examples demonstrate practical implementations of authentication injection for common security requirements.Single Sign-On (SSO)
Integrate with external SSO providers like SAML, OAuth, or LDAP for seamless authentication
Role-Based Auth
Add extra verification for specific user roles, capabilities, or department restrictions
Device Registration
Require device registration and management for enhanced mobile security
Geolocation Blocking
Restrict access based on geographic location with country-level controls
Time-Based Access
Control access based on business hours, maintenance windows, or schedules
Custom Headers
Validate custom headers for API versioning, client identification, or feature flags
SSO Integration
SSO Integration
Role-Based Authentication
Role-Based Authentication
Device Registration
Device Registration
Geolocation Blocking
Geolocation Blocking
Time-Based Access
Time-Based Access
Custom Headers
Custom Headers
All authentication extensions follow WordPress coding standards and integrate seamlessly with CoCart’s existing authentication system. The examples above demonstrate practical implementations for common security requirements.