freshcrate
Home > Security > pyotp

pyotp

Python One Time Password Library

Description

PyOTP - The Python One-Time Password Library ============================================ PyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA) or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in. Open MFA standards are defined in `RFC 4226 <https://tools.ietf.org/html/rfc4226>`_ (HOTP: An HMAC-Based One-Time Password Algorithm) and in `RFC 6238 <https://tools.ietf.org/html/rfc6238>`_ (TOTP: Time-Based One-Time Password Algorithm). PyOTP implements server-side support for both of these standards. Client-side support can be enabled by sending authentication codes to users over SMS or email (HOTP) or, for TOTP, by instructing users to use `Google Authenticator <https://en.wikipedia.org/wiki/Google_Authenticator>`_, `Authy <https://www.authy.com/>`_, or another compatible app. Users can set up auth tokens in their apps easily by using their phone camera to scan `otpauth:// <https://github.com/google/google-authenticator/wiki/Key-Uri-Format>`_ QR codes provided by PyOTP. Implementers should read and follow the `HOTP security requirements <https://tools.ietf.org/html/rfc4226#section-7>`_ and `TOTP security considerations <https://tools.ietf.org/html/rfc6238#section-5>`_ sections of the relevant RFCs. At minimum, application implementers should follow this checklist: - Ensure transport confidentiality by using HTTPS - Ensure HOTP/TOTP secret confidentiality by storing secrets in a controlled access database - Deny replay attacks by rejecting one-time passwords that have been used by the client (this requires storing the most recently authenticated timestamp, OTP, or hash of the OTP in your database, and rejecting the OTP when a match is seen) - Throttle (rate limit) brute-force attacks against your application's login functionality (see RFC 4226, section 7.3) - When implementing a "greenfield" application, consider supporting `FIDO U2F <https://en.wikipedia.org/wiki/Universal_2nd_Factor>`_/`WebAuthn <https://www.w3.org/TR/webauthn/>`_ in addition to HOTP/TOTP. U2F uses asymmetric cryptography to avoid using a shared secret design, which strengthens your MFA solution against server-side attacks. Hardware U2F also sequesters the client secret in a dedicated single-purpose device, which strengthens your clients against client-side attacks. And by automating scoping of credentials to relying party IDs (application origin/domain names), U2F adds protection against phishing attacks. One implementation of FIDO U2F/WebAuthn is PyOTP's sister project, `PyWARP <https://github.com/pyauth/pywarp>`_. We also recommend that implementers read the `OWASP Authentication Cheat Sheet <https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Authentication_Cheat_Sheet.md>`_ and `NIST SP 800-63-3: Digital Authentication Guideline <https://pages.nist.gov/800-63-3/>`_ for a high level overview of authentication best practices. Quick overview of using One Time Passwords on your phone -------------------------------------------------------- * OTPs involve a shared secret, stored both on the phone and the server * OTPs can be generated on a phone without internet connectivity * OTPs should always be used as a second factor of authentication (if your phone is lost, you account is still secured with a password) * Google Authenticator and other OTP client apps allow you to store multiple OTP secrets and provision those using a QR Code Installation ------------ :: pip install pyotp Usage ----- Time-based OTPs ~~~~~~~~~~~~~~~ :: import pyotp import time totp = pyotp.TOTP('base32secret3232') totp.now() # => '492039' # OTP verified for current time totp.verify('492039') # => True time.sleep(30) totp.verify('492039') # => False Counter-based OTPs ~~~~~~~~~~~~~~~~~~ :: import pyotp hotp = pyotp.HOTP('base32secret3232') hotp.at(0) # => '260182' hotp.at(1) # => '055283' hotp.at(1401) # => '316439' # OTP verified with a counter hotp.verify('316439', 1401) # => True hotp.verify('316439', 1402) # => False Generating a Secret Key ~~~~~~~~~~~~~~~~~~~~~~~ A helper function is provided to generate a 32-character base32 secret, compatible with Google Authenticator and other OTP apps:: pyotp.random_base32() Some applications want the secret key to be formatted as a hex-encoded string:: pyotp.random_hex() # returns a 40-character hex-encoded secret Google Authenticator Compatible ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PyOTP works with the Google Authenticator iPhone and Android app, as well as other OTP apps like Authy. PyOTP includes the ability to generate provisioning URIs for use with the QR Code scanner built into these MFA client apps:: pyotp.totp.TOTP('JBSWY3DPEHPK3PXP').provisioning_uri(name='alice@google.com', issuer_name='Secure App') >>> 'otpauth://totp/Secure%20App:alice%40google.com?se

Release History

VersionChangesUrgencyDate
2.9.0Imported from PyPI (2.9.0)Low4/21/2026
v2.9.0- Add `parse_uri()` support for Steam TOTP (#153) - Test and documentation improvementsLow7/27/2023
v2.8.0- Modify OTP generation to run in constant time (#148) - Documentation improvements - Drop Python 3.6 support; introduce Python 3.11 supportLow12/14/2022
v2.7.0- Support Steam TOTP (#142) - Build, test, and documentation updatesLow9/11/2022
v2.6.0- Raise default and minimum base32 secret length to 32, and hex secret length to 40 (160 bits as recommended by the RFC) (#115). - Fix issue where provisioning_uri would return invalid results after calling verify() (#115).Low2/4/2021
v2.5.1- parse_uri accepts and ignores optional image parameter (#114)Low1/30/2021
v2.5.0- Add optional image parameter to provisioning_uri (#113) - Support for 7-digit codes in 'parse_uri' (#111) - Raise default and minimum base32 secret length to 26Low1/29/2021
v2.4.1- parse_uri: Fix handling of period, counter (#108) - Add support for timezone aware datetime as argument to `TOTP.timecode()` (#107)Low10/16/2020
v2.4.0- Fix data type for at(for_time) (#85) - Add support for parsing provisioning URIs (#84) - Raise error when trying to generate secret that is too short (The secret must be at least 128 bits) - Add random_hex function (#82)Low7/29/2020
v2.3.0- Fix comparison behavior on Python 2.7Low7/26/2019
v2.2.9Release v2.2.9Low7/26/2019
v2.2.7- Have random_base32() use 'secrets' as rand source (#66) - Documentation: Add security considerations, minimal security checklist, other improvements - Update setup.py to reference correct licenseLow11/6/2018
v2.2.6- Fix tests wrt double-quoting in provisioning URIsLow6/10/2017
v2.2.5- Quote issuer QS parameter in provisioning_uri. Fixes #47. - Raise an exception if a negative integer is passed to at() (#41). - Documentation and release infrastructure improvements.Low6/3/2017
v2.2.4- Restore Python 2.6 compatibility (however, Python 2.6 is not supported) - Documentation and test improvements - Fix release infra script, part 2 Low1/4/2017
v2.2.3- Restore Python 2.6 compatibility (however, Python 2.6 is not supported) - Documentation and test improvements - Fix release infra script Low1/4/2017
v2.2.2- Restore Python 2.6 compatibility (however, Python 2.6 is not supported) - Documentation and test improvements Low1/4/2017
v2.2.1- Avoid using python-future; it has subdependencies that limit compatibility (#34) - Make test suite pass on 32-bit platforms (#30) - Timing attack resistance fix: don't reveal string length to attacker. Thanks to Eeo Jun (#28). - Support algorithm, digits, period parameters in provisioning_uri. Thanks to Dionisio E Alonso (#33). - Minor style and packaging infrastructure fixes. Low8/30/2016
v2.2.0See v2.2.1 Low8/30/2016

Dependencies & License Audit

Loading dependencies...

Similar Packages

azure-identityMicrosoft Azure Identity Library for Pythonazure-template_0.1.0b6187637
azure-keyvault-secretsMicrosoft Corporation Key Vault Secrets Client Library for Pythonazure-template_0.1.0b6187637
azure-storage-queueMicrosoft Azure Azure Queue Storage Client Library for Pythonazure-template_0.1.0b6187637
azure-data-tablesMicrosoft Azure Azure Data Tables Client Library for Pythonazure-template_0.1.0b6187637
azure-appconfigurationMicrosoft App Configuration Data Client Library for Pythonazure-template_0.1.0b6187637