Skip to content

Commit 907e0ed

Browse files
authored
Implement API changes of Flask-JWT-Extended 2.0 (fixes #175) (#177)
* Implement API changes of Flask-JWT-Extended 2.0 (fixes #175) * Fix change of parameter names in flask 2.0 * Add print statement (I know...) * Another try to find out what's wrong * Fix "0 days" translation
1 parent 5c07086 commit 907e0ed

File tree

7 files changed

+22
-28
lines changed

7 files changed

+22
-28
lines changed

gramps_webapi/api/auth.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
from typing import Iterable
2525

2626
from flask import abort, current_app
27-
from flask_jwt_extended import (
28-
get_jwt_claims,
29-
verify_jwt_in_request,
30-
verify_jwt_refresh_token_in_request,
31-
)
27+
from flask_jwt_extended import get_jwt, verify_jwt_in_request
3228

3329

3430
def jwt_required_ifauth(func):
@@ -49,7 +45,7 @@ def jwt_refresh_token_required_ifauth(func):
4945
@wraps(func)
5046
def wrapper(*args, **kwargs):
5147
if not current_app.config.get("DISABLE_AUTH"):
52-
verify_jwt_refresh_token_in_request()
48+
verify_jwt_in_request(refresh=True)
5349
return func(*args, **kwargs)
5450

5551
return wrapper
@@ -59,7 +55,7 @@ def has_permissions(scope: Iterable[str]) -> bool:
5955
"""Check a set of permissions and return False if any are missing."""
6056
if current_app.config.get("DISABLE_AUTH"):
6157
return True
62-
claims = get_jwt_claims()
58+
claims = get_jwt()
6359
user_permissions = set(claims.get("permissions", []))
6460
required_permissions = set(scope)
6561
missing_permissions = required_permissions - user_permissions

gramps_webapi/api/file.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ def _check_path(self) -> None:
9393

9494
def send_file(self):
9595
"""Send media file to client."""
96-
return send_from_directory(
97-
directory=self.base_dir, filename=self.path_rel, mimetype=self.mime
98-
)
96+
return send_from_directory(self.base_dir, self.path_rel, mimetype=self.mime)
9997

10098
def send_cropped(self, x1: int, y1: int, x2: int, y2: int, square: bool = False):
10199
"""Send cropped image."""

gramps_webapi/api/resources/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def get_tokens(
4343
):
4444
"""Create access token (and refresh token if desired)."""
4545
access_token = create_access_token(
46-
identity=username, user_claims={"permissions": list(permissions)}
46+
identity=username, additional_claims={"permissions": list(permissions)}
4747
)
4848
if not include_refresh:
4949
return {"access_token": access_token}

gramps_webapi/api/resources/user.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import datetime
2424

2525
from flask import abort, current_app, jsonify, render_template
26-
from flask_jwt_extended import create_access_token, get_jwt_claims, get_jwt_identity
26+
from flask_jwt_extended import create_access_token, get_jwt, get_jwt_identity
2727
from flask_limiter import Limiter
2828
from flask_limiter.util import get_remote_address
2929
from webargs import fields
@@ -191,7 +191,7 @@ def post(self, user_name):
191191
identity=user_name,
192192
# the hash of the existing password is stored in the token in order
193193
# to make sure the rest token can only be used once
194-
user_claims={"old_hash": auth_provider.get_pwhash(user_name)},
194+
additional_claims={"old_hash": auth_provider.get_pwhash(user_name)},
195195
# password reset has to be triggered within 1h
196196
expires_delta=datetime.timedelta(hours=1),
197197
)
@@ -215,7 +215,7 @@ def post(self, args):
215215
abort(405)
216216
if args["new_password"] == "":
217217
abort(400)
218-
claims = get_jwt_claims()
218+
claims = get_jwt()
219219
username = get_jwt_identity()
220220
# the old PW hash is stored in the reset JWT to check if the token has
221221
# been used already
@@ -231,7 +231,7 @@ def get(self):
231231
if auth_provider is None:
232232
abort(405)
233233
username = get_jwt_identity()
234-
claims = get_jwt_claims()
234+
claims = get_jwt()
235235
# the old PW hash is stored in the reset JWT to check if the token has
236236
# been used already
237237
if claims["old_hash"] != auth_provider.get_pwhash(username):

gramps_webapi/api/resources/util.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ def get_sex_profile(person: Person) -> str:
110110

111111

112112
def get_event_participants_for_handle(
113-
db_handle: DbReadBase,
114-
handle: Handle,
115-
locale: GrampsLocale = glocale,
113+
db_handle: DbReadBase, handle: Handle, locale: GrampsLocale = glocale,
116114
) -> Dict:
117115
"""Get event participants given a handle."""
118116
result = {"people": [], "families": []}
@@ -366,7 +364,9 @@ def get_person_profile_for_object(
366364
if "all" in args or "age" in args:
367365
options.append("age")
368366
if birth_event is not None:
369-
birth["age"] = locale.translation.sgettext("0 days")
367+
birth["age"] = locale.translation.ngettext(
368+
"{number_of} day", "{number_of} days", 0
369+
).format(number_of=0)
370370
if death_event is not None:
371371
death["age"] = (
372372
Span(birth_event.date, death_event.date)
@@ -446,7 +446,9 @@ def get_family_profile_for_object(
446446
)
447447
if "all" in args or "span" in args:
448448
if marriage_event is not None:
449-
marriage["span"] = locale.translation.sgettext("0 days")
449+
marriage["span"] = locale.translation.ngettext(
450+
"{number_of} day", "{number_of} days", 0
451+
).format(number_of=0)
450452
if divorce_event is not None:
451453
divorce["span"] = (
452454
Span(marriage_event.date, divorce_event.date)
@@ -656,9 +658,7 @@ def get_soundex(
656658

657659

658660
def get_reference_profile_for_object(
659-
db_handle: DbReadBase,
660-
obj: GrampsObject,
661-
locale: GrampsLocale = glocale,
661+
db_handle: DbReadBase, obj: GrampsObject, locale: GrampsLocale = glocale,
662662
) -> Dict:
663663
"""Return reference profiles for an object."""
664664
profile = {}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"Flask-Caching",
3636
"Flask-Compress",
3737
"Flask-Cors",
38-
"Flask-JWT-Extended",
38+
"Flask-JWT-Extended>=4.2.1",
3939
"Flask-Limiter",
4040
"webargs",
4141
"SQLAlchemy",

tests/test_endpoints/test_families.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -540,11 +540,11 @@ def test_get_families_parameter_profile_expected_result(self):
540540
def test_get_families_parameter_profile_expected_result_with_locale(self):
541541
"""Test expected profile response for a locale."""
542542
rv = check_success(
543-
self, TEST_URL + "?page=1&keys=profile&profile=all&locale=de"
543+
self, TEST_URL + "?page=1&keys=profile&profile=all&locale=it"
544544
)
545-
self.assertEqual(rv[0]["profile"]["father"]["birth"]["age"], "0 Tage")
546-
self.assertEqual(rv[0]["profile"]["father"]["birth"]["type"], "Geburt")
547-
self.assertEqual(rv[0]["profile"]["relationship"], "Verheiratet")
545+
self.assertEqual(rv[0]["profile"]["father"]["birth"]["age"], "0 giorni")
546+
self.assertEqual(rv[0]["profile"]["father"]["birth"]["type"], "Nascita")
547+
self.assertEqual(rv[0]["profile"]["relationship"], "Sposati")
548548

549549
def test_get_families_parameter_backlinks_validate_semantics(self):
550550
"""Test invalid backlinks parameter and values."""

0 commit comments

Comments
 (0)