3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * A sample module to show best practices for StatusNet plugins
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author James Walker <james@status.net>
25 * @copyright 2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
27 * @link http://status.net/
31 const REL_SALMON = 'salmon';
32 const REL_MENTIONED = 'mentioned';
34 // XXX: these are deprecated
35 const NS_REPLIES = "http://salmon-protocol.org/ns/salmon-replies";
36 const NS_MENTIONS = "http://salmon-protocol.org/ns/salmon-mention";
39 * Sign and post the given Atom entry as a Salmon message.
41 * Side effects: may generate a keypair on-demand for the given user,
42 * which can be very slow on some systems.
44 * @param string $endpoint_uri
45 * @param string $xml string representation of payload
46 * @param Profile $actor local user profile whose keys to sign with
47 * @return boolean success
49 public function post($endpoint_uri, $xml, Profile $actor)
51 if (empty($endpoint_uri)) {
52 common_debug('No endpoint URI for Salmon post to '.$actor->getUri());
56 foreach ($this->formatClasses() as $class) {
58 $envelope = $this->createMagicEnv($xml, $actor, $class);
59 } catch (Exception $e) {
60 common_log(LOG_ERR, "Salmon unable to sign: " . $e->getMessage());
64 $headers = array('Content-Type: application/magic-envelope+xml');
67 $client = new HTTPClient();
68 $client->setBody($envelope);
69 $response = $client->post($endpoint_uri, $headers);
70 } catch (HTTP_Request2_Exception $e) {
71 common_log(LOG_ERR, "Salmon ($class) post to $endpoint_uri failed: " . $e->getMessage());
74 if ($response->getStatus() != 200) {
75 common_log(LOG_ERR, "Salmon ($class) at $endpoint_uri returned status " .
76 $response->getStatus() . ': ' . $response->getBody());
87 * List the magic envelope signature class variants in the order we try them.
88 * Multiples are needed for backwards-compat with StatusNet prior to 0.9.7,
89 * which used a draft version of the magic envelope spec.
91 * FIXME: Deprecate and remove. GNU social shouldn't have to interface with SN<0.9.7
93 protected function formatClasses() {
94 return array('MagicEnvelope', 'MagicEnvelopeCompat');
98 * Encode the given string as a signed MagicEnvelope XML document,
99 * using the keypair for the given local user profile.
101 * Side effects: will create and store a keypair on-demand if one
102 * hasn't already been generated for this user. This can be very slow
105 * @param string $text XML fragment to sign, assumed to be Atom
106 * @param Profile $actor Profile of a local user to use as signer
107 * @param string $class to override the magic envelope signature version, pass a MagicEnvelope subclass here
109 * @return string XML string representation of magic envelope
111 * @throws Exception on bad profile input or key generation problems
112 * @fixme if signing fails, this seems to return the original text without warning. Is there a reason for this?
114 public function createMagicEnv($text, $actor, $class='MagicEnvelope')
116 if (!in_array($class, $this->formatClasses())) {
117 throw new ServerException('Bad class parameter for createMagicEnv');
120 $magic_env = new $class();
122 // We only generate keys for our local users of course, so let
123 // getUser throw an exception if the profile is not local.
124 $user = $actor->getUser();
126 // Find already stored key
127 $magicsig = Magicsig::getKV('user_id', $user->id);
128 if (!$magicsig instanceof Magicsig) {
129 // No keypair yet, let's generate one.
130 $magicsig = new Magicsig();
131 $magicsig->generate($user->id);
135 $env = $magic_env->signMessage($text, 'application/atom+xml', $magicsig->toString());
136 } catch (Exception $e) {
139 return $magic_env->toXML($env);
143 * Check if the given magic envelope is well-formed and correctly signed.
144 * Needs to have network access to fetch public keys over the web.
145 * Both current and back-compat signature formats will be checked.
147 * Side effects: exceptions and caching updates may occur during network
150 * @param string $text XML fragment of magic envelope
153 * @throws Exception on bad profile input or key generation problems
154 * @fixme could hit fatal errors or spew output on invalid XML
156 public function verifyMagicEnv($text)
158 foreach ($this->formatClasses() as $class) {
159 $magic_env = new $class();
161 $env = $magic_env->parse($text);
163 if ($magic_env->verify($env)) {