use const instead of -1

This commit is contained in:
Matthias Pfefferle 2023-05-15 10:48:34 +02:00
parent 7d5cfb3078
commit 7456d36834
5 changed files with 33 additions and 6 deletions

View file

@ -41,6 +41,7 @@ function init() {
require_once \dirname( __FILE__ ) . '/includes/model/class-activity.php';
require_once \dirname( __FILE__ ) . '/includes/model/class-post.php';
require_once \dirname( __FILE__ ) . '/includes/model/class-user.php';
require_once \dirname( __FILE__ ) . '/includes/model/class-follower.php';
require_once \dirname( __FILE__ ) . '/includes/class-migration.php';

View file

@ -2,6 +2,7 @@
namespace Activitypub;
use WP_Error;
use Activitypub\Model\User;
/**
* ActivityPub HTTP Class
@ -62,7 +63,7 @@ class Http {
*/
public static function get( $url ) {
$date = \gmdate( 'D, d M Y H:i:s T' );
$signature = Signature::generate_signature( -1, 'get', $url, $date );
$signature = Signature::generate_signature( User::APPLICATION_USER_ID, 'get', $url, $date );
$wp_version = \get_bloginfo( 'version' );
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );

View file

@ -4,6 +4,7 @@ namespace Activitypub;
use WP_Error;
use DateTime;
use DateTimeZone;
use Activitypub\Model\User;
/**
* ActivityPub Signature Class
@ -26,7 +27,7 @@ class Signature {
self::generate_key_pair( $user_id );
}
if ( -1 === $user_id ) {
if ( User::APPLICATION_USER_ID === $user_id ) {
$key = \get_option( 'activitypub_magic_sig_public_key' );
} else {
$key = \get_user_meta( $user_id, 'magic_sig_public_key', true );
@ -52,7 +53,7 @@ class Signature {
self::generate_key_pair( $user_id );
}
if ( -1 === $user_id ) {
if ( User::APPLICATION_USER_ID === $user_id ) {
$key = \get_option( 'activitypub_magic_sig_private_key' );
} else {
$key = \get_user_meta( $user_id, 'magic_sig_private_key', true );
@ -85,7 +86,7 @@ class Signature {
\openssl_pkey_export( $key, $priv_key );
$detail = \openssl_pkey_get_details( $key );
if ( -1 === $user_id ) {
if ( User::APPLICATION_USER_ID === $user_id ) {
// private key
\update_option( 'activitypub_magic_sig_private_key', $priv_key );
@ -140,7 +141,7 @@ class Signature {
\openssl_sign( $signed_string, $signature, $key, \OPENSSL_ALGO_SHA256 );
$signature = \base64_encode( $signature ); // phpcs:ignore
if ( -1 === $user_id ) {
if ( User::APPLICATION_USER_ID === $user_id ) {
$key_id = \get_rest_url( null, 'activitypub/1.0/service#main-key' );
} else {
$key_id = \get_author_posts_url( $user_id ) . '#main-key';

View file

@ -0,0 +1,23 @@
<?php
namespace Activitypub\Model;
/**
* ActivityPub User Class
*
* @author Matthias Pfefferle
*/
class User {
/**
* The ID of the Blog User
*
* @var int
*/
const BLOG_USER_ID = 0;
/**
* The ID of the Application User
*
* @var int
*/
const APPLICATION_USER_ID = -1;
}

View file

@ -3,6 +3,7 @@ namespace Activitypub\Rest;
use WP_REST_Response;
use Activitypub\Signature;
use Activitypub\Model\User;
/**
* ActivityPub Server REST-Class
@ -57,7 +58,7 @@ class Server {
$json->publicKey = (object) array( // phpcs:ignore WordPress.NamingConventions
'id' => \get_rest_url( null, 'activitypub/1.0/application#main-key' ),
'owner' => \get_rest_url( null, 'activitypub/1.0/application' ),
'publicKeyPem' => Signature::get_public_key( -1 ), // phpcs:ignore WordPress.NamingConventions
'publicKeyPem' => Signature::get_public_key( User::APPLICATION_USER_ID ), // phpcs:ignore WordPress.NamingConventions
);
$response = new WP_REST_Response( $json, 200 );