]>
Commit | Line | Data |
---|---|---|
2af4a5fc MF |
1 | #!/usr/bin/python2.4 |
2 | # | |
3 | # Copyright 2010 Google Inc. All Rights Reserved. | |
4 | # | |
5 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | # you may not use this file except in compliance with the License. | |
7 | # You may obtain a copy of the License at | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, software | |
12 | # distributed under the License is distributed on an "AS IS" BASIS, | |
13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | # See the License for the specific language governing permissions and | |
15 | # limitations under the License. | |
16 | # | |
17 | ||
18 | """Support library for the Salmon Protocol. | |
19 | ||
20 | See Salmon I-D for specification. This module | |
21 | implements a support library for Salmon on top of the | |
22 | Magic Envelope library and other bits. | |
23 | """ | |
24 | ||
25 | __author__ = 'jpanzer@google.com (John Panzer)' | |
26 | ||
27 | ||
28 | #import base64 | |
29 | #import xml.dom.minidom as dom | |
30 | ||
31 | import magicsig | |
32 | ||
33 | ||
34 | class SalmonProtocol(object): | |
35 | """Implementation of Salmon Protocol.""" | |
36 | ||
37 | magicenv = magicsig.MagicEnvelopeProtocol() | |
38 | ||
39 | def _GetKeypair(self, signer_uri): | |
40 | return self.key_retriever.LookupPublicKey(signer_uri) | |
41 | ||
42 | def SignSalmon(self, text, mimetype, requestor_id): | |
43 | """Signs a Salmon on behalfo the the current_user. | |
44 | ||
45 | Input text must be in a recognized format so authorship can be | |
46 | verified. | |
47 | ||
48 | Args: | |
49 | text: Text of message to be signed. | |
50 | mimetype: The MIME type of the message to sign. | |
51 | requestor_id: The id of the requestor (usually current logged in user). | |
52 | Returns: | |
53 | The Magic Envelope parameters from section 3.1 of the | |
54 | Magic Signatures spec, as a dict. | |
55 | """ | |
56 | ||
57 | assert mimetype == 'application/atom+xml' | |
58 | ||
59 | requestor_id = magicsig.NormalizeUserIdToUri(requestor_id) | |
60 | ||
61 | if not self.magicenv.IsAllowedSigner(text, | |
62 | magicsig.NormalizeUserIdToUri(requestor_id)): | |
63 | # TODO: Fix authorship if missing author, raise | |
64 | # exception otherwise. | |
65 | return | |
66 | ||
67 | env = magicsig.Envelope( | |
68 | self.magicenv, | |
69 | raw_data_to_sign=text, | |
70 | signer_uri=requestor_id, | |
71 | signer_key=self._GetKeypair(requestor_id), | |
72 | data_type='application/atom+xml', | |
73 | encoding='base64url', | |
74 | alg='RSA-SHA256') | |
75 | ||
76 | ||
77 | return env.ToXML() | |
78 | ||
79 | def ParseSalmon(self, text, mimetype): | |
80 | """Parses a salmon from text with given mimetype. | |
81 | ||
82 | Returns: | |
83 | The salmon data as a dict, with fields: | |
84 | """ | |
85 | ||
86 | self.magicenv.key_retriever = self.key_retriever | |
87 | ||
88 | return magicsig.Envelope( | |
89 | self.magicenv, | |
90 | mime_type=mimetype, | |
91 | document=text).ToAtom() |