-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_tokens.py
277 lines (229 loc) · 10.1 KB
/
test_tokens.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
""" Bearer token tests """
# pylint:disable=missing-docstring,too-many-statements
import json
import logging
import flask
import pytest
import werkzeug.exceptions as http_error
from publ import tokens, user
from . import PublMock
LOGGER = logging.getLogger(__name__)
def test_token_flow(mocker):
app = flask.Flask(__name__)
app.secret_key = 'random bytes'
mock_time = mocker.patch('time.time')
with app.app_context():
mock_time.return_value = 100
token = tokens.get_token('somebody', 1600)
parsed = tokens.parse_token(token)
assert parsed['me'] == 'somebody'
assert 'scope' not in parsed
mock_time.return_value = 1800
with pytest.raises(http_error.Unauthorized):
parsed = tokens.parse_token(token)
token = tokens.get_token('someone', 1600, 'read')
parsed = tokens.parse_token(token)
assert parsed['me'] == 'someone'
assert parsed['scope'] == 'read'
with pytest.raises(http_error.Unauthorized):
parsed = tokens.parse_token('kwijibo')
def test_request():
app = flask.Flask(__name__)
with app.test_request_context('/foo'):
tokens.request(user.User('moo'))
assert 'needs_auth' not in flask.g
with app.test_request_context('/bar'):
tokens.request(None)
assert flask.g.needs_auth
def test_ticketauth_flow(requests_mock):
import authl.disposition
app = PublMock()
app.add_url_rule('/_tokens', 'tokens', tokens.indieauth_endpoint,
methods=['GET', 'POST'])
stash = {}
def ticket_endpoint(request, _):
import urllib.parse
args = urllib.parse.parse_qs(request.text)
assert 'subject' in args
assert 'ticket' in args
assert 'resource' in args
stash['ticket'] = args['ticket']
with app.test_client() as client:
req = client.post(token_endpoint, data={
'grant_type': 'ticket',
'ticket': args['ticket']
})
token = json.loads(req.data)
assert 'access_token' in token
assert token['token_type'].lower() == 'bearer'
stash['response'] = token
with app.test_request_context('/'):
token_endpoint = flask.url_for('tokens')
foo_tickets = requests_mock.get('https://foo.example/', text='''
<link rel="ticket_endpoint" href="https://foo.example/tickets">
<p class="h-card"><span class="p-name">boop</span></p>
''')
bar_tickets = requests_mock.get('https://bar.example/', text='''
<link rel="ticket_endpoint" href="https://foo.example/tickets">
''')
requests_mock.post('https://foo.example/tickets', text=ticket_endpoint)
# Ad-hoc request flow
with app.test_request_context('/bogus'):
request_url = flask.url_for('tokens', me='https://foo.example/')
with app.test_client() as client:
req = client.get(request_url)
LOGGER.info("Got ticket redemption response %d: %s",
req.status_code, req.data)
assert req.status_code == 202
assert req.data == b'Ticket sent'
assert foo_tickets.call_count == 1
assert stash['response']['token_type'].lower() == 'bearer'
assert stash['response']['me'] == 'https://foo.example/'
token = tokens.parse_token(stash['response']['access_token'])
assert token['me'] == 'https://foo.example/'
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["response"]["access_token"]}'
})
assert req.status_code == 200
assert req.headers['Content-Type'] == 'application/json'
verified = json.loads(req.data)
assert verified['me'] == 'https://foo.example/'
token_user = user.User(verified['me'])
assert token_user.profile['name'] == 'boop'
# Provisional request flow
with app.test_request_context('/bogus'):
request_url = flask.url_for('tokens')
with app.test_client() as client:
req = client.post(request_url, data={'action': 'ticket',
'subject': 'https://foo.example'})
LOGGER.info("Got ticket redemption response %d: %s",
req.status_code, req.data)
assert req.status_code == 202
assert req.data == b'Ticket sent'
# should be cached from previous test
assert foo_tickets.call_count == 1
assert stash['response']['token_type'].lower() == 'bearer'
assert stash['response']['me'] == 'https://foo.example/'
token = tokens.parse_token(stash['response']['access_token'])
assert token['me'] == 'https://foo.example/'
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["response"]["access_token"]}'
})
assert req.status_code == 200
assert req.headers['Content-Type'] == 'application/json'
verified = json.loads(req.data)
assert verified['me'] == 'https://foo.example/'
# Login flow
stash.clear()
with app.test_request_context():
user.register(authl.disposition.Verified(
'https://bar.example/',
'/',
{'endpoints': {
'ticket_endpoint': 'https://foo.example/tickets'
}}))
assert bar_tickets.called # page still needs to be retrieved to get the profile
assert stash['response']['token_type'].lower() == 'bearer'
assert stash['response']['me'] == 'https://bar.example/'
token = tokens.parse_token(stash['response']['access_token'])
assert token['me'] == 'https://bar.example/'
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["response"]["access_token"]}'
})
assert req.status_code == 200
assert req.headers['Content-Type'] == 'application/json'
verified = json.loads(req.data)
assert verified['me'] == 'https://bar.example/'
# Attempt to redeem a token as if it were a ticket
with app.test_request_context():
req = client.post(token_endpoint, data={
'grant_type': 'ticket',
'ticket': stash['response']['access_token']})
assert req.status_code == 401
# Redeem the refresh_token
with app.test_client() as client:
req = client.post(token_endpoint, data={
'grant_type': 'refresh_token',
'refresh_token': stash['response']['refresh_token']
})
assert req.status_code == 200
assert req.headers['Content-Type'] == 'application/json'
refreshed = json.loads(req.data)
assert refreshed['me'] == 'https://bar.example/'
# Verify that redemption of a plain token fails
with app.test_client() as client:
req = client.post(token_endpoint, data={
'grant_type': 'refresh_token',
'refresh_token': stash['response']['access_token']
})
assert req.status_code == 401
# Verify that a ticket can't be used as a bearer token
with app.test_client() as client:
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["ticket"]}'
})
assert req.status_code == 401
# Verify that a refresh_token can't be used as a bearer token
with app.test_client() as client:
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["response"]["refresh_token"]}'
})
assert req.status_code == 401
def test_ticketauth_canonical(requests_mock):
"""
Ensure that rel="canonical" is being correctly respected on TicketAuth grants,
and that identity URLs are being properly canonicalized
"""
app = PublMock()
app.add_url_rule('/_tokens', 'tokens', tokens.indieauth_endpoint,
methods=['GET', 'POST'])
stash = {}
def ticket_endpoint(request, _):
import urllib.parse
args = urllib.parse.parse_qs(request.text)
assert 'subject' in args
assert 'ticket' in args
assert 'resource' in args
stash['ticket'] = args['ticket']
with app.test_client() as client:
req = client.post(token_endpoint, data={
'grant_type': 'ticket',
'ticket': args['ticket']
})
token = json.loads(req.data)
assert 'access_token' in token
assert token['token_type'].lower() == 'bearer'
stash['response'] = token
with app.test_request_context('/'):
token_endpoint = flask.url_for('tokens')
for scheme in ('http', 'https'):
requests_mock.get(f'{scheme}://canonical.ticketauth', text='''
<link rel="ticket_endpoint" href="https://foo.example/tickets">
<link rel="canonical" href="https://canonical.ticketAuth">
<p class="h-card"><span class="p-name">pachelbel</span></p>
''')
requests_mock.post('https://foo.example/tickets', text=ticket_endpoint)
def test_url(identity, match):
with app.test_request_context('/bogus'):
request_url = flask.url_for('tokens')
with app.test_client() as client:
req = client.post(request_url, data={'action': 'ticket',
'subject': identity})
LOGGER.info("Got ticket redemption response %d: %s",
req.status_code, req.data)
assert req.status_code == 202
assert req.data == b'Ticket sent'
assert stash['response']['token_type'].lower() == 'bearer'
assert stash['response']['me'] == match
token = tokens.parse_token(stash['response']['access_token'])
assert token['me'] == match
req = client.get(token_endpoint, headers={
'Authorization': f'Bearer {stash["response"]["access_token"]}'
})
assert req.status_code == 200
assert req.headers['Content-Type'] == 'application/json'
verified = json.loads(req.data)
assert verified['me'] == match
for url in ('http://canonical.ticketauth', 'https://canonical.ticketauth',
'http://Canonical.TicketAuth'):
test_url(url, 'https://canonical.ticketauth/')