1
1
Fork 0

The signature creation now moved into the Diaspora class. That's much cleaner.

This commit is contained in:
Michael Vogel 2016-03-19 15:49:47 +01:00
parent 5c44a787bf
commit d676ae0f32
3 changed files with 125 additions and 124 deletions

View file

@ -2190,13 +2190,18 @@ class diaspora {
*
* @return string the handle in the format user@domain.tld
*/
private function my_handle($me) {
private function my_handle($contact) {
if ($contact["addr"] != "")
return $contact["addr"];
// Normally we should have a filled "addr" field - but in the past this wasn't the case
// So - just in case - we build the the address here.
return $me["nickname"]."@".substr(App::get_baseurl(), strpos(App::get_baseurl(),"://") + 3);
if ($contact["nickname"] != "")
$nick = $contact["nickname"];
else
$nick = $contact["nick"];
return $nick."@".substr(App::get_baseurl(), strpos(App::get_baseurl(),"://") + 3);
}
/**
@ -2689,7 +2694,7 @@ class diaspora {
"guid" => $item["guid"],
"target_type" => $target_type,
"parent_guid" => $parent["guid"],
"author_signature" => $authorsig,
"author_signature" => "",
"diaspora_handle" => self::my_handle($owner)));
}
@ -3052,5 +3057,118 @@ class diaspora {
foreach($recips as $recip)
self::build_and_transmit($profile, $recip, "profile", $message, false, "", true);
}
/**
* @brief Stores the signature for likes that are created on our system
*
* @param array $contact The contact array of the "like"
* @param int $post_id The post id of the "like"
*
* @return bool Success
*/
function store_like_signature($contact, $post_id) {
$enabled = intval(get_config('system','diaspora_enabled'));
if (!$enabled) {
logger('Diaspora support disabled, not storing like signature', LOGGER_DEBUG);
return false;
}
// Is the contact the owner? Then fetch the private key
if (!$contact['self'] OR ($contact['uid'] == 0)) {
logger("No owner post, so not storing signature", LOGGER_DEBUG);
return false;
}
$r = q("SELECT `prvkey` FROM `user` WHERE `uid` = %d LIMIT 1", intval($contact['uid']));
if(!$r)
return false;
$contact["uprvkey"] = $r[0]['prvkey'];
$r = q("SELECT * FROM `item` WHERE `id` = %d LIMIT 1", intval($post_id));
if (!$r)
return false;
if (!in_array($r[0]["verb"], array(ACTIVITY_LIKE, ACTIVITY_DISLIKE)))
return false;
$message = self::construct_like($r[0], $contact);
$message["author_signature"] = self::signature($contact, $message);
// In the future we will store the signature more flexible to support new fields.
// Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
// (We are transmitting this data here via DFRN)
$signed_text = $message["positive"].";".$message["guid"].";".$message["target_type"].";".
$message["parent_guid"].";".$message["diaspora_handle"];
q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
intval($post_id),
dbesc($signed_text),
dbesc($message["author_signature"]),
dbesc($message["diaspora_handle"])
);
// This here will replace the lines above, once Diaspora changed its protocol
//q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
// intval($message_id),
// dbesc(json_encode($message))
//);
logger('Stored diaspora like signature');
return true;
}
/**
* @brief Stores the signature for comments that are created on our system
*
* @param array $item The item array of the comment
* @param array $contact The contact array of the item owner
* @param string $uprvkey The private key of the sender
* @param int $message_id The message id of the comment
*
* @return bool Success
*/
function store_comment_signature($item, $contact, $uprvkey, $message_id) {
if ($uprvkey == "") {
logger('No private key, so not storing comment signature', LOGGER_DEBUG);
return false;
}
$enabled = intval(get_config('system','diaspora_enabled'));
if (!$enabled) {
logger('Diaspora support disabled, not storing comment signature', LOGGER_DEBUG);
return false;
}
$contact["uprvkey"] = $uprvkey;
$message = self::construct_comment($item, $contact);
$message["author_signature"] = self::signature($contact, $message);
// In the future we will store the signature more flexible to support new fields.
// Right now we cannot change this since old Friendica versions (prior to 3.5) can only handle this format.
// (We are transmitting this data here via DFRN)
$signed_text = $message["guid"].";".$message["parent_guid"].";".
$message["text"].";".$message["diaspora_handle"];
q("INSERT INTO `sign` (`iid`,`signed_text`,`signature`,`signer`) VALUES (%d,'%s','%s','%s')",
intval($message_id),
dbesc($signed_text),
dbesc($message["author_signature"]),
dbesc($message["diaspora_handle"])
);
// This here will replace the lines above, once Diaspora changed its protocol
//q("INSERT INTO `sign` (`iid`,`signed_text`) VALUES (%d,'%s')",
// intval($message_id),
// dbesc(json_encode($message))
//);
logger('Stored diaspora comment signature');
return true;
}
}
?>

View file

@ -1,4 +1,5 @@
<?php
require_once("include/diaspora.php");
/**
* @brief add/remove activity to an item
@ -237,7 +238,7 @@ EOT;
// Save the author information for the like in case we need to relay to Diaspora
store_diaspora_like_sig($activity, $post_type, $contact, $post_id);
diaspora::store_like_signature($contact, $post_id);
$arr['id'] = $post_id;
@ -247,83 +248,3 @@ EOT;
return true;
}
function store_diaspora_like_sig($activity, $post_type, $contact, $post_id) {
// Note that we can only create a signature for a user of the local server. We don't have
// a key for remote users. That is ok, because if a remote user is "unlike"ing a post, it
// means we are the relay, and for relayable_retractions, Diaspora
// only checks the parent_author_signature if it doesn't have to relay further
$enabled = intval(get_config('system','diaspora_enabled'));
if(! $enabled) {
logger('mod_like: diaspora support disabled, not storing like signature', LOGGER_DEBUG);
return;
}
logger('mod_like: storing diaspora like signature');
if(($activity === ACTIVITY_LIKE) && ($post_type === t('status'))) {
// Only works for NETWORK_DFRN
$contact_baseurl_start = strpos($contact['url'],'://') + 3;
$contact_baseurl_length = strpos($contact['url'],'/profile') - $contact_baseurl_start;
$contact_baseurl = substr($contact['url'], $contact_baseurl_start, $contact_baseurl_length);
$diaspora_handle = $contact['nick'] . '@' . $contact_baseurl;
// This code could never had worked (the return values form the queries were used in a wrong way.
// Additionally it is needlessly complicated. Either the contact is owner or not. And we have this data already.
/*
// Get contact's private key if he's a user of the local Friendica server
$r = q("SELECT `contact`.`uid` FROM `contact` WHERE `url` = '%s' AND `self` = 1 LIMIT 1",
dbesc($contact['url'])
);
if( $r) {
$contact_uid = $r['uid'];
$r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
intval($contact_uid)
);
if( $r)
$contact_uprvkey = $r['prvkey'];
}
*/
// Is the contact the owner? Then fetch the private key
if ($contact['self'] AND ($contact['uid'] > 0)) {
$r = q("SELECT prvkey FROM user WHERE uid = %d LIMIT 1",
intval($contact['uid'])
);
if($r)
$contact_uprvkey = $r[0]['prvkey'];
}
$r = q("SELECT guid, parent FROM `item` WHERE id = %d LIMIT 1",
intval($post_id)
);
if( $r) {
$p = q("SELECT guid FROM `item` WHERE id = %d AND parent = %d LIMIT 1",
intval($r[0]['parent']),
intval($r[0]['parent'])
);
if( $p) {
$signed_text = 'true;'.$r[0]['guid'].';Post;'.$p[0]['guid'].';'.$diaspora_handle;
if(isset($contact_uprvkey))
$authorsig = base64_encode(rsa_sign($signed_text,$contact_uprvkey,'sha256'));
else
$authorsig = '';
q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
intval($post_id),
dbesc($signed_text),
dbesc($authorsig),
dbesc($diaspora_handle)
);
}
}
}
return;
}

View file

@ -24,6 +24,7 @@ require_once('include/threads.php');
require_once('include/text.php');
require_once('include/items.php');
require_once('include/Scrape.php');
require_once('include/diaspora.php');
function item_post(&$a) {
@ -900,7 +901,7 @@ function item_post(&$a) {
// Store the comment signature information in case we need to relay to Diaspora
store_diaspora_comment_sig($datarray, $author, ($self ? $user['prvkey'] : false), $parent_item, $post_id);
diaspora::store_comment_signature($datarray, $author, ($self ? $user['prvkey'] : false), $post_id);
} else {
$parent = $post_id;
@ -1245,42 +1246,3 @@ function handle_tag($a, &$body, &$inform, &$str_tags, $profile_uid, $tag, $netwo
return array('replaced' => $replaced, 'contact' => $r[0]);
}
function store_diaspora_comment_sig($datarray, $author, $uprvkey, $parent_item, $post_id) {
// We won't be able to sign Diaspora comments for authenticated visitors - we don't have their private key
$enabled = intval(get_config('system','diaspora_enabled'));
if(! $enabled) {
logger('mod_item: diaspora support disabled, not storing comment signature', LOGGER_DEBUG);
return;
}
logger('mod_item: storing diaspora comment signature');
require_once('include/bb2diaspora.php');
$signed_body = html_entity_decode(bb2diaspora($datarray['body']));
// Only works for NETWORK_DFRN
$contact_baseurl_start = strpos($author['url'],'://') + 3;
$contact_baseurl_length = strpos($author['url'],'/profile') - $contact_baseurl_start;
$contact_baseurl = substr($author['url'], $contact_baseurl_start, $contact_baseurl_length);
$diaspora_handle = $author['nick'] . '@' . $contact_baseurl;
$signed_text = $datarray['guid'] . ';' . $parent_item['guid'] . ';' . $signed_body . ';' . $diaspora_handle;
if( $uprvkey !== false )
$authorsig = rsa_sign($signed_text,$uprvkey,'sha256');
else
$authorsig = '';
q("insert into sign (`iid`,`signed_text`,`signature`,`signer`) values (%d,'%s','%s','%s') ",
intval($post_id),
dbesc($signed_text),
dbesc(base64_encode($authorsig)),
dbesc($diaspora_handle)
);
return;
}