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

[bidi][java] Add authentication handlers #14334

Merged
merged 3 commits into from
Aug 6, 2024

Conversation

pujagani
Copy link
Contributor

@pujagani pujagani commented Aug 2, 2024

User description

Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.

Description

Related to #13993

Motivation and Context

Highi-level APIs for W3C BiDi for authentication

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • I have read the contributing document.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

PR Type

Enhancement, Tests


Description

  • Updated onAuthRequired method in Network module to return a listener ID.
  • Introduced a new Network interface for managing authentication handlers.
  • Implemented the Network interface in the RemoteNetwork class.
  • Added network management capabilities to the RemoteWebDriver class.
  • Added tests for the new authentication handler methods in WebNetworkTest.
  • Updated Bazel build configuration to include the new network package.

Changes walkthrough 📝

Relevant files
Enhancement
Network.java
Update `onAuthRequired` method to return listener ID         

java/src/org/openqa/selenium/bidi/module/Network.java

  • Modified onAuthRequired method to return a long value.
  • Updated method to handle authentication listener with return type.
  • +3/-3     
    Network.java
    Introduce `Network` interface for authentication handlers

    java/src/org/openqa/selenium/remote/Network.java

  • Added new interface Network with methods for authentication handlers.
  • +31/-0   
    RemoteNetwork.java
    Implement `Network` interface in `RemoteNetwork` class     

    java/src/org/openqa/selenium/remote/RemoteNetwork.java

  • Implemented Network interface in RemoteNetwork class.
  • Added methods to manage authentication handlers.
  • +79/-0   
    RemoteWebDriver.java
    Add network management to `RemoteWebDriver`                           

    java/src/org/openqa/selenium/remote/RemoteWebDriver.java

  • Added remoteNetwork field and network method to RemoteWebDriver.
  • +9/-0     
    Tests
    WebNetworkTest.java
    Add tests for network authentication handlers                       

    java/test/org/openqa/selenium/WebNetworkTest.java

  • Added tests for authentication handler methods in WebNetworkTest.
  • +91/-0   
    Configuration changes
    BUILD.bazel
    Update Bazel build file for network package                           

    java/src/org/openqa/selenium/remote/BUILD.bazel

    • Updated Bazel build file to include new network package.
    +1/-0     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link
    Contributor

    qodo-merge-pro bot commented Aug 2, 2024

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Key issues to review

    Return Type Change
    The method onAuthRequired has changed its return type from void to long. Ensure that this change is documented and that all calling code is updated to handle the new return type correctly.

    Error Handling
    The method removeAuthenticationHandler and clearAuthenticationHandlers lack error handling for cases where the id or intercept might not be valid or already removed. Consider adding error handling to prevent runtime exceptions.

    Copy link
    Contributor

    qodo-merge-pro bot commented Aug 2, 2024

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Possible bug
    Validate non-null parameters in addAuthenticationHandler

    Consider checking for null or invalid inputs in addAuthenticationHandler method to
    prevent potential issues when the usernameAndPassword is null.

    java/src/org/openqa/selenium/remote/RemoteNetwork.java [44]

     public long addAuthenticationHandler(UsernameAndPassword usernameAndPassword) {
    +    if (usernameAndPassword == null) {
    +        throw new IllegalArgumentException("Username and password should not be null.");
    +    }
         String intercept =
             network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED));
     
    • Apply this suggestion
    Suggestion importance[1-10]: 10

    Why: Adding a null check for usernameAndPassword in addAuthenticationHandler prevents potential null pointer exceptions, ensuring the method handles invalid inputs gracefully.

    10
    Enhancement
    Use ConcurrentHashMap to ensure thread safety

    Ensure thread safety for operations on authCallbackIdMap since it might be accessed
    by multiple threads. Consider using ConcurrentHashMap instead of HashMap for
    authCallbackIdMap.

    java/src/org/openqa/selenium/remote/RemoteNetwork.java [36]

    -private final Map<Long, String> authCallbackIdMap = new HashMap<>();
    +private final Map<Long, String> authCallbackIdMap = new ConcurrentHashMap<>();
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Ensuring thread safety by using ConcurrentHashMap is crucial for preventing concurrency issues, especially if authCallbackIdMap is accessed by multiple threads.

    9
    Add logging for removing intercepts and listeners

    When removing intercepts and listeners, consider logging these operations to help
    with debugging and maintaining the system.

    java/src/org/openqa/selenium/remote/RemoteNetwork.java [63-64]

    +logger.info("Removing intercept with ID: " + intercept);
     network.removeIntercept(intercept);
    +logger.info("Removing listener with ID: " + id);
     this.biDi.removeListener(id);
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding logging for removing intercepts and listeners can aid in debugging and maintaining the system, though it is a minor enhancement compared to functional changes.

    7
    Possible issue
    Add exception handling around addListener calls

    Consider handling the case where addListener might throw an exception due to invalid
    parameters or other issues. You can wrap the addListener calls in a try-catch block
    to manage such exceptions gracefully.

    java/src/org/openqa/selenium/bidi/module/Network.java [174-176]

    -return this.bidi.addListener(authRequired, consumer);
    -return this.bidi.addListener(browsingContextIds, authRequired, consumer);
    +try {
    +    return this.bidi.addListener(authRequired, consumer);
    +} catch (Exception e) {
    +    // handle exception
    +}
    +try {
    +    return this.bidi.addListener(browsingContextIds, authRequired, consumer);
    +} catch (Exception e) {
    +    // handle exception
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding exception handling around addListener calls is a good practice to manage potential runtime exceptions, improving the robustness of the code.

    8

    @pujagani pujagani merged commit 97eb16c into SeleniumHQ:trunk Aug 6, 2024
    28 of 30 checks passed
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant