PyOTP 1.4.2__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyotp/__init__.py +18 -0
- pyotp/hotp.py +40 -0
- pyotp/otp.py +60 -0
- pyotp/totp.py +58 -0
- pyotp/utils.py +79 -0
- pyotp-1.4.2.dist-info/DESCRIPTION.rst +112 -0
- pyotp-1.4.2.dist-info/METADATA +132 -0
- pyotp-1.4.2.dist-info/RECORD +11 -0
- pyotp-1.4.2.dist-info/WHEEL +6 -0
- pyotp-1.4.2.dist-info/metadata.json +1 -0
- pyotp-1.4.2.dist-info/top_level.txt +1 -0
pyotp/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from __future__ import print_function, unicode_literals, division, absolute_import
|
|
2
|
+
|
|
3
|
+
import random as _random
|
|
4
|
+
|
|
5
|
+
from pyotp.hotp import HOTP
|
|
6
|
+
from pyotp.otp import OTP
|
|
7
|
+
from pyotp.totp import TOTP
|
|
8
|
+
from . import utils
|
|
9
|
+
|
|
10
|
+
VERSION = '1.4.2'
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def random_base32(length=16, random=_random.SystemRandom(),
|
|
14
|
+
chars=list('ABCDEFGHIJKLMNOPQRSTUVWXYZ234567')):
|
|
15
|
+
return ''.join(
|
|
16
|
+
random.choice(chars)
|
|
17
|
+
for _ in range(length)
|
|
18
|
+
)
|
pyotp/hotp.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from __future__ import print_function, unicode_literals, division, absolute_import
|
|
2
|
+
|
|
3
|
+
from pyotp.otp import OTP
|
|
4
|
+
from pyotp import utils
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class HOTP(OTP):
|
|
8
|
+
def at(self, count):
|
|
9
|
+
"""
|
|
10
|
+
Generates the OTP for the given count
|
|
11
|
+
@param [Integer] count counter
|
|
12
|
+
@returns [Integer] OTP
|
|
13
|
+
"""
|
|
14
|
+
return self.generate_otp(count)
|
|
15
|
+
|
|
16
|
+
def verify(self, otp, counter):
|
|
17
|
+
"""
|
|
18
|
+
Verifies the OTP passed in against the current time OTP
|
|
19
|
+
@param [String/Integer] otp the OTP to check against
|
|
20
|
+
@param [Integer] counter the counter of the OTP
|
|
21
|
+
"""
|
|
22
|
+
return utils.strings_equal(str(otp), str(self.at(counter)))
|
|
23
|
+
|
|
24
|
+
def provisioning_uri(self, name, initial_count=0, issuer_name=None):
|
|
25
|
+
"""
|
|
26
|
+
Returns the provisioning URI for the OTP
|
|
27
|
+
This can then be encoded in a QR Code and used
|
|
28
|
+
to provision the Google Authenticator app
|
|
29
|
+
@param [String] name of the account
|
|
30
|
+
@param [Integer] initial_count starting counter value, defaults to 0
|
|
31
|
+
@param [String] the name of the OTP issuer; this will be the
|
|
32
|
+
organization title of the OTP entry in Authenticator
|
|
33
|
+
@return [String] provisioning uri
|
|
34
|
+
"""
|
|
35
|
+
return utils.build_uri(
|
|
36
|
+
self.secret,
|
|
37
|
+
name,
|
|
38
|
+
initial_count=initial_count,
|
|
39
|
+
issuer_name=issuer_name,
|
|
40
|
+
)
|
pyotp/otp.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
from __future__ import print_function, unicode_literals, division, absolute_import
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import hashlib
|
|
5
|
+
import hmac
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class OTP(object):
|
|
9
|
+
def __init__(self, s, digits=6, digest=hashlib.sha1):
|
|
10
|
+
"""
|
|
11
|
+
@param [String] secret in the form of base32
|
|
12
|
+
@option options digits [Integer] (6)
|
|
13
|
+
Number of integers in the OTP
|
|
14
|
+
Google Authenticate only supports 6 currently
|
|
15
|
+
@option options digest [Callable] (hashlib.sha1)
|
|
16
|
+
Digest used in the HMAC
|
|
17
|
+
Google Authenticate only supports 'sha1' currently
|
|
18
|
+
@returns [OTP] OTP instantiation
|
|
19
|
+
"""
|
|
20
|
+
self.digits = digits
|
|
21
|
+
self.digest = digest
|
|
22
|
+
self.secret = s
|
|
23
|
+
|
|
24
|
+
def generate_otp(self, input):
|
|
25
|
+
"""
|
|
26
|
+
@param [Integer] input the number used seed the HMAC
|
|
27
|
+
Usually either the counter, or the computed integer
|
|
28
|
+
based on the Unix timestamp
|
|
29
|
+
"""
|
|
30
|
+
hmac_hash = hmac.new(
|
|
31
|
+
self.byte_secret(),
|
|
32
|
+
self.int_to_bytestring(input),
|
|
33
|
+
self.digest,
|
|
34
|
+
).digest()
|
|
35
|
+
|
|
36
|
+
hmac_hash = bytearray(hmac_hash)
|
|
37
|
+
offset = hmac_hash[-1] & 0xf
|
|
38
|
+
code = ((hmac_hash[offset] & 0x7f) << 24 |
|
|
39
|
+
(hmac_hash[offset + 1] & 0xff) << 16 |
|
|
40
|
+
(hmac_hash[offset + 2] & 0xff) << 8 |
|
|
41
|
+
(hmac_hash[offset + 3] & 0xff))
|
|
42
|
+
return code % 10 ** self.digits
|
|
43
|
+
|
|
44
|
+
def byte_secret(self):
|
|
45
|
+
return base64.b32decode(self.secret, casefold=True)
|
|
46
|
+
|
|
47
|
+
@staticmethod
|
|
48
|
+
def int_to_bytestring(i, padding=8):
|
|
49
|
+
"""
|
|
50
|
+
Turns an integer to the OATH specified
|
|
51
|
+
bytestring, which is fed to the HMAC
|
|
52
|
+
along with the secret
|
|
53
|
+
"""
|
|
54
|
+
result = bytearray()
|
|
55
|
+
while i != 0:
|
|
56
|
+
result.append(i & 0xFF)
|
|
57
|
+
i >>= 8
|
|
58
|
+
# It's necessary to convert the final result from bytearray to bytes because
|
|
59
|
+
# the hmac functions in python 2.6 and 3.3 don't work with bytearray
|
|
60
|
+
return bytes(bytearray(reversed(result)).rjust(padding, b'\0'))
|
pyotp/totp.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
from __future__ import print_function, unicode_literals, division, absolute_import
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
import time
|
|
5
|
+
|
|
6
|
+
from pyotp import utils
|
|
7
|
+
from pyotp.otp import OTP
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TOTP(OTP):
|
|
11
|
+
def __init__(self, *args, **kwargs):
|
|
12
|
+
"""
|
|
13
|
+
@option options [Integer] interval (30) the time interval in seconds
|
|
14
|
+
for OTP This defaults to 30 which is standard.
|
|
15
|
+
"""
|
|
16
|
+
self.interval = kwargs.pop('interval', 30)
|
|
17
|
+
super(TOTP, self).__init__(*args, **kwargs)
|
|
18
|
+
|
|
19
|
+
def at(self, for_time):
|
|
20
|
+
"""
|
|
21
|
+
Accepts either a Unix timestamp integer or a Time object.
|
|
22
|
+
Time objects will be adjusted to UTC automatically
|
|
23
|
+
@param [Time/Integer] time the time to generate an OTP for
|
|
24
|
+
"""
|
|
25
|
+
if not isinstance(for_time, datetime.datetime):
|
|
26
|
+
for_time = datetime.datetime.fromtimestamp(int(for_time))
|
|
27
|
+
return self.generate_otp(self.timecode(for_time))
|
|
28
|
+
|
|
29
|
+
def now(self):
|
|
30
|
+
"""
|
|
31
|
+
Generate the current time OTP
|
|
32
|
+
@return [Integer] the OTP as an integer
|
|
33
|
+
"""
|
|
34
|
+
return self.generate_otp(self.timecode(datetime.datetime.now()))
|
|
35
|
+
|
|
36
|
+
def verify(self, otp, for_time=None):
|
|
37
|
+
"""
|
|
38
|
+
Verifies the OTP passed in against the current time OTP
|
|
39
|
+
@param [String/Integer] otp the OTP to check against
|
|
40
|
+
"""
|
|
41
|
+
if for_time is None:
|
|
42
|
+
for_time = datetime.datetime.now()
|
|
43
|
+
|
|
44
|
+
return utils.strings_equal(str(otp), str(self.at(for_time)))
|
|
45
|
+
|
|
46
|
+
def provisioning_uri(self, name, issuer_name=None):
|
|
47
|
+
"""
|
|
48
|
+
Returns the provisioning URI for the OTP
|
|
49
|
+
This can then be encoded in a QR Code and used
|
|
50
|
+
to provision the Google Authenticator app
|
|
51
|
+
@param [String] name of the account
|
|
52
|
+
@return [String] provisioning uri
|
|
53
|
+
"""
|
|
54
|
+
return utils.build_uri(self.secret, name, issuer_name=issuer_name)
|
|
55
|
+
|
|
56
|
+
def timecode(self, for_time):
|
|
57
|
+
i = time.mktime(for_time.timetuple())
|
|
58
|
+
return int(i / self.interval)
|
pyotp/utils.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
from __future__ import print_function, unicode_literals, division, absolute_import
|
|
2
|
+
|
|
3
|
+
try:
|
|
4
|
+
from urllib.parse import quote
|
|
5
|
+
except ImportError:
|
|
6
|
+
from urllib import quote
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def build_uri(secret, name, initial_count=None, issuer_name=None):
|
|
10
|
+
"""
|
|
11
|
+
Returns the provisioning URI for the OTP; works for either TOTP or HOTP.
|
|
12
|
+
|
|
13
|
+
This can then be encoded in a QR Code and used to provision the Google
|
|
14
|
+
Authenticator app.
|
|
15
|
+
|
|
16
|
+
For module-internal use.
|
|
17
|
+
|
|
18
|
+
See also:
|
|
19
|
+
http://code.google.com/p/google-authenticator/wiki/KeyUriFormat
|
|
20
|
+
|
|
21
|
+
@param [String] the hotp/totp secret used to generate the URI
|
|
22
|
+
@param [String] name of the account
|
|
23
|
+
@param [Integer] initial_count starting counter value, defaults to None.
|
|
24
|
+
If none, the OTP type will be assumed as TOTP.
|
|
25
|
+
@param [String] the name of the OTP issuer; this will be the
|
|
26
|
+
organization title of the OTP entry in Authenticator
|
|
27
|
+
@return [String] provisioning uri
|
|
28
|
+
"""
|
|
29
|
+
# initial_count may be 0 as a valid param
|
|
30
|
+
is_initial_count_present = (initial_count is not None)
|
|
31
|
+
|
|
32
|
+
otp_type = 'hotp' if is_initial_count_present else 'totp'
|
|
33
|
+
base = 'otpauth://%s/' % otp_type
|
|
34
|
+
|
|
35
|
+
if issuer_name:
|
|
36
|
+
issuer_name = quote(issuer_name)
|
|
37
|
+
base += '%s:' % issuer_name
|
|
38
|
+
|
|
39
|
+
uri = '%(base)s%(name)s?secret=%(secret)s' % {
|
|
40
|
+
'name': quote(name, safe='@'),
|
|
41
|
+
'secret': secret,
|
|
42
|
+
'base': base,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if is_initial_count_present:
|
|
46
|
+
uri += '&counter=%s' % initial_count
|
|
47
|
+
|
|
48
|
+
if issuer_name:
|
|
49
|
+
uri += '&issuer=%s' % issuer_name
|
|
50
|
+
|
|
51
|
+
return uri
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def strings_equal(s1, s2):
|
|
55
|
+
"""
|
|
56
|
+
Timing-attack resistant string comparison.
|
|
57
|
+
|
|
58
|
+
Normal comparison using == will short-circuit on the first mismatching
|
|
59
|
+
character. This avoids that by scanning the whole string, though we
|
|
60
|
+
still reveal to a timing attack whether the strings are the same
|
|
61
|
+
length.
|
|
62
|
+
"""
|
|
63
|
+
try:
|
|
64
|
+
# Python 3.3+ and 2.7.7+ include a timing-attack-resistant
|
|
65
|
+
# comparison function, which is probably more reliable than ours.
|
|
66
|
+
# Use it if available.
|
|
67
|
+
from hmac import compare_digest
|
|
68
|
+
|
|
69
|
+
return compare_digest(s1, s2)
|
|
70
|
+
except ImportError:
|
|
71
|
+
pass
|
|
72
|
+
|
|
73
|
+
if len(s1) != len(s2):
|
|
74
|
+
return False
|
|
75
|
+
|
|
76
|
+
differences = 0
|
|
77
|
+
for c1, c2 in zip(s1, s2):
|
|
78
|
+
differences |= ord(c1) ^ ord(c2)
|
|
79
|
+
return differences == 0
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
PyOTP - The Python One-Time Password Library
|
|
2
|
+
============================================
|
|
3
|
+
|
|
4
|
+
PyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA)
|
|
5
|
+
or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.
|
|
6
|
+
|
|
7
|
+
Open MFA standards are defined in `RFC 4226 <https://tools.ietf.org/html/rfc4226>`_ (HOTP: An HMAC-Based One-Time
|
|
8
|
+
Password Algorithm) and in `RFC 6238 <https://tools.ietf.org/html/rfc6238>`_ (TOTP: Time-Based One-Time Password
|
|
9
|
+
Algorithm). PyOTP implements server-side support for both of these standards. Client-side support can be enabled by
|
|
10
|
+
sending authentication codes to users over SMS or email (HOTP) or, for TOTP, by instructing users to use `Google
|
|
11
|
+
Authenticator <https://en.wikipedia.org/wiki/Google_Authenticator>`_, `Authy <https://www.authy.com/>`_, or another
|
|
12
|
+
compatible 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
|
+
|
|
15
|
+
Quick overview of using One Time Passwords on your phone
|
|
16
|
+
--------------------------------------------------------
|
|
17
|
+
|
|
18
|
+
* OTPs involve a shared secret, stored both on the phone and the server
|
|
19
|
+
* OTPs can be generated on a phone without internet connectivity
|
|
20
|
+
* OTPs should always be used as a second factor of authentication(if your phone is lost, you account is still secured with a password)
|
|
21
|
+
* Google Authenticator allows you to store multiple OTP secrets and provision those using a QR Code(no more typing in the secret)
|
|
22
|
+
|
|
23
|
+
Installation
|
|
24
|
+
------------
|
|
25
|
+
::
|
|
26
|
+
|
|
27
|
+
pip install pyotp
|
|
28
|
+
|
|
29
|
+
Usage
|
|
30
|
+
-----
|
|
31
|
+
|
|
32
|
+
Time based OTPs
|
|
33
|
+
~~~~~~~~~~~~~~~
|
|
34
|
+
::
|
|
35
|
+
|
|
36
|
+
totp = pyotp.TOTP('base32secret3232')
|
|
37
|
+
totp.now() # => 492039
|
|
38
|
+
|
|
39
|
+
# OTP verified for current time
|
|
40
|
+
totp.verify(492039) # => True
|
|
41
|
+
time.sleep(30)
|
|
42
|
+
totp.verify(492039) # => False
|
|
43
|
+
|
|
44
|
+
Counter-based OTPs
|
|
45
|
+
~~~~~~~~~~~~~~~~~~
|
|
46
|
+
::
|
|
47
|
+
|
|
48
|
+
hotp = pyotp.HOTP('base32secret3232')
|
|
49
|
+
hotp.at(0) # => 260182
|
|
50
|
+
hotp.at(1) # => 55283
|
|
51
|
+
hotp.at(1401) # => 316439
|
|
52
|
+
|
|
53
|
+
# OTP verified with a counter
|
|
54
|
+
hotp.verify(316439, 1401) # => True
|
|
55
|
+
hotp.verify(316439, 1402) # => False
|
|
56
|
+
|
|
57
|
+
Generating a base32 Secret Key
|
|
58
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
59
|
+
::
|
|
60
|
+
|
|
61
|
+
pyotp.random_base32() # returns a 16 character base32 secret. Compatible with Google Authenticator
|
|
62
|
+
|
|
63
|
+
Google Authenticator Compatible
|
|
64
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
65
|
+
|
|
66
|
+
The library works with the Google Authenticator iPhone and Android app, and also includes the ability to generate
|
|
67
|
+
provisioning URI's for use with the QR Code scanner built into the app::
|
|
68
|
+
|
|
69
|
+
totp.provisioning_uri("alice@google.com") # => 'otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP'
|
|
70
|
+
hotp.provisioning_uri("alice@google.com", 0) # => 'otpauth://hotp/alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=0'
|
|
71
|
+
|
|
72
|
+
This can then be rendered as a QR Code which can then be scanned and added to the users list of OTP credentials.
|
|
73
|
+
|
|
74
|
+
Working example
|
|
75
|
+
~~~~~~~~~~~~~~~
|
|
76
|
+
|
|
77
|
+
Scan the following barcode with your phone, using Google Authenticator
|
|
78
|
+
|
|
79
|
+

|
|
80
|
+
|
|
81
|
+
Now run the following and compare the output::
|
|
82
|
+
|
|
83
|
+
import pyotp
|
|
84
|
+
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
|
|
85
|
+
print "Current OTP: %s" % totp.now()
|
|
86
|
+
|
|
87
|
+
Links
|
|
88
|
+
~~~~~
|
|
89
|
+
|
|
90
|
+
* `Project home page (GitHub) <https://github.com/pyotp/pyotp>`_
|
|
91
|
+
* `Documentation (Read the Docs) <https://pyotp.readthedocs.org/en/latest/>`_
|
|
92
|
+
* `Package distribution (PyPI) <https://pypi.python.org/pypi/pyotp>`_
|
|
93
|
+
* `Change log <https://github.com/pyotp/pyotp/blob/master/Changes.rst>`_
|
|
94
|
+
* `RFC 4226: HOTP: An HMAC-Based One-Time Password <https://tools.ietf.org/html/rfc4226>`_
|
|
95
|
+
* `RFC 6238: TOTP: Time-Based One-Time Password Algorithm <https://tools.ietf.org/html/rfc6238>`_
|
|
96
|
+
* `ROTP <https://github.com/mdp/rotp>`_ - Original Ruby OTP library by `Mark Percival <https://github.com/mdp>`_
|
|
97
|
+
* `OTPHP <https://github.com/lelag/otphp>`_ - PHP port of ROTP by `Le Lag <https://github.com/lelag>`_
|
|
98
|
+
|
|
99
|
+
.. image:: https://img.shields.io/travis/pyotp/pyotp.svg
|
|
100
|
+
:target: https://travis-ci.org/pyotp/pyotp
|
|
101
|
+
.. image:: https://img.shields.io/coveralls/pyotp/pyotp.svg
|
|
102
|
+
:target: https://coveralls.io/r/pyotp/pyotp?branch=master
|
|
103
|
+
.. image:: https://img.shields.io/pypi/v/pyotp.svg
|
|
104
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
105
|
+
.. image:: https://img.shields.io/pypi/dm/pyotp.svg
|
|
106
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
107
|
+
.. image:: https://img.shields.io/pypi/l/pyotp.svg
|
|
108
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
109
|
+
.. image:: https://readthedocs.org/projects/pyotp/badge/?version=latest
|
|
110
|
+
:target: https://pyotp.readthedocs.org/
|
|
111
|
+
|
|
112
|
+
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.0
|
|
2
|
+
Name: pyotp
|
|
3
|
+
Version: 1.4.2
|
|
4
|
+
Summary: Python One Time Password Library
|
|
5
|
+
Home-page: https://github.com/pyotp/pyotp
|
|
6
|
+
Author: PyOTP contributors
|
|
7
|
+
Author-email: kislyuk@gmail.com
|
|
8
|
+
License: BSD License
|
|
9
|
+
Platform: MacOS X
|
|
10
|
+
Platform: Posix
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
13
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
14
|
+
Classifier: Operating System :: POSIX
|
|
15
|
+
Classifier: Programming Language :: Python
|
|
16
|
+
Classifier: Programming Language :: Python :: 2.7
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.4
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
|
|
21
|
+
PyOTP - The Python One-Time Password Library
|
|
22
|
+
============================================
|
|
23
|
+
|
|
24
|
+
PyOTP is a Python library for generating and verifying one-time passwords. It can be used to implement two-factor (2FA)
|
|
25
|
+
or multi-factor (MFA) authentication methods in web applications and in other systems that require users to log in.
|
|
26
|
+
|
|
27
|
+
Open MFA standards are defined in `RFC 4226 <https://tools.ietf.org/html/rfc4226>`_ (HOTP: An HMAC-Based One-Time
|
|
28
|
+
Password Algorithm) and in `RFC 6238 <https://tools.ietf.org/html/rfc6238>`_ (TOTP: Time-Based One-Time Password
|
|
29
|
+
Algorithm). PyOTP implements server-side support for both of these standards. Client-side support can be enabled by
|
|
30
|
+
sending authentication codes to users over SMS or email (HOTP) or, for TOTP, by instructing users to use `Google
|
|
31
|
+
Authenticator <https://en.wikipedia.org/wiki/Google_Authenticator>`_, `Authy <https://www.authy.com/>`_, or another
|
|
32
|
+
compatible app. Users can set up auth tokens in their apps easily by using their phone camera to scan `otpauth://
|
|
33
|
+
<https://github.com/google/google-authenticator/wiki/Key-Uri-Format>`_ QR codes provided by PyOTP.
|
|
34
|
+
|
|
35
|
+
Quick overview of using One Time Passwords on your phone
|
|
36
|
+
--------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
* OTPs involve a shared secret, stored both on the phone and the server
|
|
39
|
+
* OTPs can be generated on a phone without internet connectivity
|
|
40
|
+
* OTPs should always be used as a second factor of authentication(if your phone is lost, you account is still secured with a password)
|
|
41
|
+
* Google Authenticator allows you to store multiple OTP secrets and provision those using a QR Code(no more typing in the secret)
|
|
42
|
+
|
|
43
|
+
Installation
|
|
44
|
+
------------
|
|
45
|
+
::
|
|
46
|
+
|
|
47
|
+
pip install pyotp
|
|
48
|
+
|
|
49
|
+
Usage
|
|
50
|
+
-----
|
|
51
|
+
|
|
52
|
+
Time based OTPs
|
|
53
|
+
~~~~~~~~~~~~~~~
|
|
54
|
+
::
|
|
55
|
+
|
|
56
|
+
totp = pyotp.TOTP('base32secret3232')
|
|
57
|
+
totp.now() # => 492039
|
|
58
|
+
|
|
59
|
+
# OTP verified for current time
|
|
60
|
+
totp.verify(492039) # => True
|
|
61
|
+
time.sleep(30)
|
|
62
|
+
totp.verify(492039) # => False
|
|
63
|
+
|
|
64
|
+
Counter-based OTPs
|
|
65
|
+
~~~~~~~~~~~~~~~~~~
|
|
66
|
+
::
|
|
67
|
+
|
|
68
|
+
hotp = pyotp.HOTP('base32secret3232')
|
|
69
|
+
hotp.at(0) # => 260182
|
|
70
|
+
hotp.at(1) # => 55283
|
|
71
|
+
hotp.at(1401) # => 316439
|
|
72
|
+
|
|
73
|
+
# OTP verified with a counter
|
|
74
|
+
hotp.verify(316439, 1401) # => True
|
|
75
|
+
hotp.verify(316439, 1402) # => False
|
|
76
|
+
|
|
77
|
+
Generating a base32 Secret Key
|
|
78
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
79
|
+
::
|
|
80
|
+
|
|
81
|
+
pyotp.random_base32() # returns a 16 character base32 secret. Compatible with Google Authenticator
|
|
82
|
+
|
|
83
|
+
Google Authenticator Compatible
|
|
84
|
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
85
|
+
|
|
86
|
+
The library works with the Google Authenticator iPhone and Android app, and also includes the ability to generate
|
|
87
|
+
provisioning URI's for use with the QR Code scanner built into the app::
|
|
88
|
+
|
|
89
|
+
totp.provisioning_uri("alice@google.com") # => 'otpauth://totp/alice@google.com?secret=JBSWY3DPEHPK3PXP'
|
|
90
|
+
hotp.provisioning_uri("alice@google.com", 0) # => 'otpauth://hotp/alice@google.com?secret=JBSWY3DPEHPK3PXP&counter=0'
|
|
91
|
+
|
|
92
|
+
This can then be rendered as a QR Code which can then be scanned and added to the users list of OTP credentials.
|
|
93
|
+
|
|
94
|
+
Working example
|
|
95
|
+
~~~~~~~~~~~~~~~
|
|
96
|
+
|
|
97
|
+
Scan the following barcode with your phone, using Google Authenticator
|
|
98
|
+
|
|
99
|
+

|
|
100
|
+
|
|
101
|
+
Now run the following and compare the output::
|
|
102
|
+
|
|
103
|
+
import pyotp
|
|
104
|
+
totp = pyotp.TOTP("JBSWY3DPEHPK3PXP")
|
|
105
|
+
print "Current OTP: %s" % totp.now()
|
|
106
|
+
|
|
107
|
+
Links
|
|
108
|
+
~~~~~
|
|
109
|
+
|
|
110
|
+
* `Project home page (GitHub) <https://github.com/pyotp/pyotp>`_
|
|
111
|
+
* `Documentation (Read the Docs) <https://pyotp.readthedocs.org/en/latest/>`_
|
|
112
|
+
* `Package distribution (PyPI) <https://pypi.python.org/pypi/pyotp>`_
|
|
113
|
+
* `Change log <https://github.com/pyotp/pyotp/blob/master/Changes.rst>`_
|
|
114
|
+
* `RFC 4226: HOTP: An HMAC-Based One-Time Password <https://tools.ietf.org/html/rfc4226>`_
|
|
115
|
+
* `RFC 6238: TOTP: Time-Based One-Time Password Algorithm <https://tools.ietf.org/html/rfc6238>`_
|
|
116
|
+
* `ROTP <https://github.com/mdp/rotp>`_ - Original Ruby OTP library by `Mark Percival <https://github.com/mdp>`_
|
|
117
|
+
* `OTPHP <https://github.com/lelag/otphp>`_ - PHP port of ROTP by `Le Lag <https://github.com/lelag>`_
|
|
118
|
+
|
|
119
|
+
.. image:: https://img.shields.io/travis/pyotp/pyotp.svg
|
|
120
|
+
:target: https://travis-ci.org/pyotp/pyotp
|
|
121
|
+
.. image:: https://img.shields.io/coveralls/pyotp/pyotp.svg
|
|
122
|
+
:target: https://coveralls.io/r/pyotp/pyotp?branch=master
|
|
123
|
+
.. image:: https://img.shields.io/pypi/v/pyotp.svg
|
|
124
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
125
|
+
.. image:: https://img.shields.io/pypi/dm/pyotp.svg
|
|
126
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
127
|
+
.. image:: https://img.shields.io/pypi/l/pyotp.svg
|
|
128
|
+
:target: https://pypi.python.org/pypi/pyotp
|
|
129
|
+
.. image:: https://readthedocs.org/projects/pyotp/badge/?version=latest
|
|
130
|
+
:target: https://pyotp.readthedocs.org/
|
|
131
|
+
|
|
132
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
pyotp/__init__.py,sha256=l3t-6ZXG4K9JrF6n_NzGfHeDl5Fxje5z4ZrrRetz3mA,446
|
|
2
|
+
pyotp/hotp.py,sha256=HNpFU0E9h_nUg-47yiVGtvOwxRKs-_8YZCRzJgoD_zQ,1380
|
|
3
|
+
pyotp/otp.py,sha256=Va2Sf-ITUf3VDYz4kra5nMZ_KKVYufDyTV0KlRnYUOw,2033
|
|
4
|
+
pyotp/totp.py,sha256=mLZL-G7IdIoEk-BcdyTL-EzXonK94Gkr_xtEa2eB8b8,1971
|
|
5
|
+
pyotp/utils.py,sha256=_wqL2s2pE9jHFpoXaBWELlngWoktPYLOZ5vYRp0q7i0,2349
|
|
6
|
+
pyotp-1.4.2.dist-info/DESCRIPTION.rst,sha256=5zAiiWMILqZD0S3kDsNUlYoNdl2aC6yaquMM2cCRAE0,4756
|
|
7
|
+
pyotp-1.4.2.dist-info/METADATA,sha256=CBg_xdU--8D3yhkrRIgT0_xnBVe_5z6VhbKcy5VmqxY,5452
|
|
8
|
+
pyotp-1.4.2.dist-info/metadata.json,sha256=0J2O--25ja42URBYCS3wfKd16kPxHRMdDebf9kvyn-s,789
|
|
9
|
+
pyotp-1.4.2.dist-info/RECORD,,
|
|
10
|
+
pyotp-1.4.2.dist-info/top_level.txt,sha256=rFDgz6Sm4nbkYk6tlSRiMcJPyYdnY6LcZiAaYTxVc5k,6
|
|
11
|
+
pyotp-1.4.2.dist-info/WHEEL,sha256=6lxp_S3wZGmTBtGMVmNNLyvKFcp7HqQw2Wn4YYk-Suo,110
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"license": "BSD License", "document_names": {"description": "DESCRIPTION.rst"}, "name": "pyotp", "metadata_version": "2.0", "contacts": [{"role": "author", "email": "kislyuk@gmail.com", "name": "PyOTP contributors"}], "generator": "bdist_wheel (0.23.0)", "summary": "Python One Time Password Library", "project_urls": {"Home": "https://github.com/pyotp/pyotp"}, "platform": "MacOS X", "version": "1.4.2", "classifiers": ["Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: MacOS :: MacOS X", "Operating System :: POSIX", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Topic :: Software Development :: Libraries :: Python Modules"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
pyotp
|