flake8 format

main
Bob Mottram 2020-04-04 15:14:25 +01:00
parent b63bf2c72d
commit dc2386c583
1 changed files with 133 additions and 116 deletions

View File

@ -13,10 +13,7 @@ try:
except ImportError:
from Crypto.PublicKey import RSA
from Crypto.Util import number
import requests
import json
import os
import time
from session import getJson
from cache import storeWebfingerInCache
from cache import getWebfingerFromCache
@ -24,25 +21,27 @@ from utils import loadJson
from utils import loadJsonOnionify
from utils import saveJson
def parseHandle(handle: str) -> (str, str):
if '.' not in handle:
return None, None
handleStr = handle.replace('https://', '').replace('http://', '')
handleStr = handleStr.replace('dat://', '').replace('i2p://', '')
if '/@' in handle:
domain,nickname= \
handle.replace('https://','').replace('http://','').replace('dat://','').replace('i2p://','').split('/@')
domain, nickname = handleStr.split('/@')
else:
if '/users/' in handle:
domain,nickname= \
handle.replace('https://','').replace('http://','').replace('i2p://','').replace('dat://','').split('/users/')
domain, nickname = handleStr.split('/users/')
else:
if '@' in handle:
nickname, domain = handle.split('@')
else:
return None, None
return nickname, domain
def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
def webfingerHandle(session, handle: str, httpPrefix: str,
cachedWebfingers: {},
fromDomain: str, projectVersion: str) -> {}:
if not session:
print('WARN: No session specified for webfingerHandle')
@ -58,7 +57,8 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
# wfPort=int(wfPortStr)
# if wfPort==80 or wfPort==443:
wfDomain = wfDomain.split(':')[0]
wf=getWebfingerFromCache(nickname+'@'+wfDomain,cachedWebfingers)
wf = getWebfingerFromCache(nickname + '@' + wfDomain,
cachedWebfingers)
if wf:
return wf
url = '{}://{}/.well-known/webfinger'.format(httpPrefix, domain)
@ -69,7 +69,9 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
'Accept': 'application/jrd+json'
}
try:
result=getJson(session,url,hdr,par,projectVersion,httpPrefix,fromDomain)
result = \
getJson(session, url, hdr, par, projectVersion,
httpPrefix, fromDomain)
except Exception as e:
print("Unable to webfinger " + url)
print('nickname: ' + str(nickname))
@ -78,20 +80,26 @@ def webfingerHandle(session,handle: str,httpPrefix: str,cachedWebfingers: {}, \
print('params: ' + str(par))
print(e)
return None
storeWebfingerInCache(nickname+'@'+wfDomain,result,cachedWebfingers)
storeWebfingerInCache(nickname + '@' + wfDomain,
result, cachedWebfingers)
return result
def generateMagicKey(publicKeyPem) -> str:
"""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)
mod=base64.urlsafe_b64encode(number.long_to_bytes(privkey.n)).decode("utf-8")
pubexp=base64.urlsafe_b64encode(number.long_to_bytes(privkey.e)).decode("utf-8")
modBytes = number.long_to_bytes(privkey.n)
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}"
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
"""
originalDomain = domain
@ -111,7 +119,8 @@ def storeWebfingerEndpoint(nickname: str,domain: str,port: int,baseDir: str, \
saveJson(wfJson, filename)
return True
def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
def createWebfingerEndpoint(nickname: str, domain: str, port: int,
httpPrefix: str, publicKeyPem) -> {}:
"""Creates a webfinger endpoint for a user
"""
@ -129,8 +138,10 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
personName = 'actor'
personId = httpPrefix + "://" + domain + "/" + personName
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 = {
"aliases": [
httpPrefix + "://" + domain + "/@" + personName,
@ -143,7 +154,7 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
"type": "text/html"
},
{
"href": httpPrefix+"://"+domain+"/users/"+nickname+".atom",
"href": actor + ".atom",
"rel": "http://schemas.google.com/g/2010#updates-from",
"type": "application/atom+xml"
},
@ -161,6 +172,7 @@ def createWebfingerEndpoint(nickname: str,domain: str,port: int, \
}
return account
def webfingerNodeInfo(httpPrefix: str, domainFull: str) -> {}:
""" /.well-known/nodeinfo endpoint
"""
@ -174,6 +186,7 @@ def webfingerNodeInfo(httpPrefix: str,domainFull: str) -> {}:
}
return nodeinfo
def webfingerMeta(httpPrefix: str, domainFull: str) -> str:
"""Return /.well-known/host-meta
"""
@ -184,14 +197,16 @@ def webfingerMeta(httpPrefix: str,domainFull: str) -> str:
metaStr += "<hm:Host>" + domainFull + "</hm:Host>"
metaStr += ""
metaStr += "<Link rel=lrdd"
metaStr+=" template="+httpPrefix+"://"+domainFull+"/describe?uri={uri}'>"
metaStr += " template=" + httpPrefix + "://" + domainFull + \
"/describe?uri={uri}'>"
metaStr += " <Title>Resource Descriptor</Title>"
metaStr += " </Link>"
metaStr += "</XRD>"
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) -> {}:
"""Lookup the webfinger endpoint for an account
"""
@ -204,7 +219,9 @@ def webfingerLookup(path: str,baseDir: str, \
print('DEBUG: WEBFINGER handle ' + handle)
else:
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:
print('DEBUG: WEBFINGER handle ' + handle)
if not handle: