wordpress-activitypub/includes/rest/class-webfinger.php

115 lines
2.4 KiB
PHP
Raw Normal View History

2018-12-08 00:02:18 +01:00
<?php
namespace Activitypub\Rest;
2018-12-08 00:02:18 +01:00
2023-04-20 15:22:11 +02:00
use WP_Error;
use WP_REST_Response;
2023-07-03 20:02:00 +02:00
use Activitypub\Collection\Users as User_Collection;
2023-04-20 15:22:11 +02:00
2019-02-24 13:01:28 +01:00
/**
* ActivityPub WebFinger REST-Class
*
* @author Matthias Pfefferle
*
* @see https://webfinger.net/
*/
class Webfinger {
2019-02-24 13:01:28 +01:00
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
2023-04-20 15:22:11 +02:00
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
\add_action( 'webfinger_user_data', array( self::class, 'add_webfinger_discovery' ), 10, 3 );
}
2019-02-28 19:31:55 +01:00
/**
* Register routes
*/
public static function register_routes() {
2019-09-27 10:12:59 +02:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
2022-01-27 13:09:11 +01:00
'/webfinger',
array(
array(
2020-09-18 16:36:09 +02:00
'methods' => \WP_REST_Server::READABLE,
2023-04-20 15:22:11 +02:00
'callback' => array( self::class, 'webfinger' ),
2020-09-18 16:36:09 +02:00
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),
)
);
}
/**
* Render JRD file
*
* @param WP_REST_Request $request
* @return WP_REST_Response
*/
public static function webfinger( $request ) {
$resource = $request->get_param( 'resource' );
2023-07-03 20:02:00 +02:00
$user = User_Collection::get_by_resource( $resource );
if ( is_wp_error( $user ) ) {
return $user;
}
2023-05-30 11:37:21 +02:00
$aliases = array(
$user->get_url(),
$user->get_at_url(),
);
$json = array(
'subject' => $resource,
2023-05-30 11:37:21 +02:00
'aliases' => array_values( array_unique( $aliases ) ),
'links' => array(
array(
'rel' => 'self',
'type' => 'application/activity+json',
'href' => $user->get_url(),
),
array(
'rel' => 'http://webfinger.net/rel/profile-page',
'type' => 'text/html',
'href' => $user->get_url(),
),
),
);
2023-04-20 15:22:11 +02:00
return new WP_REST_Response( $json, 200 );
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['resource'] = array(
'required' => true,
'type' => 'string',
'pattern' => '^acct:(.+)@(.+)$',
);
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;
}
}