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/
32 const ENCODING = 'base64url';
34 const NS = 'http://salmon-protocol.org/ns/magic-env';
36 private function normalizeUser($user_id)
38 if (substr($user_id, 0, 5) == 'http:' ||
39 substr($user_id, 0, 6) == 'https:' ||
40 substr($user_id, 0, 5) == 'acct:') {
44 if (strpos($user_id, '@') !== FALSE) {
45 return 'acct:' . $user_id;
48 return 'http://' . $user_id;
51 public function getKeyPair($signer_uri)
53 $disco = new Discovery();
55 $xrd = $disco->lookup($signer_uri);
56 if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
57 list($type, $keypair) = explode(';', $link['href']);
61 throw new Exception('Unable to locate signer public key');
65 public function signMessage($text, $mimetype, $signer_uri)
67 $signer_uri = $this->normalizeUser($signer_uri);
69 if (!$this->checkAuthor($text, $signer_uri)) {
70 throw new Exception("Unable to determine entry author.");
73 $signature_alg = Magicsig::fromString($this->getKeyPair($signer_uri));
74 $armored_text = base64_encode($text);
77 'data' => $armored_text,
78 'encoding' => MagicEnvelope::ENCODING,
79 'data_type' => $mimetype,
80 'sig' => $signature_alg->sign($armored_text),
81 'alg' => $signature_alg->getName()
87 public function unfold($env)
89 $dom = new DOMDocument();
90 $dom->loadXML(base64_decode($env['data']));
92 if ($dom->documentElement->tagName != 'entry') {
96 $prov = $dom->createElementNS(MagicEnvelope::NS, 'me:provenance');
97 $prov->setAttribute('xmlns:me', MagicEnvelope::NS);
98 $data = $dom->createElementNS(MagicEnvelope::NS, 'me:data', $env['data']);
99 $data->setAttribute('type', $env['data_type']);
100 $prov->appendChild($data);
101 $enc = $dom->createElementNS(MagicEnvelope::NS, 'me:encoding', $env['encoding']);
102 $prov->appendChild($enc);
103 $alg = $dom->createElementNS(MagicEnvelope::NS, 'me:alg', $env['alg']);
104 $prov->appendChild($alg);
105 $sig = $dom->createElementNS(MagicEnvelope::NS, 'me:sig', $env['sig']);
106 $prov->appendChild($sig);
108 $dom->documentElement->appendChild($prov);
110 return $dom->saveXML();
113 public function getAuthor($text) {
114 $doc = new DOMDocument();
115 if (!$doc->loadXML($text)) {
119 if ($doc->documentElement->tagName == 'entry') {
120 $authors = $doc->documentElement->getElementsByTagName('author');
121 foreach ($authors as $author) {
122 $uris = $author->getElementsByTagName('uri');
123 foreach ($uris as $uri) {
124 return $this->normalizeUser($uri->nodeValue);
130 public function checkAuthor($text, $signer_uri)
132 return ($this->getAuthor($text) == $signer_uri);
135 public function verify($env)
137 if ($env['alg'] != 'RSA-SHA256') {
141 if ($env['encoding'] != MagicEnvelope::ENCODING) {
145 $text = base64_decode($env['data']);
146 $signer_uri = $this->getAuthor($text);
148 $verifier = Magicsig::fromString($this->getKeyPair($signer_uri));
150 return $verifier->verify($env['data'], $env['sig']);
153 public function parse($text)
155 $dom = DOMDocument::loadXML($text);
156 return $this->fromDom($dom);
159 public function fromDom($dom)
161 if ($dom->documentElement->tagName == 'entry') {
162 $env_element = $dom->getElementsByTagNameNS(MagicEnvelope::NS, 'provenance')->item(0);
163 } else if ($dom->documentElement->tagName == 'me:env') {
164 $env_element = $dom->documentElement;
169 $data_element = $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'data')->item(0);
172 'data' => trim($data_element->nodeValue),
173 'data_type' => $data_element->getAttribute('type'),
174 'encoding' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'encoding')->item(0)->nodeValue,
175 'alg' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'alg')->item(0)->nodeValue,
176 'sig' => $env_element->getElementsByTagNameNS(MagicEnvelope::NS, 'sig')->item(0)->nodeValue,