Skip to content

Sign the example apps into WordPress.com from a launch-argument token - #1652

Merged
jkmassel merged 6 commits into
trunkfrom
jkmassel/demo-app-wpcom-sign-in
Sep 29, 2026
Merged

jkmassel merged 6 commits into
trunkfrom
jkmassel/demo-app-wpcom-sign-in

Conversation

@jkmassel

@jkmassel jkmassel commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

Adds make example-app-login, which signs the Swift or Android example app into WordPress.com with a bearer token in one command. You don't need OAuth app credentials in wp_com_test_credentials.json, and you don't need to tap through a sign-in flow. Modelled on wordpress-mobile/WordPress-iOS#25801.

The demo apps have no sign-in UI to automate (unlike the WordPress app), so the approach is simpler: the app stores the launch token as its WordPress.com account at startup.

Changes

1. make example-app-login / scripts/example-app-signin.sh

make example-app-login                    # Swift example app, the booted iOS Simulator
make example-app-login PLATFORM=android   # Android example app, the connected emulator/device
make example-app-login RESET=1            # clear the app's data first
make example-app-login DEVICE=<id>        # a simulator UDID or adb serial
  • iOS launches com.automattic.Example with simctl launch -wpcom-token. --reset uninstalls and reinstalls the app.
  • Android launches WelcomeActivity with am start -S --es wpcom-token. --reset runs pm clear.
  • The script targets the running simulator/device, and prompts you to choose when several are running.
  • It reports which source the token came from, but never prints the token itself.

The token never appears on the command line. It resolves from WPCOM_TOKEN, then bearer_token in the repo-root wp_com_test_credentials.json (the same token the WordPress.com integration tests use), then ~/.wpcom-token. If none is set, the script fails with a pointer to wp_com_test_credentials.json-example. There is deliberately no --wpcom-token flag, because a token passed as an argument would be saved to shell history.

~/.wpcom-token covers fresh worktrees that don't have a copy of the gitignored credentials file, and it's the same file WordPress-iOS's make sim-login reads.

adb shell joins its arguments into a command line that the device's shell runs, and WordPress.com tokens contain shell metacharacters (#, (, $). The script single-quotes the token before passing it.

2. Launch-time sign-in in the apps

  • LoginManager.swift: init stores the -wpcom-token argument as the WordPress.com account when none is signed in. It reads ProcessInfo.arguments rather than UserDefaults, which parses argument values as property lists.
  • WelcomeActivity.kt: stores the wpcom-token extra when no global WordPress.com account exists. It runs only on a fresh onCreate, and removes the extra once it's been used.

Both apps read the token at most once per launch, so signing out stays signed out while the still-present token is ignored.

3. Fix: WordPress.com Sign Out button missing on iPhone

WPComRootView attached its .toolbar to the NavigationView. A NavigationView drops toolbar items once it collapses to a single column on iPhone, so there was no way to sign out. The toolbar is now attached to the sidebar column.

4. Fix: Android example app offered OAuth sign-in with placeholder credentials

generateWpComCredentials passed client_id / client_secret through as-is. The placeholders from wp_com_test_credentials.json-example (0 / "") therefore became non-null WpComCredentials, and Sign in to WordPress.com was enabled with credentials that can't work. Those placeholders are also required: a file holding only a bearer_token fails to compile integration_test_credentials without them. Both values must now be non-zero/non-empty, or both generate as null, matching WPComOAuthCredentials in the Swift example app.

5. Docs

A new section in Documentation/local-development.md covers the Makefile target, how the token is resolved, and the raw simctl/adb forms.

Test plan

  • iOS (iPhone 18 Pro Max, iOS 27): make example-app-login RESET=1 → the WordPress.com tab lands signed in, and Me loads the account's profile. The UI was driven with Xcode 27's device-interaction tools.
  • iOS: tapping Sign Out after a token launch → the app stays signed out in the same process, and accounts.json holds 0 accounts.
  • iOS: launching again while signed in → no duplicate account is stored.
  • Android (API 30 emulator): make example-app-login PLATFORM=android → the account appears on the Sites screen with no taps, and Me loads the profile. This confirms the token's shell quoting survives adb shell.
  • Script paths, with stubbed xcrun/adb on macOS's bash 3.2: device auto-selection, --reset ordering, rejection of --wpcom-token and unknown platforms.
  • Credentials-file flow, for real: with bearer_token in the repo-root wp_com_test_credentials.json, the script reports Using the WordPress.com token from wp_com_test_credentials.json. Me then loads the profile on both the iOS Simulator and the Android emulator, after RESET=1.
  • Token resolution, with stubs: WPCOM_TOKEN wins over everything, and the credentials file wins over ~/.wpcom-token. The example file's unedited replace_with_your_oauth2_token placeholder is ignored, and with nothing configured the script exits 1 with the error.
  • generateWpComCredentials output: 0/"" → null/null; both set → both emitted; client_id only → null/null; keys missing → null/null.
  • Android emulator, with client_id: 0 / client_secret: "": Sign in to WordPress.com is disabled, with the "Add client_id and client_secret…" hint.
  • swift format lint --strict, SwiftLint, and :example:androidApp:compileDebugKotlin :example:androidApp:detekt are clean.

Changelog

  • I've added an entry to CHANGELOG.md under ## [Unreleased], using the Keep a Changelog categories (Added, Changed, Deprecated, Removed, Fixed, Security). Prefix breaking changes with **BREAKING:**.

@wpmobilebot

wpmobilebot commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

XCFramework Build

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

.package(url: "https://github.com/automattic/wordpress-rs", branch: "pr-build/1652")

Built from e0a26f1

@jkmassel
jkmassel enabled auto-merge (squash) September 28, 2026 22:31
jkmassel and others added 6 commits September 29, 2026 08:08
The `.toolbar` was attached to the `NavigationView`, which drops toolbar items once it collapses to a single column on iPhone, so there was no way to sign out. Attach it to the sidebar column instead.

Co-Authored-By: Claude <noreply@anthropic.com>
Adds `make example-app-login` (wrapping `scripts/example-app-signin.sh`), which launches the Swift example app on an iOS Simulator with `-wpcom-token`, or the Android example app on an emulator with a `wpcom-token` intent extra. The app stores the token as its WordPress.com account at launch, so no OAuth app credentials or taps are needed.

The token is read from `WPCOM_TOKEN` or `~/.wpcom-token` (or a hidden prompt), never the command line. Modelled on wordpress-mobile/WordPress-iOS#25801.

Co-Authored-By: Claude <noreply@anthropic.com>
Resolve the token from `WPCOM_TOKEN`, then the repo's `wp_com_test_credentials.json` `bearer_token` (the one the WordPress.com integration tests use), then `~/.wpcom-token`, and fail with a pointer to the credentials file otherwise. This replaces the hidden paste-and-save prompt, which assumed a developer had a token to hand but nowhere to keep it.

Co-Authored-By: Claude <noreply@anthropic.com>
Prints `Using the WordPress.com token from <source>` (never the token itself), so it's clear whether `WPCOM_TOKEN`, `wp_com_test_credentials.json`, or `~/.wpcom-token` won.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
`generateWpComCredentials` emitted `client_id: 0` / `client_secret: ""` from `wp_com_test_credentials.json-example` as real credentials, enabling a WordPress.com OAuth button that can't work. The placeholders are required for a bearer-token-only file to satisfy `integration_test_credentials`, so require both values to be non-zero/non-empty or generate both as `null`, matching the Swift example app.

Co-Authored-By: Claude <noreply@anthropic.com>
@jkmassel
jkmassel force-pushed the jkmassel/demo-app-wpcom-sign-in branch from a2fddff to e0a26f1 Compare September 29, 2026 14:08
@jkmassel
jkmassel merged commit 5fa7360 into trunk Sep 29, 2026
36 checks passed
@jkmassel
jkmassel deleted the jkmassel/demo-app-wpcom-sign-in branch September 29, 2026 14:38
jkmassel added a commit to wordpress-mobile/GutenbergKit that referenced this pull request Oct 11, 2026
* task: sign the demo apps into WordPress.com from a launch-argument token

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.

* task: reuse a stored WordPress.com token when adding a site to the demo 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.

* task: boot a simulator named with DEVICE, and try stored WordPress.com 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.
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.

3 participants