kopia lustrzana https://github.com/jcarbaugh/python-webfinger
add support for resource parameter
rodzic
4ef89a6405
commit
c643b17b66
42
webfinger.py
42
webfinger.py
|
@ -70,13 +70,20 @@ class WebFingerClient(object):
|
||||||
content = resp.content
|
content = resp.content
|
||||||
return content if raw else rd.loads(content, resp.headers.get('Content-Type'))
|
return content if raw else rd.loads(content, resp.headers.get('Content-Type'))
|
||||||
|
|
||||||
def hostmeta(self, secure=True):
|
def hostmeta(self, resource=None, rel=None, secure=True, raw=False):
|
||||||
""" Load host-meta resource from WebFinger provider.
|
""" Load host-meta resource from WebFinger provider.
|
||||||
Defaults to a secure (SSL) connection unless secure=False.
|
Defaults to a secure (SSL) connection unless secure=False.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
protocol = "https" if secure else "http"
|
protocol = "https" if secure else "http"
|
||||||
|
|
||||||
|
# create querystring params
|
||||||
|
params = {}
|
||||||
|
if resource:
|
||||||
|
params['resource'] = resource
|
||||||
|
if rel:
|
||||||
|
params['rel'] = rel
|
||||||
|
|
||||||
# use unofficial endpoint, if enabled
|
# use unofficial endpoint, if enabled
|
||||||
if not self._official and self._host in UNOFFICIAL_ENDPOINTS:
|
if not self._official and self._host in UNOFFICIAL_ENDPOINTS:
|
||||||
host = UNOFFICIAL_ENDPOINTS[self._host]
|
host = UNOFFICIAL_ENDPOINTS[self._host]
|
||||||
|
@ -87,20 +94,20 @@ class WebFingerClient(object):
|
||||||
url = "%s://%s/.well-known/host-meta" % (protocol, host)
|
url = "%s://%s/.well-known/host-meta" % (protocol, host)
|
||||||
|
|
||||||
# attempt to load from host-meta.json resource
|
# attempt to load from host-meta.json resource
|
||||||
resp = self._session.get("%s.json" % url)
|
resp = self._session.get("%s.json" % url, params=params)
|
||||||
|
|
||||||
if resp.status_code == 404:
|
if resp.status_code == 404:
|
||||||
# on failure, load from RFC 6415 host-meta resource
|
# on failure, load from RFC 6415 host-meta resource
|
||||||
resp = self._session.get(url, headers={"Accept": "application/json"}) # fall back to XRD
|
resp = self._session.get(url, params=params, headers={"Accept": "application/json"})
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
# raise error if request was not successful
|
# raise error if request was not successful
|
||||||
raise WebFingerException("host-meta not found")
|
raise WebFingerException("host-meta not found")
|
||||||
|
|
||||||
# load XRD or JRD based on HTTP response Content-Type header
|
# load XRD or JRD based on HTTP response Content-Type header
|
||||||
return rd.loads(resp.content, resp.headers.get('Content-Type'))
|
content = resp.content
|
||||||
|
return content if raw else rd.loads(content, resp.headers.get('Content-Type'))
|
||||||
|
|
||||||
def finger(self, username, rel=None):
|
def finger(self, username, resource=None, rel=None):
|
||||||
""" Perform a WebFinger query based on the given username.
|
""" Perform a WebFinger query based on the given username.
|
||||||
The `rel` parameter, if specified, will be passed to the provider,
|
The `rel` parameter, if specified, will be passed to the provider,
|
||||||
but be aware that providers are not required to implement the
|
but be aware that providers are not required to implement the
|
||||||
|
@ -108,13 +115,24 @@ class WebFingerClient(object):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hm = self.hostmeta() # attempt SSL host-meta retrieval
|
# attempt SSL host-meta retrieval
|
||||||
|
hm = self.hostmeta(resource=resource, rel=rel)
|
||||||
|
secure = True
|
||||||
except (requests.RequestException, requests.HTTPError):
|
except (requests.RequestException, requests.HTTPError):
|
||||||
hm = self.hostmeta(secure=False) # on failure, attempt non-SSL
|
# on failure, attempt non-SSL
|
||||||
|
hm = self.hostmeta(resource=resource, rel=rel, secure=False)
|
||||||
|
secure = False
|
||||||
|
|
||||||
if hm is None:
|
if hm is None:
|
||||||
raise WebFingerException("Unable to load or parse host-meta")
|
raise WebFingerException("Unable to load or parse host-meta")
|
||||||
|
|
||||||
|
if resource and hm.subject == resource:
|
||||||
|
|
||||||
|
# resource query worked, return LRDD response
|
||||||
|
return WebFingerResponse(hm, secure)
|
||||||
|
|
||||||
|
else:
|
||||||
|
|
||||||
# find template for LRDD document
|
# find template for LRDD document
|
||||||
template = hm.find_link(WEBFINGER_TYPES, attr='template')
|
template = hm.find_link(WEBFINGER_TYPES, attr='template')
|
||||||
secure = template.startswith('https://')
|
secure = template.startswith('https://')
|
||||||
|
@ -122,11 +140,11 @@ class WebFingerClient(object):
|
||||||
url = template.replace('{uri}',
|
url = template.replace('{uri}',
|
||||||
urllib.quote_plus('acct:%s@%s' % (username, self._host)))
|
urllib.quote_plus('acct:%s@%s' % (username, self._host)))
|
||||||
|
|
||||||
data = self.rd(url)
|
lrdd = self.rd(url)
|
||||||
return WebFingerResponse(data, secure)
|
return WebFingerResponse(lrdd, secure)
|
||||||
|
|
||||||
|
|
||||||
def finger(identifier, rel=None, timeout=None, official=False):
|
def finger(identifier, resource=None, rel=None, timeout=None, official=False):
|
||||||
""" Shortcut method for invoking WebFingerClient.
|
""" Shortcut method for invoking WebFingerClient.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@ -136,7 +154,7 @@ def finger(identifier, rel=None, timeout=None, official=False):
|
||||||
(username, host) = identifier.split('@')
|
(username, host) = identifier.split('@')
|
||||||
|
|
||||||
client = WebFingerClient(host, timeout=timeout, official=official)
|
client = WebFingerClient(host, timeout=timeout, official=official)
|
||||||
return client.finger(username, rel=rel)
|
return client.finger(username, resource=resource, rel=rel)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Ładowanie…
Reference in New Issue