Show More
@@ -5,11 +5,12 b' from boards.utils import get_file_mimety' | |||||
5 | from django.db import transaction |
|
5 | from django.db import transaction | |
6 | from boards.models import KeyPair, GlobalId, Signature, Post, Tag |
|
6 | from boards.models import KeyPair, GlobalId, Signature, Post, Tag | |
7 |
|
7 | |||
8 | EXCEPTION_NODE = 'Sync node returned an error: {}' |
|
8 | EXCEPTION_NODE = 'Sync node returned an error: {}.' | |
9 | EXCEPTION_OP = 'Load the OP first' |
|
9 | EXCEPTION_OP = 'Load the OP first.' | |
10 | EXCEPTION_DOWNLOAD = 'File was not downloaded' |
|
10 | EXCEPTION_DOWNLOAD = 'File was not downloaded.' | |
11 | EXCEPTION_HASH = 'File hash does not match attachment hash' |
|
11 | EXCEPTION_HASH = 'File hash does not match attachment hash.' | |
12 | EXCEPTION_SIGNATURE = 'Invalid model signature for {}' |
|
12 | EXCEPTION_SIGNATURE = 'Invalid model signature for {}.' | |
|
13 | EXCEPTION_AUTHOR_SIGNATURE = 'Model {} has no author signature.' | |||
13 | ENCODING_UNICODE = 'unicode' |
|
14 | ENCODING_UNICODE = 'unicode' | |
14 |
|
15 | |||
15 | TAG_MODEL = 'model' |
|
16 | TAG_MODEL = 'model' | |
@@ -166,10 +167,10 b' class SyncManager:' | |||||
166 | tag_content = tag_model.find(TAG_CONTENT) |
|
167 | tag_content = tag_model.find(TAG_CONTENT) | |
167 |
|
168 | |||
168 | content_str = et.tostring(tag_content, ENCODING_UNICODE) |
|
169 | content_str = et.tostring(tag_content, ENCODING_UNICODE) | |
169 | signatures = SyncManager._verify_model(content_str, tag_model) |
|
|||
170 |
|
170 | |||
171 | tag_id = tag_content.find(TAG_ID) |
|
171 | tag_id = tag_content.find(TAG_ID) | |
172 | global_id, exists = GlobalId.from_xml_element(tag_id) |
|
172 | global_id, exists = GlobalId.from_xml_element(tag_id) | |
|
173 | signatures = SyncManager._verify_model(global_id, content_str, tag_model) | |||
173 |
|
174 | |||
174 | if exists: |
|
175 | if exists: | |
175 | print('Post with same ID already exists') |
|
176 | print('Post with same ID already exists') | |
@@ -253,7 +254,7 b' class SyncManager:' | |||||
253 | return et.tostring(response, ENCODING_UNICODE) |
|
254 | return et.tostring(response, ENCODING_UNICODE) | |
254 |
|
255 | |||
255 | @staticmethod |
|
256 | @staticmethod | |
256 | def _verify_model(content_str, tag_model): |
|
257 | def _verify_model(global_id, content_str, tag_model): | |
257 | """ |
|
258 | """ | |
258 | Verifies all signatures for a single model. |
|
259 | Verifies all signatures for a single model. | |
259 | """ |
|
260 | """ | |
@@ -261,11 +262,16 b' class SyncManager:' | |||||
261 | signatures = [] |
|
262 | signatures = [] | |
262 |
|
263 | |||
263 | tag_signatures = tag_model.find(TAG_SIGNATURES) |
|
264 | tag_signatures = tag_model.find(TAG_SIGNATURES) | |
|
265 | has_author_signature = False | |||
264 | for tag_signature in tag_signatures: |
|
266 | for tag_signature in tag_signatures: | |
265 | signature_type = tag_signature.get(ATTR_TYPE) |
|
267 | signature_type = tag_signature.get(ATTR_TYPE) | |
266 | signature_value = tag_signature.get(ATTR_VALUE) |
|
268 | signature_value = tag_signature.get(ATTR_VALUE) | |
267 | signature_key = tag_signature.get(ATTR_KEY) |
|
269 | signature_key = tag_signature.get(ATTR_KEY) | |
268 |
|
270 | |||
|
271 | if global_id.key_type == signature_type and\ | |||
|
272 | global_id.key == signature_key: | |||
|
273 | has_author_signature = True | |||
|
274 | ||||
269 | signature = Signature(key_type=signature_type, |
|
275 | signature = Signature(key_type=signature_type, | |
270 | key=signature_key, |
|
276 | key=signature_key, | |
271 | signature=signature_value) |
|
277 | signature=signature_value) | |
@@ -274,6 +280,8 b' class SyncManager:' | |||||
274 | raise SyncException(EXCEPTION_SIGNATURE.format(content_str)) |
|
280 | raise SyncException(EXCEPTION_SIGNATURE.format(content_str)) | |
275 |
|
281 | |||
276 | signatures.append(signature) |
|
282 | signatures.append(signature) | |
|
283 | if not has_author_signature: | |||
|
284 | raise SyncException(EXCEPTION_AUTHOR_SIGNATURE.format(content_str)) | |||
277 |
|
285 | |||
278 | return signatures |
|
286 | return signatures | |
279 |
|
287 |
General Comments 0
You need to be logged in to leave comments.
Login now