kopia lustrzana https://github.com/snarfed/bridgy-fed
add MagicKey datastore model
rodzic
6ddd1d66d9
commit
ba1d49ffa3
|
@ -0,0 +1,41 @@
|
|||
"""Datastore model classes.
|
||||
|
||||
Based on webfinger-unofficial/user.py.
|
||||
"""
|
||||
from django_salmon import magicsigs
|
||||
from google.appengine.ext import ndb
|
||||
from oauth_dropins.webutil.models import StringIdModel
|
||||
|
||||
|
||||
class MagicKey(StringIdModel):
|
||||
"""Stores a user's public/private key pair used for Magic Signatures.
|
||||
|
||||
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.
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
return key
|
|
@ -0,0 +1,30 @@
|
|||
# coding=utf-8
|
||||
"""Unit tests for models.py."""
|
||||
import unittest
|
||||
|
||||
from google.appengine.datastore import datastore_stub_util
|
||||
from google.appengine.ext import testbed
|
||||
|
||||
import models
|
||||
|
||||
|
||||
class ModelsTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.testbed = testbed.Testbed()
|
||||
self.testbed.activate()
|
||||
hrd_policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=.5)
|
||||
self.testbed.init_datastore_v3_stub(consistency_policy=hrd_policy)
|
||||
self.testbed.init_memcache_stub()
|
||||
|
||||
def tearDown(self):
|
||||
self.testbed.deactivate()
|
||||
|
||||
def test_magic_key_get_or_create(self):
|
||||
key = models.MagicKey.get_or_create('x@y.z')
|
||||
assert key.mod
|
||||
assert key.public_exponent
|
||||
assert key.private_exponent
|
||||
|
||||
same = models.MagicKey.get_or_create('x@y.z')
|
||||
self.assertEquals(same, key)
|
Ładowanie…
Reference in New Issue