Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5
feat(platform): add organization parameter to OAuth authorization redirect#550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -206,6 +206,10 @@ def profile_edit_url(self) -> str: | ||
| ] | ||
| client_id_interactive: Annotated[str, Field(description="OAuth client ID for interactive flows")] | ||
| organization_id: Annotated[ | ||
| str | None, Field(description="Optional Auth0 organization ID parameter for the /authorize OAuth endpoint") | ||
| ] = None | ||
santi698 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. santi698 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| @computed_field # type: ignore[prop-decorator] | ||
| @property | ||
| def tenant_domain(self) -> str: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -61,6 +61,7 @@ | ||
| settings.auth_retry_attempts = 3 | ||
| settings.auth_jwk_set_cache_ttl = 300 | ||
| settings.refresh_token = None | ||
| settings.organization_id = None | ||
| mock_settings.return_value = settings | ||
| yield mock_settings | ||
| @@ -339,25 +340,22 @@ | ||
| class TestAuthorizationCodeFlow: | ||
| """Test cases for the authorization code flow with PKCE.""" | ||
| @pytest.mark.unit | ||
| @staticmethod | ||
| def test_perform_authorization_code_flow_success(record_property, mock_settings) -> None: | ||
| """Test successful authorization code flow with PKCE.""" | ||
| record_property("tested-item-id", "SPEC-PLATFORM-SERVICE") | ||
| # Mock OAuth session | ||
| def _run_pkce_flow() -> tuple[str | None, MagicMock]: | ||
| """Set up common PKCE flow mocks, run the flow, and return (token, mock_session). | ||
| Returns: | ||
| tuple: The returned token and the OAuth2Session mock for further assertions. | ||
| """ | ||
| mock_session = MagicMock(spec=OAuth2Session) | ||
| mock_session.authorization_url.return_value = ("https://test.auth/authorize?code_challenge=abc", None) | ||
| mock_session.fetch_token.return_value = {"access_token": "pkce.token"} | ||
| # Mock HTTP server | ||
| mock_server = MagicMock() | ||
| # Setup mocks for the redirect URI parsing | ||
| mock_redirect_parsed = MagicMock() | ||
| mock_redirect_parsed.hostname = "localhost" | ||
| mock_redirect_parsed.port = 8000 | ||
| # Create a custom HTTPServer mock implementation that simulates a callback | ||
| class MockHTTPServer: | ||
| def __init__(self, *args, **kwargs) -> None: | ||
| pass | ||
| @@ -365,10 +363,9 @@ | ||
| def __enter__(self) -> MagicMock: | ||
| return mock_server | ||
| def __exit__(self, *args) -> None: | ||
Check failure on line 366 in tests/aignostics/platform/authentication_test.py
| ||
| pass | ||
| # Create a mock for the auth result | ||
| mock_auth_result = MagicMock() | ||
| mock_auth_result.token = "pkce.token" # noqa: S105 - Test credential | ||
| mock_auth_result.error = None | ||
| @@ -379,20 +376,19 @@ | ||
| patch("urllib.parse.urlparse", return_value=mock_redirect_parsed), | ||
| patch("aignostics.platform._authentication.AuthenticationResult", return_value=mock_auth_result), | ||
| ): | ||
| # Simulate a successful server response by making handle_request set the token | ||
| def handle_request_side_effect(): | ||
| # This simulates what the HTTP handler would do on success | ||
| mock_auth_result.token = "pkce.token" # noqa: S105 - Test credential | ||
| mock_server.handle_request.side_effect = handle_request_side_effect | ||
| # Call the function under test | ||
| mock_server.handle_request.side_effect = lambda: None | ||
| token = _perform_authorization_code_with_pkce_flow() | ||
santi698 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # Assertions | ||
| assert token == "pkce.token" # noqa: S105 - Test credential | ||
| mock_server.handle_request.assert_called_once() | ||
| mock_session.authorization_url.assert_called_once() | ||
| return token, mock_session | ||
| @pytest.mark.unit | ||
| @staticmethod | ||
| def test_perform_authorization_code_flow_success(record_property, mock_settings) -> None: | ||
| """Test successful authorization code flow with PKCE.""" | ||
| record_property("tested-item-id", "SPEC-PLATFORM-SERVICE") | ||
| token, mock_session = TestAuthorizationCodeFlow._run_pkce_flow() | ||
| assert token == "pkce.token" # noqa: S105 - Test credential | ||
| mock_session.authorization_url.assert_called_once() | ||
| @pytest.mark.unit | ||
| @staticmethod | ||
| @@ -466,6 +462,27 @@ | ||
| with pytest.raises(RuntimeError, match=AUTHENTICATION_FAILED): | ||
| _perform_authorization_code_with_pkce_flow() | ||
| @pytest.mark.unit | ||
| @staticmethod | ||
| def test_perform_authorization_code_flow_with_organization(record_property, mock_settings) -> None: | ||
| """Test authorization code flow includes organization parameter when set.""" | ||
| record_property("tested-item-id", "SPEC-PLATFORM-SERVICE") | ||
| mock_settings.return_value.organization_id = "test-org" | ||
| token, mock_session = TestAuthorizationCodeFlow._run_pkce_flow() | ||
| assert token == "pkce.token" # noqa: S105 | ||
| assert mock_session.authorization_url.call_args[1].get("organization") == "test-org" | ||
| @pytest.mark.unit | ||
| @staticmethod | ||
| def test_perform_authorization_code_flow_without_organization(record_property, mock_settings) -> None: | ||
| """Test authorization code flow omits organization parameter when unset.""" | ||
| record_property("tested-item-id", "SPEC-PLATFORM-SERVICE") | ||
| # organization_id is None by default in mock_settings fixture | ||
| assert mock_settings.return_value.organization_id is None | ||
| token, mock_session = TestAuthorizationCodeFlow._run_pkce_flow() | ||
| assert token == "pkce.token" # noqa: S105 | ||
| assert "organization" not in mock_session.authorization_url.call_args[1] | ||
| class TestDeviceFlow: | ||
| """Test cases for the device flow authentication.""" | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.