Skip to content

Commit 1fd8df8

Browse files
committed
Fix mypy type errors
1 parent ae6aed7 commit 1fd8df8

File tree

4 files changed

+15
-95
lines changed

4 files changed

+15
-95
lines changed

lib/galaxy/authnz/oidc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def auth_params(self, state=None):
5757
# Add PKCE parameters if enabled
5858
if self.PKCE_ENABLED:
5959
# Generate PKCE challenge
60-
code_verifier, code_challenge = generate_pkce_pair(code_length=96)
60+
code_verifier, code_challenge = generate_pkce_pair(96)
6161
params["code_challenge"] = code_challenge
6262
params["code_challenge_method"] = "S256"
6363
# Store verifier in session for later use

lib/galaxy/authnz/psa_authnz.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import time
4+
from typing import cast
45
from urllib.parse import quote
56

67
import jwt
@@ -20,6 +21,7 @@
2021
)
2122
from sqlalchemy import func
2223
from sqlalchemy.exc import IntegrityError
24+
from sqlalchemy.orm import Session
2325

2426
from galaxy.exceptions import MalformedContents
2527
from galaxy.model import (
@@ -117,8 +119,6 @@
117119
# Update the user record with any changed info from the auth service.
118120
"social_core.pipeline.user.user_details",
119121
"galaxy.authnz.psa_authnz.decode_access_token",
120-
# Set appropriate redirect URL based on context
121-
"galaxy.authnz.psa_authnz.set_redirect_url",
122122
)
123123

124124
DISCONNECT_PIPELINE = ("galaxy.authnz.psa_authnz.allowed_to_disconnect", "galaxy.authnz.psa_authnz.disconnect")
@@ -810,7 +810,10 @@ def associate_by_email_if_logged_in(
810810
return
811811

812812
# Check if an account with this email exists
813+
# sa_session is guaranteed to be set by on_the_fly_config() before pipeline runs
813814
sa_session = UserAuthnzToken.sa_session
815+
if sa_session is None:
816+
raise RuntimeError("sa_session must be set by on_the_fly_config before pipeline execution")
814817
existing_user = sa_session.query(User).where(func.lower(User.email) == email.lower()).first()
815818

816819
if user:
@@ -872,7 +875,10 @@ def check_user_creation_confirmation(
872875
# Check if there's an existing user with this email
873876
email = details.get("email")
874877
if email:
878+
# sa_session is guaranteed to be set by on_the_fly_config() before pipeline runs
875879
sa_session = UserAuthnzToken.sa_session
880+
if sa_session is None:
881+
raise RuntimeError("sa_session must be set by on_the_fly_config before pipeline execution")
876882
existing_user = sa_session.query(User).where(func.lower(User.email) == email.lower()).first()
877883

878884
# If no existing user, redirect to confirmation page
@@ -900,18 +906,3 @@ def check_user_creation_confirmation(
900906

901907
# Continue with user creation if confirmation is not required or user already exists
902908
return
903-
904-
905-
def set_redirect_url(strategy=None, backend=None, details=None, user=None, is_new=False, social=None, **kwargs):
906-
"""
907-
Placeholder pipeline step for redirect URL handling.
908-
909-
The actual redirect URL logic is handled in the callback() method
910-
based on the fixed_delegated_auth setting, where LOGIN_REDIRECT_URL
911-
is set before calling do_complete().
912-
913-
This step exists to maintain compatibility with the pipeline but
914-
doesn't need to do anything.
915-
"""
916-
# No action needed - redirect URL is set in callback() method
917-
return

lib/galaxy/model/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10882,7 +10882,7 @@ class UserAuthnzToken(Base, UserMixin, RepresentById):
1088210882
)
1088310883

1088410884
# This static property is set at: galaxy.authnz.psa_authnz.PSAAuthnz
10885-
sa_session = None
10885+
sa_session: ClassVar[Optional[Session]] = None
1088610886

1088710887
def __init__(self, provider, uid, extra_data=None, lifetime=None, assoc_type=None, user=None):
1088810888
self.provider = provider

test/unit/authnz/test_oidc_backends.py

Lines changed: 5 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
)
1515

1616
import pytest
17+
from social_core.utils import setting_name
1718

1819
from galaxy.authnz.cilogon import CILogonOpenIdConnect
1920
from galaxy.authnz.keycloak import KeycloakOpenIdConnect
21+
from galaxy.authnz.psa_authnz import (
22+
associate_by_email_if_logged_in,
23+
check_user_creation_confirmation,
24+
)
2025

2126

2227
class MockStrategy:
@@ -506,11 +511,6 @@ def test_callback_user_not_created_when_does_not_exist(self):
506511
they should be redirected to a confirmation page instead of having their
507512
account created immediately.
508513
"""
509-
from galaxy.authnz.psa_authnz import (
510-
check_user_creation_confirmation,
511-
setting_name,
512-
)
513-
514514
# Setup strategy with require_create_confirmation enabled
515515
strategy = MockStrategy(
516516
{
@@ -680,11 +680,6 @@ class TestFixedDelegatedAuth:
680680

681681
def test_fixed_delegated_auth_auto_associates_existing_user(self):
682682
"""Test that fixed_delegated_auth automatically associates with existing user."""
683-
from galaxy.authnz.psa_authnz import (
684-
associate_by_email_if_logged_in,
685-
setting_name,
686-
)
687-
688683
strategy = MockStrategy(
689684
{
690685
"FIXED_DELEGATED_AUTH": True,
@@ -724,11 +719,6 @@ def test_fixed_delegated_auth_auto_associates_existing_user(self):
724719

725720
def test_fixed_delegated_auth_continues_when_no_user(self):
726721
"""Test that fixed_delegated_auth continues to user creation when no user exists."""
727-
from galaxy.authnz.psa_authnz import (
728-
associate_by_email_if_logged_in,
729-
setting_name,
730-
)
731-
732722
strategy = MockStrategy(
733723
{
734724
"FIXED_DELEGATED_AUTH": True,
@@ -764,11 +754,6 @@ def test_fixed_delegated_auth_continues_when_no_user(self):
764754

765755
def test_without_fixed_delegated_auth_prompts_for_login(self):
766756
"""Test that without fixed_delegated_auth, users are prompted to log in."""
767-
from galaxy.authnz.psa_authnz import (
768-
associate_by_email_if_logged_in,
769-
setting_name,
770-
)
771-
772757
strategy = MockStrategy(
773758
{
774759
"FIXED_DELEGATED_AUTH": False,
@@ -809,61 +794,5 @@ def test_without_fixed_delegated_auth_prompts_for_login(self):
809794
assert "connect_external_email=" in result
810795

811796

812-
class TestRedirectURL:
813-
"""Test the set_redirect_url pipeline step."""
814-
815-
def test_fixed_delegated_auth_redirects_to_root(self):
816-
"""Test that fixed_delegated_auth redirects to root URL."""
817-
from galaxy.authnz.psa_authnz import (
818-
set_redirect_url,
819-
setting_name,
820-
)
821-
822-
strategy = MockStrategy(
823-
{
824-
"FIXED_DELEGATED_AUTH": True,
825-
setting_name("LOGIN_REDIRECT_URL"): "http://localhost:8080/",
826-
}
827-
)
828-
829-
backend = Mock()
830-
831-
set_redirect_url(
832-
strategy=strategy,
833-
backend=backend,
834-
details={},
835-
user=Mock(),
836-
)
837-
838-
# Should set redirect to root URL
839-
assert strategy.session.get("next") == "http://localhost:8080/"
840-
841-
def test_without_fixed_delegated_auth_redirects_to_external_ids(self):
842-
"""Test that without fixed_delegated_auth, redirect goes to user/external_ids."""
843-
from galaxy.authnz.psa_authnz import (
844-
set_redirect_url,
845-
setting_name,
846-
)
847-
848-
strategy = MockStrategy(
849-
{
850-
"FIXED_DELEGATED_AUTH": False,
851-
setting_name("LOGIN_REDIRECT_URL"): "http://localhost:8080/",
852-
}
853-
)
854-
855-
backend = Mock()
856-
857-
set_redirect_url(
858-
strategy=strategy,
859-
backend=backend,
860-
details={},
861-
user=Mock(),
862-
)
863-
864-
# Should set redirect to user/external_ids
865-
assert strategy.session.get("next") == "http://localhost:8080/user/external_ids"
866-
867-
868797
if __name__ == "__main__":
869798
pytest.main([__file__, "-v"])

0 commit comments

Comments
 (0)