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

Add Android Request Interception #30

Merged
merged 4 commits into from
Nov 1, 2024
Merged

Conversation

jkmassel
Copy link
Contributor

This initial implementation allows us to inject authentication at runtime if needed.

You can test it by making the demo app activity conform to GutenbergRequestInterceptor, then adding:


    override fun interceptRequest(request: WebResourceRequest): WebResourceRequest {

        if (request.url.host.isNullOrEmpty()) {
            return request
        }

        if (!request.url.host!!.contains("wordpress.com")) {
            return request
        }

        request.requestHeaders["Authorization"] =
            "Bearer [redacted – replace this string]"

        return request
    }

Once you've added a bearer token, copy the URL to an image on a private blog, and insert a new image block with that URL. The editor will now display it.

@jkmassel jkmassel added the bug Something isn't working label Oct 16, 2024
@jkmassel jkmassel requested a review from dcalhoun October 16, 2024 22:22
Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

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

I attempted testing this quickly by rebasing it atop #29 and using wordpress-mobile/WordPress-Android#21223 to build WordPress-Android with these changes.

When testing it this way—and without conforming to GutenbergRequestInterceptor—I experience CORS errors for all requests for the site's REST API. Below are screenshots of the network failures and one of the requests with and without these changes applied.

Screenshots

Request Failures Request Example
cors-failures Screenshot 2024-10-17 at 12 33 02

I didn't spend time debugging it aside from inspecting the requests. Maybe some of the headers are erroneously discarded by the interception logic? WDYT?

Hopefully #29 and wordpress-mobile/WordPress-Android#21223 merge later today, which will simplify further testing. Update: The referenced pull requests are now merged.

@dcalhoun dcalhoun force-pushed the add/android-request-interception branch from ac9d15f to 8ed393e Compare October 23, 2024 00:11
@dcalhoun
Copy link
Member

Friendly note: I rebased this atop the latest trunk branch to resolve merge conflicts to make testing easier.

* fix: Rely upon the WebView for completing non-intercepted requests

Previously, a manual request was created for _all_ requests. This was
unnecessary and resulted in header values being unexpectedly discarded.

* refactor: Remove unused imports

* refactor: Remove redundant semicolon

* refactor: Remove unused import

* refactor: Remove unused view parameter

This was added to mirror the `shouldInterceptRequest` method signature,
but is unnecessary complexity.

* refactor: Separate check and handle request methods

Mirror the iOS method structure.
The networking logic that required these dependencies was hoisted to the
host app.
Copy link
Member

@dcalhoun dcalhoun left a comment

Choose a reason for hiding this comment

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

Changes look sound, testing plan succeeds for me. 🚀

Noting that I removed (69b8e54) the previously added okhttp dependencies from this change set, as the networking logic was hoisted to the host app.

@dcalhoun dcalhoun merged commit d6fbfc7 into trunk Nov 1, 2024
8 checks passed
@dcalhoun dcalhoun deleted the add/android-request-interception branch November 1, 2024 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants