Skip to content

Commit 850df6b

Browse files
committed
style: apply consistent formatting across the jwt changes
1 parent ef9d845 commit 850df6b

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

reana_server/decorators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ def wrapper(*args, **kwargs):
3838
user = get_user_from_token(request.headers["X-Gitlab-Token"])
3939
elif "access_token" in request.args:
4040
user = get_user_from_token(request.args.get("access_token"))
41-
elif include_jwt and request.headers['Authorization']:
42-
user = _get_user_from_jwt(request.headers['Authorization'])
41+
elif include_jwt and request.headers["Authorization"]:
42+
user = _get_user_from_jwt(request.headers["Authorization"])
4343

4444
if not user:
4545
return jsonify(message="User not signed in"), 401

reana_server/utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,14 @@ def _get_user_from_invenio_user(id):
434434
raise ValueError("User access token revoked.")
435435
return user
436436

437+
437438
def _get_user_by_idpid(idp_id):
438439
user = Session.query(User).filter_by(idp_id=idp_id).one_or_none()
439440
if not user:
440441
raise ValueError("No users registered with this id")
441442
return user
442443

444+
443445
def _get_reana_yaml_from_gitlab(webhook_data, user_id):
444446
reana_yaml = "reana.yaml"
445447
if webhook_data["object_kind"] == "push":
@@ -676,7 +678,7 @@ def fetch_and_parse_jwk():
676678
Raises:
677679
ValueError: If JWK fetch fails or no matching key found
678680
"""
679-
if not hasattr(fetch_and_parse_jwk, '_cache'):
681+
if not hasattr(fetch_and_parse_jwk, "_cache"):
680682
fetch_and_parse_jwk._cache = None
681683

682684
if not fetch_and_parse_jwk._cache:
@@ -690,7 +692,6 @@ def fetch_and_parse_jwk():
690692
return jwks
691693

692694

693-
694695
def _get_user_from_jwt(header: str) -> User:
695696
"""Get user from JWT token.
696697
@@ -704,7 +705,7 @@ def _get_user_from_jwt(header: str) -> User:
704705
ValueError: If token is invalid or user not found
705706
"""
706707
try:
707-
if not header.startswith('Bearer '):
708+
if not header.startswith("Bearer "):
708709
raise ValueError("Invalid authorization header format")
709710

710711
token = header.split(" ")[1]
@@ -715,7 +716,7 @@ def _get_user_from_jwt(header: str) -> User:
715716
claims = jwt.decode(token, key_set)
716717
claims.validate()
717718

718-
idp_id = claims.get('sub')
719+
idp_id = claims.get("sub")
719720
if not idp_id:
720721
raise ValueError("Token missing subject claim")
721722

tests/test_decorators.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_signing_required_with_token(user0: User):
3232
assert code == 401
3333
assert error["message"] == "User has no active tokens"
3434

35+
3536
def test_signin_required_with_jwt(user0: User):
3637
"""Test `signin_required` with JWT token authentication."""
3738
mock_endpoint = Mock(return_value=(jsonify(message="Success"), 200))
@@ -54,7 +55,10 @@ def test_signin_required_with_jwt(user0: User):
5455
# Should call the endpoint since authentication succeeded
5556
mock_endpoint.assert_called_once()
5657
assert code == 200
57-
assert json.loads(response.get_data(as_text=True))["message"] == "Success"
58+
assert (
59+
json.loads(response.get_data(as_text=True))["message"] == "Success"
60+
)
61+
5862

5963
def test_signin_required_with_invalid_jwt(user0: User):
6064
"""Test `signin_required` with invalid JWT token."""
@@ -70,12 +74,18 @@ def test_signin_required_with_invalid_jwt(user0: User):
7074
mock_request_context.__enter__.return_value = mock_request
7175

7276
with patch("reana_server.decorators.current_user", mock_current_user):
73-
with patch("reana_server.decorators._get_user_from_jwt", side_effect=ValueError("Invalid token")):
77+
with patch(
78+
"reana_server.decorators._get_user_from_jwt",
79+
side_effect=ValueError("Invalid token"),
80+
):
7481
with patch("reana_server.decorators.request", mock_request):
7582
decorated_endpoint = signin_required(include_jwt=True)(mock_endpoint)
7683
response, code = decorated_endpoint()
7784

7885
# Should not call the endpoint since authentication failed
7986
mock_endpoint.assert_not_called()
8087
assert code == 403
81-
assert json.loads(response.get_data(as_text=True))["message"] == "Invalid token"
88+
assert (
89+
json.loads(response.get_data(as_text=True))["message"]
90+
== "Invalid token"
91+
)

0 commit comments

Comments
 (0)