Yoast\WP\SEO\MyYoast_Client\Infrastructure\Registration

Client_Registration::do_registerprivateYoast 1.0

Performs the actual DCR registration request.

Метод класса: Client_Registration{}

Хуков нет.

Возвращает

Registered_Client. The registration result.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->do_register( $redirect_uris ): Registered_Client;
$redirect_uris(string[]) (обязательный)
The OAuth redirect URIs to register.

Код Client_Registration::do_register() Yoast 28.3

private function do_register( array $redirect_uris ): Registered_Client {
	try {
		$registration_endpoint = $this->discovery_client->get_document()->get_registration_endpoint();
	} catch ( Discovery_Failed_Exception | Server_Capability_Exception $e ) {
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
		throw new Registration_Failed_Exception( 'OIDC discovery failed: ' . $e->getMessage(), 0, $e );
	}

	$software_statement   = $this->issuer_config->get_software_statement();
	$initial_access_token = $this->issuer_config->get_initial_access_token();

	if ( $software_statement === '' || $initial_access_token === '' ) {
		throw new Registration_Failed_Exception( 'Software statement and initial access token must be configured.' );
	}

	// Ensure a registration key pair exists.
	$key_pair   = $this->key_pair_manager->get_or_create_key_pair( Key_Pair_Manager::PURPOSE_REGISTRATION );
	$public_jwk = $this->key_pair_manager->get_public_key_jwk( $key_pair );

	$request_body = [
		'software_statement'         => $software_statement,
		'redirect_uris'              => $redirect_uris,
		'grant_types'                => [ 'authorization_code', 'refresh_token', 'client_credentials' ],
		'token_endpoint_auth_method' => 'private_key_jwt',
		'jwks'                       => [ 'keys' => [ $public_jwk ] ],
		'dpop_bound_access_tokens'   => true,
	];

	// phpcs:ignore Yoast.Yoast.JsonEncodeAlternative.Found -- Encoding for HTTP request body, not user-facing output.
	$json = \wp_json_encode( $request_body );
	if ( $json === false ) {
		throw new Registration_Failed_Exception( 'Failed to JSON-encode DCR request body.' );
	}

	$result = $this->http_client->request(
		'POST',
		$registration_endpoint,
		[
			'headers' => [
				'Authorization' => 'Bearer ' . $initial_access_token,
				'Content-Type'  => 'application/json',
				'Accept'        => 'application/json',
			],
			'body'    => $json,
			'timeout' => 15,
		],
	);

	if ( $result->is_transport_failure() ) {
		$error_message = (string) $result->get_body_value( 'error_description', '' );
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
		throw new Registration_Failed_Exception( 'DCR request failed: ' . $error_message );
	}

	// The server temporarily refuses new registrations (rollout brake engaged).
	// Surface it as a typed transient failure carrying the (display-only) retry hint.
	if ( $result->get_status() === 503 && $result->get_body_value( 'error' ) === 'temporarily_unavailable' ) {
		$error_message = (string) $result->get_body_value( 'error_description', 'Client registration is temporarily disabled.' );
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
		throw new Registration_Temporarily_Unavailable_Exception( $error_message, $this->get_retry_after_seconds( $result ) );
	}

	if ( $result->get_status() === 429 ) {
		$this->logger->warning( 'DCR was rate-limited (HTTP 429).' );
		// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
		throw new Rate_Limited_Exception( 'DCR was rate-limited (HTTP 429).', $this->get_retry_after_seconds( $result ) );
	}

	if ( $result->get_status() !== 201 ) {
		$error_message = (string) $result->get_body_value( 'error_description', $result->get_body_value( 'error', '' ) );
		throw new Registration_Failed_Exception(
			// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Internal exception message.
			\sprintf( 'DCR returned HTTP %d: %s', $result->get_status(), $error_message ),
		);
	}

	$body = $result->get_body();
	if ( ! \is_array( $body ) || empty( $body['client_id'] ) ) {
		throw new Registration_Failed_Exception( 'DCR returned invalid response.' );
	}

	return $this->store_credentials( $body );
}