##// END OF EJS Templates
Actually added the sync_key model. Added signature block to the 'get' response...
neko259 -
r794:e6c8be6a decentral
parent child Browse files
Show More
@@ -0,0 +1,54 b''
1 import base64
2 from ecdsa import SigningKey, VerifyingKey
3 from django.db import models
4
5 TYPE_ECDSA = 'ecdsa'
6
7 APP_LABEL_BOARDS = 'boards'
8
9
10 class KeyPairManager(models.Manager):
11 def generate_key(self, key_type=TYPE_ECDSA):
12 if key_type == TYPE_ECDSA:
13 private = SigningKey.generate()
14 public = private.get_verifying_key()
15
16 private_key_str = private.to_pem().decode()
17 public_key_str = public.to_pem().decode()
18
19 return self.create(public_key=public_key_str,
20 private_key=private_key_str,
21 key_type=TYPE_ECDSA)
22 else:
23 return None
24
25 def verify(self, public_key_str, string, signature, key_type=TYPE_ECDSA):
26 if key_type == TYPE_ECDSA:
27 public = VerifyingKey.from_pem(public_key_str)
28 signature_byte = base64.b64decode(signature)
29 try:
30 return public.verify(signature_byte,
31 string.encode())
32 except BadSignatureError:
33 return False
34 else:
35 return False
36
37
38 class KeyPair(models.Model):
39 class Meta:
40 app_label = APP_LABEL_BOARDS
41
42 objects = KeyPairManager()
43
44 public_key = models.TextField()
45 private_key = models.TextField()
46 key_type = models.TextField()
47
48 def __str__(self):
49 return '%s: %s' % (self.key_type, self.public_key)
50
51 def sign(self, string):
52 private = SigningKey.from_pem(self.private_key)
53 signature_byte = private.sign(string.encode())
54 return base64.b64encode(signature_byte)
@@ -1,37 +1,56 b''
1 <?xml version="1.1" encoding="UTF-8" ?>
1 <?xml version="1.1" encoding="UTF-8" ?>
2 <response>
2 <response>
3 <!--
4 Valid statuses are 'success' and 'error'.
5 -->
3 <status>success</status>
6 <status>success</status>
4 <models>
7 <models>
5 <model name="post">
8 <!--
9 ref-id is used only to reference the model block in the
10 signatures block. It could be any string that is unique throughout
11 the file.
12 -->
13 <model name="post" ref-id="1">
6 <id key="id1" local-id="1" />
14 <id key="id1" local-id="1" />
7 <title>13</title>
15 <title>13</title>
8 <text>Thirteen</text>
16 <text>Thirteen</text>
9 <thread>id1/12</thread>
17 <thread>id1/12</thread>
10 <pub-time>12</pub-time>
18 <pub-time>12</pub-time>
11 <edit-time>13</edit-time>
19 <edit-time>13</edit-time>
12 <!--
20 <!--
13 Next and previous are the links to sequential models.
21 Next and previous are the links to sequential models.
14 In the case of an image board, next are replies and previous
22 In the case of an image board, next are replies and previous
15 are the posts we replied to.
23 are the posts we replied to.
16 -->
24 -->
17 <previous>
25 <previous>
18 <id key="id1" local-id="3" />
26 <id key="id1" local-id="3" />
19 <id key="id23" local-id="5" />
27 <id key="id23" local-id="5" />
20 </previous>
28 </previous>
21 <next>
29 <next>
22 <id key="id2" local-id="3" />
30 <id key="id2" local-id="3" />
23 <id key="id43" local-id="5" />
31 <id key="id43" local-id="5" />
24 </next>
32 </next>
25 </model>
33 </model>
26 <model name="post">
34 <model name="post" ref-id="2">
27 <id key="id1" local-id="id2" />
35 <id key="id1" local-id="id2" />
28 <title>13</title>
36 <title>13</title>
29 <text>Thirteen</text>
37 <text>Thirteen</text>
30 <pub-time>12</pub-time>
38 <pub-time>12</pub-time>
31 <edit-time>13</edit-time>
39 <edit-time>13</edit-time>
32 <tags>
40 <tags>
33 <tag>tag1</tag>
41 <tag>tag1</tag>
34 </tags>
42 </tags>
35 </model>
43 </model>
36 </models>
44 </models>
45
46 <!-- The signature block is separate from the model block because the
47 signature value should not be included while getting the block sign.
48 When signing a model block, it should be in the canonical XML form.
49 See http://www.w3.org/TR/xml-c14n11/ for details.
50 -->
51 <signatures>
52 <signature model-ref="1" key="id1" value="dhefhtreh" />
53 <signature model-ref="1" key="id45" value="dsgfgdhefhtreh" />
54 <signature model-ref="2" key="id2" value="dehdfh" />
55 </signatures>
37 </response>
56 </response>
General Comments 0
You need to be logged in to leave comments. Login now