diff --git a/onetimepass/db/models.py b/onetimepass/db/models.py index 56b883b..3ec76ce 100644 --- a/onetimepass/db/models.py +++ b/onetimepass/db/models.py @@ -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 """ @@ -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 @@ -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, ): diff --git a/onetimepass/enum.py b/onetimepass/enum.py index 69669f9..3a1d85c 100644 --- a/onetimepass/enum.py +++ b/onetimepass/enum.py @@ -22,7 +22,7 @@ class OTPType(CaseInsensitiveStrEnum): TOTP = "TOTP" -class OTPAlgorithm(CaseInsensitiveStrEnum): +class HashAlgorithm(CaseInsensitiveStrEnum): SHA1 = "SHA1" SHA256 = "SHA256" SHA512 = "SHA512" diff --git a/onetimepass/otp.py b/onetimepass/otp.py index f32f997..11db9c1 100644 --- a/onetimepass/otp.py +++ b/onetimepass/otp.py @@ -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 @@ -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 @@ -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, @@ -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 @@ -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: @@ -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", @@ -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, ): @@ -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: @@ -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, @@ -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: diff --git a/onetimepass/algorithm.py b/onetimepass/otp_algorithm.py similarity index 100% rename from onetimepass/algorithm.py rename to onetimepass/otp_algorithm.py diff --git a/onetimepass/otpauth/schemas.py b/onetimepass/otpauth/schemas.py index 23ec68a..302424d 100644 --- a/onetimepass/otpauth/schemas.py +++ b/onetimepass/otpauth/schemas.py @@ -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") diff --git a/tests/test_algorithm.py b/tests/test_otp_algorithm.py similarity index 96% rename from tests/test_algorithm.py rename to tests/test_otp_algorithm.py index 9a59558..3b73c29 100644 --- a/tests/test_algorithm.py +++ b/tests/test_otp_algorithm.py @@ -6,7 +6,7 @@ import itertools import unittest -from onetimepass import algorithm +from onetimepass import otp_algorithm def get_secret(hash_algorithm: str) -> bytes: @@ -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", @@ -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" @@ -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)