+ } elseif ($actor instanceof Profile) {
+ // So far we only allow setting with _either_ $xml _or_ $actor as that's
+ // all our circumstances require. But it may be confusing for new developers.
+ // The idea is that feeding XML must be followed by interpretation and then
+ // running $magic_env->verify($profile), just as in SalmonAction->prepare(...)
+ // and supplying an $actor (which right now has to be a User) will require
+ // defining the $data, $data_type etc. attributes manually afterwards before
+ // signing the envelope..
+ $this->setActor($actor);
+ }
+ }
+
+ /**
+ * Retrieve Salmon keypair first by checking local database, but
+ * if it's not found, attempt discovery if it has been requested.
+ *
+ * @param Profile $profile The profile we're looking up keys for.
+ * @param boolean $discovery Network discovery if no local cache?
+ */
+ public function getKeyPair(Profile $profile, $discovery=false) {
+ if (!$profile->isLocal()) common_debug('Getting magic-public-key for non-local profile id=='.$profile->getID());
+ $magicsig = Magicsig::getKV('user_id', $profile->getID());
+
+ if ($discovery && !$magicsig instanceof Magicsig) {
+ if (!$profile->isLocal()) common_debug('magic-public-key not found, will do discovery for profile id=='.$profile->getID());
+ // Throws exception on failure, but does not try to _load_ the keypair string.
+ $keypair = $this->discoverKeyPair($profile);
+
+ $magicsig = new Magicsig();
+ $magicsig->user_id = $profile->getID();
+ $magicsig->importKeys($keypair);
+ // save the public key for this profile in our database.
+ // TODO: If the profile generates a new key remotely, we must be able to replace
+ // this (of course after callback-verification).
+ $magicsig->insert();
+ } elseif (!$magicsig instanceof Magicsig) { // No discovery request, so we'll give up.
+ throw new ServerException(sprintf('No public key found for profile (id==%d)', $profile->id));