-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Webfinger: Use Rest Controller structure
- Loading branch information
Showing
6 changed files
with
456 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
/** | ||
* WebFinger REST-Class file. | ||
* | ||
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Rest; | ||
|
||
/** | ||
* ActivityPub WebFinger REST-Class. | ||
* | ||
* @author Matthias Pfefferle | ||
* | ||
* @see https://webfinger.net/ | ||
*/ | ||
class Webfinger_Controller extends \WP_REST_Controller { | ||
/** | ||
* The namespace of this controller's route. | ||
* | ||
* @var string | ||
*/ | ||
protected $namespace = ACTIVITYPUB_REST_NAMESPACE; | ||
|
||
/** | ||
* The base of this controller's route. | ||
* | ||
* @var string | ||
*/ | ||
protected $rest_base = 'webfinger'; | ||
|
||
/** | ||
* Register routes. | ||
*/ | ||
public function register_routes() { | ||
\register_rest_route( | ||
$this->namespace, | ||
'/' . $this->rest_base, | ||
array( | ||
array( | ||
'methods' => \WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_item' ), | ||
'permission_callback' => '__return_true', | ||
'args' => array( | ||
'resource' => array( | ||
'description' => 'The WebFinger resource.', | ||
'type' => 'string', | ||
'required' => true, | ||
'pattern' => '^(acct:)|^(https?://)(.+)$', | ||
), | ||
), | ||
), | ||
'schema' => array( $this, 'get_item_schema' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Retrieves the WebFinger profile. | ||
* | ||
* @param \WP_REST_Request $request The request object. | ||
* | ||
* @return \WP_REST_Response Response object. | ||
*/ | ||
public function get_item( $request ) { | ||
/** | ||
* Action triggered prior to the ActivityPub profile being created and sent to the client. | ||
*/ | ||
\do_action( 'activitypub_rest_webfinger_pre' ); | ||
|
||
$resource = $request->get_param( 'resource' ); | ||
$response = $this->get_profile( $resource ); | ||
$code = 200; | ||
|
||
if ( \is_wp_error( $response ) ) { | ||
$code = 400; | ||
$error_data = $response->get_error_data(); | ||
|
||
if ( isset( $error_data['status'] ) ) { | ||
$code = $error_data['status']; | ||
} | ||
} | ||
|
||
return new \WP_REST_Response( | ||
$response, | ||
$code, | ||
array( | ||
'Access-Control-Allow-Origin' => '*', | ||
'Content-Type' => 'application/jrd+json; charset=' . \get_option( 'blog_charset' ), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get the WebFinger profile. | ||
* | ||
* @param string $webfinger The WebFinger resource. | ||
* | ||
* @return array|\WP_Error The WebFinger profile or WP_Error if not found. | ||
*/ | ||
public function get_profile( $webfinger ) { | ||
/** | ||
* Filter the WebFinger data. | ||
* | ||
* @param array $data The WebFinger data. | ||
* @param string $webfinger The WebFinger resource. | ||
*/ | ||
return \apply_filters( 'webfinger_data', array(), $webfinger ); | ||
} | ||
|
||
/** | ||
* Retrieves the schema for the WebFinger endpoint. | ||
* | ||
* @return array Schema data. | ||
*/ | ||
public function get_item_schema() { | ||
if ( $this->schema ) { | ||
return $this->add_additional_fields_schema( $this->schema ); | ||
} | ||
|
||
$this->schema = array( | ||
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'title' => 'webfinger', | ||
'type' => 'object', | ||
'required' => array( 'subject', 'links' ), | ||
'properties' => array( | ||
'subject' => array( | ||
'description' => 'The subject of this WebFinger record.', | ||
'type' => 'string', | ||
'format' => 'uri', | ||
), | ||
'aliases' => array( | ||
'description' => 'Alternative identifiers for the subject.', | ||
'type' => 'array', | ||
'items' => array( | ||
'type' => 'string', | ||
'format' => 'uri', | ||
), | ||
), | ||
'links' => array( | ||
'description' => 'Links associated with the subject.', | ||
'type' => 'array', | ||
'items' => array( | ||
'type' => 'object', | ||
'properties' => array( | ||
'rel' => array( | ||
'description' => 'The relation type of the link.', | ||
'type' => 'string', | ||
'required' => true, | ||
), | ||
'type' => array( | ||
'description' => 'The content type of the link.', | ||
'type' => 'string', | ||
), | ||
'href' => array( | ||
'description' => 'The target URL of the link.', | ||
'type' => 'string', | ||
'format' => 'uri', | ||
), | ||
'template' => array( | ||
'description' => 'A URI template for the link.', | ||
'type' => 'string', | ||
'format' => 'uri', | ||
), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
|
||
return $this->add_additional_fields_schema( $this->schema ); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* REST Controller Testcase file. | ||
* | ||
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Tests; | ||
|
||
/** | ||
* REST Controller Testcase. | ||
*/ | ||
abstract class Test_REST_Controller_Testcase extends \WP_Test_REST_TestCase { | ||
|
||
/** | ||
* The REST server. | ||
* | ||
* @var \WP_REST_Server | ||
*/ | ||
protected $server; | ||
|
||
/** | ||
* Set up the test. | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
add_filter( 'rest_url', array( $this, 'filter_rest_url_for_leading_slash' ), 10, 2 ); | ||
|
||
/** @var \WP_REST_Server $wp_rest_server */ | ||
global $wp_rest_server; | ||
$wp_rest_server = new \Spy_REST_Server(); | ||
do_action( 'rest_api_init', $wp_rest_server ); | ||
} | ||
|
||
/** | ||
* Tear down the test. | ||
*/ | ||
public function tear_down() { | ||
remove_filter( 'rest_url', array( $this, 'test_rest_url_for_leading_slash' ) ); | ||
|
||
/** @var WP_REST_Server $wp_rest_server */ | ||
global $wp_rest_server; | ||
$wp_rest_server = null; | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Test get_item. | ||
*/ | ||
abstract public function test_get_item(); | ||
|
||
/** | ||
* Test register_routes. | ||
*/ | ||
abstract public function test_get_item_schema(); | ||
|
||
/** | ||
* Filter REST URL for leading slash. | ||
* | ||
* @param string $url URL. | ||
* @param string $path Path. | ||
* @return string | ||
*/ | ||
public function filter_rest_url_for_leading_slash( $url, $path ) { | ||
if ( is_multisite() || get_option( 'permalink_structure' ) ) { | ||
return $url; | ||
} | ||
|
||
// Make sure path for rest_url has a leading slash for proper resolution. | ||
if ( 0 !== strpos( $path, '/' ) ) { | ||
$this->fail( | ||
sprintf( | ||
'REST API URL "%s" should have a leading slash.', | ||
$path | ||
) | ||
); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
Oops, something went wrong.