|
3 | 3 | User Guide |
4 | 4 | ========== |
5 | 5 |
|
| 6 | +Here are the detail guide on how to use TOTP and HOTP. |
| 7 | + |
6 | 8 | .. _totp: |
7 | 9 |
|
8 | 10 | Use TOTP |
9 | 11 | -------- |
10 | 12 |
|
11 | | -Use ``base32`` Secret |
12 | | -~~~~~~~~~~~~~~~~~~~~~ |
| 13 | +TOTP is a **Time-Based One-Time Password Algorithm** defined in |
| 14 | +RFC6238_. |
13 | 15 |
|
14 | | -Add to Authenticator App |
15 | | -~~~~~~~~~~~~~~~~~~~~~~~~ |
| 16 | +.. _RFC6238: https://www.rfc-editor.org/rfc/rfc6238 |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + import time |
| 21 | + from otpauth import TOTP |
| 22 | +
|
| 23 | + totp = TOTP(b"user-secret") |
| 24 | +
|
| 25 | + # generate a code for now |
| 26 | + code: int = totp.generate() |
| 27 | +
|
| 28 | + totp.verify(code) # True |
| 29 | +
|
| 30 | + time.sleep(31) |
| 31 | + totp.verify(code) # False |
| 32 | +
|
| 33 | +Most of the time, you DO NOT NEED TO change the default configuration, |
| 34 | +but if you want, here is how: |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + totp = TOTP(b"user-secret", digit=8, algorithm="SHA256") |
16 | 39 |
|
17 | 40 | .. _hotp: |
18 | 41 |
|
19 | 42 | Use HOTP |
20 | 43 | -------- |
| 44 | + |
| 45 | +HOTP is a **An HMAC-Based One-Time Password Algorithm** defined in |
| 46 | +RFC4226_. |
| 47 | + |
| 48 | +.. _RFC4226: https://www.rfc-editor.org/rfc/rfc4226 |
| 49 | + |
| 50 | + |
| 51 | +.. code-block:: python |
| 52 | +
|
| 53 | + from otpauth import HOTP |
| 54 | +
|
| 55 | + htop = HOTP(b"user-secret") |
| 56 | +
|
| 57 | + # generate a code for now |
| 58 | + code: int = htop.generate(4) |
| 59 | +
|
| 60 | + htop.verify(code, 4) # True |
| 61 | + htop.verify(code, 6) # False |
| 62 | +
|
| 63 | +.. tip:: TOTP is based on HOTP, most of the time you would use HOTP. |
| 64 | + |
| 65 | +Use ``base32`` Secret |
| 66 | +--------------------- |
| 67 | + |
| 68 | +When the **secret** you have is a ``base32`` encoded string, you don't have to |
| 69 | +decode it yourself. Instead, you can create the :class:`TOTP` and :class:`HTOP` |
| 70 | +instance with: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + totp = TOTP.from_b32encode("OB4XI2DPNY") |
| 75 | + hotp = HOTP.from_b32encode("OB4XI2DPNY") |
| 76 | +
|
| 77 | +It works well with and without the padding ``=``. e.g.: |
| 78 | + |
| 79 | +.. code-block:: python |
| 80 | +
|
| 81 | + totp = TOTP.from_b32encode("OB4XI2DPNY======") |
| 82 | + hotp = HOTP.from_b32encode("OB4XI2DPNY======") |
| 83 | +
|
| 84 | +This method also accepts secret in bytes. |
| 85 | + |
| 86 | +Add to Authenticator App |
| 87 | +------------------------ |
| 88 | + |
| 89 | +There is a method ``.to_uri`` to generate the URI that most authenticator apps |
| 90 | +support. The `Key URI Format`_ looks like: |
| 91 | + |
| 92 | + |
| 93 | +.. code-block:: text |
| 94 | +
|
| 95 | + otpauth://TYPE/LABEL?PARAMETERS |
| 96 | +
|
| 97 | +.. _`Key URI Format`: https://github.com/google/google-authenticator/wiki/Key-Uri-Format |
| 98 | + |
| 99 | +An example of :meth:`TOTP.to_uri`: |
| 100 | + |
| 101 | +.. code-block:: python |
| 102 | +
|
| 103 | + >>> totp = TOTP.from_b32encode("OB4XI2DPNY") |
| 104 | + >>> totp.to_uri("Typlog:lepture.com", "Authlib") |
| 105 | + "otpauth://totp/Typlog:lepture.com?secret=OB4XI2DPNY&issuer=Authlib&algorithm=SHA1&digits=6&period=30" |
| 106 | +
|
| 107 | +Here shows the QR code of this URI: |
| 108 | + |
| 109 | +.. figure:: _static/example-qr.png |
| 110 | + :align: center |
| 111 | + :width: 160 |
| 112 | + :height: 160 |
| 113 | + |
| 114 | + You can test with an authenticator app. |
0 commit comments