some small improvements

This commit is contained in:
Matthias Pfefferle 2023-05-22 11:31:46 +02:00
parent e04ccdc961
commit 467a349b16

View file

@ -1,9 +1,12 @@
<?php <?php
namespace Activitypub\Rest; namespace Activitypub\Rest;
use stdClass;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\Signature; use Activitypub\Signature;
use Activitypub\Model\User; use Activitypub\Model\User;
use function Activitypub\get_context;
use function Activitypub\get_rest_url_by_path; use function Activitypub\get_rest_url_by_path;
@ -47,17 +50,17 @@ class Server {
* @return WP_REST_Response The JSON profile of the Application Actor. * @return WP_REST_Response The JSON profile of the Application Actor.
*/ */
public static function application_actor() { public static function application_actor() {
$json = new \stdClass(); $json = new stdClass();
$json->{'@context'} = \Activitypub\get_context(); $json->{'@context'} = get_context();
$json->id = get_rest_url_by_path( 'application' ); $json->id = get_rest_url_by_path( 'application' );
$json->type = 'Application'; $json->type = 'Application';
$json->preferredUsername = str_replace( array( '.' ), '-', wp_parse_url( get_site_url(), PHP_URL_HOST ) ); // phpcs:ignore WordPress.NamingConventions $json->preferredUsername = str_replace( array( '.' ), '-', wp_parse_url( get_site_url(), PHP_URL_HOST ) ); // phpcs:ignore WordPress.NamingConventions
$json->name = get_bloginfo( 'name' ); $json->name = get_bloginfo( 'name' );
$json->summary = 'WordPress-ActivityPub application actor'; $json->summary = __( 'WordPress-ActivityPub application actor', 'activitypub' );
$json->manuallyApprovesFollowers = true; // phpcs:ignore WordPress.NamingConventions $json->manuallyApprovesFollowers = true; // phpcs:ignore WordPress.NamingConventions
$json->icon = array( get_site_icon_url() ); // phpcs:ignore WordPress.NamingConventions short array syntax $json->icon = array( get_site_icon_url() ); // phpcs:ignore WordPress.NamingConventions short array syntax
$json->publicKey = (object) array( // phpcs:ignore WordPress.NamingConventions $json->publicKey = array( // phpcs:ignore WordPress.NamingConventions
'id' => get_rest_url_by_path( 'application#main-key' ), 'id' => get_rest_url_by_path( 'application#main-key' ),
'owner' => get_rest_url_by_path( 'application' ), 'owner' => get_rest_url_by_path( 'application' ),
'publicKeyPem' => Signature::get_public_key( User::APPLICATION_USER_ID ), // phpcs:ignore WordPress.NamingConventions 'publicKeyPem' => Signature::get_public_key( User::APPLICATION_USER_ID ), // phpcs:ignore WordPress.NamingConventions
@ -73,7 +76,7 @@ class Server {
/** /**
* Callback function to authorize each api requests * Callback function to authorize each api requests
* *
* @see \WP_REST_Request * @see WP_REST_Request
* *
* @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client. * @param WP_REST_Response|WP_HTTP_Response|WP_Error|mixed $response Result to send to the client.
* Usually a WP_REST_Response or WP_Error. * Usually a WP_REST_Response or WP_Error.
@ -84,23 +87,25 @@ class Server {
*/ */
public static function authorize_activitypub_requests( $response, $handler, $request ) { public static function authorize_activitypub_requests( $response, $handler, $request ) {
$route = $request->get_route(); $route = $request->get_route();
if ( ! str_starts_with( $route, '/activitypub' ) ) { if ( ! str_starts_with( $route, '/activitypub' ) ) {
return $response; return $response;
} }
if ( get_rest_url_by_path( 'webfinger' ) !== $route ) {
return $response;
}
if ( 'POST' === $request->get_method() ) { if ( 'POST' === $request->get_method() ) {
$verified_request = Signature::verify_http_signature( $request ); $verified_request = Signature::verify_http_signature( $request );
if ( \is_wp_error( $verified_request ) ) { if ( \is_wp_error( $verified_request ) ) {
return $verified_request; return $verified_request;
} }
} else { } elseif ( 'GET' === $request->get_method() ) {
if ( get_rest_url_by_path( 'webfinger' ) !== $route ) { if ( ACTIVITYPUB_SECURE_MODE ) {
// SecureMode/Authorized fetch. $verified_request = Signature::verify_http_signature( $request );
if ( ACTIVITYPUB_SECURE_MODE ) { if ( \is_wp_error( $verified_request ) ) {
$verified_request = Signature::verify_http_signature( $request ); return $verified_request;
if ( \is_wp_error( $verified_request ) ) {
return $verified_request;
}
} }
} }
} }