Skip to content

Commit

Permalink
totp: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xunzhou authored and briantist committed Jun 17, 2023
1 parent 36306e1 commit d79410a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
12 changes: 6 additions & 6 deletions docs/usage/secrets_engines/totp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Create Key
'mykey',
url="otpauth://totp/Google:test@gmail.com?secret=Y64VEVMBTSXCYIWRSHRNDZW62MPGVU2G&issuer=Google"
)
print('New TOTP key response: {}'.format(create_key_response))
print(f"New TOTP key response: {create_key_response}")
Read Key
Expand All @@ -29,7 +29,7 @@ Read Key
client = hvac.Client()
read_key_response = client.secrets.totp.read_key('mykey')
print('Current TOTP key: {}'.format(read_key_response['data']))
print(f"Current TOTP key: {read_key_response['data']}")
List Keys
Expand All @@ -43,7 +43,7 @@ List Keys
client = hvac.Client()
list_keys_response = client.secrets.totp.list_keys()
print('Current keys : {}'.format(list_keys_response['data']['keys']))
print(f"Current keys : {list_keys_response['data']['keys']}")
Delete Key
Expand All @@ -57,7 +57,7 @@ Delete Key
client = hvac.Client()
delete_key_response = client.secrets.totp.delete_key('mykey')
print('Delete TOTP key response: {}'.format(delete_key_response))
print(f"Delete TOTP key response: {delete_key_response}")
Generate Code
Expand All @@ -72,7 +72,7 @@ Generate Code
client = hvac.Client()
generate_code_response = client.secrets.totp.generate_code('mykey')
print('Current OTP: {}'.format(generate_code_response['data']['code']))
print(f"Current OTP: {generate_code_response['data']['code']}")
Validate Code
Expand All @@ -87,5 +87,5 @@ Validate Code
otp = client.secrets.totp.generate_code('mykey')['data']['code']
validate_code_response = client.secrets.totp.validate_code('mykey', otp)
print('Validate OTP: {}'.format(validate_code_response['data']['valid']))
print(f"Validate OTP: {validate_code_response['data']['valid']}")
48 changes: 12 additions & 36 deletions hvac/api/secrets_engines/totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,48 +69,25 @@ def create_key(

if generate:
if issuer == "" or account_name == "":
error_msg = 'required issuer and account_name when generate is true, got "{issuer}", "{account_name}"'
raise exceptions.ParamValidationError(
error_msg.format(
issuer=issuer,
account_name=account_name,
)
)
error_msg = f'required issuer and account_name when generate is true, got "{issuer}", "{account_name}"'
raise exceptions.ParamValidationError(error_msg)
if skew not in ALLOWED_SKEW:
error_msg = 'value can be either 0 or 1, got "{skew}"'
raise exceptions.ParamValidationError(
error_msg.format(
skew=skew,
)
)
error_msg = f'value can be either 0 or 1, got "{skew}"'
raise exceptions.ParamValidationError(error_msg)
else:
if url == "":
if not url:
if not key:
error_msg = 'key is required if generate is false and url is empty'
raise exceptions.ParamValidationError(
error_msg
)
raise exceptions.ParamValidationError(error_msg)
if algorithm not in ALLOWED_ALGORITHMS:
error_msg = 'Options include "SHA1", "SHA256" and "SHA512", got "{algorithm}"'
raise exceptions.ParamValidationError(
error_msg.format(
algorithm=algorithm,
)
)
error_msg = f'Options include "SHA1", "SHA256" and "SHA512", got "{algorithm}"'
raise exceptions.ParamValidationError(error_msg)
if digits not in ALLOWED_DIGITS:
error_msg = 'This value can be either 0 or 1. Only used if generate is true, got "{digits}"'
raise exceptions.ParamValidationError(
error_msg.format(
digits=digits,
)
)
error_msg = f'This value can be either 0 or 1. Only used if generate is true, got "{digits}"'
raise exceptions.ParamValidationError(error_msg)
if not qr_size >= 0:
error_msg = 'qr_size should greater or equal to 0, got "{qr_size}"'
raise exceptions.ParamValidationError(
error_msg.format(
qr_size=qr_size,
)
)
error_msg = f'qr_size should greater or equal to 0, got "{qr_size}"'
raise exceptions.ParamValidationError(error_msg)

params = {
"generate": generate,
Expand Down Expand Up @@ -159,7 +136,6 @@ def read_key(
)

return self._adapter.get(url=api_path)
pass

def list_keys(
self,
Expand Down
2 changes: 1 addition & 1 deletion hvac/constants/totp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
DEFAULT_MOUNT_POINT = "totp"
ALLOWED_ALGORITHMS = ['SHA1', 'SHA256', 'SHA512']
ALLOWED_DIGITS = [6, 8]
ALLOWED_SKEW = [0, 1]
ALLOWED_SKEW = [0, 1]
1 change: 0 additions & 1 deletion tests/integration_tests/api/secrets_engines/test_totp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
from unittest import TestCase
from hvac.api.system_backend import mount

from parameterized import parameterized, param

Expand Down

0 comments on commit d79410a

Please sign in to comment.