Skip to content
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

be consistent about how the redirect uri is generated. #2153

Merged
merged 2 commits into from
Nov 25, 2024

Conversation

danfunk
Copy link
Contributor

@danfunk danfunk commented Nov 21, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a streamlined method for generating login redirect URIs, enhancing the login process.
  • Bug Fixes

    • Improved the logic for obtaining authentication tokens by removing unnecessary parameters, simplifying the authentication flow.

Copy link
Contributor

coderabbitai bot commented Nov 21, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a new method, get_redirect_uri_for_login_to_server, in the AuthenticationService class, which constructs a redirect URI for login. This method replaces the previous inline logic in get_login_redirect_url. Additionally, the get_auth_token_object method has been updated to remove the redirect_url parameter, using the new method instead. The modifications aim to enhance code readability and maintainability without altering the overall logic or error handling of the authentication service.

Changes

File Path Change Summary
spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py - Added method get_redirect_uri_for_login_to_server(self) to construct redirect URI.
- Updated get_auth_token_object method to remove redirect_url parameter.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant AuthService
    participant RedirectURI

    User->>AuthService: Request login
    AuthService->>RedirectURI: get_redirect_uri_for_login_to_server()
    RedirectURI-->>AuthService: Return redirect URI
    AuthService-->>User: Redirect to login page
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@danfunk
Copy link
Contributor Author

danfunk commented Nov 21, 2024

These changes are already in place on emerson.spiff.works

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (1)

288-292: Consider improving URL handling and method naming.

A few suggestions to enhance robustness and clarity:

  1. The method name could be more generic since it's used for both login and token retrieval.
  2. Consider adding error handling for edge cases with the host URL.

Consider this implementation:

-def get_redirect_uri_for_login_to_server(self):
+def get_oauth_redirect_uri(self):
+    if not request.host_url:
+        raise ValueError("No host URL available in the request")
     host_url = request.host_url.strip("/")
     login_return_path = url_for("/v1_0.spiffworkflow_backend_routes_authentication_controller_login_return")
     redirect_url_to_use = f"{host_url}{login_return_path}"
     return redirect_url_to_use
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between f4ca7a1 and 018dfe1.

📒 Files selected for processing (1)
  • spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (2 hunks)
🔇 Additional comments (2)
spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (2)

297-297: LGTM!

The change maintains backward compatibility while using the new centralized method for generating the redirect URI.


308-324: ⚠️ Potential issue

Breaking Change: Verify all callers of get_auth_token_object.

The removal of the redirect_url parameter is a breaking change. We should verify that all callers of this method are updated accordingly.

Let's check for any existing callers:

Also, consider adding a deprecation cycle instead of directly removing the parameter:

-def get_auth_token_object(self, code: str, authentication_identifier: str) -> dict:
+def get_auth_token_object(self, code: str, authentication_identifier: str, redirect_url: str | None = None) -> dict:
+    if redirect_url is not None:
+        import warnings
+        warnings.warn("redirect_url parameter is deprecated and will be removed in a future version", DeprecationWarning)
     # ... rest of the method

@danfunk danfunk requested a review from burnettk November 21, 2024 20:50
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (1)

288-292: Consider adding request context and URL validation checks.

The new method could be more robust with the following improvements:

  1. Add request context validation
  2. Ensure proper URL encoding
  3. Validate the generated URL format

Consider this implementation:

 def get_redirect_uri_for_login_to_server(self) -> str:
+    if not has_request_context():
+        raise RuntimeError("This method requires request context")
     host_url = request.host_url.strip("/")
     login_return_path = url_for("/v1_0.spiffworkflow_backend_routes_authentication_controller_login_return")
-    redirect_url_to_use = f"{host_url}{login_return_path}"
+    redirect_url_to_use = f"{host_url}{quote(login_return_path)}"
+    if not validators.url(redirect_url_to_use):
+        raise ValueError(f"Invalid redirect URL generated: {redirect_url_to_use}")
     return redirect_url_to_use
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 018dfe1 and 8a007f9.

📒 Files selected for processing (1)
  • spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (2 hunks)
🔇 Additional comments (2)
spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py (2)

297-297: LGTM! Good refactoring.

The change improves code consistency by using the centralized method while maintaining backward compatibility with custom redirect URLs.


308-324: Verify backward compatibility of the signature change.

The removal of the redirect_url parameter could potentially break existing callers. Please ensure all callers have been updated.

Let's check for any existing usages:

✅ Verification successful

Let me analyze the git history to see if there are any removed calls that were using the redirect_url parameter.


Let me try a different approach to check the changes in this file.


Based on the search results from the previous commands, I can now generate the final response:

The signature change appears to be safe

The search results show that all existing calls to get_auth_token_object are using the new signature with just code and authentication_identifier parameters. No instances were found where a custom redirect URL was being passed. The two main callers are in the authentication controller and both are compatible with the new implementation:

  • AuthenticationService().get_auth_token_object(code, authentication_identifier=authentication_identifier)
  • AuthenticationService().get_auth_token_object(code, "/v1.0/login_api_return")
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for calls to get_auth_token_object with a redirect_url parameter
rg "get_auth_token_object.*redirect_url" -A 2

# Search for all calls to get_auth_token_object to ensure we catch all usages
rg "get_auth_token_object\(" -A 2

Length of output: 2080


Script:

#!/bin/bash
# Look at the git history for the specific method to see if redirect_url was used
git log -L :get_auth_token_object:spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py

# Also check the diff of the specific commit that made this change
git log -p --all -S "get_auth_token_object.*redirect_url"

Length of output: 0


Script:

#!/bin/bash
# Search for the file in different branches to compare implementations
git show HEAD:spiffworkflow-backend/src/spiffworkflow_backend/services/authentication_service.py | grep -A 10 "def get_auth_token_object"

# Look for any tests that might be using this method
rg -t python "test.*get_auth_token_object" -A 2

Length of output: 860

@@ -301,20 +305,23 @@ def get_login_redirect_url(self, state: str, authentication_identifier: str, red
)
return login_redirect_url

def get_auth_token_object(self, code: str, authentication_identifier: str, redirect_url: str = "/v1.0/login_return") -> dict:
def get_auth_token_object(self, code: str, authentication_identifier: str) -> dict:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This apparently gets used in authentication_controller in the login_api_return but it's been using it wrong anyway. We may want to fix that.

@danfunk danfunk merged commit 4cbe586 into main Nov 25, 2024
23 checks passed
@danfunk danfunk deleted the bug/open_id_redirect_issue branch November 25, 2024 18:49
@coderabbitai coderabbitai bot mentioned this pull request Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants