##// END OF EJS Templates
Save signatures when the post is parsed for the later use
neko259 -
r1244:0f0731ca decentral
parent child Browse files
Show More
@@ -75,7 +75,7 b' class PostManager(models.Manager):'
75 return post
75 return post
76
76
77 @transaction.atomic
77 @transaction.atomic
78 def import_post(self, title: str, text: str, pub_time: str,
78 def import_post(self, title: str, text: str, pub_time: str, global_id,
79 opening_post=None, tags=list()):
79 opening_post=None, tags=list()):
80 if opening_post is None:
80 if opening_post is None:
81 thread = boards.models.thread.Thread.objects.create(
81 thread = boards.models.thread.Thread.objects.create(
@@ -88,7 +88,7 b' class PostManager(models.Manager):'
88 pub_time=pub_time,
88 pub_time=pub_time,
89 poster_ip=NO_IP,
89 poster_ip=NO_IP,
90 last_edit_time=pub_time,
90 last_edit_time=pub_time,
91 thread_id=thread.id)
91 thread_id=thread.id, global_id=global_id)
92
92
93 post.build_url()
93 post.build_url()
94 post.connect_replies()
94 post.connect_replies()
@@ -107,10 +107,7 b' class SyncManager:'
107 for tag_model in tag_models:
107 for tag_model in tag_models:
108 tag_content = tag_model.find(TAG_CONTENT)
108 tag_content = tag_model.find(TAG_CONTENT)
109
109
110 valid = SyncManager._verify_model(tag_content, tag_model)
110 signatures = SyncManager._verify_model(tag_content, tag_model)
111
112 if not valid:
113 raise Exception('Invalid model signature')
114
111
115 tag_id = tag_content.find(TAG_ID)
112 tag_id = tag_content.find(TAG_ID)
116 global_id, exists = GlobalId.from_xml_element(tag_id)
113 global_id, exists = GlobalId.from_xml_element(tag_id)
@@ -119,6 +116,9 b' class SyncManager:'
119 print('Post with same ID already exists')
116 print('Post with same ID already exists')
120 else:
117 else:
121 global_id.save()
118 global_id.save()
119 for signature in signatures:
120 signature.global_id = global_id
121 signature.save()
122
122
123 title = tag_content.find(TAG_TITLE).text
123 title = tag_content.find(TAG_TITLE).text
124 text = tag_content.find(TAG_TEXT).text
124 text = tag_content.find(TAG_TEXT).text
@@ -143,8 +143,8 b' class SyncManager:'
143
143
144 post = Post.objects.import_post(
144 post = Post.objects.import_post(
145 title=title, text=text, pub_time=pub_time,
145 title=title, text=text, pub_time=pub_time,
146 opening_post=opening_post, tags=tags)
146 opening_post=opening_post, tags=tags,
147 post.global_id = global_id
147 global_id=global_id)
148 else:
148 else:
149 # TODO Throw an exception?
149 # TODO Throw an exception?
150 pass
150 pass
@@ -155,7 +155,7 b' class SyncManager:'
155 Verifies all signatures for a single model.
155 Verifies all signatures for a single model.
156 """
156 """
157
157
158 valid = True
158 signatures = []
159
159
160 tag_signatures = tag_model.find(TAG_SIGNATURES)
160 tag_signatures = tag_model.find(TAG_SIGNATURES)
161 for tag_signature in tag_signatures:
161 for tag_signature in tag_signatures:
@@ -163,11 +163,13 b' class SyncManager:'
163 signature_value = tag_signature.get(ATTR_VALUE)
163 signature_value = tag_signature.get(ATTR_VALUE)
164 signature_key = tag_signature.get(ATTR_KEY)
164 signature_key = tag_signature.get(ATTR_KEY)
165
165
166 signature = Signature(key_type=signature_type,
167 key=signature_key,
168 signature=signature_value)
169 signatures.append(signature)
170
166 if not KeyPair.objects.verify(
171 if not KeyPair.objects.verify(
167 signature_key,
172 signature, et.tostring(tag_content, ENCODING_UNICODE)):
168 et.tostring(tag_content, ENCODING_UNICODE),
173 raise Exception('Invalid model signature')
169 signature_value, signature_type):
170 valid = False
171 break
172
174
173 return valid
175 return signatures
@@ -25,10 +25,10 b' class KeyPairManager(models.Manager):'
25 else:
25 else:
26 raise Exception('Key type not supported')
26 raise Exception('Key type not supported')
27
27
28 def verify(self, public_key_str, string, signature, key_type=TYPE_ECDSA):
28 def verify(self, signature, string):
29 if key_type == TYPE_ECDSA:
29 if signature.key_type == TYPE_ECDSA:
30 public = VerifyingKey.from_string(base64.b64decode(public_key_str))
30 public = VerifyingKey.from_string(base64.b64decode(signature.key))
31 signature_byte = base64.b64decode(signature)
31 signature_byte = base64.b64decode(signature.signature)
32 try:
32 try:
33 return public.verify(signature_byte, string.encode())
33 return public.verify(signature_byte, string.encode())
34 except BadSignatureError:
34 except BadSignatureError:
@@ -2,7 +2,7 b' from base64 import b64encode'
2 import logging
2 import logging
3
3
4 from django.test import TestCase
4 from django.test import TestCase
5 from boards.models import KeyPair, GlobalId, Post
5 from boards.models import KeyPair, GlobalId, Post, Signature
6 from boards.models.post.sync import SyncManager
6 from boards.models.post.sync import SyncManager
7
7
8 logger = logging.getLogger(__name__)
8 logger = logging.getLogger(__name__)
@@ -17,9 +17,11 b' class KeyTest(TestCase):'
17 def test_validation(self):
17 def test_validation(self):
18 key = KeyPair.objects.generate_key(key_type='ecdsa')
18 key = KeyPair.objects.generate_key(key_type='ecdsa')
19 message = 'msg'
19 message = 'msg'
20 signature = key.sign(message)
20 signature_value = key.sign(message)
21 valid = KeyPair.objects.verify(key.public_key, message, signature,
21
22 key_type='ecdsa')
22 signature = Signature(key_type='ecdsa', key=key.public_key,
23 signature=signature_value)
24 valid = KeyPair.objects.verify(signature, message)
23
25
24 self.assertTrue(valid, 'Message verification failed.')
26 self.assertTrue(valid, 'Message verification failed.')
25
27
@@ -67,3 +67,6 b' class SyncTest(TestCase):'
67 SyncManager.parse_response_get(response)
67 SyncManager.parse_response_get(response)
68 self.assertEqual(1, Post.objects.count(),
68 self.assertEqual(1, Post.objects.count(),
69 'The same post was imported twice.')
69 'The same post was imported twice.')
70
71 self.assertEqual(1, parsed_post.global_id.signature_set.count(),
72 'Signature was not saved.')
General Comments 0
You need to be logged in to leave comments. Login now