vast-ai/vast-cli / vastai
vastai
FlaggedVast.ai CLI to manage GPU instances, volumes, serverless endpoints, and billing.
cli/commands/auth.py
View on GitHub"""CLI commands for authentication and 2FA."""
import getpass
import json
import os
import re
import sys
import tempfile
from datetime import datetime, timezone
from vastai.cli.parser import argument
from vastai.cli.display import deindent, display_table
from vastai.api import auth as auth_api
from vastai.cli.util import SUCCESS, WARN, FAIL, format_key_suffix
# ---------------------------------------------------------------------------
# TFA helper functions (ported from old/vast.py)
# ---------------------------------------------------------------------------
TFA_METHOD_FIELDS = (
("id", "ID", "{}", None, True),
("user_id", "User ID", "{}", None, True),
("is_primary", "Primary", "{}", None, True),
("method", "Method", "{}", None, True),
("label", "Label", "{}", None, True),
("phone_number", "Phone Number", "{}", None, False),
("created_at", "Created (UTC)", "{}", lambda x: datetime.fromtimestamp(x, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') if x else "N/A", True),
("last_used", "Last Used (UTC)", "{}", lambda x: datetime.fromtimestamp(x, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') if x else "Never", True),
("fail_count", "Failures", "{}", None, True),
("locked_until", "Locked Until (UTC)", "{}", lambda x: datetime.fromtimestamp(x, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') if x else "N/A", True),
)
def display_tfa_methods(methods):
"""Helper function to display 2FA methods in a table."""
method_fields = TFA_METHOD_FIELDS
has_sms = any(m['method'] == 'sms' for m in methods)
if not has_sms: # Don't show Phone Number column if the user has no SMS methods
method_fields = tuple(field for field in TFA_METHOD_FIELDS if field[0] != 'phone_number')
display_table(methods, method_fields, replace_spaces=False)
def confirm_destructive_action(prompt="Are you sure? (y/n): "):
"""Prompt user for confirmation of destructive actions."""
try:
response = input(f" {prompt}").strip().lower()
return 'y' in response
except (EOFError, KeyboardInterrupt):
print("\nOperation cancelled.")
raise
def handle_failed_tfa_verification(args, e):
"""Parse TFA error responses and provide contextual error messages."""
try:
error_data = e.response.json()
except Exception:
print(f"\n{FAIL} Error: {e}")
return
error_msg = error_data.get("msg", str(e))
error_code = error_data.get("error", "")
if args.raw:
print(json.dumps(error_data, indent=2))
print(f"\n{FAIL} Error: {error_msg}")
# Provide helpful context for common errors
if error_code in {"tfa_locked", "2fa_verification_failed"}:
fail_count = error_data.get("fail_count", 0)
locked_until = error_data.get("locked_until")
if fail_count > 0:
print(f" Failed attempts: {fail_count}")
if locked_until:
lock_time_sec = (datetime.fromtimestamp(locked_until) - datetime.now()).seconds
minutes, seconds = divmod(lock_time_sec, 60)
print(f" Time Remaining for 2FA Lock: {minutes} minutes and {seconds} seconds...")
elif error_code == "2fa_expired":
# Note: Only SMS uses tfa challenges that expire when verifying
print("\n The SMS code and secret have expired. Please start over:")
print(" vastai tfa send-sms")
def format_backup_codes(backup_codes):
"""Format backup codes for display or file output."""
output_lines = [
"=" * 60, " VAST.AI 2FA BACKUP CODES", "=" * 60,
f"\nGenerated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
f"\n{WARN} WARNING: All previous backup codes are now invalid!",
"\nYour New Backup Codes (one-time use only):",
"-" * 40,
]
for i, code in enumerate(backup_codes, 1):
output_lines.append(f" {i:2d}. {code}")
output_lines.extend([
"-" * 40,
"\nIMPORTANT:",
" \u2022 Each code can only be used once",
" \u2022 Store them in a secure location",
" \u2022 Use these codes to log in if you lose access to your 2FA device",
"\n" + "=" * 60,
])
return "\n".join(output_lines)
def _save_to_file(content, filepath):
"""Save content to file, creating parent directories if needed."""
try:
filepath = os.path.abspath(os.path.expanduser(filepath))
parent_dir = os.path.dirname(filepath)
if parent_dir:
os.makedirs(parent_dir, exist_ok=True)
with open(filepath, "w") as f:
f.write(content)
return True
except (IOError, OSError) as e:
print(f"\n{FAIL} Error saving file: {e}")
return False
def _get_backup_codes_filename():
"""Generate a timestamped filename for backup codes."""
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
return f"vastai_backup_codes_{timestamp}.txt"
def save_backup_codes(backup_codes):
"""Save or display 2FA backup codes based on user choice."""
print(f"\nBackup codes regenerated successfully! {SUCCESS}")
print(f"\n{WARN} WARNING: All previous backup codes are now invalid!")
formatted_content = format_backup_codes(backup_codes)
filename = _get_backup_codes_filename()
while True:
print("\nHow would you like to save your new backup codes?")
print(f" 1. Save to default location (~/Downloads/{filename})")
print(f" 2. Save to a custom path")
print(f" 3. Print to screen ({WARN} potentially unsafe - visible to onlookers)")
try:
choice = input("\nEnter choice (1-3): ").strip()
if choice in {'1', '2'}:
if choice == '1':
downloads_dir = os.path.expanduser("~/Downloads")
filepath = os.path.join(downloads_dir, filename)
else: # choice == '2'
custom_path = input("\nEnter full path for backup codes file: ").strip()
if not custom_path:
print("Error: Path cannot be empty")
continue
filepath = os.path.abspath(os.path.expanduser(custom_path))
if os.path.isdir(filepath):
filepath = os.path.join(filepath, filename)
if _save_to_file(formatted_content, filepath):
print(f"\n{SUCCESS} Backup codes saved to: {filepath}")
print(f"\nIMPORTANT:")
print(f" \u2022 The file contains {len(backup_codes)} one-time use backup codes")
if choice == '1':
print(f" \u2022 Move this file to a secure location")
return
else:
print("Please try again with a different path.")
continue
elif choice == '3':
print(f"\n{WARN} WARNING: Printing sensitive codes to screen!")
confirm = input("\nAre you sure? Anyone nearby can see these codes. (yes/no): ").strip().lower()
if confirm in {'yes', 'y'}:
print("\n" + formatted_content + "\n")
return
else:
print("Cancelled. Please choose another option.")
continue
else:
print("Invalid choice. Please enter 1, 2, or 3.")
except (EOFError, KeyboardInterrupt):
print("\n\nOperation cancelled. Your backup codes were generated but not saved.")
print("You will need to regenerate them to get new codes.")
raise
from vastai.cli.utils import get_parser as _get_parser, get_client # noqa: F401
parser = _get_parser()
# ---------------------------------------------------------------------------
# set api-key
# ---------------------------------------------------------------------------
# Vast API keys are 64 hex characters.
API_KEY_PATTERN = re.compile(r"[0-9a-fA-F]{64}")
def read_api_key_input():
"""The key from a hidden prompt at a terminal; empty without one, so a script
whose stdin is an open pipe fails at once instead of waiting on it."""
if not sys.stdin.isatty():
return ""
try:
return getpass.getpass("Paste your API key (input is hidden): ").strip()
except (EOFError, KeyboardInterrupt):
print()
return ""
def write_private_file(path, text):
"""Write ``text`` to ``path`` readable by the owner only from the first
byte, via a temp file renamed into place so it is never half-written."""
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path), prefix=".vast_api_key.")
try:
with os.fdopen(fd, "wb") as f: # binary: no \n -> \r\n translation on Windows
f.write(text.encode())
os.replace(tmp, path)
except BaseException:
os.unlink(tmp)
raise
@parser.command(
argument("new_api_key", nargs="?", help="API key to save. Leave it out at a terminal to paste it at a hidden prompt, which keeps it out of your shell history"),
usage="vastai set api-key [APIKEY]",
help="Set api-key (get your api-key from the console/CLI)",
)
def set__api_key(args):
"""Set the api-key."""
from vastai.cli.util import APIKEY_FILE, APIKEY_FILE_HOME
# Exit rather than return 1: under --raw a returned value is printed as JSON and exits 0.
api_key = args.new_api_key.strip() if args.new_api_key is not None else read_api_key_input()
if not api_key:
print(f"\n{FAIL} Error: No API key given. Pass it (vastai set api-key KEY) or run this at a terminal to be prompted. Create one at https://cloud.vast.ai/manage-keys/?tab=api-keys")
sys.exit(1)
if not API_KEY_PATTERN.fullmatch(api_key):
print(f"\n{FAIL} Error: That isn't a Vast API key (expected 64 hex characters), so it wasn't saved.")
sys.exit(1)
write_private_file(APIKEY_FILE, api_key)
print("Your api key has been saved in {}".format(APIKEY_FILE))
if os.path.exists(APIKEY_FILE_HOME):
os.remove(APIKEY_FILE_HOME)
if getattr(args, "explain", False):
print("Removed legacy api key file at {} (no longer used).".format(APIKEY_FILE_HOME))
env_key = os.environ.get("VAST_API_KEY")
if env_key:
print(f"\n{WARN} VAST_API_KEY is set in your environment (ends in {format_key_suffix(env_key)}) and overrides the key you just saved.")
print("Unset VAST_API_KEY to use the saved key.")
# ---------------------------------------------------------------------------
# set role
# ---------------------------------------------------------------------------
@parser.command(
argument("role", help="'host' or 'client'", choices=["host", "client"]),
usage="vastai set role host|client",
help="Set host role to view host-only commands",
epilog=deindent("""
This only changes what --help and tab completion display — every command
still runs regardless of role, since the server enforces real permissions.
"""),
)
def set__role(args):
"""Set the client/host CLI display role."""
from vastai.cli.util import set_role_file
set_role_file(args.role)
print(f"CLI role set to '{args.role}'.")
if args.role == "client":
print("Host-only commands are now hidden from --help and tab completion.")
else:
print("All commands (client + host) are now shown in --help and tab completion.")
# ---------------------------------------------------------------------------
# Note: set__user and show__user are in billing.py.
# Note: show__ipaddrs is in billing.py.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# show audit-logs
# ---------------------------------------------------------------------------
@parser.command(
usage="vastai show audit-logs [--api-key API_KEY] [--raw]",
help="Display account's history of important actions",
)
def show__audit_logs(args):
"""Show the history of important actions."""
from vastai.cli.display import display_table, audit_log_fields
client = get_client(args)
rows = auth_api.show_audit_logs(client)
if args.raw:
return rows
else:
display_table(rows, audit_log_fields)
# ---------------------------------------------------------------------------
# environment variables
# ---------------------------------------------------------------------------
@parser.command(
argument("-s", "--show-values", action="store_true", help="Show the values of environment variables"),
usage="vastai show env-vars [-s]",
help="Show user environment variables",
)
def show__env_vars(args):
"""Show the environment variables for the current user."""
client = get_client(args)
env_vars = auth_api.show_env_vars(client)
if args.raw:
if not args.show_values:
masked_env_vars = {k: "*****" for k, v in env_vars.items()}
return masked_env_vars
else:
return env_vars
else:
if not env_vars:
print("No environment variables found.")
else:
for key, value in env_vars.items():
print(f"Name: {key}")
if args.show_values:
print(f"Value: {value}")
else:
print("Value: *****")
print("---")
if not args.show_values:
print("\nNote: Values are hidden. Use --show-values or -s option to display them.")
@parser.command(
argument("name", help="Environment variable name", type=str),
argument("value", help="Environment variable value", type=str),
usage="vastai create env-var <name> <value>",
help="Create a new user environment variable",
)
def create__env_var(args):
"""Create a new environment variable for the current user."""
client = get_client(args)
result = auth_api.create_env_var(client, name=args.name, value=args.value)
if result.get("success"):
print(result.get("msg", "Environment variable created successfully."))
else:
print(f"Failed to create environment variable: {result.get('msg', 'Unknown error')}")
@parser.command(
argument("name", help="Environment variable name to update", type=str),
argument("value", help="New environment variable value", type=str),
usage="vastai update env-var <name> <value>",
help="Update an existing user environment variable",
)
def update__env_var(args):
"""Update an existing environment variable for the current user."""
client = get_client(args)
result = auth_api.update_env_var(client, name=args.name, value=args.value)
if result.get("success"):
print(result.get("msg", "Environment variable updated successfully."))
else:
print(f"Failed to update environment variable: {result.get('msg', 'Unknown error')}")
@parser.command(
argument("name", help="Environment variable name to delete", type=str),
usage="vastai delete env-var <name>",
help="Delete a user environment variable",
)
def delete__env_var(args):
"""Delete an environment variable for the current user."""
client = get_client(args)
result = auth_api.delete_env_var(client, name=args.name)
if result.get("success"):
print(result.get("msg", "Environment variable deleted successfully."))
else:
print(f"Failed to delete environment variable: {result.get('msg', 'Unknown error')}")
# ---------------------------------------------------------------------------
# Note: show__scheduled_jobs and delete__scheduled_job are in billing.py.
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# 2FA commands
# ---------------------------------------------------------------------------
@parser.command(
argument("code", help="6-digit verification code from SMS or Authenticator app", type=str),
argument("-t", "--method-type", choices=["sms", "totp"], help="New 2FA Method type to activate", type=str, default="totp"),
argument("--secret", help="Secret token from setup process (required)", type=str, required=True),
argument("--phone-number", help="Phone number for SMS method (E.164 format)", type=str, default=None),
argument("-l", "--label", help="Label for the new 2FA method", type=str, default=None),
usage="vastai tfa activate CODE --secret SECRET [--method-type METHOD_TYPE] [--phone-number PHONE_NUMBER] [--label LABEL]",
help="Activate a new 2FA method by verifying the code",
epilog=deindent(f"""
Complete the 2FA setup process by verifying your code.
{'*'*120}
NOTE: Prior to running this command, you must authorize your attempt to create a new 2FA method by following the instructions in the `vastai tfa auth-new` command.
This is required to ensure that only you can add new 2FA methods to your account.
{'*'*120}
For TOTP (Authenticator app):
1. Run 'vastai tfa totp-setup' to get the manual key/QR code and secret
2. Enter the manual key or scan the QR code with your Authenticator app
3. Run this command with the 6-digit code from your app and the secret token from step 1
For SMS:
1. Run 'vastai tfa send-sms --phone-number <PHONE_NUMBER>' to receive SMS and get secret token
2. Run this command with the code you received via SMS and the phone number it was sent to
If this is your first 2FA method, backup codes will be generated and displayed.
Save these backup codes in a secure location!
Examples:
vastai tfa activate --method-type totp --secret abc123def456 123456
vastai tfa activate --method-type sms --secret abc123def456 --phone-number +12345678901 123456
vastai tfa activate --method-type sms --secret abc123def456 --phone-number +12345678901 --label "Work Phone" 123456
"""),
)
def tfa__activate(args):
"""Activate a new 2FA method by confirming the verification code."""
client = get_client(args)
response_data = auth_api.tfa_activate(
client, code=args.code, secret=args.secret, method_type=args.method_type,
phone_number=args.phone_number, label=args.label,
)
method_name = "SMS" if args.phone_number or args.method_type == "sms" else "TOTP (Authenticator App)"
print(f"\n{SUCCESS} {method_name} 2FA method activated successfully!")
if "backup_codes" in response_data:
save_backup_codes(response_data["backup_codes"])
@parser.command(
argument("-id", "--id-to-delete", help="ID of the 2FA method to delete (see `vastai tfa status`)", type=int, default=None),
argument("-c", "--code", mutex_group='code_grp', required=True, help="2FA code from your Authenticator app, SMS, or Email to authorize deletion", type=str),
argument("-t", "--method-type", mutex_group="type_grp", choices=["email", "sms", "totp"],
help="2FA Method type. Only use when you only have one method of this type", type=str, default=None),
argument("-s", "--secret", help="Secret token (required for SMS or Email 2FA)", type=str, default=None),
argument("-bc", "--backup-code", mutex_group='code_grp', required=True, help="One-time backup code (alternative to regular 2FA code)", type=str, default=None),
argument("--method-id", mutex_group="type_grp", help="2FA Method ID to use if you have more than one of the same type ('id' from `tfa status`)", type=str, default=None),
usage="vastai tfa delete [--id-to-delete ID] [--code CODE] [--method-type METHOD_TYPE] [--secret SECRET] [--backup-code BACKUP_CODE] [--method-id ID]",
help="Remove a 2FA method from your account",
epilog=deindent(f"""
Remove a 2FA method from your account.
This action requires 2FA verification to prevent unauthorized removals.
{'*'*120}
NOTE: If you do not specify --id-to-delete, the system will attempt to delete the method you are using to authenticate.
However please be advised, it is much safer to specify the ID to avoid confusion if you have multiple methods.
{'*'*120}
Use `vastai tfa status` to see your active methods and their IDs.
Examples:
# Delete method #123, authorize with email code and secret from `tfa send-email`
vastai tfa delete --id-to-delete 123 --method-type email --secret abc123def456 -c 456789
# Delete method #123, authorize with TOTP/Authenticator code
vastai tfa delete --id-to-delete 123 --method-type totp --code 456789
# Delete method #123, authorize with SMS and secret from `tfa send-sms`
vastai tfa delete -id 123 --method-type sms --secret abc123def456 -c 456789
# Delete method #123, authorize with backup code
vastai tfa delete --id-to-delete 123 --backup-code ABCD-EFGH-IJKL
# Delete method #123, specify which TOTP method to use if you have multiple
vastai tfa delete -id 123 --method-id 456 -c 456789
# Delete the TOTP method you are using to authenticate (use with caution)
vastai tfa delete -c 456789
"""),
)
def tfa__delete(args):
"""Remove a 2FA method from the user's account."""
if args.method_type in ("sms", "email") and not args.secret:
print(f"\n{FAIL} Error: --secret is required for deletion authorization when using --method-type {args.method_type}.")
print("\nPlease use: `vastai tfa send-sms` or `vastai tfa send-email` to get the missing secret and try again.")
return 1
prompt = "\nAre you sure you want to delete this 2FA method? (y|n): "
if confirm_destructive_action(prompt) == False:
print("Operation cancelled.")
return
client = get_client(args)
try:
response_data = auth_api.tfa_delete(
client, code=args.code, backup_code=args.backup_code,
method_type=args.method_type, secret=args.secret, method_id=args.method_id,
id_to_delete=args.id_to_delete,
)
print(f"\n{SUCCESS} 2FA method deleted successfully.")
if "remaining_methods" in response_data:
remaining = response_data["remaining_methods"]
print(f"\nYou have {remaining} 2FA method{'s' if remaining != 1 else ''} remaining.")
else:
print(f"\n{WARN} WARNING: You have removed all 2FA methods from your account.")
print("Your backup codes have been invalidated and 2FA is now fully disabled.")
except Exception as e:
if hasattr(e, 'response'):
handle_failed_tfa_verification(args, e)
else:
print(f"Failed to delete 2FA method: {e}")
return 1
@parser.command(
argument("-c", "--code", mutex_group='code_grp', required=True, help="2FA code from Authenticator app, SMS, or Email", type=str),
argument("-t", "--method-type", mutex_group="type_grp", choices=["email", "sms", "totp"],
help="2FA Method type. Only use when you only have one method of this type", type=str, default=None),
argument("-s", "--secret", help="Secret token from previous login step (required for SMS or Email 2FA)", type=str, default=None),
argument("-bc", "--backup-code", mutex_group='code_grp', required=True, help="One-time backup code (alternative to regular 2FA code)", type=str, default=None),
argument("-id", "--method-id", mutex_group="type_grp", help="2FA Method ID if you have more than one of the same type ('id' from `tfa status`)", type=str, default=None),
usage="vastai tfa login [--code CODE] [--method-type METHOD_TYPE] [--secret SECRET] [--backup-code BACKUP_CODE]",
help="Complete 2FA login by verifying code",
epilog=deindent("""
Complete Two-Factor Authentication login by providing the 2FA code.
For Email: Include the --method-type email flag and provide -s/--secret from the `tfa send-email` command response
For TOTP: Include the --method-type totp and provide the 6-digit code from your Authenticator app
For SMS: Include the --method-type sms flag and provide -s/--secret from the `tfa send-sms` command response
For backup code: Use --backup-code instead of code (codes may only be used once)
Examples:
vastai tfa login --method-type totp -c 123456
vastai tfa login --method-type sms --code 123456 --secret abc123def456
vastai tfa login -t email -c 123456 -s abc123def456
vastai tfa login --backup-code ABCD-EFGH-IJKL
"""),
)
def tfa__login(args):
"""Complete 2FA login and store the session key."""
from vastai.cli.util import TFAKEY_FILE
client = get_client(args)
try:
response_data = auth_api.tfa_login(
client, code=args.code, backup_code=args.backup_code,
method_type=args.method_type, secret=args.secret, method_id=args.method_id,
)
if "session_key" in response_data:
session_key = response_data["session_key"]
if session_key != args.api_key:
with open(TFAKEY_FILE, "w") as f:
f.write(session_key)
print(f"{SUCCESS} 2FA login successful! Session key saved to {TFAKEY_FILE}")
else:
print(f"{SUCCESS} 2FA login successful! Your session key has been refreshed.")
# Display remaining backup codes if present
if "backup_codes_remaining" in response_data:
remaining = response_data["backup_codes_remaining"]
if remaining == 0:
print(f"{WARN} Warning: You have no backup codes remaining! Please generate new backup codes immediately to avoid being locked out of your account if you lose access to your 2FA device.")
elif remaining <= 3:
print(f"{WARN} Warning: You only have {remaining} backup codes remaining. Consider regenerating them.")
else:
print(f"Backup codes remaining: {remaining}")
else:
print("2FA login successful but a session key was not returned. Please check that you have an API Key that's properly set up")
except Exception as e:
if hasattr(e, 'response'):
handle_failed_tfa_verification(args, e)
else:
print(f"2FA login failed: {e}")
return 1
@parser.command(
argument("-p", "--phone-number", help="Phone number to receive SMS code (E.164 format, e.g., +1234567890)", type=str, default=None),
argument("-s", "--secret", help="Secret token from the original 2FA login attempt", type=str, required=True),
usage="vastai tfa resend-sms --secret SECRET [--phone-number PHONE_NUMBER]",
help="Resend SMS 2FA code",
epilog=deindent("""
Resend the SMS verification code to your phone.
This is useful if:
\u2022 You didn't receive the original SMS
\u2022 The code expired before you could use it
\u2022 You accidentally deleted the message
You must provide the same secret token from the original request.
Example:
vastai tfa resend-sms --secret abc123def456
"""),
)
def tfa__resend_sms(args):
"""Resend SMS 2FA code to the user's phone."""
client = get_client(args)
response_data = auth_api.tfa_resend_sms(client, secret=args.secret, phone_number=args.phone_number)
print("SMS code resent successfully!")
print(f"\n{response_data['msg']}")
@parser.command(
argument("-c", "--code", mutex_group='code_grp', required=True, help="2FA code from Authenticator app, SMS, or Email", type=str),
argument("-t", "--method-type", mutex_group="type_grp", choices=["email", "sms", "totp"],
help="2FA Method type. Only use when you only have one method of this type", type=str, default=None),
argument("-s", "--secret", help="Secret token from previous login step (required for SMS or Email 2FA)", type=str, default=None),
argument("-bc", "--backup-code", mutex_group='code_grp', required=True, help="One-time backup code (alternative to regular 2FA code)", type=str, default=None),
argument("-id", "--method-id", mutex_group="type_grp", help="2FA Method ID if you have more than one of the same type ('id' from `tfa status`)", type=str, default=None),
usage="vastai tfa regen-codes [--code CODE] [--method-type METHOD_TYPE] [--secret SECRET] [--backup-code BACKUP_CODE] [--method-id ID]",
help="Regenerate backup codes for 2FA",
epilog=deindent("""
Generate a new set of backup codes for your account.
This action requires 2FA verification to prevent unauthorized regeneration.
WARNING: This will invalidate all existing backup codes!
Any previously generated codes will no longer work.
Backup codes are one-time use codes that allow you to log in
if you lose access to your primary 2FA method (lost phone, etc).
You should regenerate your backup codes if:
\u2022 You've used several codes and are running low
\u2022 You think your codes may have been compromised
\u2022 You lost your saved codes and need new ones
Important: Save the new codes in a secure location immediately!
They will not be shown again.
Examples:
vastai tfa regen-codes --code --method-type totp 123456
vastai tfa regen-codes -c 123456 -t sms --secret abc123def456
vastai tfa regen-codes --backup-code ABCD-EFGH-IJKL
"""),
)
def tfa__regen_codes(args):
"""Regenerate backup codes for 2FA recovery."""
prompt = "\nThis will invalidate all existing backup codes. Continue? (y|n): "
if confirm_destructive_action(prompt) == False:
print("Operation cancelled.")
return
client = get_client(args)
try:
response_data = auth_api.tfa_regen_codes(
client, code=args.code, backup_code=args.backup_code,
method_type=args.method_type, secret=args.secret, method_id=args.method_id,
)
if "backup_codes" in response_data:
save_backup_codes(response_data["backup_codes"])
else:
print(f"\n{SUCCESS} Backup codes regenerated successfully!")
print("(No codes returned in response - this may be an error)")
except Exception as e:
if hasattr(e, 'response'):
handle_failed_tfa_verification(args, e)
else:
print(f"Failed to regenerate backup codes: {e}")
return 1
@parser.command(
argument("-p", "--phone-number", help="Phone number to receive SMS code (E.164 format, e.g., +1234567890)", type=str, default=None),
usage="vastai tfa send-sms [--phone-number PHONE_NUMBER]",
help="Request a 2FA SMS verification code",
epilog=deindent("""
Request a two-factor authentication code to be sent via SMS.
If --phone-number is not provided, uses the phone number on your account.
The secret token will be returned and must be used with 'vastai tfa activate'.
Examples:
vastai tfa send-sms
vastai tfa send-sms --phone-number +12345678901
"""),
)
def tfa__send_sms(args):
"""Request a 2FA SMS code to be sent to the user's phone."""
client = get_client(args)
response_data = auth_api.tfa_send_sms(client, phone_number=args.phone_number)
secret = response_data.get("secret", "")
print("SMS code sent successfully!")
print(f" Secret token: {secret}")
print(f"\nOnce you receive the SMS code:")
phone_num = f"--phone-number {args.phone_number}" if args.phone_number else "[--phone-number <PHONE_NUMBER>]"
print(f"\n If you are setting up SMS 2FA for the first time, run:")
print(f" vastai tfa activate --method-type sms --secret {secret} {phone_num} [--label <LABEL>] <CODE>")
print(f"\n Otherwise you can complete your 2FA log in with:")
print(f" vastai tfa login --method-type sms --secret {secret} -c <CODE>\n")
@parser.command(
usage="vastai tfa send-email",
help="Request a 2FA Email verification code",
epilog=deindent("""
Request a two-factor authentication code to be sent via Email.
The secret token will be returned and must be used with 'vastai tfa activate'.
Examples:
vastai tfa send-email
"""),
)
def tfa__send_email(args):
"""Request a 2FA email code to be sent to the user's email."""
client = get_client(args)
response_data = auth_api.tfa_send_email(client)
secret = response_data.get("secret", "")
print(f"{SUCCESS} Email code sent successfully!")
print(f" Secret token: {secret}")
print(f"\nOnce you receive the Email code:")
print(f"\n You can complete your 2FA log in with:")
print(f" vastai tfa login --method-type email --secret {secret} -c <CODE>\n")
def print_next_steps_after_new_method_auth():
print(f"\nNext Steps:"
"\n To add a new SMS 2FA method:"
"\n 1. Run `vastai tfa send-sms --phone-number <PHONE_NUMBER>` to receive SMS and get secret token"
"\n 2. Run `vastai tfa activate --method-type sms --secret <SECRET> --phone-number <PHONE_NUMBER> CODE` to activate the new method with the code you received via SMS\n"
"\n To add a new TOTP (Authenticator app) 2FA method:"
"\n 1. Run `vastai tfa totp-setup` to get the manual key/QR code and secret"
"\n 2. Enter the manual key or scan the QR code with your Authenticator app"
"\n 3. Run `vastai tfa activate --method-type totp --secret <SECRET> CODE` to activate the new method with the 6-digit code from your app")
@parser.command(
argument("-c", "--code", help="2FA code from Authenticator app, SMS, or Email", type=str),
argument("-s", "--secret", help="Secret token from previous auth step", type=str, default=None),
argument("-t", "--method-type", mutex_group="type_grp", choices=["email", "sms", "totp"],
help="2FA Method type. Only use when you only have one method of this type", type=str, default="email"),
argument("-bc", "--backup-code", mutex_group='type_grp',
help="One-time backup code (alternative to regular 2FA code)", type=str, default=None),
argument("-id", "--method-id", mutex_group="type_grp",
help="2FA Method ID if you have more than one of the same type ('id' from `tfa status`)", type=str, default=None),
usage="vastai tfa auth-new {[--method-type METHOD_TYPE | --method-id ID | --backup-code BACKUP_CODE] | [--secret SECRET --code CODE]}",
help="Authorize your account to add a new 2FA method",
epilog=deindent("""
Authorize your account to add a new 2FA method by verifying via email or an existing method.
This is a required step to ensure that only you can add new 2FA methods to your account.
Step 1. Run command with your chosen verification method:
- If you have an existing 2FA method set up, you can use '--backup-code BACKUP_CODE' to immediately authorize (skip Step 2)
- Use --method-type {sms|totp} or --method-id ID to specify which existing method to use for verification (see `vastai tfa status` for available methods and their IDs)
- Use --method-type email if you have a verified email address and/or no other 2FA methods set up to receive a code via email
Step 2. When prompted, enter the 2FA code from your 2FA method of choice to confirm authorization.
Note:
If you exit the command before being able to enter the code, you can run this command again
with --secret SECRET and --code CODE to complete the authorization step as long as the code has not expired.
Examples:
# Initiating New Method Authorization
vastai tfa auth-new (method type is email by default)
vastai tfa auth-new --method-type totp
vastai tfa auth-new -t sms
vastai tfa auth-new --method-id 456
vastai tfa auth-new --backup-code ABCD-EFGH-IJKL
# Completing Authorization with code and secret if not completed in previous run
vastai tfa auth-new --secret abc123def456 --code 123456
"""),
)
def tfa__auth_new(args):
"""Authorize the user to add a new 2FA method by verifying with an existing method."""
client = get_client(args)
secret, code = args.secret, args.code
if not secret and not code:
try:
response_data = auth_api.tfa_auth_new(
client, backup_code=args.backup_code,
method_type=args.method_type, method_id=args.method_id,
)
except Exception as e:
if hasattr(e, 'response'):
handle_failed_tfa_verification(args, e)
else:
print(f"Authorization failed: {e}")
return 1
if args.backup_code and response_data.get("msg") == "Authorization successful.":
print(f"\n{SUCCESS} Successfully authorized account for adding new 2FA method using backup code")
print_next_steps_after_new_method_auth()
return 0
secret = response_data.get("secret")
if not secret:
print(f"\n{FAIL} Error: No secret token received for authorization. Please try again.")
return 1
print(f"\n{SUCCESS} Authorization initiated successfully.")
print(f"2FA Secret: {secret}")
try:
code = input("Enter 2FA code to complete authorization: ").strip()
except (EOFError, KeyboardInterrupt):
print("\nOperation cancelled.")
print("You can still complete this authorization later by running:"
f"\n vastai tfa auth-new --secret {secret} --code <CODE>")
return 1
try:
auth_api.tfa_auth_new(client, code=code, secret=secret)
print(f"\n{SUCCESS} Successfully authorized account for adding new 2FA method!")
print_next_steps_after_new_method_auth()
except Exception as e:
if hasattr(e, 'response'):
handle_failed_tfa_verification(args, e)
else:
print(f"Authorization failed: {e}")
print(f"\n{FAIL} Authorization failed. Please try again.")
return 1
@parser.command(
help="Shows the current 2FA status and configured methods",
epilog=deindent("""
Show the current 2FA status for your account, including:
\u2022 Whether or not 2FA is enabled
\u2022 A list of active 2FA methods
\u2022 The number of backup codes remaining (if 2FA is enabled)
""")
)
def tfa__status(args):
"""Show the current 2FA status for the user."""
client = get_client(args)
response_data = auth_api.tfa_status(client)
if args.raw:
return response_data
tfa_enabled = response_data.get("tfa_enabled", False)
methods = response_data.get("methods", [])
backup_codes_remaining = response_data.get("backup_codes_remaining", 0)
if not tfa_enabled or not methods:
print(f"{WARN} No active 2FA methods found")
else:
print(f"2FA Status: Enabled {SUCCESS}")
print(f"\nActive 2FA Methods:")
display_tfa_methods(methods)
print(f"\nBackup codes remaining: {backup_codes_remaining}")
@parser.command(
usage="vastai tfa totp-setup",
help="Generate TOTP secret and QR code for Authenticator app setup",
epilog=deindent("""
Set up TOTP (Time-based One-Time Password) 2FA using an Authenticator app.
This command generates a new TOTP secret and displays:
\u2022 A QR code (for scanning with your app)
\u2022 A manual entry key (for typing into your app)
\u2022 A secret token (needed for the next step)
Workflow:
1. Run this command to generate the TOTP secret
2. Add the account to your Authenticator app by either:
\u2022 Scanning the displayed QR code, OR
\u2022 Manually entering the key shown
3. Once added, your app will display a 6-digit code
4. Complete setup by running:
vastai tfa activate --method-type totp --secret <SECRET> <CODE>
Supported Authenticator Apps:
\u2022 Google Authenticator
\u2022 Microsoft Authenticator
\u2022 Authy
\u2022 1Password
\u2022 Any TOTP-compatible app
Example:
vastai tfa totp-setup
"""),
)
def tfa__totp_setup(args):
"""Generate a TOTP secret and QR code for setting up Authenticator app 2FA."""
client = get_client(args)
response_data = auth_api.tfa_totp_setup(client)
if args.raw:
return response_data
secret = response_data.get("secret", "")
provisioning_uri = response_data.get("provisioning_uri", "")
print("\n" + "=" * 60)
print("TOTP (Authenticator App) 2FA Setup")
print("=" * 60)
print("\nScan this QR code with your Authenticator app:\n")
try:
import qrcode
qr = qrcode.QRCode(border=2)
qr.add_data(provisioning_uri)
qr.make()
qr.print_ascii(tty=True)
except ImportError:
print(" [QR code display requires 'qrcode' package]")
print(f" Install with: pip install qrcode")
print(f"\n Or manually enter this URI in your app:")
print(f" {provisioning_uri}")
print("\nOR Manual Entry Key (type this into your Authenticator app):")
print(f" {secret}")
print("\nNext Steps:")
print(" 1. Your Authenticator app should now display a 6-digit code")
print(" 2. Complete setup by running:")
print(f" vastai tfa activate --method-type totp --secret {secret} <CODE>")
print("\n" + "=" * 60 + "\n")
@parser.command(
argument("method_id", metavar="METHOD_ID", help="ID of the 2FA method to update (see `vastai tfa status`)", type=int),
argument("-l", "--label", help="New label/name for this 2FA method", type=str, default=None),
argument("-p", "--set-primary", help="Set this method as the primary/default 2FA method", default=None),
usage="vastai tfa update METHOD_ID [--label LABEL] [--set-primary]",
help="Update a 2FA method's settings",
epilog=deindent("""
Update the label or primary status of a 2FA method.
The label is a friendly name to help you identify different methods
(e.g. "Work Phone", "Personal Authenticator").
The primary method is your preferred/default 2FA method.
Examples:
vastai tfa update 123 --label "Work Phone"
vastai tfa update 456 --set-primary
vastai tfa update 789 --label "Backup Authenticator" --set-primary
"""),
)
def tfa__update(args):
"""Update settings for an existing 2FA method."""
label = args.label
set_primary = args.set_primary
if set_primary is not None:
if set_primary.lower() in {'true', 't'}:
set_primary = True
elif set_primary.lower() in {'false', 'f'}:
set_primary = False
else:
print("Error: --set-primary must be <t|true> or <f|false>")
return
if label is None and set_primary is None:
print("Error: You must specify at least one field to update (--label or --set-primary)")
return 1
client = get_client(args)
try:
response_data = auth_api.tfa_update(client, method_id=args.method_id, label=label, set_primary=set_primary)
except ValueError as e:
print(f"Error: {e}")
return 1
if args.raw:
return response_data
print("\n2FA method updated successfully!")
if args.label:
print(f" New label: {args.label}")
if set_primary is not None:
print(f" Set as primary method = {set_primary}")
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |