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.
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.
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.
Copy
add_filter( 'cocart_login_permission_callback', function( $permission, $current_user, $request, $endpoint ) { // Your additional authentication logic here return $permission; // or return WP_Error to deny access}, 10, 4 );
{ "code": "cocart_device_not_registered", "message": "This device is not registered for this account", "data": { "status": 403, "device_registration_required": true }}
Rate Limited
Copy
{ "code": "cocart_rate_limited", "message": "Too many login attempts. Please try again later.", "data": { "status": 429, "retry_after": 3600 }}
add_filter( 'cocart_login_permission_callback', 'device_registration_check', 10, 4 );function device_registration_check( $permission, $current_user, $request, $endpoint ) { if ( true !== $permission ) { return $permission; } $device_id = $request->get_param( 'device_id' ); $user_agent = $request->get_header( 'user_agent' ); if ( empty( $device_id ) ) { return new WP_Error( 'cocart_device_id_required', 'Device ID is required for authentication', array( 'status' => 400 ) ); } // Check if device is registered for this user $registered_devices = get_user_meta( $current_user->ID, 'registered_devices', true ) ?: array(); if ( ! in_array( $device_id, $registered_devices ) ) { return new WP_Error( 'cocart_device_not_registered', 'This device is not registered for this account', array( 'status' => 403, 'device_registration_required' => true ) ); } return $permission;}
Geolocation Blocking
Copy
add_filter( 'cocart_login_permission_callback', 'geolocation_check', 10, 4 );function geolocation_check( $permission, $current_user, $request, $endpoint ) { if ( true !== $permission ) { return $permission; } $ip_address = CoCart_Authentication::get_ip_address(); $country_code = get_country_from_ip( $ip_address ); // Block certain countries $blocked_countries = array( 'CN', 'RU', 'KP' ); $allowed_countries = get_option( 'allowed_countries', array( 'US', 'CA', 'GB', 'AU' ) ); if ( in_array( $country_code, $blocked_countries ) ) { return new WP_Error( 'cocart_geo_blocked', 'Access from your location is not permitted', array( 'status' => 403, 'country_code' => $country_code ) ); } // Optional: Only allow specific countries if ( ! empty( $allowed_countries ) && ! in_array( $country_code, $allowed_countries ) ) { return new WP_Error( 'cocart_geo_restricted', 'Access is restricted to approved regions only', array( 'status' => 403, 'country_code' => $country_code ) ); } return $permission;}
Time-Based Access
Copy
add_filter( 'cocart_login_permission_callback', 'time_based_access', 10, 4 );function time_based_access( $permission, $current_user, $request, $endpoint ) { if ( true !== $permission ) { return $permission; } $current_time = current_time( 'H:i' ); $current_day = current_time( 'w' ); // 0 = Sunday, 6 = Saturday // Get user-specific access hours $access_schedule = get_user_meta( $current_user->ID, 'access_schedule', true ); if ( ! empty( $access_schedule ) ) { $allowed = false; // Check if current time falls within allowed schedule if ( isset( $access_schedule[ $current_day ] ) ) { $day_schedule = $access_schedule[ $current_day ]; foreach ( $day_schedule as $time_slot ) { if ( $current_time >= $time_slot['start'] && $current_time <= $time_slot['end'] ) { $allowed = true; break; } } } if ( ! $allowed ) { $next_available = get_next_available_time( $access_schedule ); return new WP_Error( 'cocart_time_restricted', 'Access is not permitted at this time', array( 'status' => 403, 'current_time' => $current_time, 'next_available' => $next_available ) ); } } return $permission;}
Custom Headers
Copy
add_filter( 'cocart_login_permission_callback', 'validate_custom_headers', 10, 4 );function validate_custom_headers( $permission, $current_user, $request, $endpoint ) { if ( true !== $permission ) { return $permission; } // Validate client identifier $client_id = $request->get_header( 'X-Client-ID' ); $app_version = $request->get_header( 'X-App-Version' ); if ( ! empty( $client_id ) ) { $client_config = get_client_configuration( $client_id ); if ( ! $client_config ) { return new WP_Error( 'cocart_unknown_client', 'Unknown client identifier.', array( 'status' => 400 ) ); } // Check minimum app version requirements if ( ! empty( $app_version ) && ! empty( $client_config['min_version'] ) ) { if ( version_compare( $app_version, $client_config['min_version'], '<' ) ) { return new WP_Error( 'cocart_outdated_client', 'Client version is outdated and no longer supported.', array( 'status' => 426, 'current_version' => $app_version, 'minimum_version' => $client_config['min_version'] ) ); } } // Store client info for logging update_user_meta( $current_user->ID, 'last_client_info', array( 'client_id' => $client_id, 'app_version' => $app_version, 'login_time' => current_time( 'mysql' ) ) ); } return $permission;}
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.