Repository navigation
task: sign the demo apps into WordPress.com with a bearer token - #771
Conversation
Adds `make login-ios-app` and `make login-android-app` (wrapping `bin/demo-app-login.sh`), which launch the iOS demo app on a Simulator with `-wpcom-token`, or the Android demo app on an emulator with a `wpcom-token` intent extra. The app stores the token as an account at launch, so a WordPress.com site can be added with no OAuth application and no taps. The token is read from `WPCOM_TOKEN` or `~/.wpcom-token`, never the command line. Modelled on Automattic/wordpress-rs#1652. The demo apps keep one account per site and need the site's numeric ID, so the script also looks the site up from the WordPress.com REST API — the account's primary site, or `SITE=<domain>` — and passes its ID and host as `-wpcom-site-id` and `-wpcom-site-host`. The app replaces any account it already holds for that site, so running the command again picks up a new token. The format of the stored WordPress.com API root now comes from one helper per platform, because the site ID is parsed back out of it.
…mo apps "Add WordPress Site" started the OAuth flow for every WordPress.com site, which needs an OAuth application in `wp_com_oauth_credentials.json` even when the app already holds a token for the same user. When the entered site is on WordPress.com, look it up with each WordPress.com token the app has stored, using wordpress-rs's `sites.getSite`. The first token whose user has `edit_posts` on the site is stored as an account for it, with no login screen. Otherwise the OAuth flow runs as before. WordPress.com returns no `capabilities` for a site the token's user isn't a member of, so that lookup fails to decode and the site goes through OAuth as well.
XCFramework BuildThis PR's XCFramework is available for testing. Add the following to your .package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/771")Built from 43e4780 |
dcalhoun
left a comment
There was a problem hiding this comment.
Very cool. Testing worked well for me. I left a few inline suggestions, but none are blocking.
| if [[ -z "$device" ]]; then | ||
| if [[ "$platform" == ios ]]; then resolve_ios_device; else resolve_android_device; fi | ||
| fi |
There was a problem hiding this comment.
DEVICE/--device skips the booted check, so naming a shut-down simulator fails with CoreSimulator's raw code=405 … Shutdown. Boot it instead?
| if [[ -z "$device" ]]; then | |
| if [[ "$platform" == ios ]]; then resolve_ios_device; else resolve_android_device; fi | |
| fi | |
| if [[ -z "$device" ]]; then | |
| if [[ "$platform" == ios ]]; then resolve_ios_device; else resolve_android_device; fi | |
| elif [[ "$platform" == ios ]]; then | |
| # Boots the simulator if it isn't running, and waits until it's ready. | |
| xcrun simctl bootstatus "$device" -b > /dev/null | |
| fi |
The boot has no window, so the app won't be visible until Device Hub is opened.
| names+=("$(printf '%s\n' "$line" | sed -E 's/^[[:space:]]*//; s/[[:space:]]*\([0-9A-Fa-f-]{36}\).*$//')") | ||
| done < <(xcrun simctl list devices booted | grep -F "(Booted)" || true) | ||
|
|
||
| choose_device "simulator" "Boot one (open Simulator, or 'xcrun simctl boot <udid>'), or pass --device <udid>." |
There was a problem hiding this comment.
Xcode 27 replaced Simulator with Device Hub, and simctl boot starts a simulator with no window, so the app launches invisibly. --device also fails for a simulator that isn't running, unless it's booted as suggested above.
| choose_device "simulator" "Boot one (open Simulator, or 'xcrun simctl boot <udid>'), or pass --device <udid>." | |
| choose_device "simulator" "Boot one from Device Hub (Simulator before Xcode 27), or pass --device <udid>." |
| return token | ||
| } | ||
|
|
||
| for token in Set(tokens) { |
There was a problem hiding this comment.
Finding from Claude:
Set iteration order is randomly seeded per process, so when several stored tokens can edit the site, the user picked varies between launches. Android's .distinct() keeps the stored order.
| for token in Set(tokens) { | |
| var seen = Set<String>() | |
| for token in tokens where seen.insert(token).inserted { |
…m tokens in stored order `make login-ios-app DEVICE=<id>` skipped the booted check, so naming a shut-down simulator failed with CoreSimulator's `code=405 … Unable to lookup in current state: Shutdown`. `bin/demo-app-login.sh` now runs `simctl bootstatus -b` on a named simulator, which boots it if it isn't running and waits until it's ready. The boot opens no window, so the app shows once the simulator is opened in Device Hub. The hint for when no simulator is running now names Device Hub, which replaced Simulator in Xcode 27. On iOS, "Add WordPress Site" walked the stored tokens through a `Set`, whose order changes between launches, so the token used varied when more than one could edit the site. It now tries them in stored order, as Android does. Suggested in review by David Calhoun.
Adds
make login-ios-appandmake login-android-app, which add a WordPress.com site to a demo app using a bearer token fromWPCOM_TOKENor~/.wpcom-token— no OAuth application and no taps. Once the app holds a token, Add WordPress Site reuses it for any other WordPress.com site that token's user can edit.Until now the only way to reach a WordPress.com site from the demo apps was the OAuth flow, which needs a WordPress.com application's client ID and secret in
wp_com_oauth_credentials.json. That file is gitignored, so a fresh worktree doesn't have it.Modelled on Automattic/wordpress-rs#1652, and reads the same
~/.wpcom-tokenfile.Changes
1. Sign in from launch arguments
-wpcom-token,-wpcom-site-idand-wpcom-site-hostlaunch arguments on iOS, orwpcom-token,wpcom-site-idandwpcom-site-hostintent extras on Android.login-ios-appandlogin-android-app, with optionalSITEandDEVICE.The script looks up a site, which the wordpress-rs version doesn't need to. The demo apps keep one account per site and parse the site's numeric ID back out of the stored API root, so a token alone isn't enough. The script asks the WordPress.com REST API for the account's primary site — or the one named by
SITE=<domain>— and passes its ID and host to the app.The token is never a command-line flag or a
makevariable, which keeps it out of shell history. It does reach the app as a launch argument, so it appears in the process list for as long assimctl launchoradb shell am startruns.2. Reuse the stored token in "Add WordPress Site"
sites.getSite. The first token whose user hasedit_postson the site is stored as an account for it, with no login screen. Otherwise the OAuth flow runs as before.Any stored WordPress.com token counts — one from
make login-*-app, or one from an earlier OAuth sign-in. Adding a site that is already listed still creates a second entry, which is what the OAuth path does today.3. One helper for the stored API root
The format
https://public-api.wordpress.com/wp/v2/sites/<id>is now built in one place per platform, because the site ID is parsed back out of it:Account.wpComSiteApiRoot(siteId:)on iOS, and the existingwordPressComSiteApiRooton Android, which is no longerprivate.docs/code/wpcom-oauth.md and bin/README.md document the usage.
Test plan
Verified on an iPhone 18 Pro simulator (iOS 27.0) and an API 36 emulator, with a token in
~/.wpcom-tokenand nowp_com_oauth_credentials.json:make login-ios-appandmake login-android-app: the account's primary site appears under WordPress Sites, and opening it shows the Editor Configuration screen with the site's URL.SITE=as a bare domain and on Android withSITE=as a URL.twentytwentyfourdemo.wordpress.com): the app falls back to OAuth, which shows "WP.com OAuth credentials not configured" in this setup.xcrun, not real simulators.make lint-web-fixpass.For the reviewer — you need a WordPress.com OAuth2 bearer token in
~/.wpcom-token, such as thebearer_tokenwordpress-rs's integration tests read fromwp_com_test_credentials.json:make login-ios-app: your primary site appears under WordPress Sites.make login-android-app: your primary site appears under WordPress Sites.wp_com_oauth_credentials.jsonconfigured, add a WordPress.com site that no stored token can edit: the WordPress.com login screen opens and the site is added afterwards. We did not run this path — no OAuth application was configured — so only the fallback into OAuth was exercised, not the sign-in that follows it.