2018-12-08 00:02:18 +01:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub\Rest;
|
2018-12-08 00:02:18 +01:00
|
|
|
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* ActivityPub WebFinger REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://webfinger.net/
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Webfinger {
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
public static function init() {
|
2019-09-27 10:12:59 +02:00
|
|
|
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Webfinger', 'register_routes' ) );
|
|
|
|
\add_action( 'webfinger_user_data', array( '\Activitypub\Rest\Webfinger', 'add_webfinger_discovery' ), 10, 3 );
|
2021-02-18 07:12:32 +01:00
|
|
|
\add_action( 'webfinger_lookup', array( '\Activitypub\Rest\Webfinger', 'webfinger_lookup' ), 10, 3 );
|
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
}
|
2019-02-28 19:31:55 +01:00
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2022-01-27 13:09:11 +01:00
|
|
|
'activitypub/1.0',
|
|
|
|
'/webfinger',
|
|
|
|
array(
|
2018-12-09 21:54:57 +01:00
|
|
|
array(
|
2020-09-18 16:36:09 +02:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ),
|
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render JRD file
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function webfinger( $request ) {
|
|
|
|
$resource = $request->get_param( 'resource' );
|
|
|
|
|
2022-05-20 08:49:05 +02:00
|
|
|
$matched = \str_contains( $resource, '@' );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
if ( ! $matched ) {
|
2019-09-27 10:12:59 +02:00
|
|
|
return new \WP_Error( 'activitypub_unsupported_resource', \__( 'Resource is invalid', 'activitypub' ), array( 'status' => 400 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 08:49:05 +02:00
|
|
|
$resource = \str_replace( 'acct:', '', $resource );
|
|
|
|
|
|
|
|
$resource_identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) );
|
|
|
|
$resource_host = \substr( \strrchr( $resource, '@' ), 1 );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
2020-05-12 19:42:09 +02:00
|
|
|
if ( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) !== $resource_host ) {
|
2019-09-27 10:12:59 +02:00
|
|
|
return new \WP_Error( 'activitypub_wrong_host', \__( 'Resource host does not match blog host', 'activitypub' ), array( 'status' => 404 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$user = \get_user_by( 'login', \esc_sql( $resource_identifier ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
|
|
|
if ( ! $user ) {
|
2019-09-27 10:12:59 +02:00
|
|
|
return new \WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$json = array(
|
|
|
|
'subject' => $resource,
|
|
|
|
'aliases' => array(
|
2019-09-27 10:12:59 +02:00
|
|
|
\get_author_posts_url( $user->ID ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
'links' => array(
|
|
|
|
array(
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
2019-09-27 10:12:59 +02:00
|
|
|
'href' => \get_author_posts_url( $user->ID ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'rel' => 'http://webfinger.net/rel/profile-page',
|
|
|
|
'type' => 'text/html',
|
2019-09-27 10:12:59 +02:00
|
|
|
'href' => \get_author_posts_url( $user->ID ),
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_REST_Response( $json, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['resource'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
2022-05-20 08:49:05 +02:00
|
|
|
'pattern' => '^acct:(.+)@(.+)$',
|
2018-12-09 21:54:57 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2018-12-08 00:02:18 +01:00
|
|
|
/**
|
|
|
|
* Add WebFinger discovery links
|
|
|
|
*
|
|
|
|
* @param array $array the jrd array
|
|
|
|
* @param string $resource the WebFinger resource
|
|
|
|
* @param WP_User $user the WordPress user
|
|
|
|
*/
|
|
|
|
public static function add_webfinger_discovery( $array, $resource, $user ) {
|
|
|
|
$array['links'][] = array(
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
2019-09-27 10:12:59 +02:00
|
|
|
'href' => \get_author_posts_url( $user->ID ),
|
2018-12-08 00:02:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
2021-02-18 07:12:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* WebFinger Lookup to find user uri
|
|
|
|
*
|
|
|
|
* @param string $resource the WebFinger resource
|
2022-09-27 23:28:45 +02:00
|
|
|
* @return array ['href', 'name']. ex: href=https://domain.tld/user/webfinger, name=webfinger@domain.tld
|
2021-02-18 07:12:32 +01:00
|
|
|
*/
|
|
|
|
public static function webfinger_lookup( $webfinger ) {
|
|
|
|
$activity_profile = null;
|
2022-04-15 09:17:00 +02:00
|
|
|
if ( \substr( $webfinger, 0, 1 ) === '@' ) {
|
2021-02-18 07:12:32 +01:00
|
|
|
$webfinger = substr( $webfinger, 1 );
|
2022-04-15 09:17:00 +02:00
|
|
|
}
|
2021-02-18 07:12:32 +01:00
|
|
|
$url_host = \explode( '@', $webfinger );
|
2022-09-28 19:18:30 +02:00
|
|
|
$webfinger_query = 'https://' . \end( $url_host ) . '/.well-known/webfinger?resource=acct%3A' . \rawurlencode( $webfinger );
|
2021-02-18 07:12:32 +01:00
|
|
|
|
|
|
|
$response = \wp_safe_remote_get( $webfinger_query );
|
|
|
|
if ( ! is_wp_error( $response ) ) {
|
|
|
|
$ap_link = json_decode( $response['body'] );
|
|
|
|
if ( isset( $ap_link->links ) ) {
|
|
|
|
foreach ( $ap_link->links as $link ) {
|
2022-04-15 09:17:00 +02:00
|
|
|
if ( ! property_exists( $link, 'type' ) ) {
|
2021-02-18 07:12:32 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-28 19:18:30 +02:00
|
|
|
if ( isset( $link->type ) && 'application/activity+json' === $link->type ) {
|
2021-02-18 07:12:32 +01:00
|
|
|
$activity_profile['href'] = $link->href;
|
|
|
|
$activity_profile['name'] = $webfinger;
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 09:17:00 +02:00
|
|
|
}
|
2021-02-18 07:12:32 +01:00
|
|
|
}
|
2022-04-15 09:17:00 +02:00
|
|
|
|
2021-02-18 07:12:32 +01:00
|
|
|
return $activity_profile;
|
|
|
|
}
|
2018-12-08 00:02:18 +01:00
|
|
|
}
|