-
Notifications
You must be signed in to change notification settings - Fork 0
/
webfinger.py
85 lines (70 loc) · 2.8 KB
/
webfinger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import urllib, urllib2
from xml.etree import ElementTree
class WebFingerResult:
def __init__(self, ssl, api, auth, template):
self.ssl = ssl
self.api = api
self.auth = auth
self.template = template
if self.api == 'simple':
self.type = 'https://www.w3.org/community/unhosted/wiki/remotestorage-2011.10#simple'
elif self.api == 'WebDAV':
self.type = 'https://www.w3.org/community/unhosted/wiki/remotestorage-2011.10#webdav'
elif self.api == 'CouchDB':
self.type = 'https://www.w3.org/community/unhosted/wiki/remotestorage-2011.10#couchdb'
else:
raise WebFingerException('api not recognized')
self.properties = {
'access-methods': ['http://oauth.net/core/1.0/parameters/auth-header'],
'auth-methods': ['http://oauth.net/discovery/1.0/consumer-identity/static'],
'http://oauth.net/core/1.0/endpoint/request': self.auth
}
template_parts = self.template.split('{category}')
if template_parts[0][-1:] == '/':
self.href = template_parts[0][:-1]
else:
self.href = template_parts[0]
if len(template_parts) == 2 and template_parts[1] != '/':
self.properties['legacy_suffix'] = template_parts[1]
class WebFingerException(Exception):
pass
class WebFinger:
def __init__(self, identifier):
if identifier[:5]=='acct:':
identifier = identifier[5:]
self.user = identifier[:identifier.find('@')]
self.host = identifier[identifier.find('@')+1:]
self.opener = urllib2.build_opener(urllib2.HTTPRedirectHandler())
self.opener.addheaders = [('User-agent', 'python-webfinger')]
def host_meta(self, protocol):
hostmeta_url = "%s://%s/.well-known/host-meta"%(protocol,self.host)
connection = self.opener.open(hostmeta_url)
response = connection.read()
connection.close()
return response
def get_template(self, host_meta):
tree = ElementTree.fromstring(host_meta)
for link in tree.findall('{http://docs.oasis-open.org/ns/xri/xrd-1.0}Link'):
template = link.attrib.get('template')
if template:
return template
def get_data(self, template):
data_url = template.replace('{uri}', '%s')%("acct:%s@%s"%(self.user, self.host))
connection = self.opener.open(data_url)
response = connection.read()
connection.close()
tree = ElementTree.fromstring(response)
for link in tree.findall('{http://docs.oasis-open.org/ns/xri/xrd-1.0}Link'):
rel = link.attrib.get('rel')
if rel=='remoteStorage':
return {'template': link.attrib.get('template'), 'api': link.attrib.get('api'), 'auth': link.attrib.get('auth')}
def finger(self):
try:
host_meta = self.host_meta('https')
self.ssl = True
except (urllib2.HTTPError, urllib2.URLError):
host_meta = self.host_meta('http')
self.ssl = False
template = self.get_template(host_meta)
data = self.get_data(template)
return WebFingerResult(ssl=self.ssl, api=data['api'], auth=data['auth'], template=data['template'])