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 CLI token support for app management and BP API #5604

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zzooeeyy
Copy link
Contributor

@zzooeeyy zzooeeyy commented Apr 3, 2025

WHY are these changes introduced?

Closes

WHAT is this pull request doing?

Add support to use the CLI token for app management API

  • Remove isAppManagementDisabled() function that disables App Management API if a CLI Token is provided
  • Perform token exchange with AppMangement API and BusinessPlatform API if a CLI token is provided when creating a session for AppManagmentClient, and assign those access token as a ServiceAccount user
  • Extend UserInfo query to get the first 2 organizations the user/CLI token service account belongs to
    • This assumes the CLI Token will always only belong to a single organization and raise an exception if the CLI Token belong in multiple orgs
  • ⚠️ With this change, CLI Tokens will be used to exchange for Partners API, BP API and App Management API, the token exchange requests will fail if the CLI token does not contain enough scopes.

How to test your changes?

Steps

  1. Ensure Partners org permission is synced to Dev Dash (steps)
  2. Enable beta "Add app management scope to CLI token" for your org in Partners internal
  3. Create a new CLI Token for your org
  4. Use the CLI token for apps within your partner and dev dash org with the CLI snapshot version

Tested flows

  • ✅ With CLI Token, shopify app config link a Partners app

  • ✅ With CLI Token, shopify app config link a dev dash app

  • ✅ With CLI Token, shopify app deploy a dev dash app

  • ✅ With CLI Token, shopify app deploy a Partners app

  • ✅ With a user session, shopify app config link a Partners app

  • ✅ With a user session, shopify app config link a dev dash app

  • ✅ With a user session, shopify app deploy a dev dash app

  • ✅ With a user session, shopify app deploy a Partners app

  • ✅ With CLI token, shopify app info to display a Partners app
    image

  • ✅ With CLI token, shopify app info to display a dev dash app
    image

Post-release steps

‼️This will break existing CLI Tokens unless this scope backfill is complete ‼️

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

Copy link
Contributor Author

zzooeeyy commented Apr 3, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch 3 times, most recently from 9ae7ebd to 2a0f42d Compare April 8, 2025 14:59
Copy link
Contributor

github-actions bot commented Apr 8, 2025

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
76.56% (-0.12% 🔻)
9405/12285
🟡 Branches
71.66% (-0.1% 🔻)
4604/6425
🟡 Functions
76.33% (-0.12% 🔻)
2444/3202
🟡 Lines
77.08% (-0.13% 🔻)
8892/11536
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / app.ts
87.5% (-0.61% 🔻)
70.33% (-0.38% 🔻)
92.98% (-0.46% 🔻)
90.21% (-0.42% 🔻)
🟢
... / loader.ts
93.27% (-0.02% 🔻)
85.15% 97.2%
94.29% (-0.01% 🔻)
🟢
... / extension-instance.ts
84.52% (-0.09% 🔻)
80.16% (+0.47% 🔼)
92.45% (-0.14% 🔻)
85.06% (-0.1% 🔻)
🟢
... / extension.ts
91.3% (+0.68% 🔼)
73.58% (+0.37% 🔼)
91.67% (-0.33% 🔻)
91.11% (+0.69% 🔼)
🔴
... / app-management-client.ts
41.58% (-0.88% 🔻)
35.71% (-2.75% 🔻)
40% (-0.4% 🔻)
40.15% (-0.76% 🔻)
🟡
... / local.ts
68.52% (-0.57% 🔻)
56.6%
52.17% (-1.99% 🔻)
68.52% (-0.57% 🔻)

Test suite run success

2177 tests passing in 954 suites.

Report generated by 🧪jest coverage report action from a0d5b24

@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch from 2a0f42d to ce7514a Compare April 8, 2025 19:26
@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch 5 times, most recently from 7692bd1 to a437d4f Compare April 9, 2025 19:00
@zzooeeyy zzooeeyy mentioned this pull request Apr 9, 2025
5 tasks
@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch 3 times, most recently from 87d5eda to 1093c77 Compare April 10, 2025 14:58
@zzooeeyy
Copy link
Contributor Author

/snapit

Copy link
Contributor

🫰✨ Thanks @zzooeeyy! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

pnpm i -g @shopify/cli@0.0.0-snapshot-20250410165640

Tip

If you get an ETARGET error, install it with NPM and the flag --@shopify:registry=https://registry.npmjs.org

Caution

After installing, validate the version by running just shopify in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

Co-authored-by: Ryan DJ Lee <ryan.d.lee@shopify.com>
Co-authored-by: Zoey Lan <zoey.lan@shopify.com>
@zzooeeyy zzooeeyy marked this pull request as ready for review April 10, 2025 17:29
@zzooeeyy zzooeeyy requested a review from a team as a code owner April 10, 2025 17:29
Copy link
Contributor

We detected some changes at packages/*/src and there are no updates in the .changeset.
If the changes are user-facing, run "pnpm changeset add" to track your changes and include them in the next release CHANGELOG.

@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch from 1093c77 to a0d5b24 Compare April 10, 2025 17:30
Copy link
Contributor

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

We found no new type declarations in this PR

Existing type declarations

packages/cli-kit/dist/private/node/session/exchange.d.ts
@@ -26,15 +26,33 @@ export declare function exchangeAccessForApplicationTokens(identityToken: Identi
  */
 export declare function refreshAccessToken(currentToken: IdentityToken): Promise<IdentityToken>;
 /**
- * Given a custom CLI token passed as ENV variable, request a valid partners API token
+ * Given a custom CLI token passed as ENV variable, request a valid Partners API token
  * This token does not accept extra scopes, just the cli one.
- * @param token - The CLI token passed as ENV variable
+ * @param token - The CLI token passed as ENV variable 
  * @returns An instance with the application access tokens.
  */
 export declare function exchangeCustomPartnerToken(token: string): Promise<{
     accessToken: string;
     userId: string;
 }>;
+/**
+ * Given a custom CLI token passed as ENV variable, request a valid App Management API token
+ * @param token - The CLI token passed as ENV variable 
+ * @returns An instance with the application access tokens.
+ */
+export declare function exchangeCliTokenForAppManagementAccessToken(token: string): Promise<{
+    accessToken: string;
+    userId: string;
+}>;
+/**
+ * Given a custom CLI token passed as ENV variable, request a valid Business Platform API token
+ * @param token - The CLI token passed as ENV variable 
+ * @returns An instance with the application access tokens.
+ */
+export declare function exchangeCliTokenForBusinessPlatformAccessToken(token: string): Promise<{
+    accessToken: string;
+    userId: string;
+}>;
 type IdentityDeviceError = 'authorization_pending' | 'access_denied' | 'expired_token' | 'slow_down' | 'unknown_failure';
 /**
  * Given a deviceCode obtained after starting a device identity flow, request an identity token.
packages/cli-kit/dist/public/node/context/local.d.ts
@@ -25,13 +25,6 @@ export declare function isDevelopment(env?: NodeJS.ProcessEnv): boolean;
  * @returns True if SHOPIFY_FLAG_VERBOSE is truthy or the flag --verbose has been passed.
  */
 export declare function isVerbose(env?: NodeJS.ProcessEnv): boolean;
-/**
- * It returns true if the App Management API is disabled.
- * This should only be relevant when using a Partners token.
- *
- * @returns True if the App Management API is disabled.
- */
-export declare function isAppManagementDisabled(): boolean;
 /**
  * Returns true if the environment in which the CLI is running is either
  * a local environment (where dev is present) or a cloud environment (spin).

}))

if (organizations.length > 1) {
throw new Error('Multiple organizations found for the CLI token')
Copy link
Contributor

Choose a reason for hiding this comment

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

let's report this as a BugError since is something that should never happen and we want it properly reported

Comment on lines +131 to +134
return exchangeCliTokenForAccessToken('business-platform', token, [
'https://api.shopify.com/auth/destinations.readonly',
'https://api.shopify.com/auth/organization.store-management',
])
Copy link
Contributor

Choose a reason for hiding this comment

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

We have a function called apiScopes that gives you the default scopes for each API so you don't need to duplicate them here. (same for partners and app-management above)

Copy link
Contributor

This will break old CLI tokens only if they try to access the app-management API right?

Copy link
Contributor

@isaacroldan isaacroldan left a comment

Choose a reason for hiding this comment

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

🎩 'ed and works, left two small comments but pre-approving, feel free to merge once those are addressed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants