Skip to content

Commit 667c1fa

Browse files
committed
docs: add recipes and contribution guide
1 parent 3d001fb commit 667c1fa

File tree

9 files changed

+258
-17
lines changed

9 files changed

+258
-17
lines changed

Makefile

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
.PHONY: clean-pyc clean-build docs test coverage
1+
.PHONY: clean-pyc clean-build docs
22

3-
test:
4-
@nosetests -s
5-
6-
coverage:
7-
@rm -f .coverage
8-
@nosetests --with-coverage --cover-package=otpauth --cover-html
93

104
clean: clean-build clean-pyc clean-docs
115

@@ -27,6 +21,3 @@ clean-docs:
2721

2822
docs:
2923
@$(MAKE) -C docs html
30-
31-
publish-docs: docs
32-
@python setup.py upload_sphinx --upload-dir=docs/_build/html/

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,27 @@ One time password implementations in Python. HOTP and TOTP.
99
[![codecov](https://badgen.net/codecov/c/github/authlib/otpauth)](https://codecov.io/gh/authlib/otpauth)
1010

1111
</div>
12+
13+
Usage
14+
-----
15+
16+
```python
17+
import otpauth
18+
19+
totp = otpauth.HOTP(b"user-secret")
20+
21+
# generate a code for now
22+
code: int = totp.generate()
23+
24+
# you may want to convert it to string
25+
str_code: str = totp.string_code(code)
26+
27+
# verify the code
28+
totp.verify(code) # => True
29+
totp.verify(str_code) # => True
30+
```
31+
32+
Copyright
33+
---------
34+
35+
2013, Hsiaoming Yang. Under BSD-3 license.

README.rst

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,25 @@ OTP Auth
33

44
One time password implementations in Python. HOTP and TOTP.
55

6-
Documentation: https://otp.authlib.org/
7-
GitHub: https://github.com/authlib/otpauth
6+
**Documentation**: https://otp.authlib.org/
7+
**GitHub**: https://github.com/authlib/otpauth
8+
9+
Usage
10+
-----
11+
12+
Most of the time, you would use a time based one time password. You can generate and
13+
verify the token with :class:`TOTP`::
14+
15+
import otpauth
16+
17+
totp = otpauth.HOTP(b"user-secret")
18+
19+
# generate a code for now
20+
code: int = totp.generate()
21+
22+
# you may want to convert it to string
23+
str_code: str = totp.string_code(code)
24+
25+
# verify the code
26+
totp.verify(code) # => True
27+
totp.verify(str_code) # => True

docs/_static/example-qr.png

4.21 KB
Loading

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Here are the list of API reference; it might be helpful for developers.
88
HOTP
99
----
1010

11+
Implementation of RFC4226, An HMAC-Based One-Time Password Algorithm.
12+
1113
.. autoclass:: HOTP
1214
:members:
1315
:inherited-members:
@@ -17,6 +19,8 @@ HOTP
1719
TOTP
1820
----
1921

22+
Implementation of RFC6238, Time-Based One-Time Password Algorithm.
23+
2024
.. autoclass:: TOTP
2125
:members:
2226
:inherited-members:

docs/conf.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
'twitter_site': 'authlib',
4949
'twitter_creator': 'lepture',
5050
'twitter_url': 'https://twitter.com/authlib',
51-
'github_url': 'https://github.com/lepture/otpauth',
51+
'github_url': 'https://github.com/authlib/otpauth',
52+
'discord_url': 'https://discord.gg/HvBVAeNAaV',
5253
'nav_links': [
5354
{'title': 'Sponsor me', 'url': 'https://github.com/sponsors/lepture'}
5455
]
@@ -63,3 +64,9 @@
6364
html_show_sourcelink = False
6465

6566
# html_favicon = "_static/light-icon.svg"
67+
68+
html_context = {
69+
"source_type": "github",
70+
"source_user": "authlib",
71+
"source_repo": "otpauth",
72+
}

docs/contribute.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,90 @@
11
Contributing
22
============
3+
4+
Thank you for considering contributing to this little library!
5+
6+
7+
Support questions
8+
-----------------
9+
10+
Please don't use the issue tracker for this. The issue tracker is a tool
11+
to address bugs and feature requests. Use one of the following resources
12+
for questions about Authlib and its related libraries.
13+
14+
- The ``#questions`` channel on our Discord chat:
15+
https://discord.gg/HvBVAeNAaV
16+
- Ask on our `GitHub Discussions`_ for long term discussion or larger
17+
questions.
18+
19+
.. _GitHub Discussions: https://github.com/authlib/otpauth/discussions
20+
21+
22+
Reporting issues
23+
----------------
24+
25+
Include the following information in your post:
26+
27+
- Describe what you expected to happen.
28+
- If possible, include a `minimal reproducible example`_ to help us
29+
identify the issue. This also helps check that the issue is not with
30+
your own code.
31+
- Describe what actually happened. Include the full traceback if there
32+
was an exception.
33+
- List your Python and ``otpauth`` versions. If possible, check if this
34+
issue is already fixed in the latest releases or the latest code in
35+
the repository.
36+
37+
.. _minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example
38+
39+
40+
Submitting patches
41+
------------------
42+
43+
If you found something you can improve, a "Pull Request" is always
44+
welcoming. Here are something you need to notice before submitting
45+
the PR.
46+
47+
If there is not an open issue for what you want to submit, prefer
48+
opening one for discussion before working on a PR. You can work on any
49+
issue that doesn't have an open PR linked to it or a maintainer assigned
50+
to it. These show up in the sidebar. No need to ask if you can work on
51+
an issue that interests you.
52+
53+
Include the following in your patch:
54+
55+
- Use `Black`_ to format your code. This and other tools will run
56+
automatically if you install `pre-commit`_ using the instructions
57+
below.
58+
- Include tests if your patch adds or changes code. Make sure the test
59+
fails without your patch.
60+
- Update any relevant docs pages and docstrings. Docs pages and
61+
docstrings should be wrapped at 72 characters.
62+
- Add an entry in ``CHANGES.rst``. Use the same style as other
63+
entries. Also include ``.. versionchanged::`` inline changelogs in
64+
relevant docstrings.
65+
66+
.. _Black: https://black.readthedocs.io
67+
.. _pre-commit: https://pre-commit.com
68+
69+
Running the tests
70+
~~~~~~~~~~~~~~~~~
71+
72+
Run the basic test suite with pytest.
73+
74+
.. code-block:: text
75+
76+
$ pytest
77+
78+
79+
Updating the docs
80+
~~~~~~~~~~~~~~~~~
81+
82+
When something has been changed, document them in the docs. You may need
83+
to add a change log in the ``changelog.rst`` file.
84+
85+
Conventional Commits
86+
~~~~~~~~~~~~~~~~~~~~
87+
88+
We are using `Conventional Commits <https://www.conventionalcommits.org/en/v1.0.0/>`_.
89+
When you ``git commit`` some changes, please follow the "Conventional Commits" for the
90+
commit message.

docs/index.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
OTP Auth
22
========
33

4+
One time password implementations in Python. HOTP and TOTP.
5+
6+
.. note::
7+
8+
This is a redesigned "v2" of otpauth. Get "v1" documentation
9+
at https://pythonhosted.org/otpauth/.
10+
11+
"v1" is considered stable, you may still use it. But "v2" has some
12+
enhancements:
13+
14+
- **Type hints**: your editor will love it.
15+
- **No Python 2**: clean code without compatible patches.
16+
417
Installation
518
------------
619

docs/recipes.rst

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,112 @@
33
User Guide
44
==========
55

6+
Here are the detail guide on how to use TOTP and HOTP.
7+
68
.. _totp:
79

810
Use TOTP
911
--------
1012

11-
Use ``base32`` Secret
12-
~~~~~~~~~~~~~~~~~~~~~
13+
TOTP is a **Time-Based One-Time Password Algorithm** defined in
14+
RFC6238_.
1315

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")
1639
1740
.. _hotp:
1841

1942
Use HOTP
2043
--------
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

Comments
 (0)