add digest header
This commit is contained in:
parent
f418e823c5
commit
0271b57844
2 changed files with 19 additions and 4 deletions
|
@ -70,7 +70,7 @@ class Signature {
|
||||||
\update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
|
\update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function generate_signature( $user_id, $url, $date ) {
|
public static function generate_signature( $user_id, $url, $date, $digest = null ) {
|
||||||
$key = self::get_private_key( $user_id );
|
$key = self::get_private_key( $user_id );
|
||||||
|
|
||||||
$url_parts = \wp_parse_url( $url );
|
$url_parts = \wp_parse_url( $url );
|
||||||
|
@ -88,7 +88,11 @@ class Signature {
|
||||||
$path .= '?' . $url_parts['query'];
|
$path .= '?' . $url_parts['query'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( ! empty( $digest ) ) {
|
||||||
|
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date\ndigest: SHA-256=$digest";
|
||||||
|
} else {
|
||||||
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
|
$signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
|
||||||
|
}
|
||||||
|
|
||||||
$signature = null;
|
$signature = null;
|
||||||
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
|
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
|
||||||
|
@ -96,10 +100,19 @@ class Signature {
|
||||||
|
|
||||||
$key_id = \get_author_posts_url( $user_id ) . '#main-key';
|
$key_id = \get_author_posts_url( $user_id ) . '#main-key';
|
||||||
|
|
||||||
|
if ( ! empty( $digest ) ) {
|
||||||
|
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="%s"', $key_id, $signature );
|
||||||
|
} else {
|
||||||
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
|
return \sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function verify_signature( $headers, $signature ) {
|
public static function verify_signature( $headers, $signature ) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function generate_digest( $body ) {
|
||||||
|
$digest = \base64_encode( \hash('sha256', $body, true ) ); // phpcs:ignore
|
||||||
|
return "$digest";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,8 @@ function get_context() {
|
||||||
|
|
||||||
function safe_remote_post( $url, $body, $user_id ) {
|
function safe_remote_post( $url, $body, $user_id ) {
|
||||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||||
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date );
|
$digest = \Activitypub\Signature::generate_digest( $body );
|
||||||
|
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date, $digest );
|
||||||
|
|
||||||
$wp_version = \get_bloginfo( 'version' );
|
$wp_version = \get_bloginfo( 'version' );
|
||||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
$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 ) {
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Accept' => 'application/activity+json',
|
'Accept' => 'application/activity+json',
|
||||||
'Content-Type' => 'application/activity+json',
|
'Content-Type' => 'application/activity+json',
|
||||||
|
'Digest' => "SHA-256=$digest",
|
||||||
'Signature' => $signature,
|
'Signature' => $signature,
|
||||||
'Date' => $date,
|
'Date' => $date,
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue