Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anodos325 committed Jan 30, 2025
1 parent 36f791c commit 99ba7ad
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions examples/two_factor_challenge_response_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from getpass import getpass


interactive = False
TOTP_INTERVAL = 30
TOTP_DIGITS = 6
TOTP_FILE = "doesnotexist"
Expand All @@ -13,11 +13,8 @@

def get_totp_secret() -> str:
""" This assumes the TOTP secret is written somewhere on client """
try:
with open(TOTP_FILE, 'r') as f:
return f.read()
except Exception:
return None
with open(TOTP_FILE, 'r') as f:
return f.read()


def get_2fa_token(secret: str) -> str:
Expand All @@ -40,9 +37,14 @@ def authenticate_client(c: truenas_api_client.Client) -> bool:
return False
case "OTP_REQUIRED":
# two-factor is configured for account
# getpass is here as example of how to prompt for password in script
# This of course shouldn't be done if script isn't interactive.
otp_token = get_2fa_token(secret) or getpass()

if interactive:
# getpass() is here as example of how to prompt for password in script
# This of course shouldn't be done if script isn't interactive.
otp_token = getpass()
else:
otp_token = get_2fa_token(secret)

resp = c.call("auth.login_ex_continue", {
"mechanism": "OTP_TOKEN",
"otp_token": otp_token
Expand All @@ -56,7 +58,8 @@ def authenticate_client(c: truenas_api_client.Client) -> bool:
raise ValueError(f'{resp["response_type"]}: Unexpected response type')


secret = get_totp_secret()
if not interactive:
secret = get_totp_secret()

with truenas_api_client.Client("wss://example.internal/api/current") as c:
# Authenticate using some pre-existing API key
Expand Down

0 comments on commit 99ba7ad

Please sign in to comment.