Skip to content

Commit

Permalink
Rename variables named *algorithm
Browse files Browse the repository at this point in the history
Fixes #47 (PR #68), i.e. unshadows the variable in a few places.

It also clarifies variables meaning in other, unrelated places, that
didn't cause the variable shadowing or any other (known) issue.
  • Loading branch information
Toreno96 authored Dec 15, 2023
1 parent 2ff3bdc commit 6592be2
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
6 changes: 3 additions & 3 deletions onetimepass/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from onetimepass import settings
from onetimepass.base_model import BaseModel
from onetimepass.db import exceptions
from onetimepass.enum import OTPAlgorithm
from onetimepass.enum import HashAlgorithm
from onetimepass.enum import OTPType

"""
Expand Down Expand Up @@ -70,7 +70,7 @@ class TOTPParams(BaseModel):
class AliasSchema(BaseModel):
secret: str
digits_count: int
hash_algorithm: OTPAlgorithm
hash_algorithm: HashAlgorithm
otp_type: OTPType
params: typing.Union[
HOTPParams, TOTPParams
Expand Down Expand Up @@ -123,7 +123,7 @@ def add_totp_alias(
issuer: str,
secret: str,
digits_count: int,
hash_algorithm: OTPAlgorithm,
hash_algorithm: HashAlgorithm,
initial_time: datetime.datetime,
time_step_seconds: int = settings.DEFAULT_TIME_STEP_SECONDS,
):
Expand Down
2 changes: 1 addition & 1 deletion onetimepass/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OTPType(CaseInsensitiveStrEnum):
TOTP = "TOTP"


class OTPAlgorithm(CaseInsensitiveStrEnum):
class HashAlgorithm(CaseInsensitiveStrEnum):
SHA1 = "SHA1"
SHA256 = "SHA256"
SHA512 = "SHA512"
Expand Down
38 changes: 19 additions & 19 deletions onetimepass/otp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import pydantic
from rich.console import Console

from onetimepass import algorithm
from onetimepass import master_key
from onetimepass import otp_algorithm
from onetimepass import settings
from onetimepass.db import BaseDB
from onetimepass.db import DatabaseSchema
Expand All @@ -27,7 +27,7 @@
from onetimepass.db.models import HOTPParams
from onetimepass.db.models import TOTPParams
from onetimepass.enum import ExportFormat
from onetimepass.enum import OTPAlgorithm
from onetimepass.enum import HashAlgorithm
from onetimepass.enum import OTPType
from onetimepass.exceptions import UnhandledFormatException
from onetimepass.exceptions import UnhandledOTPTypeException
Expand Down Expand Up @@ -155,8 +155,8 @@ def show(ctx: click.Context, alias: str, wait: int | None, minimum_verbose: bool
raise ClickUsageError(f"Alias: {alias} does not exist")
if alias_data.otp_type == OTPType.TOTP:
if wait is not None:
remaining_seconds = algorithm.get_seconds_remaining(
algorithm.TOTPParameters(
remaining_seconds = otp_algorithm.get_seconds_remaining(
otp_algorithm.TOTPParameters(
secret=base64.b32decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
Expand All @@ -167,31 +167,31 @@ def show(ctx: click.Context, alias: str, wait: int | None, minimum_verbose: bool
with Console().status("Waiting for the next OTP..."):
time.sleep(remaining_seconds)
# Reinitialize parameters to get valid result
params = algorithm.TOTPParameters(
params = otp_algorithm.TOTPParameters(
secret=base64.b32decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
time_step_seconds=alias_data.params.time_step_seconds,
)
if minimum_verbose:
click.echo(f"{algorithm.totp(params):0{params.digits_count}}")
click.echo(f"{otp_algorithm.totp(params):0{params.digits_count}}")
else:
echo_alias(
alias,
algorithm.totp(params),
algorithm.get_seconds_remaining(params),
otp_algorithm.totp(params),
otp_algorithm.get_seconds_remaining(params),
ctx.obj["color"],
params.digits_count,
)
elif alias_data.otp_type == OTPType.HOTP:
alias_data.params.counter += 1
params = algorithm.HOTPParameters(
params = otp_algorithm.HOTPParameters(
secret=base64.b32decode(alias_data.secret),
digits_count=alias_data.digits_count,
hash_algorithm=alias_data.hash_algorithm,
counter=alias_data.params.counter,
)
echo_hotp_alias(alias, algorithm.hotp(params), alias_data.digits_count)
echo_hotp_alias(alias, otp_algorithm.hotp(params), alias_data.digits_count)
# This have to be the last step of the command, to make sure the
# database is not modified if there is any unexpected exception.
# Alternatively, there should be commit/rollback mechanism added to the
Expand Down Expand Up @@ -370,7 +370,7 @@ def add_uri(ctx: click.Context, alias: str):
issuer=uri_parsed.parameters.issuer or uri_parsed.label.issuer,
secret=uri_parsed.parameters.secret,
digits_count=uri_parsed.parameters.digits,
hash_algorithm=uri_parsed.parameters.algorithm,
hash_algorithm=uri_parsed.parameters.hash_algorithm,
params=params,
)
except pydantic.ValidationError as e:
Expand All @@ -387,12 +387,12 @@ def default_add_otp_options(fn):
@click.option("label", "-l", "--label")
@click.option("issuer", "-i", "--issuer")
@click.option(
"algorithm",
"hash_algorithm",
"-a",
"--algorithm",
"--hash_algorithm",
show_default=True,
type=click.Choice([i.value for i in OTPAlgorithm]),
default=OTPAlgorithm.SHA1.value,
type=click.Choice([i.value for i in HashAlgorithm]),
default=HashAlgorithm.SHA1.value,
)
@click.option(
"digits_count",
Expand Down Expand Up @@ -425,7 +425,7 @@ def add_hotp(
alias: str,
label: str | None,
issuer: str | None,
algorithm: str,
hash_algorithm: str,
digits_count: int,
counter: int,
):
Expand All @@ -448,7 +448,7 @@ def add_hotp(
issuer=issuer,
secret=input_secret,
digits_count=digits_count,
hash_algorithm=OTPAlgorithm(algorithm),
hash_algorithm=HashAlgorithm(hash_algorithm),
params=HOTPParams(counter=counter),
)
except pydantic.ValidationError as e:
Expand Down Expand Up @@ -484,7 +484,7 @@ def add_totp(
alias: str,
label: str | None,
issuer: str | None,
algorithm: str,
hash_algorithm: str,
digits_count: int,
period: int,
initial_time: datetime.datetime,
Expand All @@ -508,7 +508,7 @@ def add_totp(
issuer=issuer,
secret=input_secret,
digits_count=digits_count,
hash_algorithm=OTPAlgorithm(algorithm),
hash_algorithm=HashAlgorithm(hash_algorithm),
params=TOTPParams(initial_time=initial_time, time_step_seconds=period),
)
except pydantic.ValidationError as e:
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion onetimepass/otpauth/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class BaseUriParameters(BaseModel, extra=Extra.forbid):
secret: str
issuer: str | None
algorithm: Literal["SHA1", "SHA256", "SHA512"] = "SHA1"
hash_algorithm: Literal["SHA1", "SHA256", "SHA512"] = "SHA1"
digits: int

@validator("issuer")
Expand Down
10 changes: 5 additions & 5 deletions tests/test_algorithm.py → tests/test_otp_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import itertools
import unittest

from onetimepass import algorithm
from onetimepass import otp_algorithm


def get_secret(hash_algorithm: str) -> bytes:
Expand Down Expand Up @@ -50,8 +50,8 @@ def test_hotp(self):
]
for counter, expected_hotp in enumerate(expected_hotps, start=counter_start):
with self.subTest(i=counter):
hotp = algorithm.hotp(
algorithm.HOTPParameters(
hotp = otp_algorithm.hotp(
otp_algorithm.HOTPParameters(
secret=get_secret("sha1"),
digits_count=6,
hash_algorithm="sha1",
Expand All @@ -65,7 +65,7 @@ class TestTOTP(unittest.TestCase):
# Based on https://tools.ietf.org/html/rfc6238#appendix-B

@dataclasses.dataclass
class TestTOTPParameters(algorithm.TOTPParameters):
class TestTOTPParameters(otp_algorithm.TOTPParameters):
secret: bytes = dataclasses.field(init=False)
digits_count: int = 8
hash_algorithm: str = "sha1"
Expand Down Expand Up @@ -236,5 +236,5 @@ def test_to_hotp_parameters(self):
def test_totp(self):
for test_vector in self.test_vectors:
with self.subTest(i=test_vector.parameters.current_time):
totp = algorithm.totp(test_vector.parameters)
totp = otp_algorithm.totp(test_vector.parameters)
self.assertEqual(totp, test_vector.expected_totp)

0 comments on commit 6592be2

Please sign in to comment.