# Multi-Factor Authentication (MFA) CLI commands # # Provides comprehensive MFA management through the control-center API use ../lib_provisioning/config/loader.nu get-config # Get API base URL from config def get-api-url [] { let config = get-config $config.api.base_url? | default "http://localhost:8080" } # Get auth token from environment or config def get-auth-token [] { $env.PROVISIONING_AUTH_TOKEN? | default "" } # Make authenticated API request def api-request [ method: string # HTTP method (GET, POST, DELETE) endpoint: string # API endpoint path body?: any # Request body (optional) ] { let base_url = get-api-url let token = get-auth-token let url = $"($base_url)/api/v1($endpoint)" let headers = { "Authorization": $"Bearer ($token)" "Content-Type": "application/json" } if ($body | is-empty) { http $method $url --headers $headers } else { http $method $url --headers $headers ($body | to json) } } # ============================================================================ # TOTP Commands # ============================================================================ # Enroll TOTP (Time-based One-Time Password) # # Example: # mfa totp enroll export def "mfa totp enroll" [] { print "đą Enrolling TOTP device..." let response = api-request "POST" "/mfa/totp/enroll" print "" print "â TOTP device enrolled successfully!" print "" print "đ Device ID:" $response.device_id print "" print "đ Manual entry secret (if QR code doesn't work):" print $" ($response.secret)" print "" print "đą Scan this QR code with your authenticator app:" print " (Google Authenticator, Authy, Microsoft Authenticator, etc.)" print "" # Save QR code to file let qr_file = $"/tmp/mfa-qr-($response.device_id).html" $"
($response.secret)
'"
print ""
}
# Verify TOTP code
#
# Example:
# mfa totp verify 123456
export def "mfa totp verify" [
code: string # 6-digit TOTP code
--device-id: string # Specific device ID (optional)
] {
print $"đ Verifying TOTP code: ($code)..."
let body = {
code: $code
device_id: $device_id
}
let response = api-request "POST" "/mfa/totp/verify" $body
if $response.verified {
print ""
print "â
TOTP verification successful!"
if $response.backup_code_used {
print "â ī¸ Note: A backup code was used"
}
print ""
} else {
print ""
print "â TOTP verification failed"
print " Please check your code and try again"
print ""
exit 1
}
}
# Disable TOTP
#
# Example:
# mfa totp disable
export def "mfa totp disable" [] {
print "â ī¸ Disabling TOTP..."
print ""
print "This will remove all TOTP devices from your account."
let confirm = input "Are you sure? (yes/no): "
if $confirm != "yes" {
print "Cancelled."
return
}
api-request "POST" "/mfa/totp/disable"
print ""
print "â
TOTP disabled successfully"
print ""
}
# Show backup codes status
#
# Example:
# mfa totp backup-codes
export def "mfa totp backup-codes" [] {
print "đ Fetching backup codes status..."
let response = api-request "GET" "/mfa/totp/backup-codes"
print ""
print "đ Backup Codes:"
for code in $response.backup_codes {
print $" ($code)"
}
print ""
}
# Regenerate backup codes
#
# Example:
# mfa totp regenerate
export def "mfa totp regenerate" [] {
print "đ Regenerating backup codes..."
print ""
print "â ī¸ This will invalidate all existing backup codes."
let confirm = input "Continue? (yes/no): "
if $confirm != "yes" {
print "Cancelled."
return
}
let response = api-request "POST" "/mfa/totp/regenerate"
print ""
print "â
New backup codes generated:"
print ""
for code in $response.backup_codes {
print $" ($code)"
}
print ""
print "đž Save these codes securely!"
print ""
}
# ============================================================================
# WebAuthn Commands
# ============================================================================
# Enroll WebAuthn device (security key)
#
# Example:
# mfa webauthn enroll --device-name "YubiKey 5"
export def "mfa webauthn enroll" [
--device-name: string = "Security Key" # Device name
] {
print $"đ Enrolling WebAuthn device: ($device_name)"
print ""
print "â ī¸ WebAuthn enrollment requires browser interaction."
print " Use the Web UI at: (get-api-url)/mfa/setup"
print ""
print " Or use the API directly with a browser-based client."
print ""
}
# List WebAuthn devices
#
# Example:
# mfa webauthn list
export def "mfa webauthn list" [] {
print "đ Fetching WebAuthn devices..."
let devices = api-request "GET" "/mfa/webauthn/devices"
if ($devices | is-empty) {
print ""
print "No WebAuthn devices registered"
print ""
return
}
print ""
print "đą WebAuthn Devices:"
print ""
for device in $devices {
print $"Device: ($device.device_name)"
print $" ID: ($device.id)"
print $" Created: ($device.created_at)"
print $" Last used: ($device.last_used | default 'Never')"
print $" Status: (if $device.enabled { 'â
Enabled' } else { 'â Disabled' })"
print $" Transports: ($device.transports | str join ', ')"
print ""
}
}
# Remove WebAuthn device
#
# Example:
# mfa webauthn remove
export def "mfa webauthn remove" [
device_id: string # Device ID to remove
] {
print $"đī¸ Removing WebAuthn device: ($device_id)"
print ""
let confirm = input "Are you sure? (yes/no): "
if $confirm != "yes" {
print "Cancelled."
return
}
api-request "DELETE" $"/mfa/webauthn/devices/($device_id)"
print ""
print "â
Device removed successfully"
print ""
}
# ============================================================================
# General MFA Commands
# ============================================================================
# Show MFA status
#
# Example:
# mfa status
export def "mfa status" [] {
print "đ Fetching MFA status..."
let status = api-request "GET" "/mfa/status"
print ""
print "đ MFA Status:"
print $" Enabled: (if $status.enabled { 'â
Yes' } else { 'â No' })"
print ""
if not ($status.totp_devices | is-empty) {
print "đą TOTP Devices:"
for device in $status.totp_devices {
print $" âĸ ID: ($device.id)"
print $" Created: ($device.created_at)"
print $" Last used: ($device.last_used | default 'Never')"
print $" Status: (if $device.enabled { 'Enabled' } else { 'Not verified' })"
}
print ""
}
if not ($status.webauthn_devices | is-empty) {
print "đ WebAuthn Devices:"
for device in $status.webauthn_devices {
print $" âĸ ($device.device_name)"
print $" ID: ($device.id)"
print $" Created: ($device.created_at)"
print $" Last used: ($device.last_used | default 'Never')"
}
print ""
}
if $status.has_backup_codes {
print "đž Backup codes: Available"
print ""
}
if (not $status.enabled) {
print "âšī¸ MFA is not enabled. Set it up with:"
print " âĸ mfa totp enroll - For TOTP (recommended)"
print " âĸ mfa webauthn enroll - For hardware keys"
print ""
}
}
# Disable all MFA methods
#
# Example:
# mfa disable
export def "mfa disable" [] {
print "â ī¸ Disabling ALL MFA methods..."
print ""
print "This will remove:"
print " âĸ All TOTP devices"
print " âĸ All WebAuthn devices"
print " âĸ All backup codes"
print ""
let confirm = input "Are you ABSOLUTELY sure? Type 'disable mfa': "
if $confirm != "disable mfa" {
print "Cancelled."
return
}
api-request "POST" "/mfa/disable"
print ""
print "â
All MFA methods have been disabled"
print ""
}
# List all MFA devices
#
# Example:
# mfa list-devices
export def "mfa list-devices" [] {
mfa status
}
# ============================================================================
# Help Command
# ============================================================================
# Show MFA help
export def "mfa help" [] {
print ""
print "đ Multi-Factor Authentication (MFA) Commands"
print ""
print "TOTP (Time-based One-Time Password):"
print " mfa totp enroll - Enroll TOTP device"
print " mfa totp verify - Verify TOTP code"
print " mfa totp disable - Disable TOTP"
print " mfa totp backup-codes - Show backup codes status"
print " mfa totp regenerate - Regenerate backup codes"
print ""
print "WebAuthn (Hardware Security Keys):"
print " mfa webauthn enroll - Enroll security key"
print " mfa webauthn list - List registered devices"
print " mfa webauthn remove - Remove device"
print ""
print "General:"
print " mfa status - Show MFA status"
print " mfa list-devices - List all devices"
print " mfa disable - Disable all MFA"
print " mfa help - Show this help"
print ""
}