Command: auth
Relevant source files
The following files were used as context for generating this wiki page:
The auth command is a primary module within the ipatool CLI responsible for managing user authentication with the Apple App Store. It provides the necessary infrastructure to log into an Apple ID, view current session information, and revoke stored credentials. Authentication is a prerequisite for other core features such as searching for apps, obtaining licenses, and downloading encrypted IPA packages.
The command operates in both interactive and non-interactive modes, supporting Two-Factor Authentication (2FA) and persistent credential storage via a secure keychain. It leverages a dependency injection pattern to interact with the underlying App Store logic, logging utilities, and OS-level resources.
Sources: cmd/auth.go:21-28, README.md:52-72
Subcommand Architecture
The auth command acts as a container for three specialized subcommands that handle distinct parts of the identity lifecycle.
| Command | Description | Usage |
|---|---|---|
login | Authenticates the user with Apple ID and password. | ipatool auth login [flags] |
info | Displays the account name and email currently stored. | ipatool auth info |
revoke | Removes stored credentials from the local keychain. | ipatool auth revoke |
Sources: cmd/auth.go:24-26, README.md:58-61
Login Logic and Two-Factor Authentication
The login subcommand implements a retry-based authentication flow. It first attempts to retrieve an "App Store Bag" (configuration metadata) to identify the correct authentication endpoint. It then performs the login request using the provided email and password.
Authentication Flow
This diagram illustrates the sequence of events during a login attempt, including the handling of 2FA requirements.
Credentials Handling
The ipatool application uses a persistent keychain to store sensitive account information. The data structure saved includes the user's name, email, password, and a specific PasswordToken used for subsequent requests like downloads.
| Field | Description | Source |
|---|---|---|
Email | The Apple ID email address. | pkg/appstore/appstore_login_test.go:215 |
PasswordToken | A token returned by Apple used to authorize operations. | pkg/appstore/appstore_login_test.go:217 |
DirectoryServicesID | The DSID associated with the Apple account. | pkg/appstore/appstore_login_test.go:219 |
StoreFront | The specific App Store territory/region code. | pkg/appstore/appstore_login_test.go:220 |
System Integration and Security
The auth command integrates with the system through several core components managed in cmd/common.go.
Keychain and Passphrase Management
The tool uses the 99designs/keyring library to interface with various backends (macOS Keychain, Linux Secret Service, or File-based storage). When running in non-interactive mode, users must provide a --keychain-passphrase to unlock the keyring. In interactive mode, the tool prompts the user for this passphrase if the backend is locked.
Sources: cmd/common.go:66-102, cmd/root.go:44
Dependency Injection
The auth subcommands rely on a Dependencies struct, which is initialized in the PersistentPreRun phase of the root command. This ensures that the App Store client, Logger, and Keychain are ready before any authentication logic executes.
Sources: cmd/root.go:28-32, cmd/common.go:105-121
Error Handling and Retries
The login command specifically uses the avast/retry-go library to handle 2FA challenges. If the App Store API returns appstore.ErrAuthCodeRequired, the command catches this error, prompts the user for the code (if interactive), and retries the login call with the AuthCode included in the payload.
If a login attempt fails with a critical error (like an expired password token or a disabled account), the tool logs the error message returned by Apple's "CustomerMessage" field and terminates the process with exit code 1.
Sources: cmd/auth.go:66-105, pkg/appstore/appstore_login_test.go:80-118, cmd/root.go:58-85
Conclusion
The auth command serves as the gateway for ipatool, managing the complex interaction with Apple's authentication servers. By abstracting the retrieval of store bags, handling 2FA retries, and securely managing tokens in the system keychain, it provides a unified identity management system that powers the rest of the tool's functionality.