Generate key pairs for actors

This commit is contained in:
Jeremy Dormitzer 2018-10-04 06:44:32 -04:00
parent 4bbd53add3
commit 133217dd5f
4 changed files with 60 additions and 1 deletions

View File

@ -24,6 +24,7 @@ function run_migrations() {
apply_migration( '0.0.2', 'migration_0_0_2' );
apply_migration( '0.0.3', 'migration_0_0_3' );
apply_migration( '0.0.4', 'migration_0_0_4' );
apply_migration( '0.0.5', 'migration_0_0_5' );
update_option( 'pterotype_previously_migrated_version', PTEROTYPE_VERSION );
}
@ -222,4 +223,20 @@ function migration_0_0_4() {
"
);
}
function migration_0_0_5() {
global $wpdb;
$wpdb->query(
'
CREATE TABLE pterotype_keys(
actor_id INT UNSIGNED PRIMARY KEY,
public_key TEXT NOT NULL,
private_key TEXT NOT NULL,
FOREIGN KEY keys_actor_fk(actor_id)
REFERENCES pterotype_actors(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
'
);
}
?>

28
includes/pgp.php Normal file
View File

@ -0,0 +1,28 @@
<?php
namespace pgp;
function gen_key( $actor_slug ) {
$rsa = new \phpseclib\Crypt\RSA();
return $rsa->createKey( 2048 );
}
function persist_key( $actor_id, $public_key, $private_key ) {
global $wpdb;
return $wpdb->replace(
'pterotype_keys',
array(
'actor_id' => $actor_id,
'public_key' => $public_key,
'private_key' => $private_key
),
array( '%d', '%s', '%s' )
);
}
function get_public_key( $actor_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare(
'SELECT public_key FROM pterotype_keys WHERE actor_id = %d', $actor_id
) );
}
?>

View File

@ -1,6 +1,8 @@
<?php
namespace actors;
require_once plugin_dir_path( __FILE__ ) . '../pgp.php';
function get_actor( $id ) {
global $wpdb;
$row = $wpdb->get_row( $wpdb->prepare(
@ -109,8 +111,20 @@ function initialize_actors() {
);
foreach ( $user_slugs as $user_slug ) {
create_actor( $user_slug, 'user' );
$actor_id = get_actor_id( $user_slug );
$keys_created = \pgp\get_public_key( $actor_id );
if ( ! $keys_created ) {
$keys = \pgp\gen_key( $user_slug );
\pgp\persist_key( $actor_id, $keys['publickey'], $keys['privatekey'] );
}
}
create_actor( PTEROTYPE_BLOG_ACTOR_SLUG, 'blog' );
$blog_actor_id = get_actor_id( PTEROTYPE_BLOG_ACTOR_SLUG );
$keys_created = \pgp\get_public_key( $blog_actor_id );
if ( ! $keys_created ) {
$keys = \pgp\gen_key( PTEROTYPE_BLOG_ACTOR_SLUG );
\pgp\persist_key( $blog_actor_id, $keys['publickey'], $keys['privatekey'] );
}
}
function create_actor( $slug, $type ) {

View File

@ -5,7 +5,7 @@ Plugin Name: Pterotype
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/init.php';
define( 'PTEROTYPE_VERSION', '0.0.4' );
define( 'PTEROTYPE_VERSION', '0.0.5' );
define( 'PTEROTYPE_BLOG_ACTOR_SLUG', '-blog' );
define( 'PTEROTYPE_BLOG_ACTOR_USERNAME', 'blog' );