Skip to content

Commit 97b4f33

Browse files
committed
2 parents 0fcc57e + 9250749 commit 97b4f33

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

includes/class-signature.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static function generate_key_pair( $user_id ) {
7070
\update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
7171
}
7272

73-
public static function generate_signature( $user_id, $url, $date ) {
73+
public static function generate_signature( $user_id, $url, $date, $digest = null ) {
7474
$key = self::get_private_key( $user_id );
7575

7676
$url_parts = \wp_parse_url( $url );
@@ -88,18 +88,31 @@ public static function generate_signature( $user_id, $url, $date ) {
8888
$path .= '?' . $url_parts['query'];
8989
}
9090

91-
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
91+
if ( ! empty( $digest ) ) {
92+
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest";
93+
} else {
94+
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
95+
}
9296

9397
$signature = null;
9498
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
9599
$signature = \base64_encode( $signature ); // phpcs:ignore
96100

97101
$key_id = \get_author_posts_url( $user_id ) . '#main-key';
98102

99-
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
103+
if ( ! empty( $digest ) ) {
104+
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="%s"', $key_id, $signature );
105+
} else {
106+
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
107+
}
100108
}
101109

102110
public static function verify_signature( $headers, $signature ) {
103111

104112
}
113+
114+
public static function generate_digest( $body ) {
115+
$digest = \base64_encode( \hash( 'sha256', $body, true ) ); // phpcs:ignore
116+
return "$digest";
117+
}
105118
}

includes/functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ function get_context() {
2323

2424
function safe_remote_post( $url, $body, $user_id ) {
2525
$date = \gmdate( 'D, d M Y H:i:s T' );
26-
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date );
26+
$digest = \Activitypub\Signature::generate_digest( $body );
27+
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date, $digest );
2728

2829
$wp_version = \get_bloginfo( 'version' );
2930
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
@@ -35,6 +36,7 @@ function safe_remote_post( $url, $body, $user_id ) {
3536
'headers' => array(
3637
'Accept' => 'application/activity+json',
3738
'Content-Type' => 'application/activity+json',
39+
'Digest' => "SHA-256=$digest",
3840
'Signature' => $signature,
3941
'Date' => $date,
4042
),

includes/rest/class-inbox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function init() {
1616
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Inbox', 'register_routes' ) );
1717
\add_filter( 'rest_pre_serve_request', array( '\Activitypub\Rest\Inbox', 'serve_request' ), 11, 4 );
1818
\add_action( 'activitypub_inbox_follow', array( '\Activitypub\Rest\Inbox', 'handle_follow' ), 10, 2 );
19-
\add_action( 'activitypub_inbox_unfollow', array( '\Activitypub\Rest\Inbox', 'handle_unfollow' ), 10, 2 );
19+
\add_action( 'activitypub_inbox_undo', array( '\Activitypub\Rest\Inbox', 'handle_unfollow' ), 10, 2 );
2020
//\add_action( 'activitypub_inbox_like', array( '\Activitypub\Rest\Inbox', 'handle_reaction' ), 10, 2 );
2121
//\add_action( 'activitypub_inbox_announce', array( '\Activitypub\Rest\Inbox', 'handle_reaction' ), 10, 2 );
2222
\add_action( 'activitypub_inbox_create', array( '\Activitypub\Rest\Inbox', 'handle_create' ), 10, 2 );

0 commit comments

Comments
 (0)