• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H04-Feb-2021-604469

LICENSEH A D04-Feb-20211.1 KiB2218

MANIFEST.inH A D29-Jul-202076 54

PKG-INFOH A D04-Feb-20219.6 KiB179135

README.rstH A D17-Oct-20207.6 KiB158115

setup.cfgH A D04-Feb-2021119 128

setup.pyH A D04-Feb-20211.2 KiB3732

test.pyH A D04-Feb-202118.4 KiB458384

README.rst

1PyOTP - The Python One-Time Password Library
2============================================
3
4PyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA)
5or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.
6
7Open MFA standards are defined in `RFC 4226 <https://tools.ietf.org/html/rfc4226>`_ (HOTP: An HMAC-Based One-Time
8Password Algorithm) and in `RFC 6238 <https://tools.ietf.org/html/rfc6238>`_ (TOTP: Time-Based One-Time Password
9Algorithm). PyOTP implements server-side support for both of these standards. Client-side support can be enabled by
10sending authentication codes to users over SMS or email (HOTP) or, for TOTP, by instructing users to use `Google
11Authenticator <https://en.wikipedia.org/wiki/Google_Authenticator>`_, `Authy <https://www.authy.com/>`_, or another
12compatible app. Users can set up auth tokens in their apps easily by using their phone camera to scan `otpauth://
13<https://github.com/google/google-authenticator/wiki/Key-Uri-Format>`_ QR codes provided by PyOTP.
14
15Implementers should read and follow the `HOTP security requirements <https://tools.ietf.org/html/rfc4226#section-7>`_
16and `TOTP security considerations <https://tools.ietf.org/html/rfc6238#section-5>`_ sections of the relevant RFCs. At
17minimum, application implementers should follow this checklist:
18
19- Ensure transport confidentiality by using HTTPS
20- Ensure HOTP/TOTP secret confidentiality by storing secrets in a controlled access database
21- Deny replay attacks by rejecting one-time passwords that have been used by the client (this requires storing the most
22  recently authenticated timestamp, OTP, or hash of the OTP in your database, and rejecting the OTP when a match is seen)
23- Throttle brute-force attacks against your application's login functionality
24- When implementing a "greenfield" application, consider supporting
25  `FIDO U2F <https://en.wikipedia.org/wiki/Universal_2nd_Factor>`_/`WebAuthn <https://www.w3.org/TR/webauthn/>`_ in
26  addition to HOTP/TOTP. U2F uses asymmetric cryptography to avoid using a shared secret design, which strengthens your
27  MFA solution against server-side attacks. Hardware U2F also sequesters the client secret in a dedicated single-purpose
28  device, which strengthens your clients against client-side attacks. And by automating scoping of credentials to
29  relying party IDs (application origin/domain names), U2F adds protection against phishing attacks. One implementation of
30  FIDO U2F/WebAuthn is PyOTP's sister project, `PyWARP <https://github.com/pyauth/pywarp>`_.
31
32We also recommend that implementers read the
33`OWASP Authentication Cheat Sheet <https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Authentication_Cheat_Sheet.md>`_ and
34`NIST SP 800-63-3: Digital Authentication Guideline <https://pages.nist.gov/800-63-3/>`_ for a high level overview of
35authentication best practices.
36
37Quick overview of using One Time Passwords on your phone
38--------------------------------------------------------
39
40* OTPs involve a shared secret, stored both on the phone and the server
41* OTPs can be generated on a phone without internet connectivity
42* OTPs should always be used as a second factor of authentication (if your phone is lost, you account is still secured with a password)
43* Google Authenticator and other OTP client apps allow you to store multiple OTP secrets and provision those using a QR Code
44
45Installation
46------------
47::
48
49    pip install pyotp
50
51Usage
52-----
53
54Time-based OTPs
55~~~~~~~~~~~~~~~
56::
57
58    totp = pyotp.TOTP('base32secret3232')
59    totp.now() # => '492039'
60
61    # OTP verified for current time
62    totp.verify('492039') # => True
63    time.sleep(30)
64    totp.verify('492039') # => False
65
66Counter-based OTPs
67~~~~~~~~~~~~~~~~~~
68::
69
70    hotp = pyotp.HOTP('base32secret3232')
71    hotp.at(0) # => '260182'
72    hotp.at(1) # => '055283'
73    hotp.at(1401) # => '316439'
74
75    # OTP verified with a counter
76    hotp.verify('316439', 1401) # => True
77    hotp.verify('316439', 1402) # => False
78
79Generating a Secret Key
80~~~~~~~~~~~~~~~~~~~~~~~
81A helper function is provided to generate a 16 character base32 secret, compatible with Google Authenticator and other OTP apps::
82
83    pyotp.random_base32()
84
85Some applications want the secret key to be formatted as a hex-encoded string::
86
87    pyotp.random_hex()  # returns a 32-character hex-encoded secret
88
89Google Authenticator Compatible
90~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91
92PyOTP works with the Google Authenticator iPhone and Android app, as well as other OTP apps like Authy. PyOTP includes the
93ability to generate provisioning URIs for use with the QR Code scanner built into these MFA client apps::
94
95    pyotp.totp.TOTP('JBSWY3DPEHPK3PXP').provisioning_uri(name='alice@google.com', issuer_name='Secure App')
96
97    >>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP&issuer=Secure%20App'
98
99    pyotp.hotp.HOTP('JBSWY3DPEHPK3PXP').provisioning_uri(name="alice@google.com", issuer_name="Secure App", initial_count=0)
100
101    >>> 'otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0'
102
103This URL can then be rendered as a QR Code (for example, using https://github.com/neocotic/qrious) which can then be scanned
104and added to the users list of OTP credentials.
105
106Parsing these URLs is also supported::
107
108    pyotp.parse_uri('otpauth://totp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP&issuer=Secure%20App')
109
110    >>> <pyotp.totp.TOTP object at 0xFFFFFFFF>
111
112    pyotp.parse_uri('otpauth://hotp/Secure%20App:alice%40google.com?secret=JBSWY3DPEHPK3PXP&issuer=Secure%20App&counter=0'
113
114    >>> <pyotp.totp.HOTP object at 0xFFFFFFFF>
115
116Working example
117~~~~~~~~~~~~~~~
118
119Scan the following barcode with your phone's OTP app (e.g. Google Authenticator):
120
121.. image:: https://chart.apis.google.com/chart?cht=qr&chs=250x250&chl=otpauth%3A%2F%2Ftotp%2Falice%40google.com%3Fsecret%3DJBSWY3DPEHPK3PXP
122
123Now run the following and compare the output::
124
125    import pyotp
126    totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
127    print("Current OTP:", totp.now())
128
129Links
130~~~~~
131
132* `Project home page (GitHub) <https://github.com/pyauth/pyotp>`_
133* `Documentation <https://pyauth.github.io/pyotp/>`_
134* `Package distribution (PyPI) <https://pypi.python.org/pypi/pyotp>`_
135* `Change log <https://github.com/pyauth/pyotp/blob/master/Changes.rst>`_
136* `RFC 4226: HOTP: An HMAC-Based One-Time Password <https://tools.ietf.org/html/rfc4226>`_
137* `RFC 6238: TOTP: Time-Based One-Time Password Algorithm <https://tools.ietf.org/html/rfc6238>`_
138* `ROTP <https://github.com/mdp/rotp>`_ - Original Ruby OTP library by `Mark Percival <https://github.com/mdp>`_
139* `OTPHP <https://github.com/lelag/otphp>`_ - PHP port of ROTP by `Le Lag <https://github.com/lelag>`_
140* `OWASP Authentication Cheat Sheet <https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Authentication_Cheat_Sheet.md>`_
141* `NIST SP 800-63-3: Digital Authentication Guideline <https://pages.nist.gov/800-63-3/>`_
142
143For new applications:
144
145* `WebAuthn <https://www.w3.org/TR/webauthn/>`_
146* `PyWARP <https://github.com/pyauth/pywarp>`_
147
148.. image:: https://github.com/pyauth/pyotp/workflows/Python%20package/badge.svg
149        :target: https://github.com/pyauth/pyotp/actions
150.. image:: https://img.shields.io/codecov/c/github/pyauth/pyotp/master.svg
151        :target: https://codecov.io/github/pyauth/pyotp?branch=master
152.. image:: https://img.shields.io/pypi/v/pyotp.svg
153        :target: https://pypi.python.org/pypi/pyotp
154.. image:: https://img.shields.io/pypi/l/pyotp.svg
155        :target: https://pypi.python.org/pypi/pyotp
156.. image:: https://readthedocs.org/projects/pyotp/badge/?version=latest
157        :target: https://pyotp.readthedocs.io/
158