Skip to content

Add CLI token support for app management and BP API #5604

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

Merged

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 20250414215916
    npm i -g @shopify/[email protected] --@shopify:registry=https://registry.npmjs.org

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.68% (+0.04% 🔼)
9492/12378
🟡 Branches
71.77% (-0.09% 🔻)
4657/6489
🟡 Functions
76.45% (+0.06% 🔼)
2461/3219
🟡 Lines
77.22% (+0.05% 🔼)
8977/11625
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🔴
... / 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

2213 tests passing in 961 suites.

Report generated by 🧪jest coverage report action from 68b53f6

@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

@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

This comment has been minimized.

@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

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 :)

@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch 3 times, most recently from e6b204f to 6163dea Compare April 14, 2025 23:09
@Shopify Shopify deleted a comment from github-actions bot Apr 14, 2025
@RyanDJLee RyanDJLee added the #gsd:43158 https://vault.shopify.io/gsd/projects/43158 label Apr 17, 2025
@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch from 6163dea to 4d86540 Compare April 21, 2025 20:29
@zzooeeyy zzooeeyy requested a review from a team as a code owner April 21, 2025 20:29
@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch 2 times, most recently from ad547ab to 03d7671 Compare April 22, 2025 14:46
@zzooeeyy zzooeeyy added this pull request to the merge queue Apr 24, 2025
@zzooeeyy zzooeeyy removed this pull request from the merge queue due to a manual request Apr 24, 2025
Co-authored-by: Ryan DJ Lee <[email protected]>
Co-authored-by: Zoey Lan <[email protected]>
@zzooeeyy zzooeeyy force-pushed the zl/add_cli_token_support_for_app_management_and_bp_api branch from 03d7671 to 68b53f6 Compare April 24, 2025 21:57
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/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).
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/private/node/session/scopes.d.ts
@@ -13,4 +13,10 @@ export declare function allDefaultScopes(extraScopes?: string[]): string[];
  * @param extraScopes - custom user-defined scopes
  * @returns Array of scopes
  */
-export declare function apiScopes(api: API, extraScopes?: string[]): string[];
\ No newline at end of file
+export declare function apiScopes(api: API, extraScopes?: string[]): string[];
+/**
+ * Returns specific scopes required for token exchange with the given API.
+ * @param api - API to get the scopes for
+ * @returns Array of transformed scopes
+ */
+export declare function tokenExchangeScopes(api: API): string[];
\ No newline at end of file

@zzooeeyy zzooeeyy added this pull request to the merge queue Apr 24, 2025
Merged via the queue into main with commit a2128b5 Apr 24, 2025
30 checks passed
@zzooeeyy zzooeeyy deleted the zl/add_cli_token_support_for_app_management_and_bp_api branch April 24, 2025 22:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#gsd:43158 https://vault.shopify.io/gsd/projects/43158
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants