Use get_or_create_remote_user from get_public_key.

This commit is contained in:
Adam Kelly 2020-05-21 16:32:28 +01:00
parent 00bd8928b0
commit 17734940ac
3 changed files with 22 additions and 19 deletions

View file

@ -54,7 +54,7 @@ def shared_inbox(request):
key = get_public_key(key_actor) key = get_public_key(key_actor)
signature.verify(key, request) signature.verify(key, request)
except ValueError: except (ValueError, requests.exceptions.HTTPError):
return HttpResponse(status=401) return HttpResponse(status=401)
handlers = { handlers = {
@ -91,20 +91,8 @@ def shared_inbox(request):
def get_public_key(key_actor): def get_public_key(key_actor):
''' try a stored key or load it from remote ''' ''' try a stored key or load it from remote '''
try: user = get_or_create_remote_user(key_actor)
user = models.User.objects.get(remote_id=key_actor) return user.public_key
public_key = user.public_key
except models.User.DoesNotExist:
response = requests.get(
key_actor,
headers={'Accept': 'application/activity+json'}
)
if not response.ok:
raise ValueError('Could not load public key')
user_data = response.json()
public_key = user_data['publicKey']['publicKeyPem']
return public_key
@app.task @app.task
def handle_follow(activity): def handle_follow(activity):

View file

@ -36,7 +36,8 @@ def get_or_create_remote_user(actor):
user.save() user.save()
avatar = get_avatar(data) avatar = get_avatar(data)
user.avatar.save(*avatar) if avatar:
user.avatar.save(*avatar)
if user.fedireads_user: if user.fedireads_user:
get_remote_reviews(user) get_remote_reviews(user)

View file

@ -1,6 +1,7 @@
import time import time
from collections import namedtuple from collections import namedtuple
from urllib.parse import urlsplit from urllib.parse import urlsplit
import pathlib
import json import json
import responses import responses
@ -73,13 +74,26 @@ class Signature(TestCase):
@responses.activate @responses.activate
def test_remote_signer(self): def test_remote_signer(self):
datafile = pathlib.Path(__file__).parent.joinpath('data/ap_user.json')
data = json.loads(datafile.read_bytes())
data['id'] = self.fake_remote.remote_id
data['publicKey']['publicKeyPem'] = self.fake_remote.public_key
del data['icon'] # Avoid having to return an avatar.
responses.add( responses.add(
responses.GET, responses.GET,
self.fake_remote.remote_id, self.fake_remote.remote_id,
json={'publicKey': { json=data,
'publicKeyPem': self.fake_remote.public_key
}},
status=200) status=200)
responses.add(
responses.GET,
'https://localhost/.well-known/nodeinfo',
status=404)
responses.add(
responses.GET,
'https://example.com/user/mouse/outbox?page=true',
json={'orderedItems': []},
status=200
)
response = self.send_test_request(sender=self.fake_remote) response = self.send_test_request(sender=self.fake_remote)
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)