Yoast\WP\SEO\MyYoast_Client\Infrastructure\Registration

Client_Registration::store_credentialsprivateYoast 1.0

Stores the DCR response credentials securely.

A registration read (RFC 7592 GET) response carries no registration access token; when the body omits the RAT, the existing stored RAT is preserved rather than overwritten with an empty value.

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

Хуков нет.

Возвращает

Registered_Client. The stored credentials.

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

// private - только в коде основоного (родительского) класса
$result = $this->store_credentials( $response_body ): Registered_Client;
$response_body(массив) (обязательный)
.

Код Client_Registration::store_credentials() Yoast 28.3

private function store_credentials( array $response_body ): Registered_Client {
	$option_key = $this->get_option_key();
	$existing   = $this->get_registered_client();

	// The RFC 7592 GET response never re-sends the RAT, so a missing key means "keep the stored
	// one" — encrypting the absent value would brick every future management call. Only a body
	// that explicitly carries a RAT (DCR / PUT) replaces it.
	if ( \array_key_exists( 'registration_access_token', $response_body ) ) {
		$rat           = $response_body['registration_access_token'];
		$encrypted_rat = $this->encryption->encrypt( $rat, self::ENCRYPTION_CONTEXT );
	}
	else {
		// Reuse the already-decrypted RAT and its stored ciphertext rather than re-encrypting.
		$rat           = ( $existing !== null ) ? $existing->get_registration_access_token() : '';
		$stored        = \get_option( $option_key, [] );
		$encrypted_rat = ( \is_array( $stored ) ) ? ( $stored['encrypted_rat'] ?? '' ) : '';
	}

	// Strip the RAT from metadata — it is stored encrypted separately.
	$metadata = $response_body;
	unset( $metadata['registration_access_token'] );

	// Preserve validation state across an in-place update or key rotation (same client_id), but
	// reset it for a fresh registration: a new client_id means the redirect URIs must be
	// re-validated from scratch. Always prune to the new redirect-URI set so a removed URI loses
	// its verification and an added one starts unverified.
	$validated_uris = [];
	if ( $existing !== null && $existing->get_client_id() === $response_body['client_id'] ) {
		$new_redirect_uris = ( $metadata['redirect_uris'] ?? [] );
		if ( \is_array( $new_redirect_uris ) ) {
			$validated_uris = \array_values( \array_intersect( $existing->get_validated_uris(), $new_redirect_uris ) );
		}
	}

	\update_option(
		$option_key,
		[
			'client_id'               => $response_body['client_id'],
			'encrypted_rat'           => $encrypted_rat,
			'registration_client_uri' => ( $response_body['registration_client_uri'] ?? '' ),
			'metadata'                => $metadata,
			'validated_uris'          => $validated_uris,
		],
		false,
	);

	$this->cached_registered_clients[ $option_key ] = new Registered_Client(
		$response_body['client_id'],
		$rat,
		( $response_body['registration_client_uri'] ?? '' ),
		$metadata,
		$validated_uris,
	);

	return $this->cached_registered_clients[ $option_key ];
}