Skip to content

Commit b7b51b6

Browse files
authored
Merge pull request #54 from virtualtam/refactor/jwt
Refactor JWT authentication to avoid depending on the unmaintained 'requests-jwt' library
2 parents 1748cdf + 63246c6 commit b7b51b6

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

setup.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ def get_package_metadata(attribute):
4040
],
4141
},
4242
install_requires=[
43-
'requests >= 2.10',
44-
'requests-jwt >= 0.4, < 0.5',
45-
'pyjwt == 1.7.1'
43+
'requests >= 2.25',
44+
'pyjwt == 2.0.1'
4645
],
4746
classifiers=[
4847
'Development Status :: 3 - Alpha',

shaarli_client/client/v1.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import time
44
from argparse import Action, ArgumentTypeError
55

6+
import jwt
67
import requests
7-
from requests_jwt import JWTAuth
88

99

1010
def check_positive_integer(value):
@@ -248,20 +248,29 @@ def _retrieve_http_params(cls, args):
248248

249249
def _request(self, method, endpoint, params, verify_certs=True):
250250
"""Send an HTTP request to this instance"""
251-
auth = JWTAuth(self.secret, alg='HS512', header_format='Bearer %s')
252-
auth.add_field('iat', lambda req: calendar.timegm(time.gmtime()))
251+
encoded_token = jwt.encode(
252+
{'iat': calendar.timegm(time.gmtime())},
253+
self.secret,
254+
algorithm='HS512',
255+
)
256+
headers = {'Authorization': 'Bearer %s' % encoded_token}
253257

254258
endpoint_uri = '%s/api/v%d/%s' % (self.uri, self.version, endpoint)
255259

256260
if method == 'GET':
257261
return requests.request(
258262
method,
259263
endpoint_uri,
260-
auth=auth,
264+
headers=headers,
261265
params=params,
262266
verify=verify_certs
263267
)
264-
return requests.request(method, endpoint_uri, auth=auth, json=params)
268+
return requests.request(
269+
method,
270+
endpoint_uri,
271+
headers=headers,
272+
json=params,
273+
)
265274

266275
def request(self, args):
267276
"""Send a parameterized request to this instance"""

tests/client/test_v1.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def test_get_info_uri(request):
119119
request.assert_called_once_with(
120120
'GET',
121121
'%s/api/v1/info' % SHAARLI_URL,
122-
auth=mock.ANY,
122+
headers=mock.ANY,
123123
verify=True,
124124
params={}
125125
)
@@ -145,7 +145,7 @@ def test_get_links_uri(request):
145145
request.assert_called_once_with(
146146
'GET',
147147
'%s/api/v1/links' % SHAARLI_URL,
148-
auth=mock.ANY,
148+
headers=mock.ANY,
149149
verify=True,
150150
params={}
151151
)
@@ -187,7 +187,7 @@ def test_post_links_uri(request):
187187
request.assert_called_once_with(
188188
'POST',
189189
'%s/api/v1/links' % SHAARLI_URL,
190-
auth=mock.ANY,
190+
headers=mock.ANY,
191191
json={}
192192
)
193193

@@ -229,7 +229,7 @@ def test_put_links_uri(request):
229229
request.assert_called_once_with(
230230
'PUT',
231231
'%s/api/v1/links/12' % SHAARLI_URL,
232-
auth=mock.ANY,
232+
headers=mock.ANY,
233233
json={}
234234
)
235235

@@ -288,7 +288,7 @@ def test_get_tags_uri(request):
288288
request.assert_called_once_with(
289289
'GET',
290290
'%s/api/v1/tags' % SHAARLI_URL,
291-
auth=mock.ANY,
291+
headers=mock.ANY,
292292
verify=True,
293293
params={}
294294
)
@@ -301,7 +301,7 @@ def test_put_tags_uri(request):
301301
request.assert_called_once_with(
302302
'PUT',
303303
'%s/api/v1/tags/some-tag' % SHAARLI_URL,
304-
auth=mock.ANY,
304+
headers=mock.ANY,
305305
json={}
306306
)
307307

@@ -340,7 +340,7 @@ def test_delete_tags_uri(request):
340340
request.assert_called_once_with(
341341
'DELETE',
342342
'%s/api/v1/tags/some-tag' % SHAARLI_URL,
343-
auth=mock.ANY,
343+
headers=mock.ANY,
344344
json={}
345345
)
346346

0 commit comments

Comments
 (0)