类文档

Basic

💡 云策文档标注

概述

Basic 类是一个 WordPress HTTP 请求库中的基本认证提供者,用于通过 Authorization 头实现 Basic HTTP 认证。它实现了 Auth 接口,提供构造、注册回调、设置 cURL 参数和添加 HTTP 头等功能。

关键要点

  • Basic 类实现 Auth 接口,处理 Basic HTTP 认证,通过 Authorization 头传递凭据。
  • 构造函数 __construct 接受一个包含用户名和密码的数组参数,若参数无效会抛出 InvalidArgument 或 ArgumentCount 异常。
  • register 方法注册回调到 Hooks 系统,用于在 cURL 或 fsockopen 请求前设置认证参数。
  • curl_before_send 方法设置 cURL 的 HTTPAUTH 和 USERPWD 选项以启用 Basic 认证。
  • fsockopen_header 方法在 HTTP 头中添加 Authorization 头,包含 base64 编码的认证字符串。
  • getAuthString 方法返回格式为 "user:pass" 的认证字符串,供其他方法使用。

代码示例

public function __construct($args = null) {
    if (is_array($args)) {
        if (count($args) !== 2) {
            throw ArgumentCount::create('an array with exactly two elements', count($args), 'authbasicbadargs');
        }

        list($this->user, $this->pass) = $args;
        return;
    }

    if ($args !== null) {
        throw InvalidArgument::create(1, '$args', 'array|null', gettype($args));
    }
}

注意事项

  • 构造函数自 2.0 版本起会抛出 InvalidArgument 和 ArgumentCount 异常,需确保传入参数为包含两个元素的数组或 null。
  • 认证字符串通过 getAuthString 方法生成,格式为 "user:pass",并在 fsockopen_header 中 base64 编码后添加到 HTTP 头。
  • 使用前需通过 register 方法将回调注册到 Hooks 系统,以确保认证逻辑在请求发送前正确执行。

📄 原文内容

Basic Authentication provider

Description

Provides a handler for Basic HTTP authentication via the Authorization header.

Methods

Name Description
Basic::__construct Constructor
Basic::curl_before_send Set cURL parameters before the data is sent
Basic::fsockopen_header Add extra headers to the request before sending
Basic::getAuthString Get the authentication string (user:pass)
Basic::register Register the necessary callbacks

Source

class Basic implements Auth {
	/**
	 * Username
	 *
	 * @var string
	 */
	public $user;

	/**
	 * Password
	 *
	 * @var string
	 */
	public $pass;

	/**
	 * Constructor
	 *
	 * @since 2.0 Throws an `InvalidArgument` exception.
	 * @since 2.0 Throws an `ArgumentCount` exception instead of the Requests base `Exception.
	 *
	 * @param array|null $args Array of user and password. Must have exactly two elements
	 *
	 * @throws WpOrgRequestsExceptionInvalidArgument When the passed argument is not an array or null.
	 * @throws WpOrgRequestsExceptionArgumentCount   On incorrect number of array elements (`authbasicbadargs`).
	 */
	public function __construct($args = null) {
		if (is_array($args)) {
			if (count($args) !== 2) {
				throw ArgumentCount::create('an array with exactly two elements', count($args), 'authbasicbadargs');
			}

			list($this->user, $this->pass) = $args;
			return;
		}

		if ($args !== null) {
			throw InvalidArgument::create(1, '$args', 'array|null', gettype($args));
		}
	}

	/**
	 * Register the necessary callbacks
	 *
	 * @see WpOrgRequestsAuthBasic::curl_before_send()
	 * @see WpOrgRequestsAuthBasic::fsockopen_header()
	 * @param WpOrgRequestsHooks $hooks Hook system
	 */
	public function register(Hooks $hooks) {
		$hooks->register('curl.before_send', [$this, 'curl_before_send']);
		$hooks->register('fsockopen.after_headers', [$this, 'fsockopen_header']);
	}

	/**
	 * Set cURL parameters before the data is sent
	 *
	 * @param resource|CurlHandle $handle cURL handle
	 */
	public function curl_before_send(&$handle) {
		curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString());
	}

	/**
	 * Add extra headers to the request before sending
	 *
	 * @param string $out HTTP header string
	 */
	public function fsockopen_header(&$out) {
		$out .= sprintf("Authorization: Basic %srn", base64_encode($this->getAuthString()));
	}

	/**
	 * Get the authentication string (user:pass)
	 *
	 * @return string
	 */
	public function getAuthString() {
		return $this->user . ':' . $this->pass;
	}
}