Minor changes in Salmon lib for Magicsig retrieval.

This commit is contained in:
Mikael Nordfeldth 2014-05-26 20:06:11 +02:00
parent 49fa34e234
commit 7c7426b473

View File

@ -87,6 +87,8 @@ class Salmon
* List the magic envelope signature class variants in the order we try them. * List the magic envelope signature class variants in the order we try them.
* Multiples are needed for backwards-compat with StatusNet prior to 0.9.7, * Multiples are needed for backwards-compat with StatusNet prior to 0.9.7,
* which used a draft version of the magic envelope spec. * which used a draft version of the magic envelope spec.
*
* FIXME: Deprecate and remove. GNU social shouldn't have to interface with SN<0.9.7
*/ */
protected function formatClasses() { protected function formatClasses() {
return array('MagicEnvelope', 'MagicEnvelopeCompat'); return array('MagicEnvelope', 'MagicEnvelopeCompat');
@ -111,24 +113,26 @@ class Salmon
*/ */
public function createMagicEnv($text, $actor, $class='MagicEnvelope') public function createMagicEnv($text, $actor, $class='MagicEnvelope')
{ {
if (!in_array($class, $this->formatClasses())) {
throw new ServerException('Bad class parameter for createMagicEnv');
}
$magic_env = new $class(); $magic_env = new $class();
$user = User::getKV('id', $actor->id); // We only generate keys for our local users of course, so let
if ($user->id) { // getUser throw an exception if the profile is not local.
// Use local key $user = $actor->getUser();
$magickey = Magicsig::getKV('user_id', $user->id);
if (!$magickey) { // Find already stored key
$magicsig = Magicsig::getKV('user_id', $user->id);
if (!$magicsig instanceof Magicsig) {
// No keypair yet, let's generate one. // No keypair yet, let's generate one.
$magickey = new Magicsig(); $magicsig = new Magicsig();
$magickey->generate($user->id); $magicsig->generate($user->id);
}
} else {
// TRANS: Exception.
throw new Exception(_m('Salmon invalid actor for signing.'));
} }
try { try {
$env = $magic_env->signMessage($text, 'application/atom+xml', $magickey->toString()); $env = $magic_env->signMessage($text, 'application/atom+xml', $magicsig->toString());
} catch (Exception $e) { } catch (Exception $e) {
return $text; return $text;
} }