automator_pro_webhook_rest_route_permission_callback
Filters the permission callback for Automator Pro webhook REST routes, allowing customization of access control.
add_filter( 'automator_pro_webhook_rest_route_permission_callback', $callback, 10, 2 );
Description
Filters the permission callback for Uncanny Automator's webhook REST API routes. Developers can use this to modify or replace the default permission checks, allowing for custom access control logic before a webhook request is processed. This hook returns `true` by default, indicating the request is permitted.
Usage
add_filter( 'automator_pro_webhook_rest_route_permission_callback', 'your_function_name', 10, 2 );
Parameters
-
$attributes(mixed) - This parameter is the default permission status, usually indicating whether access is granted or denied before any checks.
-
$request(mixed) - This parameter holds the attributes of the REST API route, typically including arguments and other metadata for the route.
Return Value
The filtered value.
Examples
/**
* Example filter to add custom webhook permission checks.
*
* This filter allows you to add additional checks beyond the default webhook
* authentication mechanism. For instance, you might want to check for a specific
* custom header or verify a token within the request body.
*
* @param bool $permission_granted Whether the permission is currently granted (true) or denied (false).
* @param array|null $attributes The route attributes, which might contain 'custom_headers'.
* @param WP_REST_Request $request The current REST request object.
* @return bool The updated permission status.
*/
add_filter( 'automator_pro_webhook_rest_route_permission_callback', function( $permission_granted, $attributes, $request ) {
// If permission is already denied, no need to proceed.
if ( ! $permission_granted ) {
return false;
}
// Check for a specific custom header if it's defined in the route attributes.
if ( ! empty( $attributes ) && isset( $attributes['custom_headers']['X-My-Custom-Auth'] ) ) {
$request_headers = $request->get_headers();
$custom_auth_header_name = 'X-My-Custom-Auth';
// Ensure the custom header exists in the request.
if ( ! isset( $request_headers[ $custom_auth_header_name ] ) ) {
// Permission denied if the required custom header is missing.
return false;
}
// Optionally, you could perform further validation on the header value.
// For example, comparing it against a stored secret.
// $expected_value = 'a-secret-token-from-settings';
// if ( $request_headers[ $custom_auth_header_name ][0] !== $expected_value ) {
// return false;
// }
}
// If all checks pass, grant permission.
return true;
}, 10, 3 ); // Priority 10, accepts 3 arguments.
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:269
uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:275
uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:278
uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:282
public static function register_rest_route( $_route, $hook = array() ) {
register_rest_route(
AUTOMATOR_REST_API_END_POINT,
$_route,
array(
'methods' => apply_filters(
'automator_pro_webhook_rest_route_methods',
array(
'POST',
'GET',
'PUT',
),
$_route
),
'callback' => array( __CLASS__, 'automator_webhook_rest_api_callback' ),
'custom_headers' => isset( $hook['custom_headers'] ) ? $hook['custom_headers'] : null,
'permission_callback' => function ( $request ) {
$attributes = $request->get_attributes();
if ( empty( $attributes ) || ! isset( $attributes['custom_headers'] ) ) {
return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', true, $attributes, $request );
}
$request_headers = $request->get_headers();
foreach ( $attributes['custom_headers'] as $header_name => $header_value ) {
if ( ! isset( $request_headers[ $header_name ] ) ) {
return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', false, $attributes, $request );
}
if ( ! array_intersect( array( $header_value ), $request_headers[ $header_name ] ) ) {
return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', false, $attributes, $request );
}
}
return apply_filters( 'automator_pro_webhook_rest_route_permission_callback', true, $attributes, $request );
},
)
);
}