Skip to main content
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
Allows you to modify the permission result after basic authentication. This is the primary hook for adding custom validation, external system checks, or role-based access control.
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
Fires when login permission is successfully granted. Perfect for audit logging and post-authentication tasks.
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
Allows customizing which authentication methods skip additional checks (like custom validation or external system verification).
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
Allows you to add additional parameters to the login endpoint for custom authentication data.
array
Array of additional parameters to add to the login endpoint

API Usage Examples

Response Examples

Successful Login

Unsupported Version

Device Not Registered

Rate Limited

Authentication Flow

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 method
4

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

  1. Always check the current permission status first - If it’s already an error, don’t override it
  2. Respect secure authentication methods - Use the cocart_login_secure_auth_methods filter appropriately
  3. Provide clear error messages - Include helpful information in error responses
  4. Use appropriate HTTP status codes - 400 for missing required data, 401 for invalid credentials
  5. Include metadata in error responses - Help clients understand what’s needed
  6. Implement rate limiting - Protect against brute force attacks
  7. Log security events - Use the permission granted action for audit trails
  8. Validate all inputs - Sanitize and validate all request parameters
  9. Follow WordPress coding standards - Maintain consistency with WordPress conventions

Security Best Practices

Always validate user input and implement proper error handling when extending authentication.
  • 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_granted action 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
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.