Skip to content

Commit 0763316

Browse files
authored
add status message if it might be returned by API (#448)
1 parent fe07d5e commit 0763316

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

includes/class-http.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function post( $url, $body, $user_id ) {
5353
$code = \wp_remote_retrieve_response_code( $response );
5454

5555
if ( $code >= 400 ) {
56-
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
56+
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
5757
}
5858

5959
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
@@ -101,7 +101,7 @@ public static function get( $url ) {
101101
$code = \wp_remote_retrieve_response_code( $response );
102102

103103
if ( $code >= 400 ) {
104-
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
104+
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
105105
}
106106

107107
\do_action( 'activitypub_safe_remote_get_response', $response, $url );

includes/class-signature.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public static function verify_http_signature( $request ) {
247247
}
248248

249249
if ( ! isset( $headers['signature'] ) ) {
250-
return new WP_Error( 'activitypub_signature', 'Request not signed', array( 'status' => 403 ) );
250+
return new WP_Error( 'activitypub_signature', __( 'Request not signed', 'activitypub' ), array( 'status' => 403 ) );
251251
}
252252

253253
if ( array_key_exists( 'signature', $headers ) ) {
@@ -257,7 +257,7 @@ public static function verify_http_signature( $request ) {
257257
}
258258

259259
if ( ! isset( $signature_block ) || ! $signature_block ) {
260-
return new WP_Error( 'activitypub_signature', 'Incompatible request signature. keyId and signature are required', array( 'status' => 403 ) );
260+
return new WP_Error( 'activitypub_signature', __( 'Incompatible request signature. keyId and signature are required', 'activitypub' ), array( 'status' => 403 ) );
261261
}
262262

263263
$signed_headers = $signature_block['headers'];
@@ -267,12 +267,12 @@ public static function verify_http_signature( $request ) {
267267

268268
$signed_data = self::get_signed_data( $signed_headers, $signature_block, $headers );
269269
if ( ! $signed_data ) {
270-
return new WP_Error( 'activitypub_signature', 'Signed request date outside acceptable time window', array( 'status' => 403 ) );
270+
return new WP_Error( 'activitypub_signature', __( 'Signed request date outside acceptable time window', 'activitypub' ), array( 'status' => 403 ) );
271271
}
272272

273273
$algorithm = self::get_signature_algorithm( $signature_block );
274274
if ( ! $algorithm ) {
275-
return new WP_Error( 'activitypub_signature', 'Unsupported signature algorithm (only rsa-sha256 and hs2019 are supported)', array( 'status' => 403 ) );
275+
return new WP_Error( 'activitypub_signature', __( 'Unsupported signature algorithm (only rsa-sha256 and hs2019 are supported)', 'activitypub' ), array( 'status' => 403 ) );
276276
}
277277

278278
if ( \in_array( 'digest', $signed_headers, true ) && isset( $body ) ) {
@@ -288,7 +288,7 @@ public static function verify_http_signature( $request ) {
288288
}
289289

290290
if ( \base64_encode( \hash( $hashalg, $body, true ) ) !== $digest[1] ) { // phpcs:ignore
291-
return new WP_Error( 'activitypub_signature', 'Invalid Digest header', array( 'status' => 403 ) );
291+
return new WP_Error( 'activitypub_signature', __( 'Invalid Digest header', 'activitypub' ), array( 'status' => 403 ) );
292292
}
293293
}
294294

@@ -301,7 +301,7 @@ public static function verify_http_signature( $request ) {
301301
$verified = \openssl_verify( $signed_data, $signature_block['signature'], $public_key, $algorithm ) > 0;
302302

303303
if ( ! $verified ) {
304-
return new WP_Error( 'activitypub_signature', 'Invalid signature', array( 'status' => 403 ) );
304+
return new WP_Error( 'activitypub_signature', __( 'Invalid signature', 'activitypub' ), array( 'status' => 403 ) );
305305
}
306306
return $verified;
307307
}
@@ -321,7 +321,7 @@ public static function get_remote_key( $key_id ) { // phpcs:ignore
321321
if ( isset( $actor['publicKey']['publicKeyPem'] ) ) {
322322
return \rtrim( $actor['publicKey']['publicKeyPem'] ); // phpcs:ignore
323323
}
324-
return new WP_Error( 'activitypub_no_remote_key_found', 'No Public-Key found' );
324+
return new WP_Error( 'activitypub_no_remote_key_found', __( 'No Public-Key found', 'activitypub' ), array( 'status' => 403 ) );
325325
}
326326

327327
/**

includes/collection/class-followers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function add_follower( $user_id, $actor ) {
170170
}
171171

172172
if ( empty( $meta ) || ! is_array( $meta ) || is_wp_error( $meta ) ) {
173-
return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ) );
173+
return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ), array( 'status' => 400 ) );
174174
}
175175

176176
$error = null;

includes/functions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) {
5454
}
5555

5656
if ( ! $actor ) {
57-
return new WP_Error( 'activitypub_no_valid_actor_identifier', \__( 'The "actor" identifier is not valid', 'activitypub' ), $actor );
57+
return new WP_Error( 'activitypub_no_valid_actor_identifier', \__( 'The "actor" identifier is not valid', 'activitypub' ), array( 'status' => 404, 'actor' => $actor ) );
5858
}
5959

6060
if ( is_wp_error( $actor ) ) {
@@ -73,7 +73,7 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) {
7373
}
7474

7575
if ( ! \wp_http_validate_url( $actor ) ) {
76-
$metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
76+
$metadata = new WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) );
7777
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
7878
return $metadata;
7979
}
@@ -95,7 +95,7 @@ function get_remote_metadata_by_actor( $actor, $cached = true ) {
9595
\set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
9696

9797
if ( ! $metadata ) {
98-
$metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
98+
$metadata = new WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), array( 'status' => 400, 'actor' => $actor ) );
9999
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
100100
return $metadata;
101101
}
@@ -416,7 +416,7 @@ function is_user_type_disabled( $type ) {
416416
$return = false;
417417
break;
418418
default:
419-
$return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ) );
419+
$return = new WP_Error( 'activitypub_wrong_user_type', __( 'Wrong user type', 'activitypub' ), array( 'status' => 400 ) );
420420
break;
421421
}
422422

includes/model/class-follower.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function is_valid() {
142142
*/
143143
public function save() {
144144
if ( ! $this->is_valid() ) {
145-
return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ) );
145+
return new WP_Error( 'activitypub_invalid_follower', __( 'Invalid Follower', 'activitypub' ), array( 'status' => 400 ) );
146146
}
147147

148148
if ( ! $this->get__id() ) {

0 commit comments

Comments
 (0)