Filter uncanny-automator

automator_outgoing_webhook_authorization_string

Filters the authorization string sent with outgoing webhooks, allowing modification before transmission.

add_filter( 'automator_outgoing_webhook_authorization_string', $callback, 10, 4 );

Description

Allows developers to modify the Authorization header string for outgoing webhooks. This filter is applied before the webhook is sent, enabling customization of authentication credentials. Developers can inspect and alter the `$authorization`, `$action_id`, `$headers`, and the webhook object (`$this`) to dynamically set or override the authorization.


Usage

add_filter( 'automator_outgoing_webhook_authorization_string', 'your_function_name', 10, 4 );

Parameters

$authorization (mixed)
This parameter contains the authorization string that will be sent with the webhook.
$action_id (mixed)
This parameter holds the authorization string that will be used for the outgoing webhook.
$headers (mixed)
This parameter contains the ID of the action that is triggering the webhook.
$this (mixed)
This parameter contains the headers that will be sent with the webhook.

Return Value

The filtered value.


Examples

/**
 * Example of modifying the Authorization header for outgoing webhooks.
 *
 * This function demonstrates how to prepend a custom prefix to the
 * authorization string if it's a Bearer token, ensuring compliance with
 * specific API requirements.
 *
 * @param mixed  $authorization The original authorization string.
 * @param mixed  $action_id     The ID of the automation action.
 * @param array  $headers       The array of outgoing webhook headers.
 * @param object $instance      The instance of the class calling the filter (AutomatorSendWebhook).
 *
 * @return string The modified authorization string.
 */
add_filter( 'automator_outgoing_webhook_authorization_string', function( $authorization, $action_id, $headers, $instance ) {
	// Check if the authorization is a string and starts with 'Bearer '
	if ( is_string( $authorization ) && strpos( $authorization, 'Bearer ' ) === 0 ) {
		// Example: Prepend a custom prefix for certain APIs.
		// In a real-world scenario, you might check $action_id or other
		// context to decide whether to modify the authorization.
		$custom_prefix = 'CustomAPIPrefix_';
		return $custom_prefix . $authorization;
	}

	// If not a Bearer token or no modification needed, return the original.
	return $authorization;
}, 10, 4 );

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

src/core/lib/webhooks/class-automator-send-webhook.php:597

public function get_authorization( $action_id, $headers, $data, $parsing_args = array() ) {

		$authorization         = isset( $data['WEBHOOK_AUTHORIZATIONS'] ) ? $data['WEBHOOK_AUTHORIZATIONS'] : null;
		$authorization_originl = get_post_meta( $action_id, 'WEBHOOK_AUTHORIZATIONS_ORIGINAL', true );
		$authorization         = ! empty( $authorization_originl ) && is_scalar( $authorization_originl ) ? $authorization_originl : $authorization;

		if ( empty( $authorization ) ) {
			return $headers;
		}

		$authorization            = $this->maybe_parse_tokens( $authorization, $parsing_args );
		$headers['Authorization'] = apply_filters( 'automator_outgoing_webhook_authorization_string', $authorization, $action_id, $headers, $this );

		return $headers;
	}

Scroll to Top