move signature to user object

This commit is contained in:
Matthias Pfefferle 2023-05-31 10:31:49 +02:00
parent e2ad08b61b
commit e1fd0e1c39
4 changed files with 122 additions and 18 deletions

View file

@ -5,6 +5,7 @@ use WP_Error;
use DateTime; use DateTime;
use DateTimeZone; use DateTimeZone;
use Activitypub\Model\User; use Activitypub\Model\User;
use Activitypub\User_Factory;
/** /**
* ActivityPub Signature Class * ActivityPub Signature Class
@ -73,7 +74,7 @@ class Signature {
* *
* @return void * @return void
*/ */
public static function generate_key_pair( $user_id ) { public static function generate_key_pair() {
$config = array( $config = array(
'digest_alg' => 'sha512', 'digest_alg' => 'sha512',
'private_key_bits' => 2048, 'private_key_bits' => 2048,
@ -84,22 +85,13 @@ class Signature {
$priv_key = null; $priv_key = null;
\openssl_pkey_export( $key, $priv_key ); \openssl_pkey_export( $key, $priv_key );
$detail = \openssl_pkey_get_details( $key ); $detail = \openssl_pkey_get_details( $key );
if ( User::APPLICATION_USER_ID === $user_id ) { return array(
// private key 'private_key' => $priv_key,
\update_option( 'activitypub_magic_sig_private_key', $priv_key ); 'public_key' => $detail['key'],
);
// public key
\update_option( 'activitypub_magic_sig_public_key', $detail['key'] );
} else {
// private key
\update_user_meta( $user_id, 'magic_sig_private_key', $priv_key );
// public key
\update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
}
} }
/** /**
@ -114,7 +106,8 @@ class Signature {
* @return string The signature. * @return string The signature.
*/ */
public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) { public static function generate_signature( $user_id, $http_method, $url, $date, $digest = null ) {
$key = self::get_private_key( $user_id ); $user = User_Factory::get_by_id( $user_id );
$key = $user->get_private_key();
$url_parts = \wp_parse_url( $url ); $url_parts = \wp_parse_url( $url );

View file

@ -40,4 +40,44 @@ class Application_User extends Blog_User {
public function get_name() { public function get_name() {
return \esc_html( \get_option( 'activitypub_application_identifier', 'application' ) ); return \esc_html( \get_option( 'activitypub_application_identifier', 'application' ) );
} }
public function get_public_key() {
$key = \get_option( 'activitypub_application_user_public_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
$key = \get_option( 'activitypub_application_user_public_key', true );
return $key;
}
/**
* @param int $user_id
*
* @return mixed
*/
public function get_private_key() {
$key = \get_option( 'activitypub_application_user_private_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
return \get_option( 'activitypub_application_user_private_key', true );
}
private function generate_key_pair() {
$key_pair = Signature::generate_key_pair();
if ( ! is_wp_error( $key_pair ) ) {
\update_option( 'activitypub_application_user_public_key', $key_pair['public_key'], true );
\update_option( 'activitypub_application_user_private_key', $key_pair['private_key'], true );
}
}
} }

View file

@ -2,6 +2,7 @@
namespace Activitypub\Model; namespace Activitypub\Model;
use WP_Query; use WP_Query;
use Activitypub\Signature;
use Activitypub\User_Factory; use Activitypub\User_Factory;
class Blog_User extends User { class Blog_User extends User {
@ -141,6 +142,42 @@ class Blog_User extends User {
} }
public function get_public_key() { public function get_public_key() {
return ''; $key = \get_option( 'activitypub_blog_user_public_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
$key = \get_option( 'activitypub_blog_user_public_key', true );
return $key;
}
/**
* @param int $user_id
*
* @return mixed
*/
public function get_private_key() {
$key = \get_option( 'activitypub_blog_user_private_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
return \get_option( 'activitypub_blog_user_private_key', true );
}
private function generate_key_pair() {
$key_pair = Signature::generate_key_pair();
if ( ! is_wp_error( $key_pair ) ) {
\update_option( 'activitypub_blog_user_public_key', $key_pair['public_key'], true );
\update_option( 'activitypub_blog_user_private_key', $key_pair['private_key'], true );
}
} }
} }

View file

@ -130,7 +130,41 @@ class User {
} }
public function get_public_key() { public function get_public_key() {
return Signature::get_public_key( $this->user_id ); $key = \get_user_meta( $this->get_user_id(), 'magic_sig_public_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
return \get_user_meta( $this->get_user_id(), 'magic_sig_public_key', true );
}
/**
* @param int $user_id
*
* @return mixed
*/
public function get_private_key() {
$key = \get_user_meta( $this->get_user_id(), 'magic_sig_private_key', true );
if ( $key ) {
return $key;
}
$this->generate_key_pair();
return \get_user_meta( $this->get_user_id(), 'magic_sig_private_key', true );
}
private function generate_key_pair() {
$key_pair = Signature::generate_key_pair();
if ( ! is_wp_error( $key_pair ) ) {
\update_user_meta( $this->get_user_id(), 'magic_sig_public_key', $key_pair['public_key'], true );
\update_user_meta( $this->get_user_id(), 'magic_sig_private_key', $key_pair['private_key'], true );
}
} }
/** /**