create WKNodeinfo class and add nodeinfo 2.1 path

This commit is contained in:
Izalia Mae 2022-11-07 08:18:25 -05:00
parent 8d17749a50
commit e3bf4258aa
2 changed files with 55 additions and 19 deletions

View file

@ -19,12 +19,18 @@ from .http_debug import http_debug
app = None app = None
HASHES = { HASHES = {
'sha1': SHA, 'sha1': SHA,
'sha256': SHA256, 'sha256': SHA256,
'sha512': SHA512 'sha512': SHA512
} }
NODEINFO_NS = {
'20': 'http://nodeinfo.diaspora.software/ns/schema/2.0',
'21': 'http://nodeinfo.diaspora.software/ns/schema/2.1'
}
def set_app(new_app): def set_app(new_app):
global app global app
@ -122,24 +128,28 @@ async def fetch_actor_key(actor):
async def fetch_nodeinfo(domain): async def fetch_nodeinfo(domain):
nodeinfo_url = None nodeinfo_url = None
wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False, activity=False) wk_nodeinfo = await request(f'https://{domain}/.well-known/nodeinfo', sign_headers=False, activity=False)
if not wk_nodeinfo: if not wk_nodeinfo:
return return
for link in wk_nodeinfo.get('links', ''): wk_nodeinfo = WKNodeinfo(wk_nodeinfo)
if link['rel'] == 'http://nodeinfo.diaspora.software/ns/schema/2.0':
nodeinfo_url = link['href'] for version in ['20', '21']:
break try:
nodeinfo_url = wk_nodeinfo.get_url(version)
except KeyError:
pass
if not nodeinfo_url: if not nodeinfo_url:
return logging.verbose(f'Failed to fetch nodeinfo url for domain: {domain}')
return False
nodeinfo_data = await request(nodeinfo_url, sign_headers=False, activity=False) nodeinfo = await request(nodeinfo_url, sign_headers=False, activity=False)
try: try:
return nodeinfo_data['software']['name'] return nodeinfo['software']['name']
except KeyError: except KeyError:
return False return False
@ -474,3 +484,29 @@ class Message(DotDict):
return self.object.id return self.object.id
return self.object return self.object
class WKNodeinfo(DotDict):
def __setitem__(self, key, value):
if key == 'links':
value = [DotDict(item) for item in value]
DotDict.__setitem__(self, key, value)
@classmethod
def new(cls, v20, v21):
return cls({
'links': [
{'rel': NODEINFO_NS['20'], 'href': v20},
{'rel': NODEINFO_NS['21'], 'href': v21}
]
})
def get_url(self, version='20'):
for item in self.links:
if item.rel == NODEINFO_NS[version]:
return item.href
raise KeyError(version)

View file

@ -162,10 +162,10 @@ async def webfinger(request):
return json_response(data) return json_response(data)
@register_route('GET', '/nodeinfo/2.0.json') @register_route('GET', '/nodeinfo/{version:\d.\d\.json}')
async def nodeinfo_2_0(request): async def nodeinfo_2_0(request):
version = request.match_info['version'][:3]
data = { data = {
# XXX - is this valid for a relay?
'openRegistrations': True, 'openRegistrations': True,
'protocols': ['activitypub'], 'protocols': ['activitypub'],
'services': { 'services': {
@ -185,22 +185,22 @@ async def nodeinfo_2_0(request):
'metadata': { 'metadata': {
'peers': request.app.database.hostnames 'peers': request.app.database.hostnames
}, },
'version': '2.0' 'version': version
} }
if version == '2.1':
data['software']['repository'] = 'https://git.pleroma.social/pleroma/relay'
return json_response(data) return json_response(data)
@register_route('GET', '/.well-known/nodeinfo') @register_route('GET', '/.well-known/nodeinfo')
async def nodeinfo_wellknown(request): async def nodeinfo_wellknown(request):
data = { data = WKNodeinfo.new(
'links': [ v20 = f'https://{request.app.config.host}/nodeinfo/2.0.json',
{ v21 = f'https://{request.app.config.host}/nodeinfo/2.1.json'
'rel': 'http://nodeinfo.diaspora.software/ns/schema/2.0', )
'href': f'https://{request.app.config.host}/nodeinfo/2.0.json'
}
]
}
return json_response(data) return json_response(data)