forked from indymedia/epicyon
flake8 format
parent
b63bf2c72d
commit
dc2386c583
65
webfinger.py
65
webfinger.py
|
@ -13,10 +13,7 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from Crypto.PublicKey import RSA
|
from Crypto.PublicKey import RSA
|
||||||
from Crypto.Util import number
|
from Crypto.Util import number
|
||||||
import requests
|
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
from session import getJson
|
from session import getJson
|
||||||
from cache import storeWebfingerInCache
|
from cache import storeWebfingerInCache
|
||||||
from cache import getWebfingerFromCache
|
from cache import getWebfingerFromCache
|
||||||
|
@ -24,25 +21,27 @@ from utils import loadJson
|
||||||
from utils import loadJsonOnionify
|
from utils import loadJsonOnionify
|
||||||
from utils import saveJson
|
from utils import saveJson
|
||||||
|
|
||||||
|
|
||||||
def parseHandle(handle: str) -> (str, str):
|
def parseHandle(handle: str) -> (str, str):
|
||||||
if '.' not in handle:
|
if '.' not in handle:
|
||||||
return None, None
|
return None, None
|
||||||
|
handleStr = handle.replace('https://', '').replace('http://', '')
|
||||||
|
handleStr = handleStr.replace('dat://', '').replace('i2p://', '')
|
||||||
if '/@' in handle:
|
if '/@' in handle:
|
||||||
domain,nickname= \
|
domain, nickname = handleStr.split('/@')
|
||||||
handle.replace('https://','').replace('http://','').replace('dat://','').replace('i2p://','').split('/@')
|
|
||||||
else:
|
else:
|
||||||
if '/users/' in handle:
|
if '/users/' in handle:
|
||||||
domain,nickname= \
|
domain, nickname = handleStr.split('/users/')
|
||||||
handle.replace('https://','').replace('http://','').replace('i2p://','').replace('dat://','').split('/users/')
|
|
||||||
else:
|
else:
|
||||||
if '@' in handle:
|
if '@' in handle:
|
||||||
nickname, domain = handle.split('@')
|
nickname, domain = handle.split('@')
|
||||||
else:
|
else:
|
||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
return nickname, domain
|
return nickname, domain
|
||||||
|
|
||||||
def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
|
|
||||||
|
def webfingerHandle(session, handle: str, httpPrefix: str,
|
||||||
|
cachedWebfingers: {},
|
||||||
fromDomain: str, projectVersion: str) -> {}:
|
fromDomain: str, projectVersion: str) -> {}:
|
||||||
if not session:
|
if not session:
|
||||||
print('WARN: No session specified for webfingerHandle')
|
print('WARN: No session specified for webfingerHandle')
|
||||||
|
@ -58,7 +57,8 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
|
||||||
# wfPort=int(wfPortStr)
|
# wfPort=int(wfPortStr)
|
||||||
# if wfPort==80 or wfPort==443:
|
# if wfPort==80 or wfPort==443:
|
||||||
wfDomain = wfDomain.split(':')[0]
|
wfDomain = wfDomain.split(':')[0]
|
||||||
wf=getWebfingerFromCache(nickname+'@'+wfDomain,cachedWebfingers)
|
wf = getWebfingerFromCache(nickname + '@' + wfDomain,
|
||||||
|
cachedWebfingers)
|
||||||
if wf:
|
if wf:
|
||||||
return wf
|
return wf
|
||||||
url = '{}://{}/.well-known/webfinger'.format(httpPrefix, domain)
|
url = '{}://{}/.well-known/webfinger'.format(httpPrefix, domain)
|
||||||
|
@ -69,7 +69,9 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
|
||||||
'Accept': 'application/jrd+json'
|
'Accept': 'application/jrd+json'
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
result=getJson(session,url,hdr,par,projectVersion,httpPrefix,fromDomain)
|
result = \
|
||||||
|
getJson(session, url, hdr, par, projectVersion,
|
||||||
|
httpPrefix, fromDomain)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Unable to webfinger " + url)
|
print("Unable to webfinger " + url)
|
||||||
print('nickname: ' + str(nickname))
|
print('nickname: ' + str(nickname))
|
||||||
|
@ -78,20 +80,26 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
|
||||||
print('params: ' + str(par))
|
print('params: ' + str(par))
|
||||||
print(e)
|
print(e)
|
||||||
return None
|
return None
|
||||||
storeWebfingerInCache(nickname+'@'+wfDomain,result,cachedWebfingers)
|
storeWebfingerInCache(nickname + '@' + wfDomain,
|
||||||
|
result, cachedWebfingers)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def generateMagicKey(publicKeyPem) -> str:
|
def generateMagicKey(publicKeyPem) -> str:
|
||||||
"""See magic_key method in
|
"""See magic_key method in
|
||||||
https://github.com/tootsuite/mastodon/blob/707ddf7808f90e3ab042d7642d368c2ce8e95e6f/app/models/account.rb
|
https://github.com/tootsuite/mastodon/blob/
|
||||||
|
707ddf7808f90e3ab042d7642d368c2ce8e95e6f/app/models/account.rb
|
||||||
"""
|
"""
|
||||||
privkey = RSA.importKey(publicKeyPem)
|
privkey = RSA.importKey(publicKeyPem)
|
||||||
mod=base64.urlsafe_b64encode(number.long_to_bytes(privkey.n)).decode("utf-8")
|
modBytes = number.long_to_bytes(privkey.n)
|
||||||
pubexp=base64.urlsafe_b64encode(number.long_to_bytes(privkey.e)).decode("utf-8")
|
mod = base64.urlsafe_b64encode(modBytes).decode("utf-8")
|
||||||
|
expBytes = number.long_to_bytes(privkey.e)
|
||||||
|
pubexp = base64.urlsafe_b64encode(expBytes).decode("utf-8")
|
||||||
return f"data:application/magic-public-key,RSA.{mod}.{pubexp}"
|
return f"data:application/magic-public-key,RSA.{mod}.{pubexp}"
|
||||||
|
|
||||||
def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \
|
|
||||||
wfJson: {}) -> bool:
|
def storeWebfingerEndpoint(nickname: str, domain: str, port: int,
|
||||||
|
baseDir: str, wfJson: {}) -> bool:
|
||||||
"""Stores webfinger endpoint for a user to a file
|
"""Stores webfinger endpoint for a user to a file
|
||||||
"""
|
"""
|
||||||
originalDomain = domain
|
originalDomain = domain
|
||||||
|
@ -111,7 +119,8 @@ def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \
|
||||||
saveJson(wfJson, filename)
|
saveJson(wfJson, filename)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
|
|
||||||
|
def createWebfingerEndpoint(nickname: str, domain: str, port: int,
|
||||||
httpPrefix: str, publicKeyPem) -> {}:
|
httpPrefix: str, publicKeyPem) -> {}:
|
||||||
"""Creates a webfinger endpoint for a user
|
"""Creates a webfinger endpoint for a user
|
||||||
"""
|
"""
|
||||||
|
@ -129,8 +138,10 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
|
||||||
personName = 'actor'
|
personName = 'actor'
|
||||||
personId = httpPrefix + "://" + domain + "/" + personName
|
personId = httpPrefix + "://" + domain + "/" + personName
|
||||||
subjectStr = "acct:" + originalDomain + "@" + originalDomain
|
subjectStr = "acct:" + originalDomain + "@" + originalDomain
|
||||||
profilePageHref=httpPrefix+'://'+domain+'/about/more?instance_actor=true'
|
profilePageHref = httpPrefix + '://' + domain + \
|
||||||
|
'/about/more?instance_actor=true'
|
||||||
|
|
||||||
|
actor = httpPrefix + "://" + domain + "/users/" + nickname
|
||||||
account = {
|
account = {
|
||||||
"aliases": [
|
"aliases": [
|
||||||
httpPrefix + "://" + domain + "/@" + personName,
|
httpPrefix + "://" + domain + "/@" + personName,
|
||||||
|
@ -143,7 +154,7 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
|
||||||
"type": "text/html"
|
"type": "text/html"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"href": httpPrefix+"://"+domain+"/users/"+nickname+".atom",
|
"href": actor + ".atom",
|
||||||
"rel": "http://schemas.google.com/g/2010#updates-from",
|
"rel": "http://schemas.google.com/g/2010#updates-from",
|
||||||
"type": "application/atom+xml"
|
"type": "application/atom+xml"
|
||||||
},
|
},
|
||||||
|
@ -161,6 +172,7 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
|
||||||
}
|
}
|
||||||
return account
|
return account
|
||||||
|
|
||||||
|
|
||||||
def webfingerNodeInfo(httpPrefix: str, domainFull: str) -> {}:
|
def webfingerNodeInfo(httpPrefix: str, domainFull: str) -> {}:
|
||||||
""" /.well-known/nodeinfo endpoint
|
""" /.well-known/nodeinfo endpoint
|
||||||
"""
|
"""
|
||||||
|
@ -174,6 +186,7 @@ def webfingerNodeInfo(httpPrefix: str,domainFull: str) -> {}:
|
||||||
}
|
}
|
||||||
return nodeinfo
|
return nodeinfo
|
||||||
|
|
||||||
|
|
||||||
def webfingerMeta(httpPrefix: str, domainFull: str) -> str:
|
def webfingerMeta(httpPrefix: str, domainFull: str) -> str:
|
||||||
"""Return /.well-known/host-meta
|
"""Return /.well-known/host-meta
|
||||||
"""
|
"""
|
||||||
|
@ -184,14 +197,16 @@ def webfingerMeta(httpPrefix: str,domainFull: str) -> str:
|
||||||
metaStr += "<hm:Host>" + domainFull + "</hm:Host>"
|
metaStr += "<hm:Host>" + domainFull + "</hm:Host>"
|
||||||
metaStr += ""
|
metaStr += ""
|
||||||
metaStr += "<Link rel=’lrdd’"
|
metaStr += "<Link rel=’lrdd’"
|
||||||
metaStr+=" template=’"+httpPrefix+"://"+domainFull+"/describe?uri={uri}'>"
|
metaStr += " template=’" + httpPrefix + "://" + domainFull + \
|
||||||
|
"/describe?uri={uri}'>"
|
||||||
metaStr += " <Title>Resource Descriptor</Title>"
|
metaStr += " <Title>Resource Descriptor</Title>"
|
||||||
metaStr += " </Link>"
|
metaStr += " </Link>"
|
||||||
metaStr += "</XRD>"
|
metaStr += "</XRD>"
|
||||||
return metaStr
|
return metaStr
|
||||||
|
|
||||||
def webfingerLookup(path: str,baseDir: str, \
|
|
||||||
domain: str,onionDomain: str, \
|
def webfingerLookup(path: str, baseDir: str,
|
||||||
|
domain: str, onionDomain: str,
|
||||||
port: int, debug: bool) -> {}:
|
port: int, debug: bool) -> {}:
|
||||||
"""Lookup the webfinger endpoint for an account
|
"""Lookup the webfinger endpoint for an account
|
||||||
"""
|
"""
|
||||||
|
@ -204,7 +219,9 @@ def webfingerLookup(path: str,baseDir: str, \
|
||||||
print('DEBUG: WEBFINGER handle ' + handle)
|
print('DEBUG: WEBFINGER handle ' + handle)
|
||||||
else:
|
else:
|
||||||
if 'resource=acct%3A' in path:
|
if 'resource=acct%3A' in path:
|
||||||
handle=path.split('resource=acct%3A')[1].replace('%40','@',1).replace('%3A',':',1).strip()
|
handle = path.split('resource=acct%3A')[1]
|
||||||
|
handle = handle.replace('%40', '@', 1)
|
||||||
|
handle = handle.replace('%3A', ':', 1).strip()
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: WEBFINGER handle ' + handle)
|
print('DEBUG: WEBFINGER handle ' + handle)
|
||||||
if not handle:
|
if not handle:
|
||||||
|
|
Loading…
Reference in New Issue