This repository was archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwebfinger.py
More file actions
163 lines (141 loc) · 5.7 KB
/
webfinger.py
File metadata and controls
163 lines (141 loc) · 5.7 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
from urllib.parse import urlsplit, urlunsplit
from oidcmsg import oidc
from oidcmsg.exception import MissingRequiredAttribute
from oidcmsg.oauth2 import Message, ResponseMessage
from oidcmsg.oidc import JRD
from oidcservice.oidc import OIC_ISSUER, WF_URL
from oidcservice.service import Service
__author__ = 'Roland Hedberg'
logger = logging.getLogger(__name__)
SCHEME = 0
NETLOC = 1
PATH = 2
QUERY = 3
FRAGMENT = 4
class WebFinger(Service):
"""
Implements RFC 7033
"""
msg_type = Message
response_cls = JRD
error_msg = ResponseMessage
synchronous = True
service_name = 'webfinger'
http_method = 'GET'
response_body_type = 'json'
def __init__(self, service_context, client_authn_factory=None,
conf=None, rel='', **kwargs):
Service.__init__(self, service_context, client_authn_factory=client_authn_factory,
conf=conf, **kwargs)
self.rel = rel or OIC_ISSUER
def update_service_context(self, resp, key='', **kwargs):
try:
links = resp['links']
except KeyError:
raise MissingRequiredAttribute('links')
else:
for link in links:
if link['rel'] == self.rel:
_href = link['href']
try:
_http_allowed = self.get_conf_attr(
'allow', default={})['http_links']
except KeyError:
_http_allowed = False
if _href.startswith('http://') and not _http_allowed:
raise ValueError(
'http link not allowed ({})'.format(_href))
self.service_context.set('issuer', link['href'])
break
return resp
@staticmethod
def create_url(part, ignore):
res = []
for a in range(0, 5):
if a in ignore:
res.append('')
else:
res.append(part[a])
return urlunsplit(tuple(res))
def query(self, resource):
"""
Given a resource identifier find the domain specifier and then
construct the webfinger request. Implements
http://openid.net/specs/openid-connect-discovery-1_0.html#NormalizationSteps
:param resource:
"""
if resource[0] in ['=', '@', '!']: # Have no process for handling these
raise ValueError('Not allowed resource identifier')
try:
part = urlsplit(resource)
except Exception:
raise ValueError('Unparsable resource')
else:
if not part[SCHEME]:
if not part[NETLOC]:
_path = part[PATH]
if not part[QUERY] and not part[FRAGMENT]:
if '/' in _path or ':' in _path:
resource = "https://{}".format(resource)
part = urlsplit(resource)
authority = part[NETLOC]
else:
if '@' in _path:
authority = _path.split('@')[1]
else:
authority = _path
resource = 'acct:{}'.format(_path)
elif part[QUERY]:
resource = "https://{}?{}".format(_path, part[QUERY])
parts = urlsplit(resource)
authority = parts[NETLOC]
else:
resource = "https://{}".format(_path)
part = urlsplit(resource)
authority = part[NETLOC]
else:
raise ValueError('Missing netloc')
else:
_scheme = part[SCHEME]
if _scheme not in ['http', 'https', 'acct']:
# assume it to be a hostname port combo,
# eg. example.com:8080
resource = 'https://{}'.format(resource)
part = urlsplit(resource)
authority = part[NETLOC]
resource = self.create_url(part, [FRAGMENT])
elif _scheme in ['http', 'https'] and not part[NETLOC]:
raise ValueError(
'No authority part in the resource specification')
elif _scheme == 'acct':
_path = part[PATH]
for c in ['/', '?']:
_path = _path.split(c)[0]
if '@' in _path:
authority = _path.split('@')[1]
else:
raise ValueError(
'No authority part in the resource specification')
authority = authority.split('#')[0]
resource = self.create_url(part, [FRAGMENT])
else:
authority = part[NETLOC]
resource = self.create_url(part, [FRAGMENT])
location = WF_URL.format(authority)
return oidc.WebFingerRequest(
resource=resource, rel=OIC_ISSUER).request(location)
def get_request_parameters(self, request_args=None, **kwargs):
if request_args is None:
request_args = {}
try:
_resource = request_args['resource']
except KeyError:
try:
_resource = kwargs['resource']
except KeyError:
try:
_resource = self.service_context.config['resource']
except KeyError:
raise MissingRequiredAttribute('resource')
return {'url': self.query(_resource), 'method': 'GET'}