WC_REST_Authentication::perform_basic_authentication │ private │ WC 1.0

Basic Authentication.

SSL-encrypted requests are not subject to sniffing or man-in-the-middle attacks, so the request can be authenticated by simply looking up the user associated with the given consumer key and confirming the consumer secret provided is valid.

Method of the class: WC_REST_Authentication{}

No Hooks.

Returns

int|bool.

Usage

// private - for code of main (parent) class only
$result = $this->perform_basic_authentication();

WC_REST_Authentication::perform_basic_authentication() code WC 11.2.2

private function perform_basic_authentication() {
	$this->auth_method = 'basic_auth';
	$consumer_key      = '';
	$consumer_secret   = '';

	// If the $_GET parameters are present, use those first.
	if ( ! empty( $_GET['consumer_key'] ) && ! empty( $_GET['consumer_secret'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Raw credentials and request data are required for OAuth signature verification.
		$consumer_key    = $_GET['consumer_key']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Raw credentials and request data are required for OAuth signature verification.
		$consumer_secret = $_GET['consumer_secret']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Raw credentials and request data are required for OAuth signature verification.
	}

	// If the above is not present, we will do full basic auth.
	if ( ! $consumer_key && ! empty( $_SERVER['PHP_AUTH_USER'] ) && ! empty( $_SERVER['PHP_AUTH_PW'] ) ) {
		$consumer_key    = $_SERVER['PHP_AUTH_USER']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Raw credentials and request data are required for OAuth signature verification.
		$consumer_secret = $_SERVER['PHP_AUTH_PW']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Raw credentials and request data are required for OAuth signature verification.
	}

	// Stop if don't have any key.
	if ( ! $consumer_key || ! $consumer_secret ) {
		return false;
	}

	// Get user data.
	$this->user = $this->get_user_data_by_consumer_key( $consumer_key );
	if ( empty( $this->user ) ) {
		return false;
	}

	// Validate user secret.
	if ( ! hash_equals( $this->user->consumer_secret, $consumer_secret ) ) { // @codingStandardsIgnoreLine
		$this->set_error( new WP_Error( 'woocommerce_rest_authentication_error', __( 'Consumer secret is invalid.', 'woocommerce' ), array( 'status' => 401 ) ) );

		return false;
	}

	return $this->user->user_id;
}