Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushNotificationRestController::find_authorization_failure │ private │ WC 1.0

Checks the JWT from the Authorization header against the request.

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

Хуков нет.

Возвращает

string|null. One of the AuthorizationFailureReason constants, or null when authorized.

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

// private - только в коде основоного (родительского) класса
$result = $this->find_authorization_failure( $request ): ?string;
$request(WP_REST_Request) (обязательный)
The request object.

Код PushNotificationRestController::find_authorization_failure() WC 11.3.0

private function find_authorization_failure( WP_REST_Request $request ): ?string {
	$header = trim( (string) $request->get_header( 'authorization' ) );

	if ( '' !== $header ) {
		$token = strncasecmp( $header, 'Bearer ', 7 ) === 0 ? substr( $header, 7 ) : $header;
	} else {
		$token = $this->get_token_from_query( $request );
	}

	if ( '' === $token ) {
		return AuthorizationFailureReason::CREDENTIAL_MISSING;
	}

	if ( ! JsonWebToken::validate( $token, wp_salt( 'auth' ) ) ) {
		return AuthorizationFailureReason::TOKEN_INVALID;
	}

	$parts = JsonWebToken::get_parts( $token );

	if ( ! isset( $parts->payload->iss ) || get_site_url() !== $parts->payload->iss ) {
		return AuthorizationFailureReason::ISSUER_INVALID;
	}

	$body_hash = hash( 'sha256', $request->get_body() );

	if ( ! isset( $parts->payload->body_hash ) || ! hash_equals( (string) $parts->payload->body_hash, $body_hash ) ) {
		return AuthorizationFailureReason::BODY_HASH_MISMATCH;
	}

	return null;
}