MagicKey: generate data: URI for href value

also fix indentation
mastodon
Ryan Barrett 2017-08-19 09:15:29 -07:00
rodzic ba1d49ffa3
commit 5499e06a6b
2 zmienionych plików z 36 dodań i 25 usunięć

Wyświetl plik

@ -8,34 +8,38 @@ from oauth_dropins.webutil.models import StringIdModel
class MagicKey(StringIdModel):
"""Stores a user's public/private key pair used for Magic Signatures.
"""Stores a user's public/private key pair used for Magic Signatures.
The key name is USERNAME@DOMAIN.
The key name is USERNAME@DOMAIN.
The modulus and exponent properties are all encoded as base64url (ie URL-safe
base64) strings as described in RFC 4648 and section 5.1 of the Magic
Signatures spec.
The modulus and exponent properties are all encoded as base64url (ie URL-safe
base64) strings as described in RFC 4648 and section 5.1 of the Magic
Signatures spec.
Magic Signatures are used to sign Salmon slaps. Details:
http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-magicsig-01.html
http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-salmon-00.html
"""
mod = ndb.StringProperty(required=True)
public_exponent = ndb.StringProperty(required=True)
private_exponent = ndb.StringProperty(required=True)
Magic Signatures are used to sign Salmon slaps. Details:
http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-magicsig-01.html
http://salmon-protocol.googlecode.com/svn/trunk/draft-panzer-salmon-00.html
"""
mod = ndb.StringProperty(required=True)
public_exponent = ndb.StringProperty(required=True)
private_exponent = ndb.StringProperty(required=True)
@staticmethod
@ndb.transactional
def get_or_create(uri):
"""Loads and returns a MagicKey from the datastore. Creates it if necessary."""
key = MagicKey.get_by_id(uri)
@staticmethod
@ndb.transactional
def get_or_create(uri):
"""Loads and returns a MagicKey. Creates it if necessary."""
key = MagicKey.get_by_id(uri)
if not key:
# this uses urandom(), and does some nontrivial math, so it can take a
# while depending on the amount of randomness available on the system.
pubexp, mod, privexp = magicsigs.generate()
key = MagicKey(id=uri, mod=mod, public_exponent=pubexp,
private_exponent=privexp)
key.put()
if not key:
# this uses urandom(), and does nontrivial math, so it can take a
# while depending on the amount of randomness available.
pubexp, mod, privexp = magicsigs.generate()
key = MagicKey(id=uri, mod=mod, public_exponent=pubexp,
private_exponent=privexp)
key.put()
return key
return key
def href(self):
return 'data:application/magic-public-key,RSA.%s.%s' % (
self.mod, self.public_exponent)

Wyświetl plik

@ -28,3 +28,10 @@ class ModelsTest(unittest.TestCase):
same = models.MagicKey.get_or_create('x@y.z')
self.assertEquals(same, key)
def test_href(self):
key = models.MagicKey.get_or_create('x@y.z')
href = key.href()
self.assertTrue(href.startswith('data:application/magic-public-key,RSA.'), href)
self.assertIn(key.mod, href)
self.assertIn(key.public_exponent, href)