Skip to content

Conversation

@ealeksandrov
Copy link
Contributor

@ealeksandrov ealeksandrov commented Oct 28, 2022

Closes #7911, #7946.

Description

This PR adds WebKitViewController that uses access token for current user to authenticate them in the context of WKWebView.
Most of the code copied from WordPress-iOS.

Testing

Test on the device, as iOS Simulator has a bug around cookies management.

  1. Switch to WP.com store.
  2. Open products tab.
  3. Start the flow to create new product ("+" button in navbar on product list)
  4. Edit the title (or anything else) to have non-blank product.
  5. Tap the "Preview" button appeared in navbar.
  6. Confirm that webpage for draft product loads correctly.
  7. Open user profile and tap "Log out". Close the webview.
  8. Tap the "Preview" button in navbar again.
  9. Confirm that webpage for draft product again loads correctly, authenticated for current user.

Video

RPReplay_Final1667395250.mp4

  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@ealeksandrov ealeksandrov added feature: product details Related to adding or editing products, including Product Settings. status: feature-flagged Behind a feature flag. Milestone is not strongly held. labels Oct 28, 2022
@ealeksandrov ealeksandrov added this to the 11.1 milestone Oct 28, 2022
@ealeksandrov ealeksandrov self-assigned this Oct 28, 2022
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Oct 28, 2022

You can test the changes from this Pull Request by:
  • Clicking here or scanning the QR code below to access App Center
  • Then installing the build number pr7974-43e4dca on your iPhone

If you need access to App Center, please ask a maintainer to add you.

Base automatically changed from issue/7911-webview-authentication to trunk October 31, 2022 17:47
@ealeksandrov ealeksandrov force-pushed the issue/7946-authenticated-webkit-vc branch from a714aa2 to bfa55f8 Compare November 2, 2022 13:10
@ealeksandrov ealeksandrov removed their assignment Nov 2, 2022
@ealeksandrov ealeksandrov marked this pull request as ready for review November 2, 2022 13:34
@peril-woocommerce
Copy link

peril-woocommerce bot commented Nov 2, 2022

Warnings
⚠️ This PR is assigned to a milestone which is closing in less than 2 days Please, make sure to get it merged by then or assign it to a later expiring milestone
⚠️ PR has more than 500 lines of code changing. Consider splitting into smaller PRs if possible.

Generated by 🚫 dangerJS

let button = UIBarButtonItem(image: .gridicon(.cross), style: .plain, target: self, action: #selector(WebKitViewController.close))
button.title = NSLocalizedString("webKit.button.dismiss", value: "Dismiss", comment: "Verb. Dismiss the web view screen.")
return button
}()
Copy link
Contributor Author

@ealeksandrov ealeksandrov Nov 2, 2022

Choose a reason for hiding this comment

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

While we don't need toolbar on the WebView yet (it's disabled by secureInteraction option), I decided to keep the (almost) full functionality. We can improve the preview feature by adding "device" button, already supported on WP-iOS with PreviewWebKitViewController subclass:

mobile desktop
Simulator Simulator

@rachelmcr
Copy link
Contributor

I tested this on my WP.com test store but unfortunately the preview isn't working for me there. I'm getting redirected to the WP.com login screen:

Should this be working for all WP.com-hosted sites, including sites with custom domains? (The domain is the big difference I see with my test store.)

@ealeksandrov
Copy link
Contributor Author

ealeksandrov commented Nov 2, 2022

@rachelmcr can you try the flow on the device? I have the same issue with simulator, but never on the device. Sorry I forgot to mention it in the description.

For the reference, I've traced the issue to these lines:

let responseCookies = self.cookies(from: response, loginURL: url)
let cookies = (session.configuration.httpCookieStorage?.cookies ?? [HTTPCookie]()) + responseCookies

On the device this code correctly combines response cookies from 2 requests.
On simulator it shows second request cookies in both objects:

  • previous requests cookie storage: session.configuration.httpCookieStorage?.cookies (auth cookie is here)
  • extraction of the cookies from the latest request: self.cookies(from: response, ...)

From the proxy log there is no difference in networking requests/responses.

@rachelmcr rachelmcr self-assigned this Nov 2, 2022
@rachelmcr
Copy link
Contributor

rachelmcr commented Nov 2, 2022

Aha, thanks! I ran into an odd issue on my device at first, where the Site.loginURL was an empty string and that resulted in the web view being blank:

(I lost track of the specific console error when that happened, but it was a problem with the web view trying to load the request ?redirect_to=https://teststore.rachel-test-domain.com/?post_type%3Dproduct%26p%3D3955%26preview%3Dtrue.)

But when I relaunched the app that seemed to correct itself, and now the previews are working consistently. Not sure if that's something worth digging into further, but thought I'd mention it.

I'll finish reviewing the code now!

Copy link
Contributor

@rachelmcr rachelmcr left a comment

Choose a reason for hiding this comment

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

After I resolved the issues mentioned above, everything tested well here! 👍

Given the amount of code here that's the same between WPiOS and WCiOS, do you think it's worth refactoring it into a shared repo (maybe the WordPressUI pod)? (Not necessarily right now, but I'm thinking about future maintenance.)

@ealeksandrov
Copy link
Contributor Author

ealeksandrov commented Nov 3, 2022

odd issue on my device at first, where the Site.loginURL was an empty string

It happens when the old Site object is loaded from Core Data, where the loginURL is missing:

Do you think hiding "preview" button for empty loginURL is reasonable? After minor migration period (1 app restart?) it will be filled in for everyone. Another approach is to force an API call to refresh the data, but I don't like that.

Given the amount of code here that's the same between WPiOS and WCiOS, do you think it's worth refactoring it into a shared repo (maybe the WordPressUI pod)? (Not necessarily right now, but I'm thinking about future maintenance.)

Agree! I considered moving this to WPAuthenticator library (also WP app comment), but it will require refactoring of both client apps and migration of additional dependencies. We can do it after we finish and validate this feature. Maybe we'll reuse it in other parts of the app and will have additional requirements by that time.

@rachelmcr
Copy link
Contributor

Do you think hiding "preview" button for empty loginURL is reasonable? After minor migration period (1 app restart?) it will be filled in for everyone. Another approach is to force an API call to refresh the data, but I don't like that.

Agreed, I think just checking for an empty loginURL makes sense. Forcing a refresh seems like overkill, especially since it should be resolved fairly quickly.

I considered moving this to WPAuthenticator library (also WP app comment), but it will require refactoring of both client apps and migration of additional dependencies. We can do it after we finish and validate this feature.

Sounds good! I missed that WP comment before but it sums up the effort pretty well. :)

@ealeksandrov
Copy link
Contributor Author

Added a check in af2e711 + unit test in 43e4dca.

@ealeksandrov ealeksandrov merged commit 587ff48 into trunk Nov 3, 2022
@ealeksandrov ealeksandrov deleted the issue/7946-authenticated-webkit-vc branch November 3, 2022 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: product details Related to adding or editing products, including Product Settings. status: feature-flagged Behind a feature flag. Milestone is not strongly held.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Product Preview: Webview Authentication (WP.com hosted sites)

4 participants