44use WP_Error ;
55use DateTime ;
66use DateTimeZone ;
7- use Activitypub \Model \User ;
87use Activitypub \Collection \Users ;
98
109/**
@@ -23,22 +22,14 @@ class Signature {
2322 *
2423 * @return mixed The public key.
2524 */
26- public static function get_public_key ( $ user_id , $ force = false ) {
25+ public static function get_public_key_for ( $ user_id , $ force = false ) {
2726 if ( $ force ) {
28- self ::generate_key_pair ( $ user_id );
27+ self ::generate_key_pair_for ( $ user_id );
2928 }
3029
31- if ( User::APPLICATION_USER_ID === $ user_id ) {
32- $ key = \get_option ( 'activitypub_magic_sig_public_key ' );
33- } else {
34- $ key = \get_user_meta ( $ user_id , 'magic_sig_public_key ' , true );
35- }
36-
37- if ( ! $ key ) {
38- return self ::get_public_key ( $ user_id , true );
39- }
30+ $ key_pair = self ::get_keypair_for ( $ user_id );
4031
41- return $ key ;
32+ return $ key_pair [ ' public_key ' ] ;
4233 }
4334
4435 /**
@@ -49,32 +40,51 @@ public static function get_public_key( $user_id, $force = false ) {
4940 *
5041 * @return mixed The private key.
5142 */
52- public static function get_private_key ( $ user_id , $ force = false ) {
43+ public static function get_private_key_for ( $ user_id , $ force = false ) {
5344 if ( $ force ) {
54- self ::generate_key_pair ( $ user_id );
45+ self ::generate_key_pair_for ( $ user_id );
5546 }
5647
57- if ( User::APPLICATION_USER_ID === $ user_id ) {
58- $ key = \get_option ( 'activitypub_magic_sig_private_key ' );
59- } else {
60- $ key = \get_user_meta ( $ user_id , 'magic_sig_private_key ' , true );
61- }
48+ $ key_pair = self ::get_keypair_for ( $ user_id );
6249
63- if ( ! $ key ) {
64- return self ::get_private_key ( $ user_id , true );
50+ return $ key_pair ['private_key ' ];
51+ }
52+
53+ /**
54+ * Return the key pair for a given user.
55+ *
56+ * @param int $user_id The WordPress User ID.
57+ *
58+ * @return array The key pair.
59+ */
60+ public static function get_keypair_for ( $ user_id ) {
61+ $ option_key = self ::get_signature_options_key_for ( $ user_id );
62+ $ key_pair = \get_option ( $ option_key );
63+
64+ if ( ! $ key_pair ) {
65+ $ key_pair = self ::generate_key_pair_for ( $ user_id );
6566 }
6667
67- return $ key ;
68+ return $ key_pair ;
6869 }
6970
7071 /**
7172 * Generates the pair keys
7273 *
7374 * @param int $user_id The WordPress User ID.
7475 *
75- * @return void
76+ * @return array The key pair.
7677 */
77- public static function generate_key_pair () {
78+ protected static function generate_key_pair_for ( $ user_id ) {
79+ $ option_key = self ::get_signature_options_key_for ( $ user_id );
80+ $ key_pair = self ::check_legacy_key_pair_for ( $ user_id );
81+
82+ if ( $ key_pair ) {
83+ \add_option ( $ option_key , $ key_pair );
84+
85+ return $ key_pair ;
86+ }
87+
7888 $ config = array (
7989 'digest_alg ' => 'sha512 ' ,
8090 'private_key_bits ' => 2048 ,
@@ -88,10 +98,76 @@ public static function generate_key_pair() {
8898
8999 $ detail = \openssl_pkey_get_details ( $ key );
90100
91- return array (
101+ // check if keys are valid
102+ if (
103+ empty ( $ priv_key ) || ! is_string ( $ priv_key ) ||
104+ ! isset ( $ detail ['key ' ] ) || ! is_string ( $ detail ['key ' ] )
105+ ) {
106+ return array (
107+ 'private_key ' => null ,
108+ 'public_key ' => null ,
109+ );
110+ }
111+
112+ $ key_pair = array (
92113 'private_key ' => $ priv_key ,
93114 'public_key ' => $ detail ['key ' ],
94115 );
116+
117+ // persist keys
118+ \add_option ( $ option_key , $ key_pair );
119+
120+ return $ key_pair ;
121+ }
122+
123+ /**
124+ * Undocumented function
125+ *
126+ * @param [type] $user_id
127+ * @return void
128+ */
129+ protected static function get_signature_options_key_for ( $ user_id ) {
130+ $ id = $ user_id ;
131+
132+ if ( $ user_id > 0 ) {
133+ $ user = \get_userdata ( $ user_id );
134+ $ id = $ user ->user_login ;
135+ }
136+
137+ return 'activitypub_keypair_for_ ' . $ id ;
138+ }
139+
140+ /**
141+ * Check if there is a legacy key pair
142+ *
143+ * @param int $user_id The WordPress User ID.
144+ *
145+ * @return array|bool The key pair or false.
146+ */
147+ protected static function check_legacy_key_pair_for ( $ user_id ) {
148+ switch ( $ user_id ) {
149+ case 0 :
150+ $ public_key = \get_option ( 'activitypub_blog_user_public_key ' );
151+ $ private_key = \get_option ( 'activitypub_blog_user_private_key ' );
152+ break ;
153+ case -1 :
154+ $ public_key = \get_option ( 'activitypub_application_user_public_key ' );
155+ $ private_key = \get_option ( 'activitypub_application_user_private_key ' );
156+ break ;
157+ default :
158+ $ public_key = \get_user_meta ( $ user_id , 'magic_sig_public_key ' , true );
159+ $ private_key = \get_user_meta ( $ user_id , 'magic_sig_private_key ' , true );
160+ break ;
161+ }
162+
163+ if ( ! empty ( $ public_key ) && is_string ( $ public_key ) && ! empty ( $ private_key ) && is_string ( $ private_key ) ) {
164+ return array (
165+ 'private_key ' => $ private_key ,
166+ 'public_key ' => $ public_key ,
167+ );
168+ }
169+
170+ return false ;
95171 }
96172
97173 /**
@@ -107,7 +183,7 @@ public static function generate_key_pair() {
107183 */
108184 public static function generate_signature ( $ user_id , $ http_method , $ url , $ date , $ digest = null ) {
109185 $ user = Users::get_by_id ( $ user_id );
110- $ key = $ user ->get__private_key ( );
186+ $ key = self :: get_private_key_for ( $ user ->get__id () );
111187
112188 $ url_parts = \wp_parse_url ( $ url );
113189
@@ -136,7 +212,6 @@ public static function generate_signature( $user_id, $http_method, $url, $date,
136212 \openssl_sign ( $ signed_string , $ signature , $ key , \OPENSSL_ALGO_SHA256 );
137213 $ signature = \base64_encode ( $ signature ); // phpcs:ignore
138214
139- $ user = Users::get_by_id ( $ user_id );
140215 $ key_id = $ user ->get_url () . '#main-key ' ;
141216
142217 if ( ! empty ( $ digest ) ) {
0 commit comments