Command: download
Relevant source files
The following files were used as context for generating this wiki page:
The download command is a core feature of ipatool that enables users to retrieve encrypted iOS application packages (.ipa files) directly from the Apple App Store. It automates the process of authenticating, resolving application metadata, purchasing (if necessary), and downloading the binary while maintaining the integrity of the application's signature and metadata.
This command serves as the primary mechanism for localizing App Store assets for analysis or backup. It utilizes a retry mechanism to handle common authentication hurdles, such as expired session tokens, ensuring a robust user experience in both interactive and automated environments.
Sources: README.md:126-146, cmd/download.go:21-30
Architecture and Command Logic
The download command is implemented using the Cobra library and integrates multiple sub-systems including the App Store client, a retry logic wrapper, and a progress tracking interface.
Execution Flow
When executed, the command first validates the presence of either an app-id or a bundle-identifier. It then enters a retry loop (maximum 2 attempts) to manage transient failures or credential expiration. The flow involves fetching account info, performing lookups for bundle identifiers, and optionally triggering a purchase if a license is missing.
Sources: cmd/download.go:25-33, cmd/download.go:104-123
The diagram above illustrates the high-level logic within the download command's RunE function.
Sources: cmd/download.go:34-100
Implementation Details
Download Input and Output
The core logic resides in the pkg/appstore package, which defines the structures for managing the download request and handling the App Store's response.
| Field | Type | Description |
|---|---|---|
Account | Account | Authenticated user credentials and session data. |
App | App | The target application object containing the Adam ID. |
OutputPath | string | User-defined destination for the .ipa file. |
ExternalVersionID | string | Specific version identifier (optional). |
Progress | *ProgressBar | Progress tracker for interactive sessions. |
Sources: pkg/appstore/appstore_download.go:21-27
File Acquisition and Patching
The process involves downloading the binary to a temporary .tmp location, applying metadata patches (such as adding the user's Apple ID to iTunesMetadata.plist), and then finalizing the file.
Sources: pkg/appstore/appstore_download.go:76-92
SINF Replication
A critical step after downloading is the replication of the "SINF" (Selective Information) data. The ReplicateSinf function ensures that the downloaded package contains the necessary DRM information by reading the SC_Info manifest or the application's Info.plist and injecting the SINF data into the correct paths within the ZIP archive.
Sources: pkg/appstore/appstore_replicate_sinf.go:24-40, pkg/appstore/appstore_replicate_sinf.go:79-100
Configuration and Flags
The command supports several flags to control its behavior and the target application.
| Flag | Shorthand | Type | Default | Description |
|---|---|---|---|---|
--app-id | -i | int64 | 0 | The Apple-assigned unique ID (Adam ID) for the app. |
--bundle-identifier | -b | string | "" | The bundle identifier (e.g., com.example.app). |
--output | -o | string | "" | The destination path or filename for the IPA. |
--purchase | bool | false | Automatically acquire a license if the user doesn't own the app. | |
--external-version-id | string | "" | Specific version to download. |
Sources: cmd/download.go:138-144, README.md:138-146
Error Handling
The command specifically monitors for two types of errors to trigger the retry logic:
- ErrPasswordTokenExpired: Triggers a re-login using the cached credentials.
- ErrLicenseRequired: If the
--purchaseflag is set, it triggers the purchase flow before retrying the download.
Sources: cmd/download.go:111-120, pkg/appstore/appstore_download.go:48-54
Summary
The download command integrates the AppStore client's ability to communicate with Apple's private APIs with local file system operations. By handling authentication tokens, automatic purchases, and post-download binary patching, it provides a seamless way to acquire iOS application packages while ensuring they are correctly attributed to the authenticated user's account.
Sources: cmd/download.go:100-123, pkg/appstore/appstore_download.go:34-102