Skip to content

Commit

Permalink
Fixes for docs rendering (pyauth#145)
Browse files Browse the repository at this point in the history
* adds `sphinx.ext.intersphinx` to link types back to
https://docs.python.org/3/library/typing.html
* Remove a default value in the `TOTP` / `HOTP` classes so the type
hints get rendered correctly (this is likely a bug in `sphinx` and just
working around it).
  • Loading branch information
alexfikl authored Nov 12, 2022
1 parent 8ced63e commit 03045d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
release = ""
language = "en"
master_doc = "index"
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]
extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode", "sphinx.ext.intersphinx"]
source_suffix = [".rst", ".md"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
pygments_style = "sphinx"
intersphinx_mapping = {
"https://docs.python.org/3": None,
}

if "readthedocs.org" in os.getcwd().split("/"):
with open("index.rst", "w") as fh:
Expand Down
7 changes: 5 additions & 2 deletions src/pyotp/hotp.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
self,
s: str,
digits: int = 6,
digest: Any = hashlib.sha1,
digest: Any = None,
name: Optional[str] = None,
issuer: Optional[str] = None,
initial_count: int = 0,
Expand All @@ -23,10 +23,13 @@ def __init__(
:param s: secret in base32 format
:param initial_count: starting HMAC counter value, defaults to 0
:param digits: number of integers in the OTP. Some apps expect this to be 6 digits, others support more.
:param digest: digest function to use in the HMAC (expected to be sha1)
:param digest: digest function to use in the HMAC (expected to be SHA1)
:param name: account name
:param issuer: issuer
"""
if digest is None:
digest = hashlib.sha1

self.initial_count = initial_count
super().__init__(s=s, digits=digits, digest=digest, name=name, issuer=issuer)

Expand Down
11 changes: 8 additions & 3 deletions src/pyotp/totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
self,
s: str,
digits: int = 6,
digest: Any = hashlib.sha1,
digest: Any = None,
name: Optional[str] = None,
issuer: Optional[str] = None,
interval: int = 30,
Expand All @@ -26,18 +26,23 @@ def __init__(
:param s: secret in base32 format
:param interval: the time interval in seconds for OTP. This defaults to 30.
:param digits: number of integers in the OTP. Some apps expect this to be 6 digits, others support more.
:param digest: digest function to use in the HMAC (expected to be sha1)
:param digest: digest function to use in the HMAC (expected to be SHA1)
:param name: account name
:param issuer: issuer
"""
if digest is None:
digest = hashlib.sha1

self.interval = interval
super().__init__(s=s, digits=digits, digest=digest, name=name, issuer=issuer)

def at(self, for_time: Union[int, datetime.datetime], counter_offset: int = 0) -> str:
"""
Accepts either a Unix timestamp integer or a datetime object.
To get the time until the next timecode change (seconds until the current OTP expires), use this instead::
To get the time until the next timecode change (seconds until the current OTP expires), use this instead:
.. code:: python
totp = pyotp.TOTP(...)
time_remaining = totp.interval - datetime.datetime.now().timestamp() % totp.interval
Expand Down

0 comments on commit 03045d5

Please sign in to comment.