PluginProbe
ActivityPub / 0.7.3
ActivityPub v0.7.3
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / class-signature.php

class-signature.php in ActivityPub 0.7.3, at includes/class-signature.php

106 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub;
3
4 /**
5 * ActivityPub Signature Class
6 *
7 * @author Matthias Pfefferle
8 */
9 class Signature {
10
11 /**
12 * @param int $user_id
13 *
14 * @return mixed
15 */
16 public static function get_public_key( $user_id, $force = false ) {
17 $key = get_user_meta( $user_id, 'magic_sig_public_key' );
18
19 if ( $key && ! $force ) {
20 return $key[0];
21 }
22
23 self::generate_key_pair( $user_id );
24 $key = get_user_meta( $user_id, 'magic_sig_public_key' );
25
26 return $key[0];
27 }
28
29 /**
30 * @param int $user_id
31 *
32 * @return mixed
33 */
34 public static function get_private_key( $user_id, $force = false ) {
35 $key = get_user_meta( $user_id, 'magic_sig_private_key' );
36
37 if ( $key && ! $force ) {
38 return $key[0];
39 }
40
41 self::generate_key_pair( $user_id );
42 $key = get_user_meta( $user_id, 'magic_sig_private_key' );
43
44 return $key[0];
45 }
46
47 /**
48 * Generates the pair keys
49 *
50 * @param int $user_id
51 */
52 public static function generate_key_pair( $user_id ) {
53 $config = array(
54 'digest_alg' => 'sha512',
55 'private_key_bits' => 2048,
56 'private_key_type' => OPENSSL_KEYTYPE_RSA,
57 );
58
59 $key = openssl_pkey_new( $config );
60 $priv_key = null;
61
62 openssl_pkey_export( $key, $priv_key );
63
64 // private key
65 update_user_meta( $user_id, 'magic_sig_private_key', $priv_key );
66
67 $detail = openssl_pkey_get_details( $key );
68
69 // public key
70 update_user_meta( $user_id, 'magic_sig_public_key', $detail['key'] );
71 }
72
73 public static function generate_signature( $user_id, $url, $date ) {
74 $key = self::get_private_key( $user_id );
75
76 $url_parts = wp_parse_url( $url );
77
78 $host = $url_parts['host'];
79 $path = '/';
80
81 // add path
82 if ( ! empty( $url_parts['path'] ) ) {
83 $path = $url_parts['path'];
84 }
85
86 // add query
87 if ( ! empty( $url_parts['query'] ) ) {
88 $path .= '?' . $url_parts['query'];
89 }
90
91 $signed_string = "(request-target): post $path\nhost: $host\ndate: $date";
92
93 $signature = null;
94 openssl_sign( $signed_string, $signature, $key, OPENSSL_ALGO_SHA256 );
95 $signature = base64_encode( $signature ); // phpcs:ignore
96
97 $key_id = get_author_posts_url( $user_id ) . '#main-key';
98
99 return sprintf( 'keyId="%s",algorithm="rsa-sha256",headers="(request-target) host date",signature="%s"', $key_id, $signature );
100 }
101
102 public static function verify_signature( $headers, $signature ) {
103
104 }
105 }
106