-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A new class is added in a `pyotp.contrib.steam` submodule. It's a subclass of `pyotp.TOTP` and overrides the `generate_otp` method. (all as mentioned in the issue [here](pyauth/pyotp#127 (comment))) Also added some tests and included the new class in the API Documentation section. Co-authored-by: Andrey Kislyuk <kislyuk@gmail.com>
- Loading branch information
Showing
6 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .steam import Steam # noqa:F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Optional | ||
import hashlib | ||
|
||
from ..totp import TOTP | ||
|
||
STEAM_CHARS = "23456789BCDFGHJKMNPQRTVWXY" # steam's custom alphabet | ||
STEAM_DEFAULT_DIGITS = 5 # Steam TOTP code length | ||
|
||
|
||
class Steam(TOTP): | ||
""" | ||
Steam's custom TOTP. Subclass of `pyotp.totp.TOTP`. | ||
""" | ||
|
||
def __init__(self, s: str, name: Optional[str] = None, | ||
issuer: Optional[str] = None, interval: int = 30) -> None: | ||
""" | ||
:param s: secret in base32 format | ||
:param interval: the time interval in seconds for OTP. This defaults to 30. | ||
:param name: account name | ||
:param issuer: issuer | ||
""" | ||
self.interval = interval | ||
super().__init__(s=s, digits=10, digest=hashlib.sha1, name=name, issuer=issuer) | ||
|
||
def generate_otp(self, input: int) -> str: | ||
""" | ||
:param input: the HMAC counter value to use as the OTP input. | ||
Usually either the counter, or the computed integer based on the Unix timestamp | ||
""" | ||
str_code = super().generate_otp(input) | ||
int_code = int(str_code) | ||
|
||
steam_code = "" | ||
total_chars = len(STEAM_CHARS) | ||
|
||
for _ in range(STEAM_DEFAULT_DIGITS): | ||
pos = int_code % total_chars | ||
char = STEAM_CHARS[int(pos)] | ||
steam_code += char | ||
int_code //= total_chars | ||
|
||
return steam_code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters