Skip to content

Commit 77d99c2

Browse files
authored
Fixauth (#176)
* Fix style issue and auth issue * Add Micropub Media Endpoint Header
1 parent 0ca960c commit 77d99c2

File tree

10 files changed

+79
-16
lines changed

10 files changed

+79
-16
lines changed

bin/install-wp-tests.sh

100644100755
File mode changed.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"wimg/php-compatibility": "*",
1212
"wp-coding-standards/wpcs": "*",
1313
"wpreadme2markdown/wp2md": "*",
14-
"phpunit/phpunit": ">=5.0"
14+
"phpunit/phpunit": ">=5.0",
15+
"phpcompatibility/phpcompatibility-wp": "^2.0"
1516
},
1617
"prefer-stable" : true,
1718
"scripts": {

includes/class-micropub-authorize.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,16 @@ public static function determine_current_user( $user_id ) {
162162
$auth = static::get_authorization_header();
163163
$token = mp_get( $_POST, 'access_token' ); // phpcs:ignore
164164
if ( ! $auth && ! $token ) {
165-
static::$error = new WP_Micropub_Error( 'unauthorized', 'missing access token', 401 );
165+
// Fail if micropub is in the requested path
166+
if ( false !== strpos( MICROPUB_NAMESPACE, $_SERVER['REQUEST_URI'] ) ) {
167+
static::$error = new WP_Micropub_Error( 'unauthorized', 'missing access token', 401 );
168+
}
166169
return $user_id;
167170
}
168171

169172
$resp = wp_remote_get(
170-
get_option( 'indieauth_token_endpoint', MICROPUB_TOKEN_ENDPOINT ), array(
173+
get_option( 'indieauth_token_endpoint', MICROPUB_TOKEN_ENDPOINT ),
174+
array(
171175
'headers' => array(
172176
'Accept' => 'application/json',
173177
'Authorization' => $auth ?: 'Bearer ' . $token,

includes/class-micropub-endpoint.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public static function get( $array, $key, $default = array() ) {
7070
public static function register_route() {
7171
$cls = get_called_class();
7272
register_rest_route(
73-
MICROPUB_NAMESPACE, '/endpoint', array(
73+
MICROPUB_NAMESPACE,
74+
'/endpoint',
75+
array(
7476
array(
7577
'methods' => WP_REST_Server::CREATABLE,
7678
'callback' => array( $cls, 'post_handler' ),
@@ -369,7 +371,8 @@ public static function query( $post_id ) {
369371
}
370372
$resp = array(
371373
'properties' => array_intersect_key(
372-
$resp['properties'], array_flip( $props )
374+
$resp['properties'],
375+
array_flip( $props )
373376
),
374377
);
375378
}
@@ -683,7 +686,9 @@ public static function default_file_handler( $post_id ) {
683686
$desc = is_array( $val ) ? $val['alt'] : null;
684687
$att_ids[] = static::check_error(
685688
Micropub_Media::media_sideload_url(
686-
$url, $post_id, $desc
689+
$url,
690+
$post_id,
691+
$desc
687692
)
688693
);
689694
}
@@ -765,8 +770,10 @@ public static function store_geodata( $args ) {
765770
$props['country-name'][0],
766771
);
767772
$args['meta_input']['geo_address'] = implode(
768-
', ', array_filter(
769-
$parts, function( $v ) {
773+
', ',
774+
array_filter(
775+
$parts,
776+
function( $v ) {
770777
return $v;
771778
}
772779
)
@@ -860,7 +867,8 @@ public static function store_mf2( $args ) {
860867
if ( isset( $meta[ $key ] ) ) {
861868
$existing = unserialize( $meta[ $key ][0] );
862869
update_post_meta(
863-
$args['ID'], $key,
870+
$args['ID'],
871+
$key,
864872
array_diff( $existing, $to_delete )
865873
);
866874
}

includes/class-micropub-media.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,51 @@ public static function init() {
1919
// register endpoint
2020
add_action( 'rest_api_init', array( $cls, 'register_route' ) );
2121

22+
// endpoint discovery
23+
add_action( 'wp_head', array( $cls, 'micropub_media_html_header' ), 99 );
24+
add_action( 'send_headers', array( $cls, 'micropub_media_http_header' ) );
25+
add_filter( 'host_meta', array( $cls, 'micropub_media_jrd_links' ) );
26+
add_filter( 'webfinger_user_data', array( $cls, 'micropub_media_jrd_links' ) );
27+
28+
}
29+
30+
public static function get_micropub_media_endpoint() {
31+
return rest_url( MICROPUB_NAMESPACE . '/media' );
32+
}
33+
34+
/**
35+
* The micropub autodicovery meta tags
36+
*/
37+
public static function micropub_media_html_header() {
38+
// phpcs:ignore
39+
printf( '<link rel="micropub_media" href="%s" />' . PHP_EOL, static::get_micropub_media_endpoint() );
40+
}
41+
42+
/**
43+
* The micropub autodicovery http-header
44+
*/
45+
public static function micropub_media_http_header() {
46+
Micropub_Endpoint::header( 'Link', '<' . static::get_micropub_media_endpoint() . '>; rel="micropub_media"' );
47+
}
48+
49+
/**
50+
* Generates webfinger/host-meta links
51+
*/
52+
public static function micropub_media_jrd_links( $array ) {
53+
$array['links'][] = array(
54+
'rel' => 'micropub_media',
55+
'href' => static::get_micropub_media_endpoint(),
56+
);
57+
return $array;
2258
}
2359

60+
2461
public static function register_route() {
2562
$cls = get_called_class();
2663
register_rest_route(
27-
MICROPUB_NAMESPACE, '/media', array(
64+
MICROPUB_NAMESPACE,
65+
'/media',
66+
array(
2867
array(
2968
'methods' => WP_REST_Server::CREATABLE,
3069
'callback' => array( $cls, 'upload_handler' ),
@@ -234,8 +273,8 @@ public static function media_sideload_url( $url, $post_id = 0, $title = null ) {
234273
// Attach media to post
235274
wp_update_post(
236275
array(
237-
'post_ID' => $id,
238-
'post_parent' => $post_id
276+
'post_ID' => $id,
277+
'post_parent' => $post_id,
239278
)
240279
);
241280
return $id;

includes/class-micropub-render.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public static function generate_post_content( $post_content, $input ) {
5050
$lines[] = sprintf(
5151
'<p>%1s <a class="u-%2s" href="%3s">%4s</a>.</p>',
5252
$verbs[ $prop ],
53-
$prop, $val['url'],
53+
$prop,
54+
$val['url'],
5455
$val['name']
5556
);
5657
}

micropub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Author: Ryan Barrett
88
* Author URI: https://snarfed.org/
99
* Text Domain: micropub
10-
* Version: 2.0.1
10+
* Version: 2.0.2
1111
*/
1212

1313
/* See README for supported filters and actions.
@@ -54,7 +54,7 @@ function load_micropub_auth() {
5454
// If this configuration option is set to 0 then load this file
5555
if ( 0 === MICROPUB_LOCAL_AUTH ) {
5656
require_once plugin_dir_path( __FILE__ ) . 'includes/class-micropub-authorize.php';
57-
57+
5858
}
5959
}
6060

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ into markdown and saved to readme.md.
204204
## Changelog
205205
206206
207+
### 2.0.2 (2018-11-12)
208+
* Fix issue with built-in auth and update compatibility testing
209+
* Add experimental endpoint discovery option(https://indieweb.org/micropub_media_endpoint#Discovery_via_link_rel)
210+
211+
207212
### 2.0.1 (2018-11-04)
208213
* Move authorization code later in load to resolve conflict
209214

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: micropub, publish, indieweb, microformats
44
Requires at least: 4.7
55
Requires PHP: 5.3
66
Tested up to: 4.9.8
7-
Stable tag: 2.0.1
7+
Stable tag: 2.0.2
88
License: CC0
99
License URI: http://creativecommons.org/publicdomain/zero/1.0/
1010
Donate link: -
@@ -199,6 +199,10 @@ into markdown and saved to readme.md.
199199

200200
== Changelog ==
201201

202+
= 2.0.2 (2018-11-12) =
203+
* Fix issue with built-in auth and update compatibility testing
204+
* Add experimental endpoint discovery option(https://indieweb.org/micropub_media_endpoint#Discovery_via_link_rel)
205+
202206
= 2.0.1 (2018-11-04) =
203207
* Move authorization code later in load to resolve conflict
204208

tests/test_authorize.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public function test_home_url() {
6363
}
6464

6565
public function test_determine_current_user() {
66+
$_SERVER['REQUEST_URI'] = MICROPUB_NAMESPACE;
6667
$user_id = Micropub_Authorize::determine_current_user( 0 );
6768
$this->assertEquals( 0, $user_id );
6869
$error = Micropub_Authorize::get_error();

0 commit comments

Comments
 (0)