2023-04-21 23:25:39 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-05-05 20:02:12 +02:00
|
|
|
use WP_REST_Response;
|
2023-04-21 23:25:39 +02:00
|
|
|
use Activitypub\Signature;
|
2023-05-15 10:48:34 +02:00
|
|
|
use Activitypub\Model\User;
|
2023-05-18 08:03:11 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
|
2023-04-21 23:25:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Server REST-Class
|
|
|
|
*
|
|
|
|
* @author Django Doucet
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#security-verification
|
|
|
|
*/
|
|
|
|
class Server {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-05-05 20:02:12 +02:00
|
|
|
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
|
2023-05-18 08:03:11 +02:00
|
|
|
\add_filter( 'rest_request_before_callbacks', array( self::class, 'authorize_activitypub_requests' ), 10, 3 );
|
2023-05-05 20:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
2023-05-18 08:03:11 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2023-05-05 20:09:12 +02:00
|
|
|
'/application',
|
2023-05-05 20:02:12 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-05-05 20:09:12 +02:00
|
|
|
'callback' => array( self::class, 'application_actor' ),
|
2023-05-05 20:02:12 +02:00
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-05-05 20:09:12 +02:00
|
|
|
* Render Application actor profile
|
2023-05-05 20:02:12 +02:00
|
|
|
*
|
2023-05-12 10:17:36 +02:00
|
|
|
* @return WP_REST_Response The JSON profile of the Application Actor.
|
2023-05-05 20:02:12 +02:00
|
|
|
*/
|
2023-05-05 20:09:12 +02:00
|
|
|
public static function application_actor() {
|
2023-05-05 20:02:12 +02:00
|
|
|
$json = new \stdClass();
|
|
|
|
|
|
|
|
$json->{'@context'} = \Activitypub\get_context();
|
2023-05-18 08:03:11 +02:00
|
|
|
$json->id = get_rest_url_by_path( 'application' );
|
2023-05-05 20:02:12 +02:00
|
|
|
$json->type = 'Application';
|
2023-05-18 08:03:11 +02:00
|
|
|
$json->preferredUsername = str_replace( array( '.' ), '-' , wp_parse_url( get_site_url(), PHP_URL_HOST ) ); // phpcs:ignore WordPress.NamingConventions
|
2023-05-05 20:02:12 +02:00
|
|
|
$json->name = get_bloginfo( 'name' );
|
2023-05-05 20:55:12 +02:00
|
|
|
$json->summary = 'WordPress-ActivityPub application actor';
|
2023-05-05 21:45:38 +02:00
|
|
|
$json->manuallyApprovesFollowers = true; // phpcs:ignore WordPress.NamingConventions
|
|
|
|
$json->icon = array( get_site_icon_url() ); // phpcs:ignore WordPress.NamingConventions short array syntax
|
|
|
|
$json->publicKey = (object) array( // phpcs:ignore WordPress.NamingConventions
|
2023-05-18 08:03:11 +02:00
|
|
|
'id' => get_rest_url_by_path( 'application#main-key' ),
|
|
|
|
'owner' => get_rest_url_by_path( 'application' ),
|
2023-05-15 10:48:34 +02:00
|
|
|
'publicKeyPem' => Signature::get_public_key( User::APPLICATION_USER_ID ), // phpcs:ignore WordPress.NamingConventions
|
2023-05-05 20:02:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$response = new WP_REST_Response( $json, 200 );
|
|
|
|
|
|
|
|
$response->header( 'Content-Type', 'application/activity+json' );
|
|
|
|
|
|
|
|
return $response;
|
2023-04-21 23:25:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function to authorize each api requests
|
|
|
|
*
|
|
|
|
* @see \WP_REST_Request
|
|
|
|
*
|
2023-05-12 10:17:36 +02:00
|
|
|
* @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.
|
|
|
|
* @param array $handler Route handler used for the request.
|
|
|
|
* @param WP_REST_Request $request Request used to generate the response.
|
2023-04-21 23:25:39 +02:00
|
|
|
*
|
2023-05-12 10:17:36 +02:00
|
|
|
* @return mixed|WP_Error The response, error, or modified response.
|
2023-04-21 23:25:39 +02:00
|
|
|
*/
|
|
|
|
public static function authorize_activitypub_requests( $response, $handler, $request ) {
|
2023-05-06 07:44:15 +02:00
|
|
|
$route = $request->get_route();
|
2023-05-09 11:57:43 +02:00
|
|
|
if ( ! str_starts_with( $route, '/activitypub' ) ) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'POST' === $request->get_method() ) {
|
|
|
|
$verified_request = Signature::verify_http_signature( $request );
|
|
|
|
if ( \is_wp_error( $verified_request ) ) {
|
|
|
|
return $verified_request;
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-18 08:03:11 +02:00
|
|
|
if ( get_rest_url_by_path( 'webfinger' ) !== $route ) {
|
2023-05-09 11:57:43 +02:00
|
|
|
// SecureMode/Authorized fetch.
|
2023-05-15 06:53:11 +02:00
|
|
|
if ( ACTIVITYPUB_SECURE_MODE ) {
|
2023-05-09 11:57:43 +02:00
|
|
|
$verified_request = Signature::verify_http_signature( $request );
|
|
|
|
if ( \is_wp_error( $verified_request ) ) {
|
|
|
|
return $verified_request;
|
2023-05-05 22:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-21 23:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-09 11:57:43 +02:00
|
|
|
|
|
|
|
return $response;
|
2023-04-21 23:25:39 +02:00
|
|
|
}
|
|
|
|
}
|