You can secure your PyPI account without a phone by generating and storing TOTP (Time-based One-Time Password) secrets using authenticator apps like Google Authenticator or Authy on your mobile device, or by utilizing hardware security keys such as YubiKey. These methods provide robust two-factor authentication while eliminating the dependency on SMS-based verification.
The Problem with SMS-Based 2FA
Two-factor authentication (2FA) is critical for protecting Python package maintainers from unauthorized access to their PyPI accounts. However, relying solely on SMS-based verification presents significant security risks. SIM swapping attacks allow malicious actors to intercept verification codes, bypassing this layer of protection entirely. Furthermore, SMS delivery can be unreliable in areas with poor network coverage, causing delays that hinder deployment workflows. For developers who prioritize security and reliability, moving away from SMS is not just an option but a necessity. The Python Packaging Authority (PyPA) encourages the use of more secure methods that are resistant to interception and do not rely on carrier infrastructure.Using Authenticator Apps (TOTP)
The most common alternative to SMS is Time-based One-Time Passwords (TOTP). This method generates a six-digit code every 30 seconds based on a shared secret key. To implement this, you first need to enable 2FA on your PyPI account through the settings interface. During the setup process, PyPI will display a QR code or a manual secret key. You should scan this QR code using a trusted authenticator application installed on your smartphone. Popular choices include Google Authenticator, Authy, and Microsoft Authenticator. These apps store the secret locally on your device, ensuring that the codes are generated offline and never transmitted over the network in a vulnerable manner. Once configured, every time you attempt to upload a package usingtwine or log into the PyPI web interface, you will be prompted for the current code from your app. This adds a strong layer of security because possession of the physical device is required to generate the valid token. It is crucial to keep your backup codes safe. When you enable TOTP, PyPI provides a set of one-time-use backup codes. Store these in a secure password manager or print them out and store them in a physically secure location. If you lose access to your authenticator app, these codes are the only way to recover your account.
Hardware Security Keys (FIDO/U2F)
For users seeking the highest level of security, hardware security keys offer a superior solution. Devices like the YubiKey support FIDO U2F and FIDO2 standards, which are immune to phishing attacks and man-in-the-middle exploits. Unlike TOTP, where codes can potentially be intercepted if a user is tricked into entering them on a fake site, hardware keys verify the domain of the website before allowing authentication. This ensures that you are always interacting with the genuine PyPI server. To use a hardware key, you must register it in your PyPI account settings alongside or instead of other methods. When logging in or uploading packages, you simply touch the button on the key to authenticate. This method is particularly effective for CI/CD pipelines if configured correctly, though it requires careful management of API tokens. Many developers find that the convenience of a single physical object outweighs the initial cost of the hardware key. It eliminates the need to remember or type codes, streamlining the authentication process while enhancing security.Configuring Twine for Secure Uploads
When deploying packages, you should avoid storing your password in plain text files like.pypirc. Instead, use environment variables or secure credential managers. For TOTP-enabled accounts, twine can prompt for the username and password interactively. Ensure that your development environment does not log sensitive information. If you are automating uploads via GitHub Actions or GitLab CI, use encrypted secrets rather than hardcoding credentials. This practice prevents accidental exposure of your credentials in public repositories or logs.
Integrating these security measures protects not only your account but also the integrity of the Python ecosystem. Malicious uploads can compromise thousands of downstream projects. By adopting TOTP or hardware keys, you contribute to a safer environment for all Python developers. Regularly review your active sessions and remove any unrecognized devices or applications. This proactive approach helps identify potential breaches early.
Locking User Accounts with Sudo Commands Without Rebooting discusses related system-level security practices that complement your application security strategy. Additionally, understanding Understanding Ubuntu 22.04 VPS Security can help you secure the servers where you build and test your packages, ensuring end-to-end protection.