fixes some of the signing test issues

This commit is contained in:
Mouse Reeve 2020-11-30 19:53:42 -08:00
parent fee5846aa8
commit 1610d81ce6
3 changed files with 14 additions and 9 deletions

View file

@ -96,6 +96,8 @@ def has_valid_signature(request, activity):
raise ValueError("Wrong actor created signature.") raise ValueError("Wrong actor created signature.")
remote_user = activitypub.resolve_remote_id(models.User, key_actor) remote_user = activitypub.resolve_remote_id(models.User, key_actor)
if not remote_user:
return False
try: try:
signature.verify(remote_user.key_pair.public_key, request) signature.verify(remote_user.key_pair.public_key, request)

View file

@ -31,7 +31,7 @@ def make_signature(sender, destination, date, digest):
'digest: %s' % digest, 'digest: %s' % digest,
] ]
message_to_sign = '\n'.join(signature_headers) message_to_sign = '\n'.join(signature_headers)
signer = pkcs1_15.new(RSA.import_key(sender.private_key)) signer = pkcs1_15.new(RSA.import_key(sender.key_pair.private_key))
signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8'))) signed_message = signer.sign(SHA256.new(message_to_sign.encode('utf8')))
signature = { signature = {
'keyId': '%s#main-key' % sender.remote_id, 'keyId': '%s#main-key' % sender.remote_id,

View file

@ -25,20 +25,23 @@ def get_follow_data(follower, followee):
).serialize() ).serialize()
return json.dumps(follow_activity) return json.dumps(follow_activity)
Sender = namedtuple('Sender', ('remote_id', 'private_key', 'public_key')) KeyPair = namedtuple('KeyPair', ('private_key', 'public_key'))
Sender = namedtuple('Sender', ('remote_id', 'key_pair'))
class Signature(TestCase): class Signature(TestCase):
def setUp(self): def setUp(self):
self.mouse = User.objects.create_user('mouse', 'mouse@example.com', '') self.mouse = User.objects.create_user(
self.rat = User.objects.create_user('rat', 'rat@example.com', '') 'mouse', 'mouse@example.com', '', local=True)
self.cat = User.objects.create_user('cat', 'cat@example.com', '') self.rat = User.objects.create_user(
'rat', 'rat@example.com', '', local=True)
self.cat = User.objects.create_user(
'cat', 'cat@example.com', '', local=True)
private_key, public_key = create_key_pair() private_key, public_key = create_key_pair()
self.fake_remote = Sender( self.fake_remote = Sender(
'http://localhost/user/remote', 'http://localhost/user/remote',
private_key, KeyPair(private_key, public_key)
public_key,
) )
def send(self, signature, now, data, digest): def send(self, signature, now, data, digest):
@ -89,7 +92,7 @@ class Signature(TestCase):
datafile = pathlib.Path(__file__).parent.joinpath('data/ap_user.json') datafile = pathlib.Path(__file__).parent.joinpath('data/ap_user.json')
data = json.loads(datafile.read_bytes()) data = json.loads(datafile.read_bytes())
data['id'] = self.fake_remote.remote_id data['id'] = self.fake_remote.remote_id
data['publicKey']['publicKeyPem'] = self.fake_remote.public_key data['publicKey']['publicKeyPem'] = self.fake_remote.key_pair.public_key
del data['icon'] # Avoid having to return an avatar. del data['icon'] # Avoid having to return an avatar.
responses.add( responses.add(
responses.GET, responses.GET,
@ -116,7 +119,7 @@ class Signature(TestCase):
datafile = pathlib.Path(__file__).parent.joinpath('data/ap_user.json') datafile = pathlib.Path(__file__).parent.joinpath('data/ap_user.json')
data = json.loads(datafile.read_bytes()) data = json.loads(datafile.read_bytes())
data['id'] = self.fake_remote.remote_id data['id'] = self.fake_remote.remote_id
data['publicKey']['publicKeyPem'] = self.fake_remote.public_key data['publicKey']['publicKeyPem'] = self.fake_remote.key_pair.public_key
del data['icon'] # Avoid having to return an avatar. del data['icon'] # Avoid having to return an avatar.
responses.add( responses.add(
responses.GET, responses.GET,