Skip to content

task: sign the demo apps into WordPress.com with a bearer token - #771

Merged
jkmassel merged 3 commits into
trunkfrom
jkmassel/wp.com-token-feature
Oct 11, 2026
Merged

jkmassel merged 3 commits into
trunkfrom
jkmassel/wp.com-token-feature

Conversation

@jkmassel

@jkmassel jkmassel commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Adds make login-ios-app and make login-android-app, which add a WordPress.com site to a demo app using a bearer token from WPCOM_TOKEN or ~/.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-token file.

Changes

1. Sign in from launch arguments

  • bin/demo-app-login.sh (new): finds the token, picks the simulator or device (prompting when more than one is running), looks up the site, and launches the app with -wpcom-token, -wpcom-site-id and -wpcom-site-host launch arguments on iOS, or wpcom-token, wpcom-site-id and wpcom-site-host intent extras on Android.
  • ios/Demo-iOS/Sources/Services/ConfigurationStorage.swift and android/app/src/main/java/com/example/gutenbergkit/MainActivity.kt: store those as a WordPress.com account at launch, replacing any account already held for that site. Running the command again therefore picks up a new token.
  • Makefile: login-ios-app and login-android-app, with optional SITE and DEVICE.

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 make variable, 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 as simctl launch or adb shell am start runs.

2. Reuse the stored token in "Add WordPress Site"

  • ios/Demo-iOS/Sources/Services/AuthenticationManager.swift and android/app/src/main/java/com/example/gutenbergkit/AuthenticationManager.kt: when the entered site is on WordPress.com, look it up with each stored WordPress.com token 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.

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 existing wordPressComSiteApiRoot on Android, which is no longer private.

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-token and no wp_com_oauth_credentials.json:

  • make login-ios-app and make 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.
  • Running the target again leaves one entry for the site. Checked on iOS with SITE= as a bare domain and on Android with SITE= as a URL.
  • Add WordPress Site with a second WordPress.com site the token's user can edit: the site is added with no login screen, and opens.
  • Add WordPress Site with a WordPress.com site the token's user can't edit (twentytwentyfourdemo.wordpress.com): the app falls back to OAuth, which shows "WP.com OAuth credentials not configured" in this setup.
  • The script exits 1 with a message for: no token; an invalid token ("WordPress.com said: The OAuth2 token is invalid."); an unknown site ("WordPress.com said: Unknown blog"); and several simulators with no selection made ("re-run with --device "). The several-simulators prompt was run against a stubbed xcrun, not real simulators.
  • SwiftLint on the changed files, Detekt, and make lint-web-fix pass.

For the reviewer — you need a WordPress.com OAuth2 bearer token in ~/.wpcom-token, such as the bearer_token wordpress-rs's integration tests read from wp_com_test_credentials.json:

  • Build and run the iOS demo app once from Xcode, then run make login-ios-app: your primary site appears under WordPress Sites.
  • Build and run the Android demo app once from Android Studio, then run make login-android-app: your primary site appears under WordPress Sites.
  • In either app, tap Add WordPress Site and enter another WordPress.com site you can edit: it is added without a login screen.
  • With wp_com_oauth_credentials.json configured, 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.

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.
@jkmassel jkmassel added [Type] Task Issues or PRs that have been broken down into an individual action to take Android iOS labels Oct 6, 2026
@jkmassel jkmassel self-assigned this Oct 6, 2026
@jkmassel
jkmassel requested a review from dcalhoun October 6, 2026 18:09
@jkmassel
jkmassel marked this pull request as ready for review October 6, 2026 18:09
@wpmobilebot

wpmobilebot commented Oct 6, 2026 •

Copy link
Copy Markdown

XCFramework Build

This PR's XCFramework is available for testing. Add the following to your Package.swift:

.package(url: "https://github.com/wordpress-mobile/GutenbergKit", branch: "pr-build/771")

Built from 43e4780

@dcalhoun dcalhoun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very cool. Testing worked well for me. I left a few inline suggestions, but none are blocking.

Comment thread bin/demo-app-login.sh
Comment on lines +203 to +205
if [[ -z "$device" ]]; then
if [[ "$platform" == ios ]]; then resolve_ios_device; else resolve_android_device; fi
fi

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

DEVICE/--device skips the booted check, so naming a shut-down simulator fails with CoreSimulator's raw code=405 … Shutdown. Boot it instead?

Suggested change
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.

Comment thread bin/demo-app-login.sh Outdated
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>."

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Suggested change
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Suggested change
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.
@jkmassel
jkmassel merged commit dd61f07 into trunk Oct 11, 2026
24 checks passed
@jkmassel
jkmassel deleted the jkmassel/wp.com-token-feature branch October 11, 2026 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Android iOS [Type] Task Issues or PRs that have been broken down into an individual action to take

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants